3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/time/timer.h>
9 #include <msp/time/units.h>
11 #include "constants.h"
13 #include "locomotive.h"
21 Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
29 control.add_locomotive(*this);
34 void Locomotive::set_speed(unsigned spd)
37 signal_speed_changing.emit(spd);
42 signal_speed_changed.emit(speed);
45 void Locomotive::set_reverse(bool rev)
52 control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
62 void Locomotive::set_function(unsigned func, bool state)
71 signal_function_changed.emit(func, state);
74 void Locomotive::refresh_status()
76 unsigned char data[2];
78 data[1] = (addr>>8)&0xFF;
79 control.command(CMD_LOK_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
82 void Locomotive::send_command(bool setf)
84 unsigned char data[4];
86 data[1] = (addr>>8)&0xFF;
93 data[2] = (speed*19-18)/2;
95 data[3] = (reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
100 for(unsigned i=0; i<4; ++i)
104 control.command(CMD_LOK, data, 4);
107 void Locomotive::status_reply(const Reply &reply)
109 if(reply.get_error()==ERR_NO_ERROR)
111 const unsigned char *data = reply.get_data();
116 speed = data[0]*2/19+1;
118 reverse = (data[1]&0x20) ? false : true;
119 funcs = (data[1]&0xF)<<1;
123 for(unsigned i=0; i<5; ++i)
124 signal_function_changed.emit(i, (funcs>>i)&1);
128 bool Locomotive::reverse_timeout()
135 } // namespace Marklin