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