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,
39 RECEIVE_OVERRUN = 0x81,
41 INVALID_COMMAND = 0x83,
64 unsigned char command;
65 unsigned short serial;
70 operator bool() const { return type!=NONE; }
92 struct ControlledVariable
96 unsigned short serial;
98 ControlledVariable(): current(), pending(), serial(0) { }
99 ControlledVariable(T v): current(v), pending(v), serial(0) { }
101 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
102 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
104 operator T() const { return current; }
119 ControlledVariable<unsigned> speed;
120 ControlledVariable<bool> reverse;
121 ControlledVariable<unsigned> funcs;
122 unsigned last_change_age;
124 Locomotive(Protocol, unsigned);
126 unsigned create_speed_dir_command(char *) const;
127 unsigned create_speed_func_command(unsigned, char *) const;
147 ControlledVariable<unsigned> state;
149 Msp::Time::TimeDelta active_time;
151 Accessory(Kind, unsigned, unsigned);
153 unsigned create_state_command(unsigned, bool, char *) const;
164 ControlledVariable<bool> state;
169 struct PendingCommand
173 unsigned char length;
174 unsigned repeat_count;
177 PendingCommand(GeneralCommand);
178 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
179 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
189 virtual bool get_work(PendingCommand &) = 0;
190 virtual void process_reply(const char *, unsigned) { }
193 class RefreshTask: public Task
196 typedef std::list<Locomotive *> LocomotivePtrList;
198 LocomotivePtrList cycle;
199 LocomotivePtrList::iterator next;
208 virtual bool get_work(PendingCommand &);
210 void add_loco(Locomotive &);
211 void remove_loco(Locomotive &);
213 Locomotive *get_next_loco();
217 class S88Task: public Task
220 ArduControl &control;
222 unsigned octets_remaining;
225 S88Task(ArduControl &);
227 virtual bool get_work(PendingCommand &);
228 virtual void process_reply(const char *, unsigned);
230 void set_n_octets(unsigned);
231 void grow_n_octets(unsigned);
234 class MfxAnnounceTask: public Task
238 Msp::Time::TimeStamp next;
243 virtual bool get_work(PendingCommand &);
245 void set_serial(unsigned);
248 class MfxSearchTask: public Task
251 ArduControl &control;
252 unsigned next_address;
253 Msp::Time::TimeStamp next;
259 MfxSearchTask(ArduControl &);
261 virtual bool get_work(PendingCommand &);
262 virtual void process_reply(const char *, unsigned);
265 class ControlThread: public Msp::Thread
268 ArduControl &control;
270 std::vector<Task *> tasks;
273 ControlThread(ArduControl &);
278 bool get_work(PendingCommand &);
279 unsigned do_command(const PendingCommand &);
280 unsigned process_reply(const char *, unsigned);
283 typedef std::map<unsigned, Locomotive> LocomotiveMap;
284 typedef std::map<unsigned, Accessory> AccessoryMap;
285 typedef std::list<Accessory *> AccessoryPtrList;
286 typedef std::map<unsigned, Sensor> SensorMap;
288 Msp::IO::Serial serial;
291 ControlledVariable<bool> power;
293 LocomotiveMap locomotives;
294 AccessoryMap accessories;
295 AccessoryPtrList accessory_queue;
296 Accessory *active_accessory;
297 Msp::Time::TimeStamp off_timeout;
298 std::list<PendingCommand> command_queue;
299 std::list<Tag> completed_commands;
306 MfxAnnounceTask mfx_announce;
307 MfxSearchTask mfx_search;
308 ControlThread thread;
310 static ProtocolInfo protocol_info[2];
313 ArduControl(const std::string &);
316 virtual void set_power(bool);
317 virtual bool get_power() const { return power; }
318 virtual void halt(bool);
319 virtual bool is_halted() const { return false; }
321 virtual const char *enumerate_protocols(unsigned) const;
323 static Protocol map_protocol(const std::string &);
325 virtual unsigned get_protocol_speed_steps(const std::string &) const;
327 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
328 virtual void remove_loco(unsigned);
329 virtual void set_loco_speed(unsigned, unsigned);
330 virtual void set_loco_reverse(unsigned, bool);
331 virtual void set_loco_function(unsigned, unsigned, bool);
333 virtual unsigned add_turnout(unsigned, const TrackType &);
334 virtual void remove_turnout(unsigned);
335 virtual void set_turnout(unsigned, unsigned);
336 virtual unsigned get_turnout(unsigned) const;
338 virtual unsigned add_signal(unsigned, const SignalType &);
339 virtual void remove_signal(unsigned);
340 virtual void set_signal(unsigned, unsigned);
341 virtual unsigned get_signal(unsigned) const;
344 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
345 void remove_accessory(Accessory::Kind, unsigned);
346 void set_accessory(Accessory::Kind, unsigned, unsigned);
347 unsigned get_accessory(Accessory::Kind, unsigned) const;
350 virtual unsigned add_sensor(unsigned);
351 virtual void remove_sensor(unsigned);
352 virtual void set_sensor(unsigned, bool) { }
353 virtual bool get_sensor(unsigned) const;
356 virtual void flush();
359 void push_command(const PendingCommand &);
360 bool pop_command(PendingCommand &);
361 void push_completed_tag(const Tag &);
362 Tag pop_completed_tag();