]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/control.cpp
Add Id tags and copyright notices to files
[r2c2.git] / source / libmarklin / control.cpp
index bd63f9f66f661c75d110820edd7ddd3610e48d2c..ecf10348a7890a890bdc155b8ff66f04563baf14 100644 (file)
@@ -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 <fcntl.h>
 #include <termios.h>
 #include <sys/poll.h>
@@ -39,6 +46,8 @@ 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)
@@ -94,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)
@@ -158,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));
        }
 
@@ -283,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;
@@ -305,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)