]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/arducontrol.h
Turn ArduControl command_queue into a Task
[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                 Task() { }
230         public:
231                 virtual ~Task() { }
232
233                 virtual bool get_work(PendingCommand &) = 0;
234                 virtual void process_reply(const char *, unsigned) { }
235         };
236
237         class CommandQueueTask: public Task
238         {
239         private:
240                 Queue<PendingCommand> queue;
241
242         public:
243                 virtual bool get_work(PendingCommand &);
244
245                 void push(const PendingCommand &);
246                 bool empty() const { return queue.empty(); }
247         };
248
249         class RefreshTask: public Task
250         {
251         private:
252                 typedef std::list<Locomotive *> LocomotivePtrList;
253
254                 LocomotivePtrList cycle;
255                 LocomotivePtrList::iterator next;
256                 unsigned round;
257                 Locomotive *loco;
258                 unsigned phase;
259                 Msp::Mutex mutex;
260
261         public:
262                 RefreshTask();
263
264                 virtual bool get_work(PendingCommand &);
265
266                 void add_loco(Locomotive &);
267                 void remove_loco(Locomotive &);
268         private:
269                 Locomotive *get_next_loco();
270                 void advance();
271         };
272
273         class S88Task: public Task
274         {
275         private:
276                 ArduControl &control;
277                 unsigned n_octets;
278                 unsigned octets_remaining;
279                 unsigned delay;
280
281         public:
282                 S88Task(ArduControl &);
283
284                 virtual bool get_work(PendingCommand &);
285                 virtual void process_reply(const char *, unsigned);
286
287                 void set_n_octets(unsigned);
288                 void grow_n_octets(unsigned);
289         };
290
291         class MfxAnnounceTask: public Task
292         {
293         private:
294                 unsigned serial;
295                 Msp::Time::TimeStamp next;
296
297         public:
298                 MfxAnnounceTask();
299
300                 virtual bool get_work(PendingCommand &);
301
302                 void set_serial(unsigned);
303                 unsigned get_serial() const { return serial; }
304         };
305
306         class MfxSearchTask: public Task
307         {
308         private:
309                 ArduControl &control;
310                 unsigned next_address;
311                 Msp::Time::TimeStamp next;
312                 unsigned size;
313                 unsigned bits;
314                 unsigned misses;
315                 Queue<MfxInfo> queue;
316
317         public:
318                 MfxSearchTask(ArduControl &);
319
320                 virtual bool get_work(PendingCommand &);
321                 virtual void process_reply(const char *, unsigned);
322
323                 void set_next_address(unsigned);
324                 bool pop_info(MfxInfo &);
325         };
326
327         class MonitorTask: public Task
328         {
329         private:
330                 float voltage;
331                 float current;
332                 float base_level;
333                 float peak_level;
334                 Msp::Time::TimeStamp next_poll;
335                 unsigned next_type;
336
337         public:
338                 MonitorTask();
339
340                 virtual bool get_work(PendingCommand &);
341                 virtual void process_reply(const char *, unsigned);
342
343                 float get_voltage() const { return voltage; }
344                 float get_current() const { return current; }
345                 void reset_peak();
346                 float get_peak() const { return peak_level-base_level; }
347         };
348
349         class ControlThread: public Msp::Thread
350         {
351         private:
352                 ArduControl &control;
353                 bool done;
354                 std::vector<Task *> tasks;
355
356         public:
357                 ControlThread(ArduControl &);
358
359                 void exit();
360         private:
361                 virtual void main();
362                 void init_baud_rate();
363                 bool get_work(PendingCommand &);
364                 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
365                 unsigned process_reply(const char *, unsigned);
366                 void set_power(bool);
367         };
368
369         typedef std::map<unsigned, Locomotive> LocomotiveMap;
370         typedef std::vector<MfxInfo> MfxInfoArray;
371         typedef std::map<unsigned, Accessory> AccessoryMap;
372         typedef std::list<Accessory *> AccessoryPtrList;
373         typedef std::map<unsigned, Sensor> SensorMap;
374
375         Msp::IO::Serial serial;
376         unsigned debug;
377         Msp::FS::Path state_file;
378
379         ControlledVariable<bool> power;
380         bool halted;
381
382         LocomotiveMap locomotives;
383         MfxInfoArray mfx_info;
384         AccessoryMap accessories;
385         AccessoryPtrList accessory_queue;
386         Accessory *active_accessory;
387         unsigned char active_index;
388         Msp::Time::TimeStamp off_timeout;
389
390         SensorMap sensors;
391
392         Msp::Time::TimeDelta command_timeout;
393         CommandQueueTask command_queue;
394         Queue<Tag> completed_commands;
395         RefreshTask refresh;
396         S88Task s88;
397         MfxAnnounceTask mfx_announce;
398         MfxSearchTask mfx_search;
399         MonitorTask monitor;
400         ControlThread thread;
401
402         static ProtocolInfo protocol_info[2];
403
404 public:
405         ArduControl(const Options &);
406         ~ArduControl();
407
408         virtual void set_power(bool);
409         virtual bool get_power() const { return power; }
410         virtual void halt(bool);
411         virtual bool is_halted() const { return halted; }
412
413         virtual const char *enumerate_protocols(unsigned) const;
414 private:
415         static Protocol map_protocol(const std::string &);
416 public:
417         virtual unsigned get_protocol_speed_steps(const std::string &) const;
418
419         virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
420         virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
421 private:
422         MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
423 public:
424         virtual void remove_loco(unsigned);
425         virtual void set_loco_speed(unsigned, unsigned);
426         virtual void set_loco_reverse(unsigned, bool);
427         virtual void set_loco_function(unsigned, unsigned, bool);
428
429         virtual unsigned add_turnout(unsigned, const TrackType &);
430         virtual void remove_turnout(unsigned);
431         virtual void set_turnout(unsigned, unsigned);
432         virtual unsigned get_turnout(unsigned) const;
433
434         virtual unsigned add_signal(unsigned, const SignalType &);
435         virtual void remove_signal(unsigned);
436         virtual void set_signal(unsigned, unsigned);
437         virtual unsigned get_signal(unsigned) const;
438
439 private:
440         unsigned add_accessory(Accessory::Kind, unsigned, unsigned, unsigned);
441         void remove_accessory(Accessory::Kind, unsigned);
442         void set_accessory(Accessory::Kind, unsigned, unsigned);
443         unsigned get_accessory(Accessory::Kind, unsigned) const;
444         void activate_accessory_by_mask(Accessory &, unsigned);
445
446 public:
447         virtual unsigned add_sensor(unsigned);
448         virtual void remove_sensor(unsigned);
449         virtual void set_sensor(unsigned, bool) { }
450         virtual bool get_sensor(unsigned) const;
451
452         virtual void tick();
453         virtual void flush();
454 private:
455         void save_state() const;
456 };
457
458 } // namespace R2C2
459
460 #endif