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,
49 MFX_SEARCH_FEEDBACK = 0xD1,
50 MFX_PING_FEEDBACK = 0xD2
65 unsigned char command;
66 unsigned short serial;
71 operator bool() const { return type!=NONE; }
93 struct ControlledVariable
97 unsigned short serial;
99 ControlledVariable(): current(), pending(), serial(0) { }
100 ControlledVariable(T v): current(v), pending(v), serial(0) { }
102 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
103 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
105 operator T() const { return current; }
120 ControlledVariable<unsigned> speed;
121 ControlledVariable<bool> reverse;
122 ControlledVariable<unsigned> funcs;
123 unsigned last_change_age;
125 Locomotive(Protocol, unsigned);
127 unsigned create_speed_dir_command(char *) const;
128 unsigned create_speed_func_command(unsigned, char *) const;
148 ControlledVariable<unsigned> state;
150 Msp::Time::TimeDelta active_time;
152 Accessory(Kind, unsigned, unsigned);
154 unsigned create_state_command(unsigned, bool, char *) const;
165 ControlledVariable<bool> state;
170 struct PendingCommand
174 unsigned char length;
175 unsigned repeat_count;
178 PendingCommand(GeneralCommand);
179 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
180 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
190 virtual bool get_work(PendingCommand &) = 0;
191 virtual void process_reply(const char *, unsigned) { }
194 class RefreshTask: public Task
197 typedef std::list<Locomotive *> LocomotivePtrList;
199 LocomotivePtrList cycle;
200 LocomotivePtrList::iterator next;
209 virtual bool get_work(PendingCommand &);
211 void add_loco(Locomotive &);
212 void remove_loco(Locomotive &);
214 Locomotive *get_next_loco();
218 class S88Task: public Task
221 ArduControl &control;
223 unsigned octets_remaining;
226 S88Task(ArduControl &);
228 virtual bool get_work(PendingCommand &);
229 virtual void process_reply(const char *, unsigned);
231 void set_n_octets(unsigned);
232 void grow_n_octets(unsigned);
235 class MfxAnnounceTask: public Task
239 Msp::Time::TimeStamp next;
244 virtual bool get_work(PendingCommand &);
246 void set_serial(unsigned);
249 class MfxSearchTask: public Task
252 ArduControl &control;
253 unsigned next_address;
254 Msp::Time::TimeStamp next;
260 MfxSearchTask(ArduControl &);
262 virtual bool get_work(PendingCommand &);
263 virtual void process_reply(const char *, unsigned);
266 class ControlThread: public Msp::Thread
269 ArduControl &control;
271 std::vector<Task *> tasks;
274 ControlThread(ArduControl &);
279 bool get_work(PendingCommand &);
280 unsigned do_command(const PendingCommand &);
281 unsigned process_reply(const char *, unsigned);
284 typedef std::map<unsigned, Locomotive> LocomotiveMap;
285 typedef std::map<unsigned, Accessory> AccessoryMap;
286 typedef std::list<Accessory *> AccessoryPtrList;
287 typedef std::map<unsigned, Sensor> SensorMap;
289 Msp::IO::Serial serial;
292 ControlledVariable<bool> power;
294 LocomotiveMap locomotives;
295 AccessoryMap accessories;
296 AccessoryPtrList accessory_queue;
297 Accessory *active_accessory;
298 Msp::Time::TimeStamp off_timeout;
299 std::list<PendingCommand> command_queue;
300 std::list<Tag> completed_commands;
307 MfxAnnounceTask mfx_announce;
308 MfxSearchTask mfx_search;
309 ControlThread thread;
311 static ProtocolInfo protocol_info[2];
314 ArduControl(const std::string &);
317 virtual void set_power(bool);
318 virtual bool get_power() const { return power; }
319 virtual void halt(bool);
320 virtual bool is_halted() const { return false; }
322 virtual const char *enumerate_protocols(unsigned) const;
324 static Protocol map_protocol(const std::string &);
326 virtual unsigned get_protocol_speed_steps(const std::string &) const;
328 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
329 virtual void remove_loco(unsigned);
330 virtual void set_loco_speed(unsigned, unsigned);
331 virtual void set_loco_reverse(unsigned, bool);
332 virtual void set_loco_function(unsigned, unsigned, bool);
334 virtual unsigned add_turnout(unsigned, const TrackType &);
335 virtual void remove_turnout(unsigned);
336 virtual void set_turnout(unsigned, unsigned);
337 virtual unsigned get_turnout(unsigned) const;
339 virtual unsigned add_signal(unsigned, const SignalType &);
340 virtual void remove_signal(unsigned);
341 virtual void set_signal(unsigned, unsigned);
342 virtual unsigned get_signal(unsigned) const;
345 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
346 void remove_accessory(Accessory::Kind, unsigned);
347 void set_accessory(Accessory::Kind, unsigned, unsigned);
348 unsigned get_accessory(Accessory::Kind, unsigned) const;
351 virtual unsigned add_sensor(unsigned);
352 virtual void remove_sensor(unsigned);
353 virtual void set_sensor(unsigned, bool) { }
354 virtual bool get_sensor(unsigned) const;
357 virtual void flush();
360 void push_command(const PendingCommand &);
361 bool pop_command(PendingCommand &);
362 void push_completed_tag(const Tag &);
363 Tag pop_completed_tag();