+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
#include <msp/time/timer.h>
#include <msp/time/units.h>
#include "command.h"
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();
}
speed=min(spd, 14U);
send_command(false);
+
+ signal_speed_changed.emit(speed);
}
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
funcs&=~(1<<func);
send_command(true);
+
+ signal_function_changed.emit(func, state);
}
void Locomotive::refresh_status()
cmd[0]=CMD_LOK_STATUS;
cmd[1]=addr&0xFF;
cmd[2]=(addr>>8)&0xFF;
- control.command(string(cmd,3)).signal_done.connect(sigc::mem_fun(this,&Locomotive::status_reply));
+ control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
}
void Locomotive::send_command(bool setf)
if((funcs>>i)&2)
cmd[4]|=(1<<i);
}
- control.command(string(cmd,5));
+ control.command(string(cmd, 5));
}
void Locomotive::status_reply(Error err, const string &reply)
{
if(err==ERR_NO_ERROR)
{
- if((unsigned char)reply[0]<=1)
+ if(static_cast<unsigned char>(reply[0])<=1)
speed=0;
else
- speed=(unsigned char)reply[0]*2/19+1;
+ speed=static_cast<unsigned char>(reply[0])*2/19+1;
reverse=(reply[1]&0x20)?false:true;
funcs=(reply[1]&0xF)<<1;
if(reply[1]&0x10)
funcs|=1;
+
+ for(unsigned i=0; i<5; ++i)
+ signal_function_changed.emit(i, (funcs>>i)&1);
}
}