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; }
80 struct ControlledVariable
84 unsigned short serial;
86 ControlledVariable(): current(), pending(), serial(0) { }
87 ControlledVariable(T v): current(v), pending(v), serial(0) { }
89 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
90 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
92 operator T() const { return current; }
107 ControlledVariable<unsigned> speed;
108 ControlledVariable<bool> reverse;
109 ControlledVariable<unsigned> funcs;
110 unsigned last_change_age;
112 Locomotive(Protocol, unsigned);
114 unsigned create_speed_dir_command(char *) const;
115 unsigned create_speed_func_command(unsigned, char *) const;
135 ControlledVariable<unsigned> state;
137 Msp::Time::TimeDelta active_time;
139 Accessory(Kind, unsigned, unsigned);
141 unsigned create_state_command(unsigned, bool, char *) const;
152 ControlledVariable<bool> state;
157 class ControlThread: public Msp::Thread
160 ArduControl &control;
164 ControlThread(ArduControl &);
175 unsigned char length;
178 QueuedCommand(GeneralCommand);
179 QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0);
180 QueuedCommand(Accessory &, Accessory::Command, unsigned = 0);
183 typedef std::map<unsigned, Locomotive> LocomotiveMap;
184 typedef std::list<Locomotive *> LocomotivePtrList;
185 typedef std::map<unsigned, Accessory> AccessoryMap;
186 typedef std::list<Accessory *> AccessoryPtrList;
187 typedef std::map<unsigned, Sensor> SensorMap;
189 Msp::IO::Serial serial;
192 ControlledVariable<bool> power;
194 LocomotiveMap locomotives;
195 LocomotivePtrList refresh_cycle;
196 LocomotivePtrList::iterator next_refresh;
197 unsigned refresh_counter;
198 AccessoryMap accessories;
199 AccessoryPtrList accessory_queue;
200 Accessory *active_accessory;
201 Msp::Time::TimeStamp off_timeout;
202 std::list<QueuedCommand> command_queue;
203 std::list<Tag> completed_commands;
206 unsigned n_s88_octets;
209 ControlThread thread;
211 static ProtocolInfo protocol_info[2];
214 ArduControl(const std::string &);
217 virtual void set_power(bool);
218 virtual bool get_power() const { return power; }
219 virtual void halt(bool);
220 virtual bool is_halted() const { return false; }
222 virtual const char *enumerate_protocols(unsigned) const;
224 static Protocol map_protocol(const std::string &);
226 virtual unsigned get_protocol_speed_steps(const std::string &) const;
228 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
229 virtual void remove_loco(unsigned);
230 virtual void set_loco_speed(unsigned, unsigned);
231 virtual void set_loco_reverse(unsigned, bool);
232 virtual void set_loco_function(unsigned, unsigned, bool);
234 void add_loco_to_refresh(Locomotive &);
235 void remove_loco_from_refresh(Locomotive &);
236 Locomotive *get_loco_to_refresh();
237 void advance_next_refresh();
240 virtual unsigned add_turnout(unsigned, const TrackType &);
241 virtual void remove_turnout(unsigned);
242 virtual void set_turnout(unsigned, unsigned);
243 virtual unsigned get_turnout(unsigned) const;
245 virtual unsigned add_signal(unsigned, const SignalType &);
246 virtual void remove_signal(unsigned);
247 virtual void set_signal(unsigned, unsigned);
248 virtual unsigned get_signal(unsigned) const;
251 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
252 void remove_accessory(Accessory::Kind, unsigned);
253 void set_accessory(Accessory::Kind, unsigned, unsigned);
254 unsigned get_accessory(Accessory::Kind, unsigned) const;
257 virtual unsigned add_sensor(unsigned);
258 virtual void remove_sensor(unsigned);
259 virtual void set_sensor(unsigned, bool) { }
260 virtual bool get_sensor(unsigned) const;
263 virtual void flush();
266 void push_command(const QueuedCommand &);
267 bool pop_command(QueuedCommand &);
268 void push_completed_tag(const Tag &);
269 Tag pop_completed_tag();