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)
32 void Locomotive::set_reverse(bool rev)
39 (new Time::Timer((500+speed*150)*Time::msec))->signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
49 void Locomotive::set_function(unsigned func, bool state)
59 void Locomotive::refresh_status()
62 cmd[0]=CMD_LOK_STATUS;
64 cmd[2]=(addr>>8)&0xFF;
65 control.command(string(cmd,3)).signal_done.connect(sigc::mem_fun(this,&Locomotive::status_reply));
68 void Locomotive::send_command(bool setf)
73 cmd[2]=(addr>>8)&0xFF;
80 cmd[3]=(speed*19-18)/2;
82 cmd[4]=(reverse?0:0x20) | ((funcs&1)?0x10:0);
87 for(unsigned i=0; i<4; ++i)
91 control.command(string(cmd,5));
94 void Locomotive::status_reply(Error err, const string &reply)
98 if((unsigned char)reply[0]<=1)
101 speed=(unsigned char)reply[0]*2/19+1;
102 reverse=(reply[1]&0x20)?false:true;
103 funcs=(reply[1]&0xF)<<1;
109 bool Locomotive::reverse_timeout()
116 } // namespace Marklin