debug(opts.get<unsigned>("debug")),
state_file("arducontrol.state"),
power(false),
+ halted(false),
active_accessory(0),
+ command_timeout(200*Time::msec),
s88(*this),
mfx_search(*this),
thread(*this)
}
}
-void ArduControl::halt(bool)
+void ArduControl::halt(bool h)
{
+ if(h==halted)
+ return;
+
+ halted = h;
+ if(halted)
+ {
+ for(LocomotiveMap::const_iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
+ set_loco_speed(i->first, 0);
+ }
+
+ signal_halt.emit(halted);
}
const char *ArduControl::enumerate_protocols(unsigned i) const
if(speed>protocol_info[loco.proto].max_speed)
throw invalid_argument("ArduControl::set_loco_speed");
+ if(speed && halted)
+ return;
+
if(loco.speed.set(speed))
{
PendingCommand cmd(loco, Locomotive::SPEED);
}
}
- while(!active_accessory && !accessory_queue.empty())
+ while(power && !active_accessory && !accessory_queue.empty())
{
Accessory &acc = *accessory_queue.front();
void ArduControl::flush()
{
+ while(!command_queue.empty() || !accessory_queue.empty())
+ tick();
}
void ArduControl::save_state() const
return true;
}
+template<typename T>
+bool ArduControl::Queue<T>::empty() const
+{
+ return items.empty();
+}
+
ArduControl::RefreshTask::RefreshTask():
next(cycle.end()),
cmd.command[1] = rates[0]>>8;
cmd.command[2] = rates[0];
cmd.length = 3;
- if(do_command(cmd)==COMMAND_OK)
+ if(do_command(cmd, Time::sec)==COMMAND_OK)
{
control.serial.set_baud_rate(rates[0]);
Time::sleep(Time::sec);
- if(do_command(cmd)==COMMAND_OK)
+ if(do_command(cmd, Time::sec)==COMMAND_OK)
{
if(control.debug>=1)
IO::print("Rate changed to %d bits/s\n", rates[0]);
return true;
}
-unsigned ArduControl::ControlThread::do_command(const PendingCommand &cmd)
+unsigned ArduControl::ControlThread::do_command(const PendingCommand &cmd, const Time::TimeDelta &timeout)
{
if(control.debug>=2)
{
if(result)
got_data = IO::poll(control.serial, IO::P_INPUT, Time::zero);
else
- got_data = IO::poll(control.serial, IO::P_INPUT);
+ got_data = IO::poll(control.serial, IO::P_INPUT, timeout);
if(!got_data)
break;
char reply[15];
unsigned pos = 0;
while(pos<rlength)
+ {
+ if(!IO::poll(control.serial, IO::P_INPUT, timeout))
+ return 0;
pos += control.serial.read(reply+pos, rlength-pos);
+ }
if(control.debug>=2)
{