return 0;
}
-void ArduControl::add_loco(unsigned addr, const string &proto_name, const VehicleType &)
+unsigned ArduControl::add_loco(unsigned addr, const string &proto_name, const VehicleType &)
{
if(!addr)
throw invalid_argument("ArduControl::add_loco");
throw invalid_argument("ArduControl::add_loco");
}
- insert_unique(locomotives, addr, Locomotive(proto, addr));
+ Locomotive loco(proto, addr);
+ insert_unique(locomotives, loco.id, loco);
+
+ return loco.id;
}
-void ArduControl::remove_loco(unsigned addr)
+void ArduControl::remove_loco(unsigned id)
{
- Locomotive &loco = get_item(locomotives, addr);
+ Locomotive &loco = get_item(locomotives, id);
remove_loco_from_refresh(loco);
- locomotives.erase(addr);
+ locomotives.erase(id);
}
-void ArduControl::set_loco_speed(unsigned addr, unsigned speed)
+void ArduControl::set_loco_speed(unsigned id, unsigned speed)
{
- Locomotive &loco = get_item(locomotives, addr);
+ Locomotive &loco = get_item(locomotives, id);
if(loco.speed.set(speed))
{
QueuedCommand cmd(loco, Locomotive::SPEED);
}
}
-void ArduControl::set_loco_reverse(unsigned addr, bool rev)
+void ArduControl::set_loco_reverse(unsigned id, bool rev)
{
- Locomotive &loco = get_item(locomotives, addr);
+ Locomotive &loco = get_item(locomotives, id);
if(loco.reverse.set(rev))
{
QueuedCommand cmd(loco, Locomotive::REVERSE);
}
}
-void ArduControl::set_loco_function(unsigned addr, unsigned func, bool state)
+void ArduControl::set_loco_function(unsigned id, unsigned func, bool state)
{
- Locomotive &loco = get_item(locomotives, addr);
+ Locomotive &loco = get_item(locomotives, id);
unsigned mask = 1<<func;
if(loco.funcs.set((loco.funcs&~mask)|(mask*state)))
{
}
}
-void ArduControl::add_turnout(unsigned addr, const TrackType &type)
+unsigned ArduControl::add_turnout(unsigned addr, const TrackType &type)
{
if(!addr || !type.is_turnout())
throw invalid_argument("ArduControl::add_turnout");
- add_accessory(Accessory::TURNOUT, addr, type.get_state_bits());
+ return add_accessory(Accessory::TURNOUT, addr, type.get_state_bits());
}
void ArduControl::remove_turnout(unsigned addr)
return get_accessory(Accessory::TURNOUT, addr);
}
-void ArduControl::add_signal(unsigned addr, const SignalType &)
+unsigned ArduControl::add_signal(unsigned addr, const SignalType &)
{
- add_accessory(Accessory::SIGNAL, addr, 1);
+ return add_accessory(Accessory::SIGNAL, addr, 1);
}
void ArduControl::remove_signal(unsigned addr)
return get_accessory(Accessory::SIGNAL, addr);
}
-void ArduControl::add_accessory(Accessory::Kind kind, unsigned addr, unsigned bits)
+unsigned ArduControl::add_accessory(Accessory::Kind kind, unsigned addr, unsigned bits)
{
AccessoryMap::iterator i = accessories.lower_bound(addr);
AccessoryMap::iterator j = accessories.upper_bound(addr+bits-1);
}
insert_unique(accessories, addr, Accessory(kind, addr, bits));
+ return addr;
}
void ArduControl::remove_accessory(Accessory::Kind kind, unsigned addr)
return acc.state;
}
-void ArduControl::add_sensor(unsigned addr)
+unsigned ArduControl::add_sensor(unsigned addr)
{
if(!addr)
throw invalid_argument("ArduControl::add_sensor");
unsigned octet_index = (addr-1)/8;
if(octet_index>=n_s88_octets)
n_s88_octets = octet_index+1;
+
+ return addr;
}
void ArduControl::remove_sensor(unsigned addr)
}
else if(tag.type==Tag::LOCOMOTIVE)
{
- LocomotiveMap::iterator i = locomotives.find(tag.address);
+ LocomotiveMap::iterator i = locomotives.find(tag.id);
if(i==locomotives.end())
continue;
if(tag.command==Locomotive::SPEED)
{
if(loco.speed.commit(tag.serial))
- signal_loco_speed.emit(loco.address, loco.speed, loco.reverse);
+ signal_loco_speed.emit(loco.id, loco.speed, loco.reverse);
}
else if(tag.command==Locomotive::REVERSE)
{
if(loco.reverse.commit(tag.serial))
- signal_loco_speed.emit(loco.address, loco.speed, loco.reverse);
+ signal_loco_speed.emit(loco.id, loco.speed, loco.reverse);
}
else if(tag.command==Locomotive::FUNCTIONS)
{
unsigned changed = old^loco.funcs;
for(unsigned j=0; changed>>j; ++j)
if((changed>>j)&1)
- signal_loco_function.emit(loco.address, j, (loco.funcs>>j)&1);
+ signal_loco_function.emit(loco.id, j, (loco.funcs>>j)&1);
}
}
}
else if(tag.type==Tag::ACCESSORY)
{
- AccessoryMap::iterator i = accessories.find(tag.address);
+ AccessoryMap::iterator i = accessories.find(tag.id);
if(i==accessories.end())
continue;
}
else if(tag.type==Tag::SENSOR)
{
- SensorMap::iterator i = sensors.find(tag.address);
+ SensorMap::iterator i = sensors.find(tag.id);
if(i==sensors.end())
continue;
type(NONE),
command(0),
serial(0),
- address(0)
+ id(0)
{ }
ArduControl::Locomotive::Locomotive(Protocol p, unsigned a):
+ id((p<<16)|a),
proto(p),
address(a),
speed(0),
stag.type = Tag::SENSOR;
stag.command = Sensor::STATE;
stag.serial = i->second.state.serial;
- stag.address = i->first;
+ stag.id = i->first;
control.push_completed_tag(stag);
}
{
tag.type = Tag::LOCOMOTIVE;
tag.command = cmd;
- tag.address = loco.address;
+ tag.id = loco.id;
if(cmd==Locomotive::SPEED)
{
tag.serial = loco.speed.serial;
{
tag.type = Tag::ACCESSORY;
tag.command = cmd;
- tag.address = acc.address;
+ tag.id = acc.address;
if(cmd==Accessory::ACTIVATE || cmd==Accessory::DEACTIVATE)
{
tag.serial = acc.state.serial;
Type type;
unsigned char command;
unsigned short serial;
- unsigned address;
+ unsigned id;
Tag();
FUNCTIONS
};
+ unsigned id;
Protocol proto;
unsigned address;
ControlledVariable<unsigned> speed;
public:
virtual unsigned get_protocol_speed_steps(const std::string &) const;
- virtual void add_loco(unsigned, const std::string &, const VehicleType &);
+ virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
virtual void remove_loco(unsigned);
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
void advance_next_refresh();
public:
- virtual void add_turnout(unsigned, const TrackType &);
+ virtual unsigned add_turnout(unsigned, const TrackType &);
virtual void remove_turnout(unsigned);
virtual void set_turnout(unsigned, unsigned);
virtual unsigned get_turnout(unsigned) const;
- virtual void add_signal(unsigned, const SignalType &);
+ virtual unsigned add_signal(unsigned, const SignalType &);
virtual void remove_signal(unsigned);
virtual void set_signal(unsigned, unsigned);
virtual unsigned get_signal(unsigned) const;
private:
- void add_accessory(Accessory::Kind, unsigned, unsigned);
+ unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
void remove_accessory(Accessory::Kind, unsigned);
void set_accessory(Accessory::Kind, unsigned, unsigned);
unsigned get_accessory(Accessory::Kind, unsigned) const;
public:
- virtual void add_sensor(unsigned);
+ virtual unsigned add_sensor(unsigned);
virtual void remove_sensor(unsigned);
virtual void set_sensor(unsigned, bool) { }
virtual bool get_sensor(unsigned) const;
}
}
-void CentralStation::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
+unsigned CentralStation::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
{
Protocol proto = map_protocol(proto_name);
}
else
command(format("request(%d, view, control, force)", id));
+
+ return addr;
}
void CentralStation::remove_loco(unsigned addr)
command(format("set(%d, func[%d, %d])", id, func, state));
}
-void CentralStation::add_turnout(unsigned addr, const TrackType &type)
+unsigned CentralStation::add_turnout(unsigned addr, const TrackType &type)
{
unsigned straight = type.get_paths();
bool left = false;
MagnetAccessory &turnout = add_accessory(addr, MagnetAccessory::TURNOUT, symbol);
turnout.bits = type.get_state_bits();
+
+ return addr;
}
void CentralStation::remove_turnout(unsigned addr)
return get_accessory_state(addr, MagnetAccessory::TURNOUT);
}
-void CentralStation::add_signal(unsigned addr, const SignalType &)
+unsigned CentralStation::add_signal(unsigned addr, const SignalType &)
{
add_accessory(addr, MagnetAccessory::SIGNAL, MagnetAccessory::SEMAPHORE_HOME);
+ return addr;
}
void CentralStation::remove_signal(unsigned addr)
signal_signal.emit(accessory.address, accessory.state);
}
-void CentralStation::add_sensor(unsigned addr)
+unsigned CentralStation::add_sensor(unsigned addr)
{
sensors.insert(SensorMap::value_type(addr, Sensor()));
if(addr>s88.size()*16)
command("create(26, add[0])");
}
+
+ return addr;
}
void CentralStation::remove_sensor(unsigned)
virtual const char *enumerate_protocols(unsigned) const;
virtual unsigned get_protocol_speed_steps(const std::string &) const;
- virtual void add_loco(unsigned, const std::string &, const VehicleType &);
+ virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
virtual void remove_loco(unsigned);
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
virtual void set_loco_function(unsigned, unsigned, bool);
- virtual void add_turnout(unsigned, const TrackType &);
+ virtual unsigned add_turnout(unsigned, const TrackType &);
virtual void remove_turnout(unsigned);
virtual void set_turnout(unsigned, unsigned);
virtual unsigned get_turnout(unsigned) const;
- virtual void add_signal(unsigned, const SignalType &);
+ virtual unsigned add_signal(unsigned, const SignalType &);
virtual void remove_signal(unsigned);
virtual void set_signal(unsigned, unsigned);
virtual unsigned get_signal(unsigned) const;
void accessory_state_changed(const MagnetAccessory &) const;
public:
- virtual void add_sensor(unsigned);
+ virtual unsigned add_sensor(unsigned);
virtual void remove_sensor(unsigned);
virtual void set_sensor(unsigned, bool) { }
virtual bool get_sensor(unsigned) const;
virtual const char *enumerate_protocols(unsigned) const = 0;
virtual unsigned get_protocol_speed_steps(const std::string &) const = 0;
- virtual void add_loco(unsigned, const std::string &, const VehicleType &) = 0;
+ virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &) = 0;
virtual void remove_loco(unsigned) = 0;
virtual void set_loco_speed(unsigned, unsigned) = 0;
virtual void set_loco_reverse(unsigned, bool) = 0;
virtual void set_loco_function(unsigned, unsigned, bool) = 0;
- virtual void add_turnout(unsigned, const TrackType &) = 0;
+ virtual unsigned add_turnout(unsigned, const TrackType &) = 0;
virtual void remove_turnout(unsigned) = 0;
virtual void set_turnout(unsigned, unsigned) = 0;
virtual unsigned get_turnout(unsigned) const = 0;
- virtual void add_signal(unsigned, const SignalType &) = 0;
+ virtual unsigned add_signal(unsigned, const SignalType &) = 0;
virtual void remove_signal(unsigned) = 0;
virtual void set_signal(unsigned, unsigned) = 0;
virtual unsigned get_signal(unsigned) const = 0;
- virtual void add_sensor(unsigned) = 0;
+ virtual unsigned add_sensor(unsigned) = 0;
virtual void remove_sensor(unsigned) = 0;
virtual void set_sensor(unsigned, bool) = 0;
virtual bool get_sensor(unsigned) const = 0;
return 0;
}
-void Dummy::add_turnout(unsigned addr, const TrackType &)
+unsigned Dummy::add_turnout(unsigned addr, const TrackType &)
{
turnouts[addr];
+ return addr;
}
void Dummy::set_turnout(unsigned addr, unsigned state)
virtual const char *enumerate_protocols(unsigned) const;
virtual unsigned get_protocol_speed_steps(const std::string &) const;
- virtual void add_loco(unsigned, const std::string &, const VehicleType &) { }
+ virtual unsigned add_loco(unsigned a, const std::string &, const VehicleType &) { return a; }
virtual void remove_loco(unsigned) { }
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
virtual void set_loco_function(unsigned, unsigned, bool);
- virtual void add_turnout(unsigned, const TrackType &);
+ virtual unsigned add_turnout(unsigned, const TrackType &);
virtual void remove_turnout(unsigned) { }
virtual void set_turnout(unsigned, unsigned);
virtual unsigned get_turnout(unsigned) const;
- virtual void add_signal(unsigned, const SignalType &) { }
+ virtual unsigned add_signal(unsigned a, const SignalType &) { return a; }
virtual void remove_signal(unsigned) { }
virtual void set_signal(unsigned, unsigned) { }
virtual unsigned get_signal(unsigned) const { return 0; }
- virtual void add_sensor(unsigned) { }
+ virtual unsigned add_sensor(unsigned a) { return a; }
virtual void remove_sensor(unsigned) { }
virtual void set_sensor(unsigned, bool);
virtual bool get_sensor(unsigned) const;
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)
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)
{
- add_turnout(addr, type.get_state_bits(), false);
+ return add_turnout(addr, type.get_state_bits(), false);
}
void Intellibox::remove_turnout(unsigned addr)
turnouts.erase(addr);
}
-void Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal)
+unsigned Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal)
{
if(!turnouts.count(addr))
{
command(CMD_TURNOUT_STATUS, addr+i, data, 2);
}
}
+
+ return addr;
}
void Intellibox::turnout_state_changed(unsigned addr, const Turnout &turnout) const
return 0;
}
-void Intellibox::add_signal(unsigned addr, const SignalType &)
+unsigned Intellibox::add_signal(unsigned addr, const SignalType &)
{
- add_turnout(addr, 1, true);
+ return add_turnout(addr, 1, true);
}
void Intellibox::remove_signal(unsigned addr)
return get_turnout(addr);
}
-void Intellibox::add_sensor(unsigned addr)
+unsigned Intellibox::add_sensor(unsigned addr)
{
if(!sensors.count(addr))
{
sensors[addr];
update_sensors = true;
}
+
+ return addr;
}
void Intellibox::remove_sensor(unsigned addr)
virtual const char *enumerate_protocols(unsigned) const;
virtual unsigned get_protocol_speed_steps(const std::string &) const;
- virtual void add_loco(unsigned, const std::string &, const VehicleType &);
+ virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
virtual void remove_loco(unsigned);
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
virtual void set_loco_function(unsigned, unsigned, bool);
- virtual void add_turnout(unsigned, const TrackType &);
+ virtual unsigned add_turnout(unsigned, const TrackType &);
virtual void remove_turnout(unsigned);
private:
- void add_turnout(unsigned, unsigned, bool);
+ unsigned add_turnout(unsigned, unsigned, bool);
void turnout_state_changed(unsigned, const Turnout &) const;
public:
virtual void set_turnout(unsigned, unsigned);
virtual unsigned get_turnout(unsigned) const;
- virtual void add_signal(unsigned, const SignalType &);
+ virtual unsigned add_signal(unsigned, const SignalType &);
virtual void remove_signal(unsigned);
virtual void set_signal(unsigned, unsigned);
virtual unsigned get_signal(unsigned) const;
public:
- virtual void add_sensor(unsigned);
+ virtual unsigned add_sensor(unsigned);
virtual void remove_sensor(unsigned);
virtual void set_sensor(unsigned, bool) { }
virtual bool get_sensor(unsigned) const;
Sensor::Sensor(Layout &l):
layout(l),
address(0),
+ id(0),
invert(false),
state(INACTIVE)
{
void Sensor::set_address(unsigned a)
{
Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
- if(driver && address)
- driver->remove_sensor(address);
+ if(driver && id)
+ driver->remove_sensor(id);
address = a;
if(driver && address)
- driver->add_sensor(address);
+ id = driver->add_sensor(address);
+ else
+ id = 0;
}
void Sensor::tick(const Time::TimeDelta &dt)
}
}
-void Sensor::event(unsigned a, bool s)
+void Sensor::event(unsigned i, bool s)
{
- if(a==address)
+ if(i==id)
{
if(s!=invert && state<MAYBE_ACTIVE)
{
protected:
Layout &layout;
unsigned address;
+ unsigned id;
bool invert;
State state;
Msp::Time::TimeDelta state_confirm_timeout;
if(layout.has_driver())
{
Driver &driver = layout.get_driver();
- driver.add_turnout(turnout_addr, type);
+ turnout_id = driver.add_turnout(turnout_addr, type);
driver.signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
}
}
Track::~Track()
{
break_links();
- if(layout.has_driver() && turnout_addr)
- layout.get_driver().remove_turnout(turnout_addr);
+ if(layout.has_driver() && turnout_id)
+ layout.get_driver().remove_turnout(turnout_id);
layout.remove(*this);
}
Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
- if(driver && turnout_addr)
- driver->remove_turnout(turnout_addr);
+ if(driver && turnout_id)
+ driver->remove_turnout(turnout_id);
turnout_addr = a;
layout.create_blocks(*this);
layout.update_routes();
if(driver && turnout_addr)
- driver->add_turnout(turnout_addr, type);
+ turnout_id = driver->add_turnout(turnout_addr, type);
+ else
+ turnout_id = 0;
}
void Track::set_sensor_address(unsigned a)
void Track::set_active_path(unsigned p)
{
- if(!turnout_addr)
+ if(!type.is_turnout())
throw logic_error("not a turnout");
if(!(type.get_paths()&(1<<p)))
throw invalid_argument("Track::set_active_path");
signal_path_changing(p);
path_changing = true;
- layout.get_driver().set_turnout(turnout_addr, p);
+ layout.get_driver().set_turnout(turnout_id, p);
}
float Track::get_path_length(int p) const
st.push_back((DataFile::Statement("flex"), true));
}
-void Track::turnout_event(unsigned addr, unsigned state)
+void Track::turnout_event(unsigned id, unsigned state)
{
- if(addr==turnout_addr)
+ if(id==turnout_id)
{
active_path = state;
path_changing = false;
float slope;
bool flex;
unsigned turnout_addr;
+ unsigned turnout_id;
unsigned sensor_addr;
std::vector<Track *> links;
unsigned active_path;
layout.add_train(*this);
- layout.get_driver().add_loco(address, protocol, loco_type);
+ loco_id = layout.get_driver().add_loco(address, protocol, loco_type);
layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event));
layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
{
if(!loco_type.get_functions().count(func))
throw invalid_argument("Train::set_function");
- layout.get_driver().set_loco_function(address, func, state);
+ layout.get_driver().set_loco_function(loco_id, func, state);
}
float Train::get_control(const string &ctrl) const
bool r = reverse;
if(loco_type.get_swap_direction())
r = !r;
- driver.set_loco_reverse(address, r);
+ driver.set_loco_reverse(loco_id, r);
allocator.reverse();
last_entry_block = BlockIter();
if(speed_step!=current_speed_step && !speed_changing && !driver.is_halted() && driver.get_power())
{
speed_changing = true;
- driver.set_loco_speed(address, speed_step);
+ driver.set_loco_speed(loco_id, speed_step);
pure_speed = false;
}
signal_control_changed.emit(ctrl.name, ctrl.value);
}
-void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev)
+void Train::loco_speed_event(unsigned id, unsigned speed, bool rev)
{
- if(addr==address)
+ if(id==loco_id)
{
current_speed_step = speed;
bool r = reverse;
if(loco_type.get_swap_direction())
r = !r;
if(rev!=r)
- layout.get_driver().set_loco_reverse(address, r);
+ layout.get_driver().set_loco_reverse(loco_id, r);
speed_changing = false;
pure_speed = false;
}
}
-void Train::loco_func_event(unsigned addr, unsigned func, bool state)
+void Train::loco_func_event(unsigned id, unsigned func, bool state)
{
- if(addr==address)
+ if(id==loco_id)
{
if(state)
functions |= 1<<func;
const VehicleType &loco_type;
unsigned address;
std::string protocol;
+ unsigned loco_id;
std::string name;
const Train *preceding_train;
std::vector<Vehicle *> vehicles;