From: Mikko Rasa Date: Mon, 12 Apr 2010 04:55:33 +0000 (+0000) Subject: Don't send redundant loco commands X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f7ee3202102bfa67fdb6ad93c4f0f90f4c8ffb2c;p=r2c2.git Don't send redundant loco commands Avoid confusing the IntelliBox by sending multiple commands at once in flush --- diff --git a/source/libmarklin/intellibox.cpp b/source/libmarklin/intellibox.cpp index 3588061..ace23e8 100644 --- a/source/libmarklin/intellibox.cpp +++ b/source/libmarklin/intellibox.cpp @@ -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::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;