]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/turnout.cpp
Forgot to add the new files
[r2c2.git] / source / libmarklin / turnout.cpp
index 30793fcff5727a6aed56e9aa697b02a4bbff0823..c022e05ea903ab5049eaf192e9860c19a8c5c7c0 100644 (file)
@@ -1,11 +1,10 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <iostream>
 #include <msp/time/timer.h>
 #include <msp/time/units.h>
 #include "command.h"
@@ -18,61 +17,104 @@ using namespace Msp;
 
 namespace Marklin {
 
-Turnout::Turnout(Control &c, unsigned a):
+Turnout::Turnout(Control &c, unsigned a, bool d):
        control(c),
        addr(a),
-       route(0)
+       path(0),
+       pending_path(0),
+       pending_cmds(0),
+       dual(d),
+       on(false)
 {
        control.add_turnout(*this);
 
        control.signal_turnout_event.connect(sigc::mem_fun(this, &Turnout::turnout_event));
 
        unsigned char data[2];
-       data[0]=addr&0xFF;
-       data[1]=(addr>>8)&0xFF;
-       control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Turnout::status_reply));
+       data[0] = addr&0xFF;
+       data[1] = (addr>>8)&0xFF;
+       control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), 1));
+       if(dual)
+       {
+               data[0] = (addr+1)&0xFF;
+               data[1] = ((addr+1)>>8)&0xFF;
+               control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), 2));
+       }
 }
 
-void Turnout::set_route(unsigned r)
+void Turnout::set_path(unsigned char p)
 {
-       route=r;
+       if(path==p || pending_cmds)
+               return;
 
-       command(true);
-       control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout));
+       signal_path_changing.emit(p);
 
-       signal_route_changed.emit(route);
+       pending_path = p;
+       on = true;
+       command(3);
 }
 
-void Turnout::command(bool on)
+void Turnout::command(unsigned char mask)
 {
        unsigned char data[2];
-       data[0]=addr&0xFF;
-       data[1]=((addr>>8)&0x7) | (on ? 0x40 : 0) | (route==0 ? 0x80 : 0);
-       control.command(CMD_TURNOUT, data, 2);
+       if(mask&1)
+       {
+               data[0] = addr&0xFF;
+               data[1] = ((addr>>8)&0x7) | (on ? 0x40 : 0) | (pending_path&1 ? 0 : 0x80);
+               control.command(CMD_TURNOUT, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::command_reply), 1));
+               pending_cmds |= 1;
+       }
+       if(dual && (mask&2))
+       {
+               data[0] = (addr+1)&0xFF;
+               data[1] = (((addr+1)>>8)&0x7) | (on ? 0x40 : 0) | (pending_path&2 ? 0 : 0x80);
+               control.command(CMD_TURNOUT, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::command_reply), 2));
+               pending_cmds |= 2;
+       }
 }
 
-void Turnout::status_reply(const Reply &reply)
+void Turnout::command_reply(const Reply &reply, unsigned char bit)
 {
+       pending_cmds &= ~bit;
        if(reply.get_error()==ERR_NO_ERROR)
        {
-               route=(reply.get_data()[0]&0x04) ? 0 : 1;
-               signal_route_changed.emit(route);
+               if(on && !pending_cmds)
+               {
+                       path = pending_path;
+                       on = false;
+                       control.set_timer(500*Time::msec).signal_timeout.connect(
+                               sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Turnout::command), 3), false));
+                       signal_path_changed.emit(path);
+               }
+       }
+       else if(reply.get_error()==ERR_NO_I2C_SPACE)
+       {
+               control.set_timer(100*Time::msec).signal_timeout.connect(
+                       sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Turnout::command), bit), false));
        }
 }
 
-bool Turnout::switch_timeout()
+void Turnout::status_reply(const Reply &reply, unsigned char bit)
 {
-       command(false);
-
-       return false;
+       if(reply.get_error()==ERR_NO_ERROR)
+       {
+               bool v = !(reply.get_data()[0]&0x04);
+               path = (path&~bit)|(v?bit:0);
+               signal_path_changed.emit(path);
+       }
 }
 
-void Turnout::turnout_event(unsigned a, bool r)
+void Turnout::turnout_event(unsigned a, bool p)
 {
-       if(a==addr && r!=route)
+       if(a==addr && p!=(path&1))
+       {
+               path = (path&2)|(p?1:0);
+               signal_path_changed.emit(path);
+       }
+       else if(dual && a==addr+1 && p!=((path>>1)&1))
        {
-               route=r;
-               signal_route_changed.emit(route);
+               path = (path&1)|(p?2:0);
+               signal_path_changed.emit(path);
        }
 }