X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Flocomotive.cpp;h=32f2ecc1a185f2fa1ac387cb4659ad61e1926f01;hb=7839b7c3d782abb5c98a24d51cae109407068c02;hp=6329196710583014bc76ee80028daf476bb77ef7;hpb=06c100aacb559fbbe7380e15981c4772092c269b;p=r2c2.git diff --git a/source/libmarklin/locomotive.cpp b/source/libmarklin/locomotive.cpp index 6329196..32f2ecc 100644 --- a/source/libmarklin/locomotive.cpp +++ b/source/libmarklin/locomotive.cpp @@ -1,31 +1,42 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include #include "command.h" #include "constants.h" #include "control.h" #include "locomotive.h" +#include "reply.h" using namespace std; using namespace Msp; namespace Marklin { -Locomotive::Locomotive(Control &c, unsigned a): +Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a): + type(t), control(c), addr(a), speed(0), reverse(false), funcs(0) { - control.add_locomotive(this); + control.add_locomotive(*this); refresh_status(); } void Locomotive::set_speed(unsigned spd) { - speed=min(spd, 14U); + spd=min(spd, 14U); + signal_speed_changing.emit(spd); + speed=spd; send_command(false); signal_speed_changed.emit(speed); @@ -38,7 +49,7 @@ void Locomotive::set_reverse(bool rev) if(speed) { - (new Time::Timer((500+speed*150)*Time::msec))->signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout)); + control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout)); set_speed(0); } else @@ -56,55 +67,61 @@ void Locomotive::set_function(unsigned func, bool state) funcs&=~(1<>8)&0xFF; - control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply)); + unsigned char data[2]; + data[0]=addr&0xFF; + data[1]=(addr>>8)&0xFF; + control.command(CMD_LOK_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply)); } void Locomotive::send_command(bool setf) { - char cmd[16]; - cmd[0]=CMD_LOK; - cmd[1]=addr&0xFF; - cmd[2]=(addr>>8)&0xFF; + unsigned char data[4]; + data[0]=addr&0xFF; + data[1]=(addr>>8)&0xFF; if(speed==0) - cmd[3]=0; + data[2]=0; else if(speed==1) - cmd[3]=2; + data[2]=2; else - cmd[3]=(speed*19-18)/2; + data[2]=(speed*19-18)/2; - cmd[4]=(reverse?0:0x20) | ((funcs&1)?0x10:0); + data[3]=(reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0); if(setf) { - cmd[4]|=0x80; + data[3]|=0x80; for(unsigned i=0; i<4; ++i) if((funcs>>i)&2) - cmd[4]|=(1<>i)&1); } }