1 #include <msp/time/timer.h>
2 #include <msp/time/units.h>
6 #include "locomotive.h"
13 Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
21 control.add_locomotive(*this);
26 void Locomotive::set_speed(unsigned spd)
32 signal_speed_changed.emit(speed);
35 void Locomotive::set_reverse(bool rev)
42 control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
52 void Locomotive::set_function(unsigned func, bool state)
61 signal_function_changed.emit(func, state);
64 void Locomotive::refresh_status()
67 cmd[0]=CMD_LOK_STATUS;
69 cmd[2]=(addr>>8)&0xFF;
70 control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
73 void Locomotive::send_command(bool setf)
78 cmd[2]=(addr>>8)&0xFF;
85 cmd[3]=(speed*19-18)/2;
87 cmd[4]=(reverse?0:0x20) | ((funcs&1)?0x10:0);
92 for(unsigned i=0; i<4; ++i)
96 control.command(string(cmd, 5));
99 void Locomotive::status_reply(Error err, const string &reply)
101 if(err==ERR_NO_ERROR)
103 if(static_cast<unsigned char>(reply[0])<=1)
106 speed=static_cast<unsigned char>(reply[0])*2/19+1;
107 reverse=(reply[1]&0x20)?false:true;
108 funcs=(reply[1]&0xF)<<1;
112 for(unsigned i=0; i<5; ++i)
113 signal_function_changed.emit(i, (funcs>>i)&1);
117 bool Locomotive::reverse_timeout()
124 } // namespace Marklin