namespace R2C2 {
-Intellibox::Intellibox(const string &dev):
- serial(dev),
+Intellibox::Intellibox(const Options &opts):
+ serial(opts.get<string>(string(), "ttyUSB0")),
power(false),
halted(false),
update_sensors(false),
return 0;
}
-void Intellibox::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
+unsigned Intellibox::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
{
Protocol proto = map_protocol(proto_name);
data[1] = (addr>>8)&0xFF;
command(CMD_LOK_STATUS, addr, data, 2);
}
+
+ return addr;
+}
+
+void Intellibox::remove_loco(unsigned addr)
+{
+ locos.erase(addr);
}
void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
signal_loco_function.emit(addr, func, state);
}
-void Intellibox::add_turnout(unsigned addr, const TrackType &type)
+unsigned Intellibox::add_turnout(unsigned addr, const TrackType &type)
+{
+ return add_turnout(addr, type.get_state_bits(), false);
+}
+
+void Intellibox::remove_turnout(unsigned addr)
+{
+ turnouts.erase(addr);
+}
+
+unsigned Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal)
{
if(!turnouts.count(addr))
{
Turnout &turnout = turnouts[addr];
- turnout.bits = type.get_state_bits();
+ turnout.bits = bits;
+ turnout.signal = signal;
unsigned char data[2];
data[0] = addr&0xFF;
command(CMD_TURNOUT_STATUS, addr+i, data, 2);
}
}
+
+ return addr;
+}
+
+void Intellibox::turnout_state_changed(unsigned addr, const Turnout &turnout) const
+{
+ if(turnout.signal)
+ signal_signal.emit(addr, turnout.state);
+ else
+ signal_turnout.emit(addr, turnout.state);
}
void Intellibox::set_turnout(unsigned addr, unsigned state)
{
turnout.state = state;
turnout.pending = state;
- signal_turnout.emit(addr, state);
+ turnout_state_changed(addr, turnout);
return;
}
return 0;
}
-void Intellibox::add_sensor(unsigned addr)
+unsigned Intellibox::add_signal(unsigned addr, const SignalType &)
+{
+ return add_turnout(addr, 1, true);
+}
+
+void Intellibox::remove_signal(unsigned addr)
+{
+ remove_turnout(addr);
+}
+
+void Intellibox::set_signal(unsigned addr, unsigned state)
+{
+ set_turnout(addr, state);
+}
+
+unsigned Intellibox::get_signal(unsigned addr) const
+{
+ return get_turnout(addr);
+}
+
+unsigned Intellibox::add_sensor(unsigned addr)
{
if(!sensors.count(addr))
{
sensors[addr];
update_sensors = true;
}
+
+ return addr;
+}
+
+void Intellibox::remove_sensor(unsigned addr)
+{
+ sensors.erase(addr);
+ update_sensors = true;
}
bool Intellibox::get_sensor(unsigned addr) const
unsigned bit = !(data[1]&0x80);
turnout.state = (turnout.state&~mask) | (bit*mask);
turnout.pending = turnout.state;
- signal_turnout.emit(addr, turnout.state);
+ turnout_state_changed(addr,turnout);
}
}
else if(cmd==CMD_EVENT_SENSOR)
if(turnout.active)
{
if(turnout.state==turnout.pending)
- signal_turnout.emit(addr, turnout.state);
+ turnout_state_changed(addr, turnout);
turnout.off_timeout = t+500*Time::msec;
}
}
{
turnout.state = (turnout.state&~mask) | (bit*mask);
turnout.pending = turnout.state;
- signal_turnout.emit(addr, turnout.state);
+ turnout_state_changed(addr, turnout);
}
turnout.synced = true;
Intellibox::Locomotive::Locomotive():
+ protocol(NONE),
ext_func(false),
speed(0),
reverse(false),
- funcs(0)
+ funcs(0),
+ pending_half_step(0)
{ }
state(0),
active(false),
synced(false),
- pending(0)
+ pending(0),
+ signal(false)
{ }