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 unsigned valid_states;
177 ControlledVariable<unsigned> state;
180 Msp::Time::TimeDelta active_time;
182 Accessory(Kind, unsigned, unsigned, unsigned);
184 unsigned create_state_command(unsigned, bool, char *) const;
195 ControlledVariable<bool> state;
200 struct PendingCommand
204 unsigned char length;
205 unsigned repeat_count;
208 PendingCommand(GeneralCommand);
209 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
210 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
221 void push(const T &);
233 virtual bool get_work(PendingCommand &) = 0;
234 virtual void process_reply(const char *, unsigned) { }
237 class CommandQueueTask: public Task
240 Queue<PendingCommand> queue;
243 virtual bool get_work(PendingCommand &);
246 class RefreshTask: public Task
249 typedef std::list<Locomotive *> LocomotivePtrList;
251 LocomotivePtrList cycle;
252 LocomotivePtrList::iterator next;
261 virtual bool get_work(PendingCommand &);
263 void add_loco(Locomotive &);
264 void remove_loco(Locomotive &);
266 Locomotive *get_next_loco();
270 class S88Task: public Task
273 ArduControl &control;
275 unsigned octets_remaining;
279 S88Task(ArduControl &);
281 virtual bool get_work(PendingCommand &);
282 virtual void process_reply(const char *, unsigned);
284 void set_n_octets(unsigned);
285 void grow_n_octets(unsigned);
288 class MfxAnnounceTask: public Task
292 Msp::Time::TimeStamp next;
297 virtual bool get_work(PendingCommand &);
299 void set_serial(unsigned);
300 unsigned get_serial() const { return serial; }
303 class MfxSearchTask: public Task
306 ArduControl &control;
307 unsigned next_address;
308 Msp::Time::TimeStamp next;
312 Queue<MfxInfo> queue;
315 MfxSearchTask(ArduControl &);
317 virtual bool get_work(PendingCommand &);
318 virtual void process_reply(const char *, unsigned);
320 void set_next_address(unsigned);
321 bool pop_info(MfxInfo &);
324 class MonitorTask: public Task
331 Msp::Time::TimeStamp next_poll;
337 virtual bool get_work(PendingCommand &);
338 virtual void process_reply(const char *, unsigned);
340 float get_voltage() const { return voltage; }
341 float get_current() const { return current; }
343 float get_peak() const { return peak_level-base_level; }
346 class ControlThread: public Msp::Thread
349 ArduControl &control;
351 std::vector<Task *> tasks;
354 ControlThread(ArduControl &);
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);
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;
372 Msp::IO::Serial serial;
374 Msp::FS::Path state_file;
376 ControlledVariable<bool> power;
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;
389 Msp::Time::TimeDelta command_timeout;
390 Queue<PendingCommand> command_queue;
391 Queue<Tag> completed_commands;
394 MfxAnnounceTask mfx_announce;
395 MfxSearchTask mfx_search;
397 ControlThread thread;
399 static ProtocolInfo protocol_info[2];
402 ArduControl(const Options &);
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; }
410 virtual const char *enumerate_protocols(unsigned) const;
412 static Protocol map_protocol(const std::string &);
414 virtual unsigned get_protocol_speed_steps(const std::string &) const;
416 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
417 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
419 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
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);
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;
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;
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);
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;
450 virtual void flush();
452 void save_state() const;