1 #ifndef LIBR2C2_ARDUCONTROL_H_
2 #define LIBR2C2_ARDUCONTROL_H_
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>
15 class ArduControl: public Driver
18 class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
21 Loader(ArduControl &);
24 void mfx_announce_serial(unsigned);
25 void mfx_locomotive(unsigned);
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,
45 MFX_ASSIGN_ADDRESS = 0x24,
48 MFX_SPEED_FUNCS8 = 0x29,
49 MFX_SPEED_FUNCS16 = 0x2A,
53 RECEIVE_OVERRUN = 0x81,
55 INVALID_COMMAND = 0x83,
59 BAUD_CHANGE_FAILED = 0xA1,
64 MFX_SEARCH_FEEDBACK = 0xD1,
65 MFX_PING_FEEDBACK = 0xD2
80 unsigned char command;
81 unsigned short serial;
86 operator bool() const { return type!=NONE; }
103 unsigned max_address;
109 struct ControlledVariable
113 unsigned short serial;
115 ControlledVariable(): current(), pending(), serial(0) { }
116 ControlledVariable(T v): current(v), pending(v), serial(0) { }
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; }
122 operator T() const { return current; }
137 ControlledVariable<unsigned> speed;
138 ControlledVariable<bool> reverse;
139 ControlledVariable<unsigned> funcs;
140 unsigned last_change_age;
142 Locomotive(Protocol, unsigned);
144 unsigned create_speed_dir_command(char *) const;
145 unsigned create_speed_func_command(unsigned, char *) const;
148 struct MfxInfo: public DetectedLocomotive
150 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
176 ControlledVariable<unsigned> state;
179 Msp::Time::TimeDelta active_time;
181 Accessory(Kind, unsigned, unsigned);
183 unsigned create_state_command(unsigned, bool, char *) const;
194 ControlledVariable<bool> state;
199 struct PendingCommand
203 unsigned char length;
204 unsigned repeat_count;
207 PendingCommand(GeneralCommand);
208 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
209 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
220 void push(const T &);
232 virtual bool get_work(PendingCommand &) = 0;
233 virtual void process_reply(const char *, unsigned) { }
236 class CommandQueueTask: public Task
239 Queue<PendingCommand> queue;
242 virtual bool get_work(PendingCommand &);
245 class RefreshTask: public Task
248 typedef std::list<Locomotive *> LocomotivePtrList;
250 LocomotivePtrList cycle;
251 LocomotivePtrList::iterator next;
260 virtual bool get_work(PendingCommand &);
262 void add_loco(Locomotive &);
263 void remove_loco(Locomotive &);
265 Locomotive *get_next_loco();
269 class S88Task: public Task
272 ArduControl &control;
274 unsigned octets_remaining;
278 S88Task(ArduControl &);
280 virtual bool get_work(PendingCommand &);
281 virtual void process_reply(const char *, unsigned);
283 void set_n_octets(unsigned);
284 void grow_n_octets(unsigned);
287 class MfxAnnounceTask: public Task
291 Msp::Time::TimeStamp next;
296 virtual bool get_work(PendingCommand &);
298 void set_serial(unsigned);
299 unsigned get_serial() const { return serial; }
302 class MfxSearchTask: public Task
305 ArduControl &control;
306 unsigned next_address;
307 Msp::Time::TimeStamp next;
311 Queue<MfxInfo> queue;
314 MfxSearchTask(ArduControl &);
316 virtual bool get_work(PendingCommand &);
317 virtual void process_reply(const char *, unsigned);
319 void set_next_address(unsigned);
320 bool pop_info(MfxInfo &);
323 class MonitorTask: public Task
330 Msp::Time::TimeStamp next_poll;
336 virtual bool get_work(PendingCommand &);
337 virtual void process_reply(const char *, unsigned);
339 float get_voltage() const { return voltage; }
340 float get_current() const { return current; }
342 float get_peak() const { return peak_level-base_level; }
345 class ControlThread: public Msp::Thread
348 ArduControl &control;
350 std::vector<Task *> tasks;
353 ControlThread(ArduControl &);
358 void init_baud_rate();
359 bool get_work(PendingCommand &);
360 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
361 unsigned process_reply(const char *, unsigned);
362 void set_power(bool);
365 typedef std::map<unsigned, Locomotive> LocomotiveMap;
366 typedef std::vector<MfxInfo> MfxInfoArray;
367 typedef std::map<unsigned, Accessory> AccessoryMap;
368 typedef std::list<Accessory *> AccessoryPtrList;
369 typedef std::map<unsigned, Sensor> SensorMap;
371 Msp::IO::Serial serial;
373 Msp::FS::Path state_file;
375 ControlledVariable<bool> power;
378 LocomotiveMap locomotives;
379 MfxInfoArray mfx_info;
380 AccessoryMap accessories;
381 AccessoryPtrList accessory_queue;
382 Accessory *active_accessory;
383 unsigned char active_index;
384 Msp::Time::TimeStamp off_timeout;
388 Msp::Time::TimeDelta command_timeout;
389 Queue<PendingCommand> command_queue;
390 Queue<Tag> completed_commands;
393 MfxAnnounceTask mfx_announce;
394 MfxSearchTask mfx_search;
396 ControlThread thread;
398 static ProtocolInfo protocol_info[2];
401 ArduControl(const Options &);
404 virtual void set_power(bool);
405 virtual bool get_power() const { return power; }
406 virtual void halt(bool);
407 virtual bool is_halted() const { return halted; }
409 virtual const char *enumerate_protocols(unsigned) const;
411 static Protocol map_protocol(const std::string &);
413 virtual unsigned get_protocol_speed_steps(const std::string &) const;
415 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
416 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
418 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
420 virtual void remove_loco(unsigned);
421 virtual void set_loco_speed(unsigned, unsigned);
422 virtual void set_loco_reverse(unsigned, bool);
423 virtual void set_loco_function(unsigned, unsigned, bool);
425 virtual unsigned add_turnout(unsigned, const TrackType &);
426 virtual void remove_turnout(unsigned);
427 virtual void set_turnout(unsigned, unsigned);
428 virtual unsigned get_turnout(unsigned) const;
430 virtual unsigned add_signal(unsigned, const SignalType &);
431 virtual void remove_signal(unsigned);
432 virtual void set_signal(unsigned, unsigned);
433 virtual unsigned get_signal(unsigned) const;
436 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
437 void remove_accessory(Accessory::Kind, unsigned);
438 void set_accessory(Accessory::Kind, unsigned, unsigned);
439 unsigned get_accessory(Accessory::Kind, unsigned) const;
442 virtual unsigned add_sensor(unsigned);
443 virtual void remove_sensor(unsigned);
444 virtual void set_sensor(unsigned, bool) { }
445 virtual bool get_sensor(unsigned) const;
448 virtual void flush();
450 void save_state() const;