1 #include <msp/time/timer.h>
2 #include <msp/time/units.h>
6 #include "locomotive.h"
13 Locomotive::Locomotive(Control &c, unsigned a):
20 control.add_locomotive(*this);
25 void Locomotive::set_speed(unsigned spd)
31 signal_speed_changed.emit(speed);
34 void Locomotive::set_reverse(bool rev)
41 control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
51 void Locomotive::set_function(unsigned func, bool state)
61 void Locomotive::refresh_status()
64 cmd[0]=CMD_LOK_STATUS;
66 cmd[2]=(addr>>8)&0xFF;
67 control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
70 void Locomotive::send_command(bool setf)
75 cmd[2]=(addr>>8)&0xFF;
82 cmd[3]=(speed*19-18)/2;
84 cmd[4]=(reverse?0:0x20) | ((funcs&1)?0x10:0);
89 for(unsigned i=0; i<4; ++i)
93 control.command(string(cmd, 5));
96 void Locomotive::status_reply(Error err, const string &reply)
100 if(static_cast<unsigned char>(reply[0])<=1)
103 speed=static_cast<unsigned char>(reply[0])*2/19+1;
104 reverse=(reply[1]&0x20)?false:true;
105 funcs=(reply[1]&0xF)<<1;
111 bool Locomotive::reverse_timeout()
118 } // namespace Marklin