]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/turnout.cpp
Forgot to add the new files
[r2c2.git] / source / libmarklin / turnout.cpp
index efa98bb5a7b5c49dd62800de0c878091fcda6747..c022e05ea903ab5049eaf192e9860c19a8c5c7c0 100644 (file)
@@ -20,8 +20,11 @@ namespace Marklin {
 Turnout::Turnout(Control &c, unsigned a, bool d):
        control(c),
        addr(a),
-       route(0),
-       dual(d)
+       path(0),
+       pending_path(0),
+       pending_cmds(0),
+       dual(d),
+       on(false)
 {
        control.add_turnout(*this);
 
@@ -30,69 +33,88 @@ Turnout::Turnout(Control &c, unsigned a, bool d):
        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), false));
+       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), true));
+               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)
 {
-       signal_route_changing.emit(r);
+       if(path==p || pending_cmds)
+               return;
 
-       route = r;
-       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&1 ? 0 : 0x80);
-       control.command(CMD_TURNOUT, data, 2);
-       if(dual)
+       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) | (route&2 ? 0 : 0x80);
-               control.command(CMD_TURNOUT, data, 2);
+               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, bool high)
+void Turnout::command_reply(const Reply &reply, unsigned char bit)
 {
+       pending_cmds &= ~bit;
        if(reply.get_error()==ERR_NO_ERROR)
        {
-               bool v = !(reply.get_data()[0]&0x04);
-               unsigned b = (high?2:1);
-               route = (route&~b)|(v?b:0);
-               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&1))
+       if(a==addr && p!=(path&1))
        {
-               route = (route&2)|(r?1:0);
-               signal_route_changed.emit(route);
+               path = (path&2)|(p?1:0);
+               signal_path_changed.emit(path);
        }
-       else if(dual && a==addr+1 && r!=((route>>1)&1))
+       else if(dual && a==addr+1 && p!=((path>>1)&1))
        {
-               route = (route&1)|(r?2:0);
-               signal_route_changed.emit(route);
+               path = (path&1)|(p?2:0);
+               signal_path_changed.emit(path);
        }
 }