X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Farducontrol.h;h=2431cf927ae8d59b7dfc11a411b7e99e7159e83d;hb=77f1712e7f8913af3ce33efc0e1e4ebbe0e5f93a;hp=f0c14f948219afce5bea6c4e2a2559d8a606f375;hpb=73e4a36bbf828e07b83a779b696875e1f80045cc;p=r2c2.git diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index f0c14f9..2431cf9 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -17,6 +17,7 @@ private: { POWER_ON = 0x01, POWER_OFF = 0x02, + READ_POWER_STATE = 0x03, READ_TRACK_CURRENT = 0x08, SET_OVERCURRENT_LIMIT = 0x09, READ_INPUT_VOLTAGE = 0x0A, @@ -25,7 +26,16 @@ private: MOTOROLA_SPEED_DIRECTION = 0x13, MOTOROLA_SPEED_FUNCTION = 0x14, MOTOROLA_SOLENOID = 0x15, + MFX_SET_STATION_ID = 0x21, + MFX_ANNOUNCE = 0x22, + MFX_SEARCH = 0x23, + MFX_ASSIGN_ADDRESS = 0x24, + MFX_PING = 0x25, + MFX_SPEED = 0x28, + MFX_SPEED_FUNCS8 = 0x29, + MFX_SPEED_FUNCS16 = 0x2A, S88_READ = 0x30, + SET_BAUD_RATE = 0x70, COMMAND_OK = 0x80, RECEIVE_OVERRUN = 0x81, FRAMING_ERROR = 0x82, @@ -33,9 +43,13 @@ private: LENGTH_ERROR = 0x84, INVALID_VALUE = 0x85, OVERCURRENT = 0xA0, + BAUD_CHANGE_FAILED = 0xA1, TRACK_CURRENT = 0xC0, INPUT_VOLTAGE = 0xC1, - S88_DATA = 0xD0 + POWER_STATE = 0xC2, + S88_DATA = 0xD0, + MFX_SEARCH_FEEDBACK = 0xD1, + MFX_PING_FEEDBACK = 0xD2 }; struct Tag @@ -66,8 +80,15 @@ private: enum Protocol { - NONE, - MM + MM, + MFX + }; + + struct ProtocolInfo + { + unsigned max_address; + unsigned max_speed; + unsigned max_func; }; template @@ -148,11 +169,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 &); @@ -160,48 +278,41 @@ 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); + void init_baud_rate(); + 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; Msp::IO::Serial serial; - bool debug; + unsigned debug; 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; Msp::Mutex mutex; + RefreshTask refresh; + S88Task s88; + MfxAnnounceTask mfx_announce; + MfxSearchTask mfx_search; ControlThread thread; + static ProtocolInfo protocol_info[2]; + public: ArduControl(const std::string &); ~ArduControl(); @@ -217,18 +328,13 @@ private: public: virtual unsigned get_protocol_speed_steps(const std::string &) const; + virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const { return 0; } 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); -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); @@ -255,8 +361,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(); };