]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/turnout.cpp
Forgot to add the new files
[r2c2.git] / source / libmarklin / turnout.cpp
index d5f96179be74de4dc71f49129340f9f9bb05a06d..c022e05ea903ab5049eaf192e9860c19a8c5c7c0 100644 (file)
@@ -21,7 +21,10 @@ Turnout::Turnout(Control &c, unsigned a, bool d):
        control(c),
        addr(a),
        path(0),
-       dual(d)
+       pending_path(0),
+       pending_cmds(0),
+       dual(d),
+       on(false)
 {
        control.add_turnout(*this);
 
@@ -30,56 +33,75 @@ 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_path(unsigned p)
+void Turnout::set_path(unsigned char p)
 {
-       signal_path_changing.emit(p);
+       if(path==p || pending_cmds)
+               return;
 
-       path = p;
-       command(true);
-       control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout));
+       signal_path_changing.emit(p);
 
-       signal_path_changed.emit(path);
+       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) | (path&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) | (path&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);
-               path = (path&~b)|(v?b:0);
-               signal_path_changed.emit(path);
+               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 p)