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,
50 MFX_SPEED_FUNCS8 = 0x29,
51 MFX_SPEED_FUNCS16 = 0x2A,
55 RECEIVE_OVERRUN = 0x81,
57 INVALID_COMMAND = 0x83,
61 BAUD_CHANGE_FAILED = 0xA1,
66 MFX_SEARCH_FEEDBACK = 0xD1,
67 MFX_PING_FEEDBACK = 0xD2,
68 MFX_READ_FEEDBACK = 0xD3
83 unsigned char command;
84 unsigned short serial;
89 operator bool() const { return type!=NONE; }
106 unsigned max_address;
112 struct ControlledVariable
116 unsigned short serial;
118 ControlledVariable(): current(), pending(), serial(0) { }
119 ControlledVariable(T v): current(v), pending(v), serial(0) { }
121 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
122 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
123 void rollback() { pending = current; ++serial; }
125 operator T() const { return current; }
140 ControlledVariable<unsigned> speed;
141 ControlledVariable<bool> reverse;
142 ControlledVariable<unsigned> funcs;
143 unsigned last_change_age;
145 Locomotive(Protocol, unsigned);
147 unsigned create_speed_dir_command(char *) const;
148 unsigned create_speed_func_command(unsigned, char *) const;
151 struct MfxInfo: public DetectedLocomotive
153 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
179 unsigned valid_states;
180 ControlledVariable<unsigned> state;
183 Msp::Time::TimeDelta active_time;
185 Accessory(Kind, unsigned, unsigned, unsigned);
187 unsigned create_state_command(unsigned, bool, char *) const;
198 ControlledVariable<bool> state;
203 struct PendingCommand
207 unsigned char length;
208 unsigned repeat_count;
211 PendingCommand(GeneralCommand);
212 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
213 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
224 void push(const T &);
226 unsigned size() const;
235 Msp::Time::TimeStamp sleep_timeout;
237 Task(const std::string &, unsigned = 0);
241 const std::string &get_name() const { return name; }
243 virtual bool get_work(PendingCommand &) = 0;
244 virtual void process_reply(const char *, unsigned) { }
246 unsigned get_priority() const { return priority; }
247 const Msp::Time::TimeStamp &get_sleep_timeout() const { return sleep_timeout; }
249 void sleep(const Msp::Time::TimeDelta &);
252 class CommandQueueTask: public Task
255 Queue<PendingCommand> queue;
260 virtual bool get_work(PendingCommand &);
262 void push(const PendingCommand &);
263 unsigned size() const { return queue.size(); }
264 bool empty() const { return queue.empty(); }
267 class RefreshTask: public Task
270 typedef std::list<Locomotive *> LocomotivePtrList;
272 LocomotivePtrList cycle;
273 LocomotivePtrList::iterator next;
282 virtual bool get_work(PendingCommand &);
284 void add_loco(Locomotive &);
285 void remove_loco(Locomotive &);
287 Locomotive *get_next_loco();
291 class S88Task: public Task
294 ArduControl &control;
296 unsigned octets_remaining;
297 Msp::Time::TimeStamp last_poll;
298 Msp::Time::TimeDelta latency;
301 S88Task(ArduControl &);
303 virtual bool get_work(PendingCommand &);
304 virtual void process_reply(const char *, unsigned);
306 void set_n_octets(unsigned);
307 void grow_n_octets(unsigned);
309 const Msp::Time::TimeDelta &get_latency() const { return latency; }
312 class MfxAnnounceTask: public Task
320 virtual bool get_work(PendingCommand &);
322 void set_serial(unsigned);
323 unsigned get_serial() const { return serial; }
326 class MfxSearchTask: public Task
329 ArduControl &control;
330 unsigned next_address;
334 Queue<MfxInfo> queue;
336 MfxInfo *pending_info;
338 unsigned read_offset;
339 unsigned read_length;
340 char read_data[0x40];
344 MfxSearchTask(ArduControl &);
346 virtual bool get_work(PendingCommand &);
347 virtual void process_reply(const char *, unsigned);
349 void set_next_address(unsigned);
350 bool pop_info(MfxInfo &);
353 class MonitorTask: public Task
365 virtual bool get_work(PendingCommand &);
366 virtual void process_reply(const char *, unsigned);
368 float get_voltage() const { return voltage; }
369 float get_current() const { return current; }
371 float get_peak() const { return peak_level-base_level; }
374 class ControlThread: public Msp::Thread
377 ArduControl &control;
379 std::vector<Task *> tasks;
382 ControlThread(ArduControl &);
387 void init_baud_rate();
388 bool get_work(PendingCommand &);
389 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
390 unsigned process_reply(const char *, unsigned);
391 void set_power(bool);
394 typedef std::map<unsigned, Locomotive> LocomotiveMap;
395 typedef std::vector<MfxInfo> MfxInfoArray;
396 typedef std::map<unsigned, Accessory> AccessoryMap;
397 typedef std::list<Accessory *> AccessoryPtrList;
398 typedef std::map<unsigned, Sensor> SensorMap;
400 Msp::IO::Serial serial;
402 Msp::FS::Path state_file;
404 ControlledVariable<bool> power;
407 LocomotiveMap locomotives;
408 MfxInfoArray mfx_info;
409 AccessoryMap accessories;
410 AccessoryPtrList accessory_queue;
411 Accessory *active_accessory;
412 unsigned char active_index;
413 Msp::Time::TimeStamp off_timeout;
417 Msp::Time::TimeDelta command_timeout;
418 CommandQueueTask command_queue;
419 Queue<Tag> completed_commands;
422 MfxAnnounceTask mfx_announce;
423 MfxSearchTask mfx_search;
425 ControlThread thread;
427 static ProtocolInfo protocol_info[2];
428 static TelemetryInfo telemetry_info[4];
431 ArduControl(const Options &);
434 virtual void set_power(bool);
435 virtual bool get_power() const { return power; }
436 virtual void halt(bool);
437 virtual bool is_halted() const { return halted; }
439 virtual const char *enumerate_protocols(unsigned) const;
441 static Protocol map_protocol(const std::string &);
443 virtual unsigned get_protocol_speed_steps(const std::string &) const;
445 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
446 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
448 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
449 MfxInfo *find_mfx_info(unsigned);
451 virtual void remove_loco(unsigned);
452 virtual void set_loco_speed(unsigned, unsigned);
453 virtual void set_loco_reverse(unsigned, bool);
454 virtual void set_loco_function(unsigned, unsigned, bool);
456 virtual unsigned add_turnout(unsigned, const TrackType &);
457 virtual void remove_turnout(unsigned);
458 virtual void set_turnout(unsigned, unsigned);
459 virtual unsigned get_turnout(unsigned) const;
461 virtual unsigned add_signal(unsigned, const SignalType &);
462 virtual void remove_signal(unsigned);
463 virtual void set_signal(unsigned, unsigned);
464 virtual unsigned get_signal(unsigned) const;
467 unsigned add_accessory(Accessory::Kind, unsigned, unsigned, unsigned);
468 void remove_accessory(Accessory::Kind, unsigned);
469 void set_accessory(Accessory::Kind, unsigned, unsigned);
470 unsigned get_accessory(Accessory::Kind, unsigned) const;
471 void activate_accessory_by_mask(Accessory &, unsigned);
474 virtual unsigned add_sensor(unsigned);
475 virtual void remove_sensor(unsigned);
476 virtual void set_sensor(unsigned, bool) { }
477 virtual bool get_sensor(unsigned) const;
479 virtual const TelemetryInfo *enumerate_telemetry(unsigned) const;
480 virtual float get_telemetry_value(const std::string &) const;
483 virtual void flush();
485 void save_state() const;