X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Farducontrol.cpp;h=e29614f6646a5c5fd3068d819c7e89c071a0c154;hb=59b3cebecfd8f1462c95bab447be70dfb5f38e92;hp=0a6c46bd9631769ea0a2e05e2c598c931192cf1e;hpb=708a7e00bf24e3abc08f96ba1cef0ce246a8c0e1;p=r2c2.git diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index 0a6c46b..e29614f 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -27,7 +27,7 @@ ArduControl::ArduControl(const string &dev): PendingCommand cmd; cmd.command[0] = READ_POWER_STATE; cmd.length = 1; - push_command(cmd); + command_queue.push(cmd); cmd.command[0] = MFX_SET_STATION_ID; cmd.command[1] = 'R'; @@ -35,7 +35,7 @@ ArduControl::ArduControl(const string &dev): cmd.command[3] = 'C'; cmd.command[4] = '2'; cmd.length = 5; - push_command(cmd); + command_queue.push(cmd); } ArduControl::~ArduControl() @@ -51,7 +51,7 @@ void ArduControl::set_power(bool p) cmd.tag.serial = power.serial; cmd.command[0] = (p ? POWER_ON : POWER_OFF); cmd.length = 1; - push_command(cmd); + command_queue.push(cmd); } } @@ -123,7 +123,7 @@ void ArduControl::set_loco_speed(unsigned id, unsigned speed) if(loco.speed.set(speed)) { PendingCommand cmd(loco, Locomotive::SPEED); - push_command(cmd); + command_queue.push(cmd); refresh.add_loco(loco); } @@ -135,7 +135,7 @@ void ArduControl::set_loco_reverse(unsigned id, bool rev) if(loco.reverse.set(rev)) { PendingCommand cmd(loco, Locomotive::REVERSE); - push_command(cmd); + command_queue.push(cmd); refresh.add_loco(loco); } @@ -153,7 +153,7 @@ void ArduControl::set_loco_function(unsigned id, unsigned func, bool state) if(func>0 || loco.proto!=MM) { PendingCommand cmd(loco, Locomotive::FUNCTIONS, func); - push_command(cmd); + command_queue.push(cmd); } refresh.add_loco(loco); @@ -273,7 +273,8 @@ bool ArduControl::get_sensor(unsigned addr) const void ArduControl::tick() { - while(Tag tag = pop_completed_tag()) + Tag tag; + while(completed_commands.pop(tag)) { if(tag.type==Tag::GENERAL) { @@ -385,7 +386,7 @@ void ArduControl::tick() for(i=0; (lowest_bit>>i)>1; ++i) ; acc.state.set(acc.state^lowest_bit); PendingCommand cmd(acc, Accessory::ACTIVATE, i); - push_command(cmd); + command_queue.push(cmd); } else accessory_queue.pop_front(); @@ -398,7 +399,7 @@ void ArduControl::tick() { off_timeout = Time::TimeStamp(); PendingCommand cmd(*active_accessory, Accessory::DEACTIVATE); - push_command(cmd); + command_queue.push(cmd); } } } @@ -407,38 +408,6 @@ void ArduControl::flush() { } -void ArduControl::push_command(const PendingCommand &cmd) -{ - MutexLock lock(mutex); - command_queue.push_back(cmd); -} - -bool ArduControl::pop_command(PendingCommand &cmd) -{ - MutexLock lock(mutex); - if(command_queue.empty()) - return false; - cmd = command_queue.front(); - command_queue.pop_front(); - return true; -} - -void ArduControl::push_completed_tag(const Tag &tag) -{ - MutexLock lock(mutex); - completed_commands.push_back(tag); -} - -ArduControl::Tag ArduControl::pop_completed_tag() -{ - MutexLock lock(mutex); - if(completed_commands.empty()) - return Tag(); - Tag tag = completed_commands.front(); - completed_commands.pop_front(); - return tag; -} - ArduControl::Tag::Tag(): type(NONE), @@ -600,6 +569,26 @@ ArduControl::PendingCommand::PendingCommand(Accessory &acc, Accessory::Command c } +template +void ArduControl::Queue::push(const T &item) +{ + MutexLock lock(mutex); + items.push_back(item); +} + +template +bool ArduControl::Queue::pop(T &item) +{ + MutexLock lock(mutex); + if(items.empty()) + return false; + + item = items.front(); + items.pop_front(); + return true; +} + + ArduControl::RefreshTask::RefreshTask(): next(cycle.end()), round(0), @@ -733,7 +722,7 @@ void ArduControl::S88Task::process_reply(const char *reply, unsigned length) tag.command = Sensor::STATE; tag.serial = i->second.state.serial; tag.id = i->first; - control.push_completed_tag(tag); + control.completed_commands.push(tag); } if(count>octets_remaining) @@ -800,7 +789,7 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd) info.address = next_address; info.name = format("%08X", bits); info.id = bits; - push_info(info); + queue.push(info); cmd.command[0] = MFX_ASSIGN_ADDRESS; cmd.command[1] = next_address>>8; @@ -863,20 +852,9 @@ void ArduControl::MfxSearchTask::process_reply(const char *reply, unsigned lengt } } -void ArduControl::MfxSearchTask::push_info(const MfxInfo &info) -{ - MutexLock lock(mutex); - queue.push_back(info); -} - bool ArduControl::MfxSearchTask::pop_info(MfxInfo &info) { - MutexLock lock(mutex); - if(queue.empty()) - return false; - info = queue.back(); - queue.pop_back(); - return true; + return queue.pop(info); } @@ -911,7 +889,7 @@ void ArduControl::ControlThread::main() for(unsigned i=0; (success && i=1) IO::print("ArduControl detected at %d bits/s\n", rate); @@ -965,7 +949,7 @@ void ArduControl::ControlThread::init_baud_rate() bool ArduControl::ControlThread::get_work(PendingCommand &cmd) { - if(control.pop_command(cmd)) + if(control.command_queue.pop(cmd)) return true; for(vector::iterator i=tasks.begin(); i!=tasks.end(); ++i) @@ -1052,7 +1036,7 @@ unsigned ArduControl::ControlThread::process_reply(const char *reply, unsigned r tag.type = Tag::GENERAL; tag.command = POWER; tag.serial = control.power.serial; - control.push_completed_tag(tag); + control.completed_commands.push(tag); } else {