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; }
121 operator T() const { return current; }
136 ControlledVariable<unsigned> speed;
137 ControlledVariable<bool> reverse;
138 ControlledVariable<unsigned> funcs;
139 unsigned last_change_age;
141 Locomotive(Protocol, unsigned);
143 unsigned create_speed_dir_command(char *) const;
144 unsigned create_speed_func_command(unsigned, char *) const;
147 struct MfxInfo: public DetectedLocomotive
149 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
175 ControlledVariable<unsigned> state;
177 Msp::Time::TimeDelta active_time;
179 Accessory(Kind, unsigned, unsigned);
181 unsigned create_state_command(unsigned, bool, char *) const;
192 ControlledVariable<bool> state;
197 struct PendingCommand
201 unsigned char length;
202 unsigned repeat_count;
205 PendingCommand(GeneralCommand);
206 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
207 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
218 void push(const T &);
229 virtual bool get_work(PendingCommand &) = 0;
230 virtual void process_reply(const char *, unsigned) { }
233 class CommandQueueTask: public Task
236 Queue<PendingCommand> queue;
239 virtual bool get_work(PendingCommand &);
242 class RefreshTask: public Task
245 typedef std::list<Locomotive *> LocomotivePtrList;
247 LocomotivePtrList cycle;
248 LocomotivePtrList::iterator next;
257 virtual bool get_work(PendingCommand &);
259 void add_loco(Locomotive &);
260 void remove_loco(Locomotive &);
262 Locomotive *get_next_loco();
266 class S88Task: public Task
269 ArduControl &control;
271 unsigned octets_remaining;
275 S88Task(ArduControl &);
277 virtual bool get_work(PendingCommand &);
278 virtual void process_reply(const char *, unsigned);
280 void set_n_octets(unsigned);
281 void grow_n_octets(unsigned);
284 class MfxAnnounceTask: public Task
288 Msp::Time::TimeStamp next;
293 virtual bool get_work(PendingCommand &);
295 void set_serial(unsigned);
296 unsigned get_serial() const { return serial; }
299 class MfxSearchTask: public Task
302 ArduControl &control;
303 unsigned next_address;
304 Msp::Time::TimeStamp next;
308 Queue<MfxInfo> queue;
311 MfxSearchTask(ArduControl &);
313 virtual bool get_work(PendingCommand &);
314 virtual void process_reply(const char *, unsigned);
316 void set_next_address(unsigned);
317 bool pop_info(MfxInfo &);
320 class ControlThread: public Msp::Thread
323 ArduControl &control;
325 std::vector<Task *> tasks;
328 ControlThread(ArduControl &);
333 void init_baud_rate();
334 bool get_work(PendingCommand &);
335 unsigned do_command(const PendingCommand &);
336 unsigned process_reply(const char *, unsigned);
339 typedef std::map<unsigned, Locomotive> LocomotiveMap;
340 typedef std::vector<MfxInfo> MfxInfoArray;
341 typedef std::map<unsigned, Accessory> AccessoryMap;
342 typedef std::list<Accessory *> AccessoryPtrList;
343 typedef std::map<unsigned, Sensor> SensorMap;
345 Msp::IO::Serial serial;
347 Msp::FS::Path state_file;
349 ControlledVariable<bool> power;
351 LocomotiveMap locomotives;
352 MfxInfoArray mfx_info;
353 AccessoryMap accessories;
354 AccessoryPtrList accessory_queue;
355 Accessory *active_accessory;
356 Msp::Time::TimeStamp off_timeout;
361 Queue<PendingCommand> command_queue;
362 Queue<Tag> completed_commands;
365 MfxAnnounceTask mfx_announce;
366 MfxSearchTask mfx_search;
367 ControlThread thread;
369 static ProtocolInfo protocol_info[2];
372 ArduControl(const std::string &);
375 virtual void set_power(bool);
376 virtual bool get_power() const { return power; }
377 virtual void halt(bool);
378 virtual bool is_halted() const { return false; }
380 virtual const char *enumerate_protocols(unsigned) const;
382 static Protocol map_protocol(const std::string &);
384 virtual unsigned get_protocol_speed_steps(const std::string &) const;
386 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
387 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
389 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
391 virtual void remove_loco(unsigned);
392 virtual void set_loco_speed(unsigned, unsigned);
393 virtual void set_loco_reverse(unsigned, bool);
394 virtual void set_loco_function(unsigned, unsigned, bool);
396 virtual unsigned add_turnout(unsigned, const TrackType &);
397 virtual void remove_turnout(unsigned);
398 virtual void set_turnout(unsigned, unsigned);
399 virtual unsigned get_turnout(unsigned) const;
401 virtual unsigned add_signal(unsigned, const SignalType &);
402 virtual void remove_signal(unsigned);
403 virtual void set_signal(unsigned, unsigned);
404 virtual unsigned get_signal(unsigned) const;
407 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
408 void remove_accessory(Accessory::Kind, unsigned);
409 void set_accessory(Accessory::Kind, unsigned, unsigned);
410 unsigned get_accessory(Accessory::Kind, unsigned) const;
413 virtual unsigned add_sensor(unsigned);
414 virtual void remove_sensor(unsigned);
415 virtual void set_sensor(unsigned, bool) { }
416 virtual bool get_sensor(unsigned) const;
419 virtual void flush();
421 void save_state() const;