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);
199 void push(const T &);
210 virtual bool get_work(PendingCommand &) = 0;
211 virtual void process_reply(const char *, unsigned) { }
214 class RefreshTask: public Task
217 typedef std::list<Locomotive *> LocomotivePtrList;
219 LocomotivePtrList cycle;
220 LocomotivePtrList::iterator next;
229 virtual bool get_work(PendingCommand &);
231 void add_loco(Locomotive &);
232 void remove_loco(Locomotive &);
234 Locomotive *get_next_loco();
238 class S88Task: public Task
241 ArduControl &control;
243 unsigned octets_remaining;
246 S88Task(ArduControl &);
248 virtual bool get_work(PendingCommand &);
249 virtual void process_reply(const char *, unsigned);
251 void set_n_octets(unsigned);
252 void grow_n_octets(unsigned);
255 class MfxAnnounceTask: public Task
259 Msp::Time::TimeStamp next;
264 virtual bool get_work(PendingCommand &);
266 void set_serial(unsigned);
269 class MfxSearchTask: public Task
272 ArduControl &control;
273 unsigned next_address;
274 Msp::Time::TimeStamp next;
278 Queue<MfxInfo> queue;
281 MfxSearchTask(ArduControl &);
283 virtual bool get_work(PendingCommand &);
284 virtual void process_reply(const char *, unsigned);
286 bool pop_info(MfxInfo &);
289 class ControlThread: public Msp::Thread
292 ArduControl &control;
294 std::vector<Task *> tasks;
297 ControlThread(ArduControl &);
302 void init_baud_rate();
303 bool get_work(PendingCommand &);
304 unsigned do_command(const PendingCommand &);
305 unsigned process_reply(const char *, unsigned);
308 typedef std::map<unsigned, Locomotive> LocomotiveMap;
309 typedef std::vector<MfxInfo> MfxInfoArray;
310 typedef std::map<unsigned, Accessory> AccessoryMap;
311 typedef std::list<Accessory *> AccessoryPtrList;
312 typedef std::map<unsigned, Sensor> SensorMap;
314 Msp::IO::Serial serial;
317 ControlledVariable<bool> power;
319 LocomotiveMap locomotives;
320 MfxInfoArray mfx_info;
321 AccessoryMap accessories;
322 AccessoryPtrList accessory_queue;
323 Accessory *active_accessory;
324 Msp::Time::TimeStamp off_timeout;
329 Queue<PendingCommand> command_queue;
330 Queue<Tag> completed_commands;
333 MfxAnnounceTask mfx_announce;
334 MfxSearchTask mfx_search;
335 ControlThread thread;
337 static ProtocolInfo protocol_info[2];
340 ArduControl(const std::string &);
343 virtual void set_power(bool);
344 virtual bool get_power() const { return power; }
345 virtual void halt(bool);
346 virtual bool is_halted() const { return false; }
348 virtual const char *enumerate_protocols(unsigned) const;
350 static Protocol map_protocol(const std::string &);
352 virtual unsigned get_protocol_speed_steps(const std::string &) const;
354 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
355 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
356 virtual void remove_loco(unsigned);
357 virtual void set_loco_speed(unsigned, unsigned);
358 virtual void set_loco_reverse(unsigned, bool);
359 virtual void set_loco_function(unsigned, unsigned, bool);
361 virtual unsigned add_turnout(unsigned, const TrackType &);
362 virtual void remove_turnout(unsigned);
363 virtual void set_turnout(unsigned, unsigned);
364 virtual unsigned get_turnout(unsigned) const;
366 virtual unsigned add_signal(unsigned, const SignalType &);
367 virtual void remove_signal(unsigned);
368 virtual void set_signal(unsigned, unsigned);
369 virtual unsigned get_signal(unsigned) const;
372 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
373 void remove_accessory(Accessory::Kind, unsigned);
374 void set_accessory(Accessory::Kind, unsigned, unsigned);
375 unsigned get_accessory(Accessory::Kind, unsigned) const;
378 virtual unsigned add_sensor(unsigned);
379 virtual void remove_sensor(unsigned);
380 virtual void set_sensor(unsigned, bool) { }
381 virtual bool get_sensor(unsigned) const;
384 virtual void flush();