]> git.tdb.fi Git - r2c2.git/commitdiff
Implement signals in Intellibox driver as well
authorMikko Rasa <tdb@tdb.fi>
Fri, 1 Feb 2013 16:56:11 +0000 (18:56 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 1 Feb 2013 17:12:19 +0000 (19:12 +0200)
source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h

index 82623e1760891a4b4e0e9d55a3094eddb5bc1f5e..73c2057942edb295eba784d596336048048cd941 100644 (file)
@@ -193,11 +193,17 @@ void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
 }
 
 void Intellibox::add_turnout(unsigned addr, const TrackType &type)
+{
+       add_turnout(addr, type.get_state_bits(), false);
+}
+
+void 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;
@@ -215,6 +221,14 @@ void Intellibox::add_turnout(unsigned addr, const TrackType &type)
        }
 }
 
+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 &turnout = turnouts[addr];
@@ -223,7 +237,7 @@ 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;
        }
 
@@ -245,6 +259,21 @@ unsigned Intellibox::get_turnout(unsigned addr) const
        return 0;
 }
 
+void Intellibox::add_signal(unsigned addr, const SignalType &)
+{
+       add_turnout(addr, 1, true);
+}
+
+void Intellibox::set_signal(unsigned addr, unsigned state)
+{
+       set_turnout(addr, state);
+}
+
+unsigned Intellibox::get_signal(unsigned addr) const
+{
+       return get_turnout(addr);
+}
+
 void Intellibox::add_sensor(unsigned addr)
 {
        if(!sensors.count(addr))
@@ -472,7 +501,7 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                        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)
@@ -541,7 +570,7 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                        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;
                        }
                }
@@ -573,7 +602,7 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                        {
                                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;
@@ -723,7 +752,8 @@ Intellibox::Turnout::Turnout():
        state(0),
        active(false),
        synced(false),
-       pending(0)
+       pending(0),
+       signal(false)
 { }
 
 
index e35da477236a6bc59e80d2d466678f39b380dd6e..4740a1ec6d6f2ccb611a5b5d2b4913eaaca5c37e 100644 (file)
@@ -85,6 +85,7 @@ private:
                Locomotive();
        };
 
+       // These are used for signals as well, but Intellibox calls them turnouts.
        struct Turnout
        {
                unsigned bits;
@@ -93,6 +94,7 @@ private:
                bool synced;
                unsigned pending;
                Msp::Time::TimeStamp off_timeout;
+               bool signal;
 
                Turnout();
        };
@@ -140,14 +142,18 @@ public:
        virtual void set_loco_function(unsigned, unsigned, bool);
 
        virtual void add_turnout(unsigned, const TrackType &);
+private:
+       void 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;
 
-       // TODO implement these
-       virtual void add_signal(unsigned, const SignalType &) { }
-       virtual void set_signal(unsigned, unsigned) { }
-       virtual unsigned get_signal(unsigned) const { return 0; }
+       virtual void add_signal(unsigned, const SignalType &);
+       virtual void set_signal(unsigned, unsigned);
+       virtual unsigned get_signal(unsigned) const;
 
+public:
        virtual void add_sensor(unsigned);
        virtual void set_sensor(unsigned, bool) { }
        virtual bool get_sensor(unsigned) const;