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_TRACK_CURRENT = 0x08,
21 SET_OVERCURRENT_LIMIT = 0x09,
22 READ_INPUT_VOLTAGE = 0x0A,
23 MOTOROLA_SPEED = 0x11,
24 MOTOROLA_REVERSE = 0x12,
25 MOTOROLA_SPEED_DIRECTION = 0x13,
26 MOTOROLA_SPEED_FUNCTION = 0x14,
27 MOTOROLA_SOLENOID = 0x15,
30 RECEIVE_OVERRUN = 0x81,
32 INVALID_COMMAND = 0x83,
53 unsigned char command;
54 unsigned short serial;
59 operator bool() const { return type!=NONE; }
74 struct ControlledVariable
78 unsigned short serial;
80 ControlledVariable(): current(), pending(), serial(0) { }
81 ControlledVariable(T v): current(v), pending(v), serial(0) { }
83 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
84 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
86 operator T() const { return current; }
100 ControlledVariable<unsigned> speed;
101 ControlledVariable<bool> reverse;
102 ControlledVariable<unsigned> funcs;
103 unsigned last_change_age;
105 Locomotive(Protocol, unsigned);
107 unsigned create_speed_dir_command(char *) const;
108 unsigned create_speed_func_command(unsigned, char *) const;
128 ControlledVariable<unsigned> state;
130 Msp::Time::TimeDelta active_time;
132 Accessory(Kind, unsigned, unsigned);
134 unsigned create_state_command(unsigned, bool, char *) const;
145 ControlledVariable<bool> state;
150 class ControlThread: public Msp::Thread
153 ArduControl &control;
157 ControlThread(ArduControl &);
168 unsigned char length;
171 QueuedCommand(GeneralCommand);
172 QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0);
173 QueuedCommand(Accessory &, Accessory::Command, unsigned = 0);
176 typedef std::map<unsigned, Locomotive> LocomotiveMap;
177 typedef std::list<Locomotive *> LocomotivePtrList;
178 typedef std::map<unsigned, Accessory> AccessoryMap;
179 typedef std::list<Accessory *> AccessoryPtrList;
180 typedef std::map<unsigned, Sensor> SensorMap;
182 Msp::IO::Serial serial;
185 ControlledVariable<bool> power;
187 LocomotiveMap locomotives;
188 LocomotivePtrList refresh_cycle;
189 LocomotivePtrList::iterator next_refresh;
190 unsigned refresh_counter;
191 AccessoryMap accessories;
192 AccessoryPtrList accessory_queue;
193 Accessory *active_accessory;
194 Msp::Time::TimeStamp off_timeout;
195 std::list<QueuedCommand> command_queue;
196 std::list<Tag> completed_commands;
199 unsigned n_s88_octets;
202 ControlThread thread;
205 ArduControl(const std::string &);
208 virtual void set_power(bool);
209 virtual bool get_power() const { return power; }
210 virtual void halt(bool);
211 virtual bool is_halted() const { return false; }
213 virtual const char *enumerate_protocols(unsigned) const;
215 static Protocol map_protocol(const std::string &);
217 virtual unsigned get_protocol_speed_steps(const std::string &) const;
219 virtual void add_loco(unsigned, const std::string &, const VehicleType &);
220 virtual void remove_loco(unsigned);
221 virtual void set_loco_speed(unsigned, unsigned);
222 virtual void set_loco_reverse(unsigned, bool);
223 virtual void set_loco_function(unsigned, unsigned, bool);
225 void add_loco_to_refresh(Locomotive &);
226 void remove_loco_from_refresh(Locomotive &);
227 Locomotive *get_loco_to_refresh();
228 void advance_next_refresh();
231 virtual void add_turnout(unsigned, const TrackType &);
232 virtual void remove_turnout(unsigned);
233 virtual void set_turnout(unsigned, unsigned);
234 virtual unsigned get_turnout(unsigned) const;
236 virtual void add_signal(unsigned, const SignalType &);
237 virtual void remove_signal(unsigned);
238 virtual void set_signal(unsigned, unsigned);
239 virtual unsigned get_signal(unsigned) const;
242 void add_accessory(Accessory::Kind, unsigned, unsigned);
243 void remove_accessory(Accessory::Kind, unsigned);
244 void set_accessory(Accessory::Kind, unsigned, unsigned);
245 unsigned get_accessory(Accessory::Kind, unsigned) const;
248 virtual void add_sensor(unsigned);
249 virtual void remove_sensor(unsigned);
250 virtual void set_sensor(unsigned, bool) { }
251 virtual bool get_sensor(unsigned) const;
254 virtual void flush();
257 void push_command(const QueuedCommand &);
258 bool pop_command(QueuedCommand &);
259 void push_completed_tag(const Tag &);
260 Tag pop_completed_tag();