X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fcontrol.cpp;h=6f7ef74dbf30bc18cd83bd111e994ef7e606c619;hb=38fb8d56efde037a71c46a58bda314655e68ab6c;hp=02b8f0f16949a2aab1c5f49013de37e32bf10415;hpb=52cbe8d99669f843f8f75c51128e2748584dd03a;p=r2c2.git diff --git a/source/libmarklin/control.cpp b/source/libmarklin/control.cpp index 02b8f0f..6f7ef74 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 © 2007-2008 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include #include @@ -7,6 +14,7 @@ #include #include "command.h" #include "control.h" +#include "reply.h" using namespace std; using namespace Msp; @@ -15,24 +23,25 @@ namespace Marklin { Control::Control(): serial_fd(-1), - p50_enabled(false), power(true), poll_sensors(false), debug(false) { } -void Control::set_power(bool p) +Control::~Control() { - power=p; - if(power) - command(string(1, CMD_POWER_ON)); - else - command(string(1, CMD_POWER_OFF)); + 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::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"); @@ -48,9 +57,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 ok = false; + bool p50 = false; for(unsigned i=0; baud[i]; i+=2) { cfsetospeed(&attr, baud[i+1]); @@ -58,44 +68,68 @@ 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"); @@ -104,12 +138,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"); @@ -118,13 +152,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"); @@ -133,72 +167,52 @@ 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(); + timer.tick(false); + if(t>next_event_query) { - next_event_query=t+300*Time::msec; - command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done)); + 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())->second->get_address(); - string cmd(3, 0); - cmd[0]=CMD_SENSOR_PARAM_SET; - cmd[1]=0; - cmd[2]=(max_addr+7)/8; - command(cmd); - command(string(1, CMD_SENSOR_REPORT)); - poll_sensors=false; + unsigned max_addr = (--sensors.end())->first; + unsigned char data[2]; + data[0] = 0; + data[1] = (max_addr+7)/8; + command(CMD_SENSOR_PARAM_SET, data, 2); + command(CMD_SENSOR_REPORT); + poll_sensors = false; } - if(queue.size() && 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) { - string resp=read_reply((Cmd)(unsigned char)queue.front().get_command()[0]); + Reply reply = Reply::read(serial_fd, queue.front().get_command()); if(debug) - { - printf("read: "); - for(unsigned i=0; i::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); + power = ((reply.get_data()[0]&0x08)!=0); + signal_power_event.emit(power); } -/*** private ***/ - -void Control::read_all(int fd, char *buf, int size) +void Control::event_query_done(const Reply &reply) { - int pos=0; - while(pos>(7-j%8))&1; - cout<>(7-j%8))&1); cout<<'\n'; + + for(unsigned j=0; j<16; ++j) + signal_sensor_event.emit(module*16+j-15, (data[i+1+j/8]>>(7-j%8))&1); } }