X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Farducontrol.h;h=46af9238af3fe845e5e19af05ea285b27987361d;hb=aada4cd3c335e6cf55d64200b9d92e8f9310fa1d;hp=3a86ceeacc09790bbfc1c7afa04ac6dab29093ff;hpb=4b287b53e9127209864292f583b8ddbdd481d022;p=r2c2.git diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index 3a86cee..46af923 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -46,7 +46,8 @@ private: INPUT_VOLTAGE = 0xC1, POWER_STATE = 0xC2, S88_DATA = 0xD0, - MFX_FEEDBACK = 0xD1 + MFX_SEARCH_FEEDBACK = 0xD1, + MFX_PING_FEEDBACK = 0xD2 }; struct Tag @@ -166,11 +167,108 @@ private: Sensor(unsigned); }; + struct PendingCommand + { + Tag tag; + char command[15]; + unsigned char length; + unsigned repeat_count; + + PendingCommand(); + PendingCommand(GeneralCommand); + PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0); + PendingCommand(Accessory &, Accessory::Command, unsigned = 0); + }; + + class Task + { + protected: + Task() { } + public: + virtual ~Task() { } + + virtual bool get_work(PendingCommand &) = 0; + virtual void process_reply(const char *, unsigned) { } + }; + + class RefreshTask: public Task + { + private: + typedef std::list LocomotivePtrList; + + LocomotivePtrList cycle; + LocomotivePtrList::iterator next; + unsigned round; + Locomotive *loco; + unsigned phase; + Msp::Mutex mutex; + + public: + RefreshTask(); + + virtual bool get_work(PendingCommand &); + + void add_loco(Locomotive &); + void remove_loco(Locomotive &); + private: + Locomotive *get_next_loco(); + void advance(); + }; + + class S88Task: public Task + { + private: + ArduControl &control; + unsigned n_octets; + unsigned octets_remaining; + + public: + S88Task(ArduControl &); + + virtual bool get_work(PendingCommand &); + virtual void process_reply(const char *, unsigned); + + void set_n_octets(unsigned); + void grow_n_octets(unsigned); + }; + + class MfxAnnounceTask: public Task + { + private: + unsigned serial; + Msp::Time::TimeStamp next; + + public: + MfxAnnounceTask(); + + virtual bool get_work(PendingCommand &); + + void set_serial(unsigned); + }; + + class MfxSearchTask: public Task + { + private: + ArduControl &control; + unsigned next_address; + Msp::Time::TimeStamp next; + unsigned size; + unsigned bits; + unsigned misses; + + public: + MfxSearchTask(ArduControl &); + + virtual bool get_work(PendingCommand &); + virtual void process_reply(const char *, unsigned); + }; + class ControlThread: public Msp::Thread { private: ArduControl &control; bool done; + std::vector tasks; public: ControlThread(ArduControl &); @@ -178,22 +276,12 @@ private: void exit(); private: virtual void main(); - }; - - struct QueuedCommand - { - Tag tag; - char command[15]; - unsigned char length; - - QueuedCommand(); - QueuedCommand(GeneralCommand); - QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0); - QueuedCommand(Accessory &, Accessory::Command, unsigned = 0); + bool get_work(PendingCommand &); + unsigned do_command(const PendingCommand &); + unsigned process_reply(const char *, unsigned); }; typedef std::map LocomotiveMap; - typedef std::list LocomotivePtrList; typedef std::map AccessoryMap; typedef std::list AccessoryPtrList; typedef std::map SensorMap; @@ -204,23 +292,20 @@ private: ControlledVariable power; LocomotiveMap locomotives; - LocomotivePtrList refresh_cycle; - LocomotivePtrList::iterator next_refresh; - unsigned refresh_counter; AccessoryMap accessories; AccessoryPtrList accessory_queue; Accessory *active_accessory; Msp::Time::TimeStamp off_timeout; - std::list command_queue; + std::list command_queue; std::list completed_commands; SensorMap sensors; - unsigned n_s88_octets; - - unsigned mfx_announce_serial; - unsigned next_mfx_address; Msp::Mutex mutex; + RefreshTask refresh; + S88Task s88; + MfxAnnounceTask mfx_announce; + MfxSearchTask mfx_search; ControlThread thread; static ProtocolInfo protocol_info[2]; @@ -245,13 +330,7 @@ public: virtual void set_loco_speed(unsigned, unsigned); virtual void set_loco_reverse(unsigned, bool); virtual void set_loco_function(unsigned, unsigned, bool); -private: - void add_loco_to_refresh(Locomotive &); - void remove_loco_from_refresh(Locomotive &); - Locomotive *get_loco_to_refresh(); - void advance_next_refresh(); -public: virtual unsigned add_turnout(unsigned, const TrackType &); virtual void remove_turnout(unsigned); virtual void set_turnout(unsigned, unsigned); @@ -278,8 +357,8 @@ public: virtual void flush(); private: - void push_command(const QueuedCommand &); - bool pop_command(QueuedCommand &); + void push_command(const PendingCommand &); + bool pop_command(PendingCommand &); void push_completed_tag(const Tag &); Tag pop_completed_tag(); };