]> git.tdb.fi Git - r2c2.git/commitdiff
Store and export information about detected MFX locomotives
authorMikko Rasa <tdb@tdb.fi>
Wed, 13 Nov 2013 06:28:22 +0000 (08:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 13 Nov 2013 06:28:22 +0000 (08:28 +0200)
source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h

index 07a2215c4cfb4386940acdf185530cbefd9204e5..47584f44bb8b145496a0b6d9a52c118f4c68fcd9 100644 (file)
@@ -84,6 +84,14 @@ unsigned ArduControl::get_protocol_speed_steps(const string &proto_name) const
        return protocol_info[map_protocol(proto_name)].max_speed;
 }
 
+const Driver::DetectedLocomotive *ArduControl::enumerate_detected_locos(unsigned i) const
+{
+       if(i>=mfx_info.size())
+               return 0;
+
+       return &mfx_info[i];
+}
+
 unsigned ArduControl::add_loco(unsigned addr, const string &proto_name, const VehicleType &)
 {
        if(!addr)
@@ -274,6 +282,23 @@ void ArduControl::tick()
                                if(power.commit(tag.serial))
                                        signal_power.emit(power.current);
                        }
+                       else if(tag.command==NEW_LOCO)
+                       {
+                               MfxInfo info;
+                               if(mfx_search.pop_info(info))
+                               {
+                                       MfxInfoArray::iterator i;
+                                       for(i=mfx_info.begin(); (i!=mfx_info.end() && i->id!=info.id); ++i) ;
+                                       if(i==mfx_info.end())
+                                       {
+                                               mfx_info.push_back(info);
+                                               i = --mfx_info.end();
+                                       }
+                                       else
+                                               *i = info;
+                                       signal_locomotive_detected.emit(*i);
+                               }
+                       }
                }
                else if(tag.type==Tag::LOCOMOTIVE)
                {
@@ -770,6 +795,13 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd)
                if(control.debug>=1)
                        IO::print("Assigning MFX address %d to decoder %08X\n", next_address, bits);
 
+               MfxInfo info;
+               info.protocol = "MFX";
+               info.address = next_address;
+               info.name = format("%08X", bits);
+               info.id = bits;
+               push_info(info);
+
                cmd.command[0] = MFX_ASSIGN_ADDRESS;
                cmd.command[1] = next_address>>8;
                cmd.command[2] = next_address;
@@ -777,6 +809,10 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd)
                        cmd.command[3+i] = bits>>(24-i*8);
                cmd.length = 7;
 
+               cmd.tag.type = Tag::GENERAL;
+               cmd.tag.command = NEW_LOCO;
+               cmd.tag.id = bits;
+
                size = 0;
                bits = 0;
                ++next_address;
@@ -827,6 +863,22 @@ void ArduControl::MfxSearchTask::process_reply(const char *reply, unsigned lengt
        }
 }
 
+void ArduControl::MfxSearchTask::push_info(const MfxInfo &info)
+{
+       MutexLock lock(mutex);
+       queue.push_back(info);
+}
+
+bool ArduControl::MfxSearchTask::pop_info(MfxInfo &info)
+{
+       MutexLock lock(mutex);
+       if(queue.empty())
+               return false;
+       info = queue.back();
+       queue.pop_back();
+       return true;
+}
+
 
 ArduControl::ControlThread::ControlThread(ArduControl &c):
        control(c),
index 2431cf927ae8d59b7dfc11a411b7e99e7159e83d..69311f8280fecbb94ce0671b636a0ec4bf9fcf0d 100644 (file)
@@ -75,7 +75,8 @@ private:
 
        enum GeneralCommand
        {
-               POWER
+               POWER,
+               NEW_LOCO
        };
 
        enum Protocol
@@ -130,6 +131,11 @@ private:
                unsigned create_speed_func_command(unsigned, char *) const;
        };
 
+       struct MfxInfo: public DetectedLocomotive
+       {
+               unsigned id;
+       };
+
        struct Accessory
        {
                enum Kind
@@ -257,12 +263,19 @@ private:
                unsigned size;
                unsigned bits;
                unsigned misses;
+               std::list<MfxInfo> queue;
+               Msp::Mutex mutex;
 
        public:
                MfxSearchTask(ArduControl &);
 
                virtual bool get_work(PendingCommand &);
                virtual void process_reply(const char *, unsigned);
+
+       private:
+               void push_info(const MfxInfo &);
+       public:
+               bool pop_info(MfxInfo &);
        };
 
        class ControlThread: public Msp::Thread
@@ -285,6 +298,7 @@ private:
        };
 
        typedef std::map<unsigned, Locomotive> LocomotiveMap;
+       typedef std::vector<MfxInfo> MfxInfoArray;
        typedef std::map<unsigned, Accessory> AccessoryMap;
        typedef std::list<Accessory *> AccessoryPtrList;
        typedef std::map<unsigned, Sensor> SensorMap;
@@ -295,6 +309,7 @@ private:
        ControlledVariable<bool> power;
 
        LocomotiveMap locomotives;
+       MfxInfoArray mfx_info;
        AccessoryMap accessories;
        AccessoryPtrList accessory_queue;
        Accessory *active_accessory;
@@ -328,7 +343,7 @@ private:
 public:
        virtual unsigned get_protocol_speed_steps(const std::string &) const;
 
-       virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const { return 0; }
+       virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
        virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
        virtual void remove_loco(unsigned);
        virtual void set_loco_speed(unsigned, unsigned);