+ 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);
+ };
+
+ template<typename T>
+ class Queue
+ {
+ private:
+ std::list<T> items;
+ Msp::Mutex mutex;
+
+ public:
+ void push(const T &);
+ bool pop(T &);
+ };
+
+ class Task
+ {
+ protected:
+ Task() { }
+ public:
+ virtual ~Task() { }
+
+ virtual bool get_work(PendingCommand &) = 0;
+ virtual void process_reply(const char *, unsigned) { }
+ };
+
+ class CommandQueueTask: public Task
+ {
+ private:
+ Queue<PendingCommand> queue;
+
+ public:
+ virtual bool get_work(PendingCommand &);
+ };
+
+ class RefreshTask: public Task
+ {
+ private:
+ typedef std::list<Locomotive *> 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);
+ unsigned get_serial() const { return serial; }
+ };
+
+ class MfxSearchTask: public Task
+ {
+ private:
+ ArduControl &control;
+ unsigned next_address;
+ Msp::Time::TimeStamp next;
+ unsigned size;
+ unsigned bits;
+ unsigned misses;
+ Queue<MfxInfo> queue;
+
+ public:
+ MfxSearchTask(ArduControl &);
+
+ virtual bool get_work(PendingCommand &);
+ virtual void process_reply(const char *, unsigned);
+
+ void set_next_address(unsigned);
+ bool pop_info(MfxInfo &);
+ };
+