3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 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"
20 Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
28 control.add_locomotive(*this);
33 void Locomotive::set_speed(unsigned spd)
39 signal_speed_changed.emit(speed);
42 void Locomotive::set_reverse(bool rev)
49 control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
59 void Locomotive::set_function(unsigned func, bool state)
68 signal_function_changed.emit(func, state);
71 void Locomotive::refresh_status()
74 cmd[0]=CMD_LOK_STATUS;
76 cmd[2]=(addr>>8)&0xFF;
77 control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
80 void Locomotive::send_command(bool setf)
85 cmd[2]=(addr>>8)&0xFF;
92 cmd[3]=(speed*19-18)/2;
94 cmd[4]=(reverse?0:0x20) | ((funcs&1)?0x10:0);
99 for(unsigned i=0; i<4; ++i)
103 control.command(string(cmd, 5));
106 void Locomotive::status_reply(Error err, const string &reply)
108 if(err==ERR_NO_ERROR)
110 if(static_cast<unsigned char>(reply[0])<=1)
113 speed=static_cast<unsigned char>(reply[0])*2/19+1;
114 reverse=(reply[1]&0x20)?false:true;
115 funcs=(reply[1]&0xF)<<1;
119 for(unsigned i=0; i<5; ++i)
120 signal_function_changed.emit(i, (funcs>>i)&1);
124 bool Locomotive::reverse_timeout()
131 } // namespace Marklin