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; }
95 struct ControlledVariable
99 unsigned short serial;
101 ControlledVariable(): current(), pending(), serial(0) { }
102 ControlledVariable(T v): current(v), pending(v), serial(0) { }
104 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
105 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
107 operator T() const { return current; }
122 ControlledVariable<unsigned> speed;
123 ControlledVariable<bool> reverse;
124 ControlledVariable<unsigned> funcs;
125 unsigned last_change_age;
127 Locomotive(Protocol, unsigned);
129 unsigned create_speed_dir_command(char *) const;
130 unsigned create_speed_func_command(unsigned, char *) const;
150 ControlledVariable<unsigned> state;
152 Msp::Time::TimeDelta active_time;
154 Accessory(Kind, unsigned, unsigned);
156 unsigned create_state_command(unsigned, bool, char *) const;
167 ControlledVariable<bool> state;
172 struct PendingCommand
176 unsigned char length;
177 unsigned repeat_count;
180 PendingCommand(GeneralCommand);
181 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
182 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
192 virtual bool get_work(PendingCommand &) = 0;
193 virtual void process_reply(const char *, unsigned) { }
196 class RefreshTask: public Task
199 typedef std::list<Locomotive *> LocomotivePtrList;
201 LocomotivePtrList cycle;
202 LocomotivePtrList::iterator next;
211 virtual bool get_work(PendingCommand &);
213 void add_loco(Locomotive &);
214 void remove_loco(Locomotive &);
216 Locomotive *get_next_loco();
220 class S88Task: public Task
223 ArduControl &control;
225 unsigned octets_remaining;
228 S88Task(ArduControl &);
230 virtual bool get_work(PendingCommand &);
231 virtual void process_reply(const char *, unsigned);
233 void set_n_octets(unsigned);
234 void grow_n_octets(unsigned);
237 class MfxAnnounceTask: public Task
241 Msp::Time::TimeStamp next;
246 virtual bool get_work(PendingCommand &);
248 void set_serial(unsigned);
251 class MfxSearchTask: public Task
254 ArduControl &control;
255 unsigned next_address;
256 Msp::Time::TimeStamp next;
262 MfxSearchTask(ArduControl &);
264 virtual bool get_work(PendingCommand &);
265 virtual void process_reply(const char *, unsigned);
268 class ControlThread: public Msp::Thread
271 ArduControl &control;
273 std::vector<Task *> tasks;
276 ControlThread(ArduControl &);
281 void init_baud_rate();
282 bool get_work(PendingCommand &);
283 unsigned do_command(const PendingCommand &);
284 unsigned process_reply(const char *, unsigned);
287 typedef std::map<unsigned, Locomotive> LocomotiveMap;
288 typedef std::map<unsigned, Accessory> AccessoryMap;
289 typedef std::list<Accessory *> AccessoryPtrList;
290 typedef std::map<unsigned, Sensor> SensorMap;
292 Msp::IO::Serial serial;
295 ControlledVariable<bool> power;
297 LocomotiveMap locomotives;
298 AccessoryMap accessories;
299 AccessoryPtrList accessory_queue;
300 Accessory *active_accessory;
301 Msp::Time::TimeStamp off_timeout;
302 std::list<PendingCommand> command_queue;
303 std::list<Tag> completed_commands;
310 MfxAnnounceTask mfx_announce;
311 MfxSearchTask mfx_search;
312 ControlThread thread;
314 static ProtocolInfo protocol_info[2];
317 ArduControl(const std::string &);
320 virtual void set_power(bool);
321 virtual bool get_power() const { return power; }
322 virtual void halt(bool);
323 virtual bool is_halted() const { return false; }
325 virtual const char *enumerate_protocols(unsigned) const;
327 static Protocol map_protocol(const std::string &);
329 virtual unsigned get_protocol_speed_steps(const std::string &) const;
331 virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const { return 0; }
332 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
333 virtual void remove_loco(unsigned);
334 virtual void set_loco_speed(unsigned, unsigned);
335 virtual void set_loco_reverse(unsigned, bool);
336 virtual void set_loco_function(unsigned, unsigned, bool);
338 virtual unsigned add_turnout(unsigned, const TrackType &);
339 virtual void remove_turnout(unsigned);
340 virtual void set_turnout(unsigned, unsigned);
341 virtual unsigned get_turnout(unsigned) const;
343 virtual unsigned add_signal(unsigned, const SignalType &);
344 virtual void remove_signal(unsigned);
345 virtual void set_signal(unsigned, unsigned);
346 virtual unsigned get_signal(unsigned) const;
349 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
350 void remove_accessory(Accessory::Kind, unsigned);
351 void set_accessory(Accessory::Kind, unsigned, unsigned);
352 unsigned get_accessory(Accessory::Kind, unsigned) const;
355 virtual unsigned add_sensor(unsigned);
356 virtual void remove_sensor(unsigned);
357 virtual void set_sensor(unsigned, bool) { }
358 virtual bool get_sensor(unsigned) const;
361 virtual void flush();
364 void push_command(const PendingCommand &);
365 bool pop_command(PendingCommand &);
366 void push_completed_tag(const Tag &);
367 Tag pop_completed_tag();