]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/arducontrol.h
Add two new telemetry values to the arducontrol driver
[r2c2.git] / source / libr2c2 / arducontrol.h
index 2431cf927ae8d59b7dfc11a411b7e99e7159e83d..fffaf8cc36854d691860e8064548fcd589579aac 100644 (file)
@@ -1,8 +1,11 @@
 #ifndef LIBR2C2_ARDUCONTROL_H_
 #define LIBR2C2_ARDUCONTROL_H_
 
+#include <deque>
 #include <msp/core/mutex.h>
 #include <msp/core/thread.h>
+#include <msp/datafile/objectloader.h>
+#include <msp/fs/path.h>
 #include <msp/io/serial.h>
 #include <msp/time/timedelta.h>
 #include <msp/time/timestamp.h>
@@ -12,6 +15,17 @@ namespace R2C2 {
 
 class ArduControl: public Driver
 {
+public:
+       class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
+       {
+       public:
+               Loader(ArduControl &);
+
+       private:
+               void mfx_announce_serial(unsigned);
+               void mfx_locomotive(unsigned);
+       };
+
 private:
        enum Command
        {
@@ -31,6 +45,7 @@ private:
                MFX_SEARCH = 0x23,
                MFX_ASSIGN_ADDRESS = 0x24,
                MFX_PING = 0x25,
+               MFX_READ = 0x26,
                MFX_SPEED = 0x28,
                MFX_SPEED_FUNCS8 = 0x29,
                MFX_SPEED_FUNCS16 = 0x2A,
@@ -49,7 +64,8 @@ private:
                POWER_STATE = 0xC2,
                S88_DATA = 0xD0,
                MFX_SEARCH_FEEDBACK = 0xD1,
-               MFX_PING_FEEDBACK = 0xD2
+               MFX_PING_FEEDBACK = 0xD2,
+               MFX_READ_FEEDBACK = 0xD3
        };
 
        struct Tag
@@ -75,7 +91,8 @@ private:
 
        enum GeneralCommand
        {
-               POWER
+               POWER,
+               NEW_LOCO
        };
 
        enum Protocol
@@ -103,6 +120,7 @@ private:
 
                bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
                bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
+               void rollback() { pending = current; ++serial; }
 
                operator T() const { return current; }
        };
@@ -130,6 +148,17 @@ private:
                unsigned create_speed_func_command(unsigned, char *) const;
        };
 
+       struct MfxInfo: public DetectedLocomotive
+       {
+               class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
+               {
+               public:
+                       Loader(MfxInfo &);
+               };
+
+               unsigned id;
+       };
+
        struct Accessory
        {
                enum Kind
@@ -147,11 +176,13 @@ private:
                Kind kind;
                unsigned address;
                unsigned bits;
+               unsigned valid_states;
                ControlledVariable<unsigned> state;
+               unsigned uncertain;
                unsigned target;
                Msp::Time::TimeDelta active_time;
 
-               Accessory(Kind, unsigned, unsigned);
+               Accessory(Kind, unsigned, unsigned, unsigned);
 
                unsigned create_state_command(unsigned, bool, char *) const;
        };
@@ -182,15 +213,55 @@ private:
                PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
        };
 
+       template<typename T>
+       class Queue
+       {
+       private:
+               std::deque<T> items;
+               Msp::Mutex mutex;
+
+       public:
+               void push(const T &);
+               bool pop(T &);
+               unsigned size() const;
+               bool empty() const;
+       };
+
        class Task
        {
        protected:
-               Task() { }
+               std::string name;
+               unsigned priority;
+               Msp::Time::TimeStamp sleep_timeout;
+
+               Task(const std::string &, unsigned = 0);
        public:
                virtual ~Task() { }
 
+               const std::string &get_name() const { return name; }
+
                virtual bool get_work(PendingCommand &) = 0;
                virtual void process_reply(const char *, unsigned) { }
+
+               unsigned get_priority() const { return priority; }
+               const Msp::Time::TimeStamp &get_sleep_timeout() const { return sleep_timeout; }
+       protected:
+               void sleep(const Msp::Time::TimeDelta &);
+       };
+
+       class CommandQueueTask: public Task
+       {
+       private:
+               Queue<PendingCommand> queue;
+
+       public:
+               CommandQueueTask();
+
+               virtual bool get_work(PendingCommand &);
+
+               void push(const PendingCommand &);
+               unsigned size() const { return queue.size(); }
+               bool empty() const { return queue.empty(); }
        };
 
        class RefreshTask: public Task
@@ -223,6 +294,8 @@ private:
                ArduControl &control;
                unsigned n_octets;
                unsigned octets_remaining;
+               Msp::Time::TimeStamp last_poll;
+               Msp::Time::TimeDelta latency;
 
        public:
                S88Task(ArduControl &);
@@ -232,13 +305,14 @@ private:
 
                void set_n_octets(unsigned);
                void grow_n_octets(unsigned);
+
+               const Msp::Time::TimeDelta &get_latency() const { return latency; }
        };
 
        class MfxAnnounceTask: public Task
        {
        private:
                unsigned serial;
-               Msp::Time::TimeStamp next;
 
        public:
                MfxAnnounceTask();
@@ -246,6 +320,7 @@ private:
                virtual bool get_work(PendingCommand &);
 
                void set_serial(unsigned);
+               unsigned get_serial() const { return serial; }
        };
 
        class MfxSearchTask: public Task
@@ -253,16 +328,47 @@ private:
        private:
                ArduControl &control;
                unsigned next_address;
-               Msp::Time::TimeStamp next;
                unsigned size;
                unsigned bits;
                unsigned misses;
+               Queue<MfxInfo> queue;
+
+               MfxInfo *pending_info;
+               unsigned read_array;
+               unsigned read_offset;
+               unsigned read_length;
+               char read_data[0x40];
+               unsigned block_size;
 
        public:
                MfxSearchTask(ArduControl &);
 
                virtual bool get_work(PendingCommand &);
                virtual void process_reply(const char *, unsigned);
+
+               void set_next_address(unsigned);
+               bool pop_info(MfxInfo &);
+       };
+
+       class MonitorTask: public Task
+       {
+       private:
+               float voltage;
+               float current;
+               float base_level;
+               float peak_level;
+               unsigned next_type;
+
+       public:
+               MonitorTask();
+
+               virtual bool get_work(PendingCommand &);
+               virtual void process_reply(const char *, unsigned);
+
+               float get_voltage() const { return voltage; }
+               float get_current() const { return current; }
+               void reset_peak();
+               float get_peak() const { return peak_level-base_level; }
        };
 
        class ControlThread: public Msp::Thread
@@ -271,56 +377,69 @@ private:
                ArduControl &control;
                bool done;
                std::vector<Task *> tasks;
+               unsigned cmd_rate;
+               unsigned cmd_count;
+               Msp::Time::TimeStamp cmd_rate_start;
 
        public:
                ControlThread(ArduControl &);
 
+               unsigned get_command_rate() const { return cmd_rate; }
+
                void exit();
        private:
                virtual void main();
                void init_baud_rate();
                bool get_work(PendingCommand &);
-               unsigned do_command(const PendingCommand &);
+               unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
                unsigned process_reply(const char *, unsigned);
+               void set_power(bool);
        };
 
        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;
 
        Msp::IO::Serial serial;
        unsigned debug;
+       Msp::FS::Path state_file;
 
        ControlledVariable<bool> power;
+       bool halted;
 
        LocomotiveMap locomotives;
+       MfxInfoArray mfx_info;
        AccessoryMap accessories;
        AccessoryPtrList accessory_queue;
        Accessory *active_accessory;
+       unsigned char active_index;
        Msp::Time::TimeStamp off_timeout;
-       std::list<PendingCommand> command_queue;
-       std::list<Tag> completed_commands;
 
        SensorMap sensors;
 
-       Msp::Mutex mutex;
+       Msp::Time::TimeDelta command_timeout;
+       CommandQueueTask command_queue;
+       Queue<Tag> completed_commands;
        RefreshTask refresh;
        S88Task s88;
        MfxAnnounceTask mfx_announce;
        MfxSearchTask mfx_search;
+       MonitorTask monitor;
        ControlThread thread;
 
        static ProtocolInfo protocol_info[2];
+       static TelemetryInfo telemetry_info[6];
 
 public:
-       ArduControl(const std::string &);
+       ArduControl(const Options &);
        ~ArduControl();
 
        virtual void set_power(bool);
        virtual bool get_power() const { return power; }
        virtual void halt(bool);
-       virtual bool is_halted() const { return false; }
+       virtual bool is_halted() const { return halted; }
 
        virtual const char *enumerate_protocols(unsigned) const;
 private:
@@ -328,8 +447,12 @@ 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 &);
+private:
+       MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
+       MfxInfo *find_mfx_info(unsigned);
+public:
        virtual void remove_loco(unsigned);
        virtual void set_loco_speed(unsigned, unsigned);
        virtual void set_loco_reverse(unsigned, bool);
@@ -346,10 +469,11 @@ public:
        virtual unsigned get_signal(unsigned) const;
 
 private:
-       unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
+       unsigned add_accessory(Accessory::Kind, unsigned, unsigned, unsigned);
        void remove_accessory(Accessory::Kind, unsigned);
        void set_accessory(Accessory::Kind, unsigned, unsigned);
        unsigned get_accessory(Accessory::Kind, unsigned) const;
+       void activate_accessory_by_mask(Accessory &, unsigned);
 
 public:
        virtual unsigned add_sensor(unsigned);
@@ -357,14 +481,13 @@ public:
        virtual void set_sensor(unsigned, bool) { }
        virtual bool get_sensor(unsigned) const;
 
+       virtual const TelemetryInfo *enumerate_telemetry(unsigned) const;
+       virtual float get_telemetry_value(const std::string &) const;
+
        virtual void tick();
        virtual void flush();
-
 private:
-       void push_command(const PendingCommand &);
-       bool pop_command(PendingCommand &);
-       void push_completed_tag(const Tag &);
-       Tag pop_completed_tag();
+       void save_state() const;
 };
 
 } // namespace R2C2