]> git.tdb.fi Git - r2c2.git/commitdiff
Refer to things in the driver with abstract ids instead of addresses
authorMikko Rasa <tdb@tdb.fi>
Thu, 7 Nov 2013 22:01:03 +0000 (00:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 7 Nov 2013 22:01:03 +0000 (00:01 +0200)
Since different protocols can and do have overlapping address spaces, the
address alone isn't sufficient to identify a thing.  Passing both address
and protocol in every function and signal would make the interface too
complicated.

At the moment only locomotives can have a protocol specified, but there's
no technical reason why other types of things couldn't have different
protocols as well.

15 files changed:
source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h
source/libr2c2/centralstation.cpp
source/libr2c2/centralstation.h
source/libr2c2/driver.h
source/libr2c2/dummy.cpp
source/libr2c2/dummy.h
source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h
source/libr2c2/sensor.cpp
source/libr2c2/sensor.h
source/libr2c2/track.cpp
source/libr2c2/track.h
source/libr2c2/train.cpp
source/libr2c2/train.h

index 213a848d95a5d921406fb750bb284fd08818a952..9cf8383bf1eadd997f5b58d4b08a68ee782e6a76 100644 (file)
@@ -70,7 +70,7 @@ unsigned ArduControl::get_protocol_speed_steps(const string &proto_name) const
                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");
@@ -83,19 +83,22 @@ void ArduControl::add_loco(unsigned addr, const string &proto_name, const Vehicl
                        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);
@@ -105,9 +108,9 @@ void ArduControl::set_loco_speed(unsigned addr, unsigned 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);
@@ -117,9 +120,9 @@ void ArduControl::set_loco_reverse(unsigned addr, bool rev)
        }
 }
 
-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)))
        {
@@ -190,12 +193,12 @@ void ArduControl::advance_next_refresh()
        }
 }
 
-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)
@@ -213,9 +216,9 @@ unsigned ArduControl::get_turnout(unsigned addr) const
        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)
@@ -233,7 +236,7 @@ unsigned ArduControl::get_signal(unsigned addr) const
        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);
@@ -247,6 +250,7 @@ void ArduControl::add_accessory(Accessory::Kind kind, unsigned addr, unsigned bi
        }
 
        insert_unique(accessories, addr, Accessory(kind, addr, bits));
+       return addr;
 }
 
 void ArduControl::remove_accessory(Accessory::Kind kind, unsigned addr)
@@ -278,7 +282,7 @@ unsigned ArduControl::get_accessory(Accessory::Kind kind, unsigned addr) const
        return acc.state;
 }
 
-void ArduControl::add_sensor(unsigned addr)
+unsigned ArduControl::add_sensor(unsigned addr)
 {
        if(!addr)
                throw invalid_argument("ArduControl::add_sensor");
@@ -287,6 +291,8 @@ void ArduControl::add_sensor(unsigned addr)
        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)
@@ -314,7 +320,7 @@ void ArduControl::tick()
                }
                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;
 
@@ -322,12 +328,12 @@ void ArduControl::tick()
                        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)
                        {
@@ -337,13 +343,13 @@ void ArduControl::tick()
                                        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;
 
@@ -370,7 +376,7 @@ void ArduControl::tick()
                }
                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;
 
@@ -456,11 +462,12 @@ ArduControl::Tag::Tag():
        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),
@@ -667,7 +674,7 @@ void ArduControl::ControlThread::main()
                                                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);
                                        }
 
@@ -697,7 +704,7 @@ ArduControl::QueuedCommand::QueuedCommand(Locomotive &loco, Locomotive::Command
 {
        tag.type = Tag::LOCOMOTIVE;
        tag.command = cmd;
-       tag.address = loco.address;
+       tag.id = loco.id;
        if(cmd==Locomotive::SPEED)
        {
                tag.serial = loco.speed.serial;
@@ -721,7 +728,7 @@ ArduControl::QueuedCommand::QueuedCommand(Accessory &acc, Accessory::Command cmd
 {
        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;
index cbdecad3a27513c70bb0e168b661d1bd6349a2e8..f0c14f948219afce5bea6c4e2a2559d8a606f375 100644 (file)
@@ -52,7 +52,7 @@ private:
                Type type;
                unsigned char command;
                unsigned short serial;
-               unsigned address;
+               unsigned id;
 
                Tag();
 
@@ -95,6 +95,7 @@ private:
                        FUNCTIONS
                };
 
+               unsigned id;
                Protocol proto;
                unsigned address;
                ControlledVariable<unsigned> speed;
@@ -216,7 +217,7 @@ private:
 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);
@@ -228,24 +229,24 @@ private:
        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;
index 2138e10702ee27497e70a641edc8fbd12d9d20ab..124c1beed8aa14efba0fc96bc59a8ab17ca7a709 100644 (file)
@@ -88,7 +88,7 @@ unsigned CentralStation::get_protocol_speed_steps(const string &name) 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);
 
@@ -109,6 +109,8 @@ void CentralStation::add_loco(unsigned addr, const string &proto_name, const Veh
        }
        else
                command(format("request(%d, view, control, force)", id));
+
+       return addr;
 }
 
 void CentralStation::remove_loco(unsigned addr)
@@ -147,7 +149,7 @@ void CentralStation::set_loco_function(unsigned addr, unsigned func, bool state)
                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;
@@ -180,6 +182,8 @@ void CentralStation::add_turnout(unsigned addr, const TrackType &type)
 
        MagnetAccessory &turnout = add_accessory(addr, MagnetAccessory::TURNOUT, symbol);
        turnout.bits = type.get_state_bits();
+
+       return addr;
 }
 
 void CentralStation::remove_turnout(unsigned addr)
@@ -197,9 +201,10 @@ unsigned CentralStation::get_turnout(unsigned addr) const
        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)
@@ -298,7 +303,7 @@ void CentralStation::accessory_state_changed(const MagnetAccessory &accessory) c
                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()));
 
@@ -307,6 +312,8 @@ void CentralStation::add_sensor(unsigned addr)
                if(addr>s88.size()*16)
                        command("create(26, add[0])");
        }
+
+       return addr;
 }
 
 void CentralStation::remove_sensor(unsigned)
index a34a3e361aeaffe8af82e36cf7e123346c5edad4..acfc7d6c807a35479586d60045879755d01163e4 100644 (file)
@@ -137,18 +137,18 @@ public:
        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;
@@ -161,7 +161,7 @@ private:
        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;
index 9648c921354c4d8fbd4c85c494eb3e3bd0b9f474..101426026a15b2c6577e7aceb27376e7c47540d8 100644 (file)
@@ -33,23 +33,23 @@ public:
 
        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;
index cb625dd6bf1f691650b72b8a820e394912f1bbea..9ccb1bcceb94f6fe9630b8ad04e328a5feffcec8 100644 (file)
@@ -42,9 +42,10 @@ unsigned Dummy::get_protocol_speed_steps(const string &) const
        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)
index 933fce61705a6355fc6a2fb2e1f7f25bf482886d..4c43d14a6910be161dbbe555ffe6909da1698372 100644 (file)
@@ -40,23 +40,23 @@ public:
 
        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;
index 338aed2544127844dff4de200cf7bbffc90f2a2a..975c143fe46baf904d00db9b19a3777c68d627a1 100644 (file)
@@ -93,7 +93,7 @@ unsigned Intellibox::get_protocol_speed_steps(const string &proto_name) 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);
 
@@ -112,6 +112,8 @@ void Intellibox::add_loco(unsigned addr, const string &proto_name, const Vehicle
                data[1] = (addr>>8)&0xFF;
                command(CMD_LOK_STATUS, addr, data, 2);
        }
+
+       return addr;
 }
 
 void Intellibox::remove_loco(unsigned addr)
@@ -197,9 +199,9 @@ void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
        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)
@@ -207,7 +209,7 @@ 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))
        {
@@ -229,6 +231,8 @@ void Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal)
                        command(CMD_TURNOUT_STATUS, addr+i, data, 2);
                }
        }
+
+       return addr;
 }
 
 void Intellibox::turnout_state_changed(unsigned addr, const Turnout &turnout) const
@@ -269,9 +273,9 @@ unsigned Intellibox::get_turnout(unsigned addr) 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)
@@ -289,13 +293,15 @@ unsigned Intellibox::get_signal(unsigned addr) const
        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)
index 46c769b925053e60553596841663816c371d7144..8058aabbc5cc1883c211b0493d0ca8ae77ece2d8 100644 (file)
@@ -136,28 +136,28 @@ public:
 
        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;
index 618a06b7b609c6ac8a930bc62c197bb4ac2db768..ecd06473fb4bdf2234a2c149f19d0b3ae1af849e 100644 (file)
@@ -9,6 +9,7 @@ namespace R2C2 {
 Sensor::Sensor(Layout &l):
        layout(l),
        address(0),
+       id(0),
        invert(false),
        state(INACTIVE)
 {
@@ -28,11 +29,13 @@ Sensor::~Sensor()
 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)
@@ -52,9 +55,9 @@ 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)
                {
index 13aaca3fe8e9a060e94db659372ecfc3433cf13e..24f0fff401184c471186a33b146b5bdcce78897a 100644 (file)
@@ -25,6 +25,7 @@ public:
 protected:
        Layout &layout;
        unsigned address;
+       unsigned id;
        bool invert;
        State state;
        Msp::Time::TimeDelta state_confirm_timeout;
index 44d992ba07290bef90ded2471b99c36c029db89c..26886344b0a1312a9bb8023c065ed6220fdcb70c 100644 (file)
@@ -46,7 +46,7 @@ Track::Track(Layout &l, const TrackType &t):
                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));
                }
        }
@@ -60,8 +60,8 @@ Track::Track(Layout &l, const TrackType &t):
 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);
 }
 
@@ -166,13 +166,15 @@ void Track::set_turnout_address(unsigned a)
 
        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)
@@ -186,14 +188,14 @@ 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
@@ -406,9 +408,9 @@ void Track::save(list<DataFile::Statement> &st) 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;
index 1d8cd714bf279a50dd726abf1852bc85955585c7..ad4ad21066b9dfc9fe7f18985bf6795c6d94c7c5 100644 (file)
@@ -43,6 +43,7 @@ private:
        float slope;
        bool flex;
        unsigned turnout_addr;
+       unsigned turnout_id;
        unsigned sensor_addr;
        std::vector<Track *> links;
        unsigned active_path;
index 50ad137723d42ec2a8c10775767a3de92c91d745..bb82fe4c34090cf22b53f90cda41aa74478dcc01 100644 (file)
@@ -58,7 +58,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
 
        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));
 
@@ -140,7 +140,7 @@ void Train::set_function(unsigned func, bool state)
 {
        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
@@ -317,7 +317,7 @@ void Train::tick(const Time::TimeDelta &dt)
                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();
@@ -329,7 +329,7 @@ void Train::tick(const Time::TimeDelta &dt)
                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;
                }
@@ -404,24 +404,24 @@ void Train::control_changed(const Controller::Control &ctrl)
        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;
index 340537a89a401242fd08cabb2c004459892ec36e..16e67a43986776890b17734b54c9c15f91461478 100644 (file)
@@ -51,6 +51,7 @@ private:
        const VehicleType &loco_type;
        unsigned address;
        std::string protocol;
+       unsigned loco_id;
        std::string name;
        const Train *preceding_train;
        std::vector<Vehicle *> vehicles;