From: Mikko Rasa Date: Wed, 13 Nov 2013 06:28:22 +0000 (+0200) Subject: Store and export information about detected MFX locomotives X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=58c56db5f740d9d3c56ff64f4062b108076c63c3;p=r2c2.git Store and export information about detected MFX locomotives --- diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index 07a2215..47584f4 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -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), diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index 2431cf9..69311f8 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -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 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 LocomotiveMap; + typedef std::vector MfxInfoArray; typedef std::map AccessoryMap; typedef std::list AccessoryPtrList; typedef std::map SensorMap; @@ -295,6 +309,7 @@ private: ControlledVariable 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);