]> git.tdb.fi Git - r2c2.git/commitdiff
Don't send redundant loco commands
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2010 04:55:33 +0000 (04:55 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2010 04:55:33 +0000 (04:55 +0000)
Avoid confusing the IntelliBox by sending multiple commands at once in flush

source/libmarklin/intellibox.cpp

index 35880610145de2d426c4c860bbf4ba9a56e43652..ace23e8f6166762926782fbd2a9dbcb01e290656 100644 (file)
@@ -96,17 +96,21 @@ void Intellibox::add_loco(unsigned addr)
 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
 {
        Locomotive &loco = locos[addr];
+       if(speed==loco.speed)
+               return;
+
        loco.speed = speed;
        loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
-       signal_loco_speed.emit(addr, speed, loco.reverse);
 }
 
 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
 {
        Locomotive &loco = locos[addr];
+       if(rev==loco.reverse)
+               return;
+
        loco.reverse = rev;
        loco_command(addr, loco.speed, rev, loco.funcs|0x100);
-       signal_loco_speed.emit(addr, loco.speed, rev);
 }
 
 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
@@ -230,8 +234,22 @@ void Intellibox::tick()
 
 void Intellibox::flush()
 {
+       Time::TimeStamp t = Time::now();
        for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
+       {
                write(serial_fd, i->data, i->length);
+               pollfd pfd = { serial_fd, POLLIN, 0 };
+               bool first = true;
+               while(poll(&pfd, 1, (first ? -1 : 0))>0)
+               {
+                       char data[16];
+                       read(serial_fd, data, 16);
+                       first = false;
+               }
+       }
+
+       queue.clear();
+       command_sent = false;
 }
 
 void Intellibox::command(Command cmd)
@@ -376,6 +394,18 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                        }
                }
        }
+       else if(cmd==CMD_LOK)
+       {
+               unsigned char err;
+               read_all(&err, 1);
+
+               if(err==ERR_NO_ERROR)
+               {
+                       unsigned addr = queue.front().addr;
+                       Locomotive &loco = locos[addr];
+                       signal_loco_speed.emit(addr, loco.speed, loco.reverse);
+               }
+       }
        else if(cmd==CMD_TURNOUT)
        {
                unsigned char err;