1 #ifndef LIBR2C2_ARDUCONTROL_H_
2 #define LIBR2C2_ARDUCONTROL_H_
5 #include <msp/core/mutex.h>
6 #include <msp/core/thread.h>
7 #include <msp/datafile/objectloader.h>
8 #include <msp/fs/path.h>
9 #include <msp/io/serial.h>
10 #include <msp/time/timedelta.h>
11 #include <msp/time/timestamp.h>
16 class ArduControl: public Driver
19 class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
22 Loader(ArduControl &);
25 void mfx_announce_serial(unsigned);
26 void mfx_locomotive(unsigned);
34 READ_POWER_STATE = 0x03,
35 READ_TRACK_CURRENT = 0x08,
36 SET_OVERCURRENT_LIMIT = 0x09,
37 READ_INPUT_VOLTAGE = 0x0A,
38 MOTOROLA_SPEED = 0x11,
39 MOTOROLA_REVERSE = 0x12,
40 MOTOROLA_SPEED_DIRECTION = 0x13,
41 MOTOROLA_SPEED_FUNCTION = 0x14,
42 MOTOROLA_SOLENOID = 0x15,
43 MFX_SET_STATION_ID = 0x21,
46 MFX_ASSIGN_ADDRESS = 0x24,
49 MFX_SPEED_FUNCS8 = 0x29,
50 MFX_SPEED_FUNCS16 = 0x2A,
54 RECEIVE_OVERRUN = 0x81,
56 INVALID_COMMAND = 0x83,
60 BAUD_CHANGE_FAILED = 0xA1,
65 MFX_SEARCH_FEEDBACK = 0xD1,
66 MFX_PING_FEEDBACK = 0xD2
81 unsigned char command;
82 unsigned short serial;
87 operator bool() const { return type!=NONE; }
104 unsigned max_address;
110 struct ControlledVariable
114 unsigned short serial;
116 ControlledVariable(): current(), pending(), serial(0) { }
117 ControlledVariable(T v): current(v), pending(v), serial(0) { }
119 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
120 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
121 void rollback() { pending = current; ++serial; }
123 operator T() const { return current; }
138 ControlledVariable<unsigned> speed;
139 ControlledVariable<bool> reverse;
140 ControlledVariable<unsigned> funcs;
141 unsigned last_change_age;
143 Locomotive(Protocol, unsigned);
145 unsigned create_speed_dir_command(char *) const;
146 unsigned create_speed_func_command(unsigned, char *) const;
149 struct MfxInfo: public DetectedLocomotive
151 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
177 unsigned valid_states;
178 ControlledVariable<unsigned> state;
181 Msp::Time::TimeDelta active_time;
183 Accessory(Kind, unsigned, unsigned, unsigned);
185 unsigned create_state_command(unsigned, bool, char *) const;
196 ControlledVariable<bool> state;
201 struct PendingCommand
205 unsigned char length;
206 unsigned repeat_count;
209 PendingCommand(GeneralCommand);
210 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
211 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
222 void push(const T &);
224 unsigned size() const;
233 Msp::Time::TimeStamp sleep_timeout;
235 Task(const std::string &, unsigned = 0);
239 const std::string &get_name() const { return name; }
241 virtual bool get_work(PendingCommand &) = 0;
242 virtual void process_reply(const char *, unsigned) { }
244 unsigned get_priority() const { return priority; }
245 const Msp::Time::TimeStamp &get_sleep_timeout() const { return sleep_timeout; }
247 void sleep(const Msp::Time::TimeDelta &);
250 class CommandQueueTask: public Task
253 Queue<PendingCommand> queue;
258 virtual bool get_work(PendingCommand &);
260 void push(const PendingCommand &);
261 unsigned size() const { return queue.size(); }
262 bool empty() const { return queue.empty(); }
265 class RefreshTask: public Task
268 typedef std::list<Locomotive *> LocomotivePtrList;
270 LocomotivePtrList cycle;
271 LocomotivePtrList::iterator next;
280 virtual bool get_work(PendingCommand &);
282 void add_loco(Locomotive &);
283 void remove_loco(Locomotive &);
285 Locomotive *get_next_loco();
289 class S88Task: public Task
292 ArduControl &control;
294 unsigned octets_remaining;
295 Msp::Time::TimeStamp last_poll;
296 Msp::Time::TimeDelta latency;
299 S88Task(ArduControl &);
301 virtual bool get_work(PendingCommand &);
302 virtual void process_reply(const char *, unsigned);
304 void set_n_octets(unsigned);
305 void grow_n_octets(unsigned);
307 const Msp::Time::TimeDelta &get_latency() const { return latency; }
310 class MfxAnnounceTask: public Task
318 virtual bool get_work(PendingCommand &);
320 void set_serial(unsigned);
321 unsigned get_serial() const { return serial; }
324 class MfxSearchTask: public Task
327 ArduControl &control;
328 unsigned next_address;
332 Queue<MfxInfo> queue;
335 MfxSearchTask(ArduControl &);
337 virtual bool get_work(PendingCommand &);
338 virtual void process_reply(const char *, unsigned);
340 void set_next_address(unsigned);
341 bool pop_info(MfxInfo &);
344 class MonitorTask: public Task
356 virtual bool get_work(PendingCommand &);
357 virtual void process_reply(const char *, unsigned);
359 float get_voltage() const { return voltage; }
360 float get_current() const { return current; }
362 float get_peak() const { return peak_level-base_level; }
365 class ControlThread: public Msp::Thread
368 ArduControl &control;
370 std::vector<Task *> tasks;
373 ControlThread(ArduControl &);
378 void init_baud_rate();
379 bool get_work(PendingCommand &);
380 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
381 unsigned process_reply(const char *, unsigned);
382 void set_power(bool);
385 typedef std::map<unsigned, Locomotive> LocomotiveMap;
386 typedef std::vector<MfxInfo> MfxInfoArray;
387 typedef std::map<unsigned, Accessory> AccessoryMap;
388 typedef std::list<Accessory *> AccessoryPtrList;
389 typedef std::map<unsigned, Sensor> SensorMap;
391 Msp::IO::Serial serial;
393 Msp::FS::Path state_file;
395 ControlledVariable<bool> power;
398 LocomotiveMap locomotives;
399 MfxInfoArray mfx_info;
400 AccessoryMap accessories;
401 AccessoryPtrList accessory_queue;
402 Accessory *active_accessory;
403 unsigned char active_index;
404 Msp::Time::TimeStamp off_timeout;
408 Msp::Time::TimeDelta command_timeout;
409 CommandQueueTask command_queue;
410 Queue<Tag> completed_commands;
413 MfxAnnounceTask mfx_announce;
414 MfxSearchTask mfx_search;
416 ControlThread thread;
418 static ProtocolInfo protocol_info[2];
419 static TelemetryInfo telemetry_info[4];
422 ArduControl(const Options &);
425 virtual void set_power(bool);
426 virtual bool get_power() const { return power; }
427 virtual void halt(bool);
428 virtual bool is_halted() const { return halted; }
430 virtual const char *enumerate_protocols(unsigned) const;
432 static Protocol map_protocol(const std::string &);
434 virtual unsigned get_protocol_speed_steps(const std::string &) const;
436 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
437 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
439 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
441 virtual void remove_loco(unsigned);
442 virtual void set_loco_speed(unsigned, unsigned);
443 virtual void set_loco_reverse(unsigned, bool);
444 virtual void set_loco_function(unsigned, unsigned, bool);
446 virtual unsigned add_turnout(unsigned, const TrackType &);
447 virtual void remove_turnout(unsigned);
448 virtual void set_turnout(unsigned, unsigned);
449 virtual unsigned get_turnout(unsigned) const;
451 virtual unsigned add_signal(unsigned, const SignalType &);
452 virtual void remove_signal(unsigned);
453 virtual void set_signal(unsigned, unsigned);
454 virtual unsigned get_signal(unsigned) const;
457 unsigned add_accessory(Accessory::Kind, unsigned, unsigned, unsigned);
458 void remove_accessory(Accessory::Kind, unsigned);
459 void set_accessory(Accessory::Kind, unsigned, unsigned);
460 unsigned get_accessory(Accessory::Kind, unsigned) const;
461 void activate_accessory_by_mask(Accessory &, unsigned);
464 virtual unsigned add_sensor(unsigned);
465 virtual void remove_sensor(unsigned);
466 virtual void set_sensor(unsigned, bool) { }
467 virtual bool get_sensor(unsigned) const;
469 virtual const TelemetryInfo *enumerate_telemetry(unsigned) const;
470 virtual float get_telemetry_value(const std::string &) const;
473 virtual void flush();
475 void save_state() const;