if(acc.kind!=kind)
throw key_error(addr);
- if(state!=acc.target)
+ if(state!=acc.target || acc.uncertain)
{
acc.target = state;
accessory_queue.push_back(&acc);
{
if(acc.state.commit(tag.serial))
{
- if(acc.state==acc.target)
- {
- if(acc.kind==Accessory::TURNOUT)
- signal_turnout.emit(acc.address, acc.state);
- else if(acc.kind==Accessory::SIGNAL)
- signal_signal.emit(acc.address, acc.state);
- }
if(&acc==active_accessory)
active_accessory = 0;
}
{
Accessory &acc = *accessory_queue.front();
- if(acc.state!=acc.target)
+ if(acc.state!=acc.target || acc.uncertain)
{
- active_accessory = &acc;
-
unsigned changes = acc.state^acc.target;
unsigned lowest_bit = changes&~(changes-1);
- unsigned i;
- for(i=0; (lowest_bit>>i)>1; ++i) ;
- active_index = i;
- acc.state.set(acc.state^lowest_bit);
- PendingCommand cmd(acc, Accessory::ACTIVATE, i);
- command_queue.push(cmd);
-
- monitor.reset_peak();
+ if(lowest_bit>>acc.bits)
+ {
+ // All remaining changes are in non-physical bits
+ acc.state.set(acc.state^changes);
+ acc.state.commit(acc.state.serial);
+ }
+ else
+ {
+ unsigned mask = (lowest_bit ? lowest_bit : acc.uncertain);
+ for(active_index=0; (mask>>active_index)>1; ++active_index) ;
+ acc.state.set(acc.state^lowest_bit);
+ PendingCommand cmd(acc, Accessory::ACTIVATE, active_index);
+ command_queue.push(cmd);
+ active_accessory = &acc;
+
+ monitor.reset_peak();
+ }
}
else
+ {
accessory_queue.pop_front();
+
+ if(acc.state==acc.target)
+ {
+ if(acc.kind==Accessory::TURNOUT)
+ signal_turnout.emit(acc.address, acc.state);
+ else if(acc.kind==Accessory::SIGNAL)
+ signal_signal.emit(acc.address, acc.state);
+ }
+ }
}
if(active_accessory && off_timeout)
{
Accessory &acc = *active_accessory;
- if(acc.kind==Accessory::TURNOUT && monitor.get_peak()<0.5f)
+ unsigned bit = 1<<active_index;
+
+ // Assume success if we were uncertain of the physical setting
+ if(acc.uncertain&bit)
+ acc.uncertain &= ~bit;
+ else if(acc.kind==Accessory::TURNOUT && monitor.get_peak()<0.5f)
{
- unsigned bit = 1<<active_index;
- if(acc.uncertain&bit)
- acc.uncertain &= ~bit;
- else
- {
- signal_turnout_failed.emit(acc.address);
- acc.state.rollback();
- acc.target ^= bit;
- }
+ signal_turnout_failed.emit(acc.address);
+ acc.state.rollback();
+ acc.target ^= bit;
}
off_timeout = Time::TimeStamp();