X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=inline;f=source%2Flibmarklin%2Fcontrol.cpp;h=ecf10348a7890a890bdc155b8ff66f04563baf14;hb=3e9c210ddc036cd015228504cc0803c909e27f84;hp=6e82614c21ebf904a65fd3d4d29b359bfe77c03e;hpb=aead7f2da831199fbb8f6950b067656b2965c2fa;p=r2c2.git diff --git a/source/libmarklin/control.cpp b/source/libmarklin/control.cpp index 6e82614..ecf1034 100644 --- a/source/libmarklin/control.cpp +++ b/source/libmarklin/control.cpp @@ -1,3 +1,10 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include #include @@ -21,6 +28,17 @@ Control::Control(): debug(false) { } +Control::~Control() +{ + for(map::iterator i=sensors.begin(); i!=sensors.end(); ++i) + delete i->second; + for(map::iterator i=turnouts.begin(); i!=turnouts.end(); ++i) + delete i->second; + for(map::iterator i=locomotives.begin(); i!=locomotives.end(); ++i) + delete i->second; + close(serial_fd); +} + void Control::set_power(bool p) { power=p; @@ -28,6 +46,13 @@ void Control::set_power(bool p) command(string(1, CMD_POWER_ON)); else command(string(1, CMD_POWER_OFF)); + + signal_power_event.emit(power); +} + +void Control::set_debug(bool d) +{ + debug=d; } void Control::open(const string &dev) @@ -78,8 +103,11 @@ void Control::open(const string &dev) break; } } + if(!ok) throw Exception("IB not detected"); + + command(string(1, CMD_STATUS)).signal_done.connect(sigc::mem_fun(this, &Control::status_done)); } Command &Control::command(const string &cmd) @@ -142,7 +170,7 @@ void Control::tick() if(t>next_event_query) { - next_event_query=t+300*Time::msec; + next_event_query=t+200*Time::msec; command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done)); } @@ -158,28 +186,28 @@ void Control::tick() poll_sensors=false; } - if(queue.size() && queue.front().get_sent()) + if(!queue.empty() && queue.front().get_sent()) { pollfd pfd={serial_fd, POLLIN, 0}; if(poll(&pfd, 1, 0)>0) { - string resp=read_reply((Cmd)(unsigned char)queue.front().get_command()[0]); + string resp=read_reply(static_cast(static_cast(queue.front().get_command()[0]))); if(debug) { printf("read: "); for(unsigned i=0; i(resp[i])); printf("(%d bytes)\n", resp.size()); } - queue.front().signal_done.emit((Error)resp[0], resp.substr(1)); + queue.front().signal_done.emit(static_cast(resp[0]), resp.substr(1)); queue.erase(queue.begin()); } else return; } - if(queue.size()) + if(!queue.empty()) { string cmd=queue.front().get_command(); @@ -195,8 +223,8 @@ void Control::tick() { printf("write: "); for(unsigned i=0; i(cmd[i])); + printf("(%d bytes)\n", cmd.size()); } write(serial_fd, cmd.data(), cmd.size()); @@ -209,19 +237,6 @@ Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt) return timer.add(dt); } -Control::~Control() -{ - for(map::iterator i=sensors.begin(); i!=sensors.end(); ++i) - delete i->second; - for(map::iterator i=turnouts.begin(); i!=turnouts.end(); ++i) - delete i->second; - for(map::iterator i=locomotives.begin(); i!=locomotives.end(); ++i) - delete i->second; - close(serial_fd); -} - -/*** private ***/ - void Control::read_all(int fd, char *buf, int size) { int pos=0; @@ -280,6 +295,9 @@ string Control::read_reply(Cmd cmd) } else { + if(cmd==CMD_STATUS) + result+=ERR_NO_ERROR; + unsigned expected_bytes=1; if(cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS) expected_bytes=2; @@ -302,12 +320,20 @@ string Control::read_reply(Cmd cmd) return result; } +void Control::status_done(Error, const string &resp) +{ + power=((resp[0]&0x08)!=0); + signal_power_event.emit(power); +} + void Control::event_query_done(Error, const string &resp) { if(resp[0]&0x20) command(string(1, CMD_EVENT_TURNOUT)).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done)); if(resp[0]&0x04) command(string(1, CMD_EVENT_SENSOR)).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done)); + if(resp.size()>1 && (resp[1]&0x40)) + command(string(1, CMD_STATUS)).signal_done.connect(sigc::mem_fun(this, &Control::status_done)); } void Control::turnout_event_done(Error, const string &resp) @@ -315,7 +341,7 @@ void Control::turnout_event_done(Error, const string &resp) unsigned count=resp[0]; for(unsigned i=0; i(resp[i*2+1])+((resp[i*2+2]&7)<<8); bool status=!(resp[i*2+2]&0x80); cout<<"Turnout "<(resp[i]); cout<<"S88 module "<