X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fintellibox.cpp;h=6efe8750a188e7dce26efeb4585d46fd438c1ba9;hb=d3907f4b0e60f246a53201b93b06e86062f1b48a;hp=ace23e8f6166762926782fbd2a9dbcb01e290656;hpb=f7ee3202102bfa67fdb6ad93c4f0f90f4c8ffb2c;p=r2c2.git diff --git a/source/libmarklin/intellibox.cpp b/source/libmarklin/intellibox.cpp index ace23e8..6efe875 100644 --- a/source/libmarklin/intellibox.cpp +++ b/source/libmarklin/intellibox.cpp @@ -20,6 +20,7 @@ namespace Marklin { Intellibox::Intellibox(const string &dev): power(false), + halted(false), update_sensors(false), command_sent(false) { @@ -80,6 +81,19 @@ void Intellibox::set_power(bool p) signal_power.emit(power); } +void Intellibox::halt(bool h) +{ + halted = h; + if(halted) + { + for(map::iterator i=locos.begin(); i!=locos.end(); ++i) + if(i->second.speed) + set_loco_speed(i->first, 0); + } + + signal_halt.emit(halted); +} + void Intellibox::add_loco(unsigned addr) { if(!locos.count(addr)) @@ -98,6 +112,8 @@ void Intellibox::set_loco_speed(unsigned addr, unsigned speed) Locomotive &loco = locos[addr]; if(speed==loco.speed) return; + if(speed && halted) + return; loco.speed = speed; loco_command(addr, speed, loco.reverse, loco.funcs|0x100); @@ -145,6 +161,7 @@ void Intellibox::set_turnout(unsigned addr, bool state) turnout.pending = state; turnout.active = true; + turnout.off_timeout = Time::TimeStamp(); turnout_command(addr, state, true); } @@ -360,7 +377,10 @@ void Intellibox::process_reply(const Time::TimeStamp &t) read_all(data, 2); unsigned addr = data[0]+((data[1]&7)<<8); - signal_turnout.emit(addr, (data[1]&0x80)!=0); + Turnout &turnout = turnouts[addr]; + turnout.state = (data[1]&0x80)!=0; + turnout.pending = turnout.state; + signal_turnout.emit(addr, turnout.state); } } else if(cmd==CMD_EVENT_SENSOR) @@ -411,10 +431,11 @@ void Intellibox::process_reply(const Time::TimeStamp &t) unsigned char err; read_all(&err, 1); + unsigned addr = queue.front().addr; + Turnout &turnout = turnouts[addr]; + if(err==ERR_NO_ERROR) { - unsigned addr = queue.front().addr; - Turnout &turnout = turnouts[addr]; turnout.state = turnout.pending; if(turnout.active) { @@ -424,6 +445,8 @@ void Intellibox::process_reply(const Time::TimeStamp &t) } else if(err==ERR_NO_I2C_SPACE) queue.push_back(queue.front()); + else + turnout.pending = turnout.state; } else if(cmd==CMD_TURNOUT_STATUS) { @@ -442,6 +465,7 @@ void Intellibox::process_reply(const Time::TimeStamp &t) if(state!=turnout.state) { turnout.state = state; + turnout.pending = state; signal_turnout.emit(addr, turnout.state); } } @@ -521,7 +545,8 @@ Intellibox::Locomotive::Locomotive(): Intellibox::Turnout::Turnout(): state(false), - active(false) + active(false), + pending(false) { }