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,
31 RECEIVE_OVERRUN = 0x81,
33 INVALID_COMMAND = 0x83,
55 unsigned char command;
56 unsigned short serial;
61 operator bool() const { return type!=NONE; }
82 struct ControlledVariable
86 unsigned short serial;
88 ControlledVariable(): current(), pending(), serial(0) { }
89 ControlledVariable(T v): current(v), pending(v), serial(0) { }
91 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
92 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
94 operator T() const { return current; }
109 ControlledVariable<unsigned> speed;
110 ControlledVariable<bool> reverse;
111 ControlledVariable<unsigned> funcs;
112 unsigned last_change_age;
114 Locomotive(Protocol, unsigned);
116 unsigned create_speed_dir_command(char *) const;
117 unsigned create_speed_func_command(unsigned, char *) const;
137 ControlledVariable<unsigned> state;
139 Msp::Time::TimeDelta active_time;
141 Accessory(Kind, unsigned, unsigned);
143 unsigned create_state_command(unsigned, bool, char *) const;
154 ControlledVariable<bool> state;
159 class ControlThread: public Msp::Thread
162 ArduControl &control;
166 ControlThread(ArduControl &);
177 unsigned char length;
180 QueuedCommand(GeneralCommand);
181 QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0);
182 QueuedCommand(Accessory &, Accessory::Command, unsigned = 0);
185 typedef std::map<unsigned, Locomotive> LocomotiveMap;
186 typedef std::list<Locomotive *> LocomotivePtrList;
187 typedef std::map<unsigned, Accessory> AccessoryMap;
188 typedef std::list<Accessory *> AccessoryPtrList;
189 typedef std::map<unsigned, Sensor> SensorMap;
191 Msp::IO::Serial serial;
194 ControlledVariable<bool> power;
196 LocomotiveMap locomotives;
197 LocomotivePtrList refresh_cycle;
198 LocomotivePtrList::iterator next_refresh;
199 unsigned refresh_counter;
200 AccessoryMap accessories;
201 AccessoryPtrList accessory_queue;
202 Accessory *active_accessory;
203 Msp::Time::TimeStamp off_timeout;
204 std::list<QueuedCommand> command_queue;
205 std::list<Tag> completed_commands;
208 unsigned n_s88_octets;
211 ControlThread thread;
213 static ProtocolInfo protocol_info[2];
216 ArduControl(const std::string &);
219 virtual void set_power(bool);
220 virtual bool get_power() const { return power; }
221 virtual void halt(bool);
222 virtual bool is_halted() const { return false; }
224 virtual const char *enumerate_protocols(unsigned) const;
226 static Protocol map_protocol(const std::string &);
228 virtual unsigned get_protocol_speed_steps(const std::string &) const;
230 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
231 virtual void remove_loco(unsigned);
232 virtual void set_loco_speed(unsigned, unsigned);
233 virtual void set_loco_reverse(unsigned, bool);
234 virtual void set_loco_function(unsigned, unsigned, bool);
236 void add_loco_to_refresh(Locomotive &);
237 void remove_loco_from_refresh(Locomotive &);
238 Locomotive *get_loco_to_refresh();
239 void advance_next_refresh();
242 virtual unsigned add_turnout(unsigned, const TrackType &);
243 virtual void remove_turnout(unsigned);
244 virtual void set_turnout(unsigned, unsigned);
245 virtual unsigned get_turnout(unsigned) const;
247 virtual unsigned add_signal(unsigned, const SignalType &);
248 virtual void remove_signal(unsigned);
249 virtual void set_signal(unsigned, unsigned);
250 virtual unsigned get_signal(unsigned) const;
253 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
254 void remove_accessory(Accessory::Kind, unsigned);
255 void set_accessory(Accessory::Kind, unsigned, unsigned);
256 unsigned get_accessory(Accessory::Kind, unsigned) const;
259 virtual unsigned add_sensor(unsigned);
260 virtual void remove_sensor(unsigned);
261 virtual void set_sensor(unsigned, bool) { }
262 virtual bool get_sensor(unsigned) const;
265 virtual void flush();
268 void push_command(const QueuedCommand &);
269 bool pop_command(QueuedCommand &);
270 void push_completed_tag(const Tag &);
271 Tag pop_completed_tag();