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/io/serial.h>
7 #include <msp/time/timedelta.h>
8 #include <msp/time/timestamp.h>
13 class ArduControl: public Driver
20 READ_POWER_STATE = 0x03,
21 READ_TRACK_CURRENT = 0x08,
22 SET_OVERCURRENT_LIMIT = 0x09,
23 READ_INPUT_VOLTAGE = 0x0A,
24 MOTOROLA_SPEED = 0x11,
25 MOTOROLA_REVERSE = 0x12,
26 MOTOROLA_SPEED_DIRECTION = 0x13,
27 MOTOROLA_SPEED_FUNCTION = 0x14,
28 MOTOROLA_SOLENOID = 0x15,
29 MFX_SET_STATION_ID = 0x21,
32 MFX_ASSIGN_ADDRESS = 0x24,
35 MFX_SPEED_FUNCS8 = 0x29,
36 MFX_SPEED_FUNCS16 = 0x2A,
40 RECEIVE_OVERRUN = 0x81,
42 INVALID_COMMAND = 0x83,
46 BAUD_CHANGE_FAILED = 0xA1,
51 MFX_SEARCH_FEEDBACK = 0xD1,
52 MFX_PING_FEEDBACK = 0xD2
67 unsigned char command;
68 unsigned short serial;
73 operator bool() const { return type!=NONE; }
96 struct ControlledVariable
100 unsigned short serial;
102 ControlledVariable(): current(), pending(), serial(0) { }
103 ControlledVariable(T v): current(v), pending(v), serial(0) { }
105 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
106 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
108 operator T() const { return current; }
123 ControlledVariable<unsigned> speed;
124 ControlledVariable<bool> reverse;
125 ControlledVariable<unsigned> funcs;
126 unsigned last_change_age;
128 Locomotive(Protocol, unsigned);
130 unsigned create_speed_dir_command(char *) const;
131 unsigned create_speed_func_command(unsigned, char *) const;
134 struct MfxInfo: public DetectedLocomotive
156 ControlledVariable<unsigned> state;
158 Msp::Time::TimeDelta active_time;
160 Accessory(Kind, unsigned, unsigned);
162 unsigned create_state_command(unsigned, bool, char *) const;
173 ControlledVariable<bool> state;
178 struct PendingCommand
182 unsigned char length;
183 unsigned repeat_count;
186 PendingCommand(GeneralCommand);
187 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
188 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
198 virtual bool get_work(PendingCommand &) = 0;
199 virtual void process_reply(const char *, unsigned) { }
202 class RefreshTask: public Task
205 typedef std::list<Locomotive *> LocomotivePtrList;
207 LocomotivePtrList cycle;
208 LocomotivePtrList::iterator next;
217 virtual bool get_work(PendingCommand &);
219 void add_loco(Locomotive &);
220 void remove_loco(Locomotive &);
222 Locomotive *get_next_loco();
226 class S88Task: public Task
229 ArduControl &control;
231 unsigned octets_remaining;
234 S88Task(ArduControl &);
236 virtual bool get_work(PendingCommand &);
237 virtual void process_reply(const char *, unsigned);
239 void set_n_octets(unsigned);
240 void grow_n_octets(unsigned);
243 class MfxAnnounceTask: public Task
247 Msp::Time::TimeStamp next;
252 virtual bool get_work(PendingCommand &);
254 void set_serial(unsigned);
257 class MfxSearchTask: public Task
260 ArduControl &control;
261 unsigned next_address;
262 Msp::Time::TimeStamp next;
266 std::list<MfxInfo> queue;
270 MfxSearchTask(ArduControl &);
272 virtual bool get_work(PendingCommand &);
273 virtual void process_reply(const char *, unsigned);
276 void push_info(const MfxInfo &);
278 bool pop_info(MfxInfo &);
281 class ControlThread: public Msp::Thread
284 ArduControl &control;
286 std::vector<Task *> tasks;
289 ControlThread(ArduControl &);
294 void init_baud_rate();
295 bool get_work(PendingCommand &);
296 unsigned do_command(const PendingCommand &);
297 unsigned process_reply(const char *, unsigned);
300 typedef std::map<unsigned, Locomotive> LocomotiveMap;
301 typedef std::vector<MfxInfo> MfxInfoArray;
302 typedef std::map<unsigned, Accessory> AccessoryMap;
303 typedef std::list<Accessory *> AccessoryPtrList;
304 typedef std::map<unsigned, Sensor> SensorMap;
306 Msp::IO::Serial serial;
309 ControlledVariable<bool> power;
311 LocomotiveMap locomotives;
312 MfxInfoArray mfx_info;
313 AccessoryMap accessories;
314 AccessoryPtrList accessory_queue;
315 Accessory *active_accessory;
316 Msp::Time::TimeStamp off_timeout;
317 std::list<PendingCommand> command_queue;
318 std::list<Tag> completed_commands;
325 MfxAnnounceTask mfx_announce;
326 MfxSearchTask mfx_search;
327 ControlThread thread;
329 static ProtocolInfo protocol_info[2];
332 ArduControl(const std::string &);
335 virtual void set_power(bool);
336 virtual bool get_power() const { return power; }
337 virtual void halt(bool);
338 virtual bool is_halted() const { return false; }
340 virtual const char *enumerate_protocols(unsigned) const;
342 static Protocol map_protocol(const std::string &);
344 virtual unsigned get_protocol_speed_steps(const std::string &) const;
346 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
347 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
348 virtual void remove_loco(unsigned);
349 virtual void set_loco_speed(unsigned, unsigned);
350 virtual void set_loco_reverse(unsigned, bool);
351 virtual void set_loco_function(unsigned, unsigned, bool);
353 virtual unsigned add_turnout(unsigned, const TrackType &);
354 virtual void remove_turnout(unsigned);
355 virtual void set_turnout(unsigned, unsigned);
356 virtual unsigned get_turnout(unsigned) const;
358 virtual unsigned add_signal(unsigned, const SignalType &);
359 virtual void remove_signal(unsigned);
360 virtual void set_signal(unsigned, unsigned);
361 virtual unsigned get_signal(unsigned) const;
364 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
365 void remove_accessory(Accessory::Kind, unsigned);
366 void set_accessory(Accessory::Kind, unsigned, unsigned);
367 unsigned get_accessory(Accessory::Kind, unsigned) const;
370 virtual unsigned add_sensor(unsigned);
371 virtual void remove_sensor(unsigned);
372 virtual void set_sensor(unsigned, bool) { }
373 virtual bool get_sensor(unsigned) const;
376 virtual void flush();
379 void push_command(const PendingCommand &);
380 bool pop_command(PendingCommand &);
381 void push_completed_tag(const Tag &);
382 Tag pop_completed_tag();