X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fcontrol.cpp;h=6c6ceb37da500e789d9a295ad68b1d0a53feb68f;hb=ae0600f7c3df5b6ef46992b1423888dd0e9a5026;hp=f51dbd09de45a61e171672bccc5ed3a139bca2f6;hpb=ca5b1fcfd52a09e3d3b2c4c011dc91ac9ad19694;p=r2c2.git diff --git a/source/libmarklin/control.cpp b/source/libmarklin/control.cpp index f51dbd0..6c6ceb3 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 @@ -7,6 +14,7 @@ #include #include "command.h" #include "control.h" +#include "reply.h" using namespace std; using namespace Msp; @@ -15,7 +23,6 @@ namespace Marklin { Control::Control(): serial_fd(-1), - p50_enabled(false), power(true), poll_sensors(false), debug(false) @@ -36,9 +43,9 @@ void Control::set_power(bool p) { power=p; if(power) - command(string(1, CMD_POWER_ON)); + command(CMD_POWER_ON); else - command(string(1, CMD_POWER_OFF)); + command(CMD_POWER_OFF); signal_power_event.emit(power); } @@ -69,6 +76,7 @@ void Control::open(const string &dev) attr.c_cflag|=CSTOPB; bool ok=false; + bool p50=false; for(unsigned i=0; baud[i]; i+=2) { cfsetospeed(&attr, baud[i+1]); @@ -79,19 +87,9 @@ void Control::open(const string &dev) pollfd pfd={serial_fd, POLLIN, 0}; if(poll(&pfd, 1, 500)>0) { - cout<<"IB detected at "<next_event_query) { next_event_query=t+200*Time::msec; - command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done)); + 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)); + 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; } @@ -184,16 +196,11 @@ void Control::tick() pollfd pfd={serial_fd, POLLIN, 0}; if(poll(&pfd, 1, 0)>0) { - string resp=read_reply(static_cast(static_cast(queue.front().get_command()[0]))); + Reply reply=Reply::read(serial_fd, queue.front().get_command()); if(debug) - { - printf("read: "); - for(unsigned i=0; i(resp[i])); - printf("(%d bytes)\n", resp.size()); - } - - queue.front().signal_done.emit(static_cast(resp[0]), resp.substr(1)); + cout<<"R: "<(cmd[i])); - printf("(%d bytes)\n", cmd.size()); - } + cout<<"W: "<1 && (resp[1]&0x40)) - command(string(1, CMD_STATUS)).signal_done.connect(sigc::mem_fun(this, &Control::status_done)); + const unsigned char *data=reply.get_data(); + if(data[0]&0x01) + command(CMD_EVENT_LOK); + if(data[0]&0x20) + command(CMD_EVENT_TURNOUT).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done)); + if(data[0]&0x04) + command(CMD_EVENT_SENSOR).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done)); + if(data[1]&0x40) + command(CMD_STATUS).signal_done.connect(sigc::mem_fun(this, &Control::status_done)); } -void Control::turnout_event_done(Error, const string &resp) +void Control::turnout_event_done(const Reply &reply) { - unsigned count=resp[0]; + const unsigned char *data=reply.get_data(); + unsigned count=data[0]; for(unsigned i=0; i(resp[i*2+1])+((resp[i*2+2]&7)<<8); - bool status=!(resp[i*2+2]&0x80); + unsigned addr=(data[i*2+1])+((data[i*2+2]&7)<<8); + bool status=!(data[i*2+2]&0x80); cout<<"Turnout "<(resp[i]); + unsigned module=data[i]; cout<<"S88 module "<>(7-j%8))&1); + cout<<((data[i+1+j/8]>>(7-j%8))&1); cout<<'\n'; for(unsigned j=0; j<16; ++j) - signal_sensor_event.emit(module*16+j-15, (resp[i+1+j/8]>>(7-j%8))&1); + signal_sensor_event.emit(module*16+j-15, (data[i+1+j/8]>>(7-j%8))&1); } }