X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Farducontrol.cpp;h=e940a2aed14527ed676a6cbb4af1786762411420;hb=ebd56306e951e751883e9f173e1b6235846e78bf;hp=213a848d95a5d921406fb750bb284fd08818a952;hpb=2399146e4235d9923ee4256c413574d4395312e3;p=r2c2.git diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index 213a848..e940a2a 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -9,6 +9,11 @@ using namespace Msp; namespace R2C2 { +ArduControl::ProtocolInfo ArduControl::protocol_info[2] = +{ + { 79, 14, 4 } // MM +}; + ArduControl::ArduControl(const string &dev): serial(dev), debug(true), @@ -57,45 +62,45 @@ ArduControl::Protocol ArduControl::map_protocol(const string &proto_name) { if(proto_name=="MM") return MM; + else if(proto_name=="MFX") + return MFX; else throw invalid_argument("ArduControl::map_protocol"); } unsigned ArduControl::get_protocol_speed_steps(const string &proto_name) const { - Protocol proto = map_protocol(proto_name); - if(proto==MM) - return 14; - else - return 0; + return protocol_info[map_protocol(proto_name)].max_speed; } -void ArduControl::add_loco(unsigned addr, const string &proto_name, const VehicleType &) +unsigned ArduControl::add_loco(unsigned addr, const string &proto_name, const VehicleType &) { if(!addr) throw invalid_argument("ArduControl::add_loco"); Protocol proto = map_protocol(proto_name); + if(addr>protocol_info[proto].max_address) + throw invalid_argument("ArduControl::add_loco"); - if(proto==MM) - { - if(addr>=80) - throw invalid_argument("ArduControl::add_loco"); - } + Locomotive loco(proto, addr); + insert_unique(locomotives, loco.id, loco); - insert_unique(locomotives, addr, Locomotive(proto, addr)); + return loco.id; } -void ArduControl::remove_loco(unsigned addr) +void ArduControl::remove_loco(unsigned id) { - Locomotive &loco = get_item(locomotives, addr); + Locomotive &loco = get_item(locomotives, id); remove_loco_from_refresh(loco); - locomotives.erase(addr); + locomotives.erase(id); } -void ArduControl::set_loco_speed(unsigned addr, unsigned speed) +void ArduControl::set_loco_speed(unsigned id, unsigned speed) { - Locomotive &loco = get_item(locomotives, addr); + Locomotive &loco = get_item(locomotives, id); + if(speed>protocol_info[loco.proto].max_speed) + throw invalid_argument("ArduControl::set_loco_speed"); + if(loco.speed.set(speed)) { QueuedCommand cmd(loco, Locomotive::SPEED); @@ -105,9 +110,9 @@ void ArduControl::set_loco_speed(unsigned addr, unsigned speed) } } -void ArduControl::set_loco_reverse(unsigned addr, bool rev) +void ArduControl::set_loco_reverse(unsigned id, bool rev) { - Locomotive &loco = get_item(locomotives, addr); + Locomotive &loco = get_item(locomotives, id); if(loco.reverse.set(rev)) { QueuedCommand cmd(loco, Locomotive::REVERSE); @@ -117,9 +122,12 @@ void ArduControl::set_loco_reverse(unsigned addr, bool rev) } } -void ArduControl::set_loco_function(unsigned addr, unsigned func, bool state) +void ArduControl::set_loco_function(unsigned id, unsigned func, bool state) { - Locomotive &loco = get_item(locomotives, addr); + Locomotive &loco = get_item(locomotives, id); + if(func>protocol_info[loco.proto].max_func) + throw invalid_argument("ArduControl::set_loco_function"); + unsigned mask = 1<=n_s88_octets) n_s88_octets = octet_index+1; + + return addr; } void ArduControl::remove_sensor(unsigned addr) @@ -314,7 +325,7 @@ void ArduControl::tick() } else if(tag.type==Tag::LOCOMOTIVE) { - LocomotiveMap::iterator i = locomotives.find(tag.address); + LocomotiveMap::iterator i = locomotives.find(tag.id); if(i==locomotives.end()) continue; @@ -322,12 +333,12 @@ void ArduControl::tick() if(tag.command==Locomotive::SPEED) { if(loco.speed.commit(tag.serial)) - signal_loco_speed.emit(loco.address, loco.speed, loco.reverse); + signal_loco_speed.emit(loco.id, loco.speed, loco.reverse); } else if(tag.command==Locomotive::REVERSE) { if(loco.reverse.commit(tag.serial)) - signal_loco_speed.emit(loco.address, loco.speed, loco.reverse); + signal_loco_speed.emit(loco.id, loco.speed, loco.reverse); } else if(tag.command==Locomotive::FUNCTIONS) { @@ -337,13 +348,13 @@ void ArduControl::tick() unsigned changed = old^loco.funcs; for(unsigned j=0; changed>>j; ++j) if((changed>>j)&1) - signal_loco_function.emit(loco.address, j, (loco.funcs>>j)&1); + signal_loco_function.emit(loco.id, j, (loco.funcs>>j)&1); } } } else if(tag.type==Tag::ACCESSORY) { - AccessoryMap::iterator i = accessories.find(tag.address); + AccessoryMap::iterator i = accessories.find(tag.id); if(i==accessories.end()) continue; @@ -370,7 +381,7 @@ void ArduControl::tick() } else if(tag.type==Tag::SENSOR) { - SensorMap::iterator i = sensors.find(tag.address); + SensorMap::iterator i = sensors.find(tag.id); if(i==sensors.end()) continue; @@ -456,11 +467,12 @@ ArduControl::Tag::Tag(): type(NONE), command(0), serial(0), - address(0) + id(0) { } ArduControl::Locomotive::Locomotive(Protocol p, unsigned a): + id((p<<16)|a), proto(p), address(a), speed(0), @@ -667,7 +679,7 @@ void ArduControl::ControlThread::main() stag.type = Tag::SENSOR; stag.command = Sensor::STATE; stag.serial = i->second.state.serial; - stag.address = i->first; + stag.id = i->first; control.push_completed_tag(stag); } @@ -697,7 +709,7 @@ ArduControl::QueuedCommand::QueuedCommand(Locomotive &loco, Locomotive::Command { tag.type = Tag::LOCOMOTIVE; tag.command = cmd; - tag.address = loco.address; + tag.id = loco.id; if(cmd==Locomotive::SPEED) { tag.serial = loco.speed.serial; @@ -721,7 +733,7 @@ ArduControl::QueuedCommand::QueuedCommand(Accessory &acc, Accessory::Command cmd { tag.type = Tag::ACCESSORY; tag.command = cmd; - tag.address = acc.address; + tag.id = acc.address; if(cmd==Accessory::ACTIVATE || cmd==Accessory::DEACTIVATE) { tag.serial = acc.state.serial;