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 &);
231 virtual bool get_work(PendingCommand &) = 0;
232 virtual void process_reply(const char *, unsigned) { }
235 class CommandQueueTask: public Task
238 Queue<PendingCommand> queue;
241 virtual bool get_work(PendingCommand &);
244 class RefreshTask: public Task
247 typedef std::list<Locomotive *> LocomotivePtrList;
249 LocomotivePtrList cycle;
250 LocomotivePtrList::iterator next;
259 virtual bool get_work(PendingCommand &);
261 void add_loco(Locomotive &);
262 void remove_loco(Locomotive &);
264 Locomotive *get_next_loco();
268 class S88Task: public Task
271 ArduControl &control;
273 unsigned octets_remaining;
277 S88Task(ArduControl &);
279 virtual bool get_work(PendingCommand &);
280 virtual void process_reply(const char *, unsigned);
282 void set_n_octets(unsigned);
283 void grow_n_octets(unsigned);
286 class MfxAnnounceTask: public Task
290 Msp::Time::TimeStamp next;
295 virtual bool get_work(PendingCommand &);
297 void set_serial(unsigned);
298 unsigned get_serial() const { return serial; }
301 class MfxSearchTask: public Task
304 ArduControl &control;
305 unsigned next_address;
306 Msp::Time::TimeStamp next;
310 Queue<MfxInfo> queue;
313 MfxSearchTask(ArduControl &);
315 virtual bool get_work(PendingCommand &);
316 virtual void process_reply(const char *, unsigned);
318 void set_next_address(unsigned);
319 bool pop_info(MfxInfo &);
322 class MonitorTask: public Task
329 Msp::Time::TimeStamp next_poll;
335 virtual bool get_work(PendingCommand &);
336 virtual void process_reply(const char *, unsigned);
338 float get_voltage() const { return voltage; }
339 float get_current() const { return current; }
341 float get_peak() const { return peak_level-base_level; }
344 class ControlThread: public Msp::Thread
347 ArduControl &control;
349 std::vector<Task *> tasks;
352 ControlThread(ArduControl &);
357 void init_baud_rate();
358 bool get_work(PendingCommand &);
359 unsigned do_command(const PendingCommand &);
360 unsigned process_reply(const char *, unsigned);
363 typedef std::map<unsigned, Locomotive> LocomotiveMap;
364 typedef std::vector<MfxInfo> MfxInfoArray;
365 typedef std::map<unsigned, Accessory> AccessoryMap;
366 typedef std::list<Accessory *> AccessoryPtrList;
367 typedef std::map<unsigned, Sensor> SensorMap;
369 Msp::IO::Serial serial;
371 Msp::FS::Path state_file;
373 ControlledVariable<bool> power;
376 LocomotiveMap locomotives;
377 MfxInfoArray mfx_info;
378 AccessoryMap accessories;
379 AccessoryPtrList accessory_queue;
380 Accessory *active_accessory;
381 unsigned char active_index;
382 Msp::Time::TimeStamp off_timeout;
387 Queue<PendingCommand> command_queue;
388 Queue<Tag> completed_commands;
391 MfxAnnounceTask mfx_announce;
392 MfxSearchTask mfx_search;
394 ControlThread thread;
396 static ProtocolInfo protocol_info[2];
399 ArduControl(const Options &);
402 virtual void set_power(bool);
403 virtual bool get_power() const { return power; }
404 virtual void halt(bool);
405 virtual bool is_halted() const { return halted; }
407 virtual const char *enumerate_protocols(unsigned) const;
409 static Protocol map_protocol(const std::string &);
411 virtual unsigned get_protocol_speed_steps(const std::string &) const;
413 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
414 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
416 MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
418 virtual void remove_loco(unsigned);
419 virtual void set_loco_speed(unsigned, unsigned);
420 virtual void set_loco_reverse(unsigned, bool);
421 virtual void set_loco_function(unsigned, unsigned, bool);
423 virtual unsigned add_turnout(unsigned, const TrackType &);
424 virtual void remove_turnout(unsigned);
425 virtual void set_turnout(unsigned, unsigned);
426 virtual unsigned get_turnout(unsigned) const;
428 virtual unsigned add_signal(unsigned, const SignalType &);
429 virtual void remove_signal(unsigned);
430 virtual void set_signal(unsigned, unsigned);
431 virtual unsigned get_signal(unsigned) const;
434 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
435 void remove_accessory(Accessory::Kind, unsigned);
436 void set_accessory(Accessory::Kind, unsigned, unsigned);
437 unsigned get_accessory(Accessory::Kind, unsigned) const;
440 virtual unsigned add_sensor(unsigned);
441 virtual void remove_sensor(unsigned);
442 virtual void set_sensor(unsigned, bool) { }
443 virtual bool get_sensor(unsigned) const;
446 virtual void flush();
448 void save_state() const;