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