X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fturnout.cpp;h=d5f96179be74de4dc71f49129340f9f9bb05a06d;hb=6dc18b0e518407bd2a86602bae1e9bbae05da7c8;hp=eca9ae2f90ee6eca92a7f60c4346ca57a024af3a;hpb=6c61179fe09af2f5366d50f10aadbf5f83438087;p=r2c2.git diff --git a/source/libmarklin/turnout.cpp b/source/libmarklin/turnout.cpp index eca9ae2..d5f9617 100644 --- a/source/libmarklin/turnout.cpp +++ b/source/libmarklin/turnout.cpp @@ -1,8 +1,15 @@ -#include +/* $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 "control.h" +#include "reply.h" #include "turnout.h" using namespace std; @@ -10,51 +17,61 @@ using namespace Msp; namespace Marklin { -Turnout::Turnout(Control &c, unsigned a): +Turnout::Turnout(Control &c, unsigned a, bool d): control(c), addr(a), - route(0) + path(0), + dual(d) { - control.add_turnout(this); + control.add_turnout(*this); control.signal_turnout_event.connect(sigc::mem_fun(this, &Turnout::turnout_event)); - char cmd[3]; - cmd[0]=CMD_TURNOUT_STATUS; - cmd[1]=addr&0xFF; - cmd[2]=(addr>>8)&0xFF; - control.command(string(cmd,3)).signal_done.connect(sigc::mem_fun(this, &Turnout::status_reply)); + unsigned char data[2]; + data[0] = addr&0xFF; + data[1] = (addr>>8)&0xFF; + control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), false)); + if(dual) + { + data[0] = (addr+1)&0xFF; + data[1] = ((addr+1)>>8)&0xFF; + control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), true)); + } } -void Turnout::set_route(unsigned r) +void Turnout::set_path(unsigned p) { - route=r; + signal_path_changing.emit(p); + path = p; command(true); - (new Time::Timer(200*Time::msec))->signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout)); + control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout)); - signal_route_changed.emit(route); + signal_path_changed.emit(path); } void Turnout::command(bool on) { - char cmd[3]; - cmd[0]=CMD_TURNOUT; - cmd[1]=addr&0xFF; - cmd[2]=((addr>>8)&0x7); - if(on) - cmd[2]|=0x40; - if(route==0) - cmd[2]|=0x80; - control.command(string(cmd,3)); + unsigned char data[2]; + data[0] = addr&0xFF; + data[1] = ((addr>>8)&0x7) | (on ? 0x40 : 0) | (path&1 ? 0 : 0x80); + control.command(CMD_TURNOUT, data, 2); + if(dual) + { + data[0] = (addr+1)&0xFF; + data[1] = (((addr+1)>>8)&0x7) | (on ? 0x40 : 0) | (path&2 ? 0 : 0x80); + control.command(CMD_TURNOUT, data, 2); + } } -void Turnout::status_reply(Error err, const string &reply) +void Turnout::status_reply(const Reply &reply, bool high) { - if(err==ERR_NO_ERROR) + if(reply.get_error()==ERR_NO_ERROR) { - route=(reply[0]&4)?0:1; - signal_route_changed.emit(route); + bool v = !(reply.get_data()[0]&0x04); + unsigned b = (high?2:1); + path = (path&~b)|(v?b:0); + signal_path_changed.emit(path); } } @@ -65,12 +82,17 @@ bool Turnout::switch_timeout() return false; } -void Turnout::turnout_event(unsigned a, bool r) +void Turnout::turnout_event(unsigned a, bool p) { - if(a==addr && r!=route) + if(a==addr && p!=(path&1)) + { + path = (path&2)|(p?1:0); + signal_path_changed.emit(path); + } + else if(dual && a==addr+1 && p!=((path>>1)&1)) { - route=r; - signal_route_changed.emit(route); + path = (path&1)|(p?2:0); + signal_path_changed.emit(path); } }