X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fcontrol.cpp;h=4f63cc20be086e90e4c56c7e2d768665444a2044;hb=6dc18b0e518407bd2a86602bae1e9bbae05da7c8;hp=6c6ceb37da500e789d9a295ad68b1d0a53feb68f;hpb=540e931ec79129c8776f48434ff5b230b53337f1;p=r2c2.git diff --git a/source/libmarklin/control.cpp b/source/libmarklin/control.cpp index 6c6ceb3..4f63cc2 100644 --- a/source/libmarklin/control.cpp +++ b/source/libmarklin/control.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2007-2008 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -36,28 +36,13 @@ Control::~Control() 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; - if(power) - command(CMD_POWER_ON); - else - command(CMD_POWER_OFF); - - signal_power_event.emit(power); -} - -void Control::set_debug(bool d) -{ - debug=d; + if(serial_fd>=0) + close(serial_fd); } void Control::open(const string &dev) { - serial_fd=::open(dev.c_str(), O_RDWR); + serial_fd = ::open(dev.c_str(), O_RDWR); if(serial_fd<0) throw Exception("Couldn't open serial port\n"); @@ -73,10 +58,10 @@ void Control::open(const string &dev) termios attr; tcgetattr(serial_fd, &attr); cfmakeraw(&attr); - attr.c_cflag|=CSTOPB; + attr.c_cflag |= CSTOPB; - bool ok=false; - bool p50=false; + bool ok = false; + bool p50 = false; for(unsigned i=0; baud[i]; i+=2) { cfsetospeed(&attr, baud[i+1]); @@ -84,13 +69,13 @@ void Control::open(const string &dev) write(serial_fd, "\xC4", 1); - pollfd pfd={serial_fd, POLLIN, 0}; + pollfd pfd = { serial_fd, POLLIN, 0 }; if(poll(&pfd, 1, 500)>0) { cout<<"IB detected at "<::const_iterator i=turnouts.find(id); + map::const_iterator i = turnouts.find(id); if(i==turnouts.end()) throw KeyError("Unknown turnout"); @@ -138,12 +139,12 @@ Turnout &Control::get_turnout(unsigned id) const void Control::add_locomotive(Locomotive &l) { - locomotives[l.get_address()]=&l; + locomotives[l.get_address()] = &l; } Locomotive &Control::get_locomotive(unsigned id) const { - map::const_iterator i=locomotives.find(id); + map::const_iterator i = locomotives.find(id); if(i==locomotives.end()) throw KeyError("Unknown locomotive"); @@ -152,13 +153,13 @@ Locomotive &Control::get_locomotive(unsigned id) const void Control::add_sensor(Sensor &s) { - sensors[s.get_address()]=&s; - poll_sensors=true; + sensors[s.get_address()] = &s; + poll_sensors = true; } Sensor &Control::get_sensor(unsigned id) const { - map::const_iterator i=sensors.find(id); + map::const_iterator i = sensors.find(id); if(i==sensors.end()) throw KeyError("Unknown sensor"); @@ -167,7 +168,7 @@ Sensor &Control::get_sensor(unsigned id) const void Control::tick() { - const Time::TimeStamp t=Time::now(); + const Time::TimeStamp t = Time::now(); for(map::const_iterator i=sensors.begin(); i!=sensors.end(); ++i) i->second->tick(); @@ -176,27 +177,27 @@ void Control::tick() if(t>next_event_query) { - next_event_query=t+200*Time::msec; + next_event_query = t+200*Time::msec; command(CMD_EVENT).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done)); } if(poll_sensors) { - unsigned max_addr=(--sensors.end())->first; + unsigned max_addr = (--sensors.end())->first; unsigned char data[2]; - data[0]=0; - data[1]=(max_addr+7)/8; + data[0] = 0; + data[1] = (max_addr+7)/8; command(CMD_SENSOR_PARAM_SET, data, 2); command(CMD_SENSOR_REPORT); - poll_sensors=false; + poll_sensors = false; } - if(!queue.empty() && queue.front().get_sent()) + if(!queue.empty() && queue.front().is_sent()) { - pollfd pfd={serial_fd, POLLIN, 0}; + pollfd pfd = { serial_fd, POLLIN, 0 }; if(poll(&pfd, 1, 0)>0) { - Reply reply=Reply::read(serial_fd, queue.front().get_command()); + Reply reply = Reply::read(serial_fd, queue.front().get_command()); if(debug) cout<<"R: "<=0) + queue.front().send(serial_fd); + else + queue.erase(queue.begin()); } } @@ -221,25 +225,15 @@ Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt) return timer.add(dt); } -void Control::read_all(int fd, char *buf, int size) -{ - int pos=0; - while(pos