-#include <iostream>
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
#include <msp/time/timer.h>
#include <msp/time/units.h>
#include "command.h"
#include "control.h"
+#include "reply.h"
#include "turnout.h"
using namespace std;
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));
- char cmd[3];
- cmd[0]=CMD_TURNOUT_STATUS;
- cmd[1]=addr&0xFF;
- cmd[2]=(addr>>8)&0xFF;
- control.command(string(cmd,3)).signal_done.connect(sigc::mem_fun(this, &Turnout::status_reply));
+ unsigned char data[2];
+ 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)
{
- char cmd[3];
- cmd[0]=CMD_TURNOUT;
- cmd[1]=addr&0xFF;
- cmd[2]=((addr>>8)&0x7);
- if(on)
- cmd[2]|=0x40;
- if(route==0)
- cmd[2]|=0x80;
- control.command(string(cmd,3));
+ unsigned char 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(Error err, const string &reply)
+void Turnout::command_reply(const Reply &reply, unsigned char bit)
{
- if(err==ERR_NO_ERROR)
+ pending_cmds &= ~bit;
+ if(reply.get_error()==ERR_NO_ERROR)
{
- route=(reply[0]&4)?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);
}
}