]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/arducontrol.h
Improve ArduControl task scheduling
[r2c2.git] / source / libr2c2 / arducontrol.h
1 #ifndef LIBR2C2_ARDUCONTROL_H_
2 #define LIBR2C2_ARDUCONTROL_H_
3
4 #include <msp/core/mutex.h>
5 #include <msp/core/thread.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/fs/path.h>
8 #include <msp/io/serial.h>
9 #include <msp/time/timedelta.h>
10 #include <msp/time/timestamp.h>
11 #include "driver.h"
12
13 namespace R2C2 {
14
15 class ArduControl: public Driver
16 {
17 public:
18         class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
19         {
20         public:
21                 Loader(ArduControl &);
22
23         private:
24                 void mfx_announce_serial(unsigned);
25                 void mfx_locomotive(unsigned);
26         };
27
28 private:
29         enum Command
30         {
31                 POWER_ON = 0x01,
32                 POWER_OFF = 0x02,
33                 READ_POWER_STATE = 0x03,
34                 READ_TRACK_CURRENT = 0x08,
35                 SET_OVERCURRENT_LIMIT = 0x09,
36                 READ_INPUT_VOLTAGE = 0x0A,
37                 MOTOROLA_SPEED = 0x11,
38                 MOTOROLA_REVERSE = 0x12,
39                 MOTOROLA_SPEED_DIRECTION = 0x13,
40                 MOTOROLA_SPEED_FUNCTION = 0x14,
41                 MOTOROLA_SOLENOID = 0x15,
42                 MFX_SET_STATION_ID = 0x21,
43                 MFX_ANNOUNCE = 0x22,
44                 MFX_SEARCH = 0x23,
45                 MFX_ASSIGN_ADDRESS = 0x24,
46                 MFX_PING = 0x25,
47                 MFX_SPEED = 0x28,
48                 MFX_SPEED_FUNCS8 = 0x29,
49                 MFX_SPEED_FUNCS16 = 0x2A,
50                 S88_READ = 0x30,
51                 SET_BAUD_RATE = 0x70,
52                 COMMAND_OK = 0x80,
53                 RECEIVE_OVERRUN = 0x81,
54                 FRAMING_ERROR = 0x82,
55                 INVALID_COMMAND = 0x83,
56                 LENGTH_ERROR = 0x84,
57                 INVALID_VALUE = 0x85,
58                 OVERCURRENT = 0xA0,
59                 BAUD_CHANGE_FAILED = 0xA1,
60                 TRACK_CURRENT = 0xC0,
61                 INPUT_VOLTAGE = 0xC1,
62                 POWER_STATE = 0xC2,
63                 S88_DATA = 0xD0,
64                 MFX_SEARCH_FEEDBACK = 0xD1,
65                 MFX_PING_FEEDBACK = 0xD2
66         };
67
68         struct Tag
69         {
70                 enum Type
71                 {
72                         NONE,
73                         GENERAL,
74                         LOCOMOTIVE,
75                         ACCESSORY,
76                         SENSOR
77                 };
78
79                 Type type;
80                 unsigned char command;
81                 unsigned short serial;
82                 unsigned id;
83
84                 Tag();
85
86                 operator bool() const { return type!=NONE; }
87         };
88
89         enum GeneralCommand
90         {
91                 POWER,
92                 NEW_LOCO
93         };
94
95         enum Protocol
96         {
97                 MM,
98                 MFX
99         };
100
101         struct ProtocolInfo
102         {
103                 unsigned max_address;
104                 unsigned max_speed;
105                 unsigned max_func;
106         };
107
108         template<typename T>
109         struct ControlledVariable
110         {
111                 T current;
112                 T pending;
113                 unsigned short serial;
114
115                 ControlledVariable(): current(), pending(), serial(0) { }
116                 ControlledVariable(T v): current(v), pending(v), serial(0) { }
117
118                 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
119                 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
120                 void rollback() { pending = current; ++serial; }
121
122                 operator T() const { return current; }
123         };
124
125         struct Locomotive
126         {
127                 enum Command
128                 {
129                         SPEED,
130                         REVERSE,
131                         FUNCTIONS
132                 };
133
134                 unsigned id;
135                 Protocol proto;
136                 unsigned address;
137                 ControlledVariable<unsigned> speed;
138                 ControlledVariable<bool> reverse;
139                 ControlledVariable<unsigned> funcs;
140                 unsigned last_change_age;
141
142                 Locomotive(Protocol, unsigned);
143
144                 unsigned create_speed_dir_command(char *) const;
145                 unsigned create_speed_func_command(unsigned, char *) const;
146         };
147
148         struct MfxInfo: public DetectedLocomotive
149         {
150                 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
151                 {
152                 public:
153                         Loader(MfxInfo &);
154                 };
155
156                 unsigned id;
157         };
158
159         struct Accessory
160         {
161                 enum Kind
162                 {
163                         TURNOUT,
164                         SIGNAL
165                 };
166
167                 enum Command
168                 {
169                         ACTIVATE,
170                         DEACTIVATE
171                 };
172
173                 Kind kind;
174                 unsigned address;
175                 unsigned bits;
176                 unsigned valid_states;
177                 ControlledVariable<unsigned> state;
178                 unsigned uncertain;
179                 unsigned target;
180                 Msp::Time::TimeDelta active_time;
181
182                 Accessory(Kind, unsigned, unsigned, unsigned);
183
184                 unsigned create_state_command(unsigned, bool, char *) const;
185         };
186
187         struct Sensor
188         {
189                 enum Command
190                 {
191                         STATE
192                 };
193
194                 unsigned address;
195                 ControlledVariable<bool> state;
196
197                 Sensor(unsigned);
198         };
199
200         struct PendingCommand
201         {
202                 Tag tag;
203                 char command[15];
204                 unsigned char length;
205                 unsigned repeat_count;
206
207                 PendingCommand();
208                 PendingCommand(GeneralCommand);
209                 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
210                 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
211         };
212
213         template<typename T>
214         class Queue
215         {
216         private:
217                 std::list<T> items;
218                 Msp::Mutex mutex;
219
220         public:
221                 void push(const T &);
222                 bool pop(T &);
223                 bool empty() const;
224         };
225
226         class Task
227         {
228         protected:
229                 std::string name;
230                 unsigned priority;
231                 Msp::Time::TimeStamp sleep_timeout;
232
233                 Task(const std::string &, unsigned = 0);
234         public:
235                 virtual ~Task() { }
236
237                 const std::string &get_name() const { return name; }
238
239                 virtual bool get_work(PendingCommand &) = 0;
240                 virtual void process_reply(const char *, unsigned) { }
241
242                 unsigned get_priority() const { return priority; }
243                 const Msp::Time::TimeStamp &get_sleep_timeout() const { return sleep_timeout; }
244         protected:
245                 void sleep(const Msp::Time::TimeDelta &);
246         };
247
248         class CommandQueueTask: public Task
249         {
250         private:
251                 Queue<PendingCommand> queue;
252
253         public:
254                 CommandQueueTask();
255
256                 virtual bool get_work(PendingCommand &);
257
258                 void push(const PendingCommand &);
259                 bool empty() const { return queue.empty(); }
260         };
261
262         class RefreshTask: public Task
263         {
264         private:
265                 typedef std::list<Locomotive *> LocomotivePtrList;
266
267                 LocomotivePtrList cycle;
268                 LocomotivePtrList::iterator next;
269                 unsigned round;
270                 Locomotive *loco;
271                 unsigned phase;
272                 Msp::Mutex mutex;
273
274         public:
275                 RefreshTask();
276
277                 virtual bool get_work(PendingCommand &);
278
279                 void add_loco(Locomotive &);
280                 void remove_loco(Locomotive &);
281         private:
282                 Locomotive *get_next_loco();
283                 void advance();
284         };
285
286         class S88Task: public Task
287         {
288         private:
289                 ArduControl &control;
290                 unsigned n_octets;
291                 unsigned octets_remaining;
292
293         public:
294                 S88Task(ArduControl &);
295
296                 virtual bool get_work(PendingCommand &);
297                 virtual void process_reply(const char *, unsigned);
298
299                 void set_n_octets(unsigned);
300                 void grow_n_octets(unsigned);
301         };
302
303         class MfxAnnounceTask: public Task
304         {
305         private:
306                 unsigned serial;
307
308         public:
309                 MfxAnnounceTask();
310
311                 virtual bool get_work(PendingCommand &);
312
313                 void set_serial(unsigned);
314                 unsigned get_serial() const { return serial; }
315         };
316
317         class MfxSearchTask: public Task
318         {
319         private:
320                 ArduControl &control;
321                 unsigned next_address;
322                 unsigned size;
323                 unsigned bits;
324                 unsigned misses;
325                 Queue<MfxInfo> queue;
326
327         public:
328                 MfxSearchTask(ArduControl &);
329
330                 virtual bool get_work(PendingCommand &);
331                 virtual void process_reply(const char *, unsigned);
332
333                 void set_next_address(unsigned);
334                 bool pop_info(MfxInfo &);
335         };
336
337         class MonitorTask: public Task
338         {
339         private:
340                 float voltage;
341                 float current;
342                 float base_level;
343                 float peak_level;
344                 unsigned next_type;
345
346         public:
347                 MonitorTask();
348
349                 virtual bool get_work(PendingCommand &);
350                 virtual void process_reply(const char *, unsigned);
351
352                 float get_voltage() const { return voltage; }
353                 float get_current() const { return current; }
354                 void reset_peak();
355                 float get_peak() const { return peak_level-base_level; }
356         };
357
358         class ControlThread: public Msp::Thread
359         {
360         private:
361                 ArduControl &control;
362                 bool done;
363                 std::vector<Task *> tasks;
364
365         public:
366                 ControlThread(ArduControl &);
367
368                 void exit();
369         private:
370                 virtual void main();
371                 void init_baud_rate();
372                 bool get_work(PendingCommand &);
373                 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
374                 unsigned process_reply(const char *, unsigned);
375                 void set_power(bool);
376         };
377
378         typedef std::map<unsigned, Locomotive> LocomotiveMap;
379         typedef std::vector<MfxInfo> MfxInfoArray;
380         typedef std::map<unsigned, Accessory> AccessoryMap;
381         typedef std::list<Accessory *> AccessoryPtrList;
382         typedef std::map<unsigned, Sensor> SensorMap;
383
384         Msp::IO::Serial serial;
385         unsigned debug;
386         Msp::FS::Path state_file;
387
388         ControlledVariable<bool> power;
389         bool halted;
390
391         LocomotiveMap locomotives;
392         MfxInfoArray mfx_info;
393         AccessoryMap accessories;
394         AccessoryPtrList accessory_queue;
395         Accessory *active_accessory;
396         unsigned char active_index;
397         Msp::Time::TimeStamp off_timeout;
398
399         SensorMap sensors;
400
401         Msp::Time::TimeDelta command_timeout;
402         CommandQueueTask command_queue;
403         Queue<Tag> completed_commands;
404         RefreshTask refresh;
405         S88Task s88;
406         MfxAnnounceTask mfx_announce;
407         MfxSearchTask mfx_search;
408         MonitorTask monitor;
409         ControlThread thread;
410
411         static ProtocolInfo protocol_info[2];
412
413 public:
414         ArduControl(const Options &);
415         ~ArduControl();
416
417         virtual void set_power(bool);
418         virtual bool get_power() const { return power; }
419         virtual void halt(bool);
420         virtual bool is_halted() const { return halted; }
421
422         virtual const char *enumerate_protocols(unsigned) const;
423 private:
424         static Protocol map_protocol(const std::string &);
425 public:
426         virtual unsigned get_protocol_speed_steps(const std::string &) const;
427
428         virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
429         virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
430 private:
431         MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
432 public:
433         virtual void remove_loco(unsigned);
434         virtual void set_loco_speed(unsigned, unsigned);
435         virtual void set_loco_reverse(unsigned, bool);
436         virtual void set_loco_function(unsigned, unsigned, bool);
437
438         virtual unsigned add_turnout(unsigned, const TrackType &);
439         virtual void remove_turnout(unsigned);
440         virtual void set_turnout(unsigned, unsigned);
441         virtual unsigned get_turnout(unsigned) const;
442
443         virtual unsigned add_signal(unsigned, const SignalType &);
444         virtual void remove_signal(unsigned);
445         virtual void set_signal(unsigned, unsigned);
446         virtual unsigned get_signal(unsigned) const;
447
448 private:
449         unsigned add_accessory(Accessory::Kind, unsigned, unsigned, unsigned);
450         void remove_accessory(Accessory::Kind, unsigned);
451         void set_accessory(Accessory::Kind, unsigned, unsigned);
452         unsigned get_accessory(Accessory::Kind, unsigned) const;
453         void activate_accessory_by_mask(Accessory &, unsigned);
454
455 public:
456         virtual unsigned add_sensor(unsigned);
457         virtual void remove_sensor(unsigned);
458         virtual void set_sensor(unsigned, bool) { }
459         virtual bool get_sensor(unsigned) const;
460
461         virtual void tick();
462         virtual void flush();
463 private:
464         void save_state() const;
465 };
466
467 } // namespace R2C2
468
469 #endif