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; }
101 ControlledVariable<unsigned> speed;
102 ControlledVariable<bool> reverse;
103 ControlledVariable<unsigned> funcs;
104 unsigned last_change_age;
106 Locomotive(Protocol, unsigned);
108 unsigned create_speed_dir_command(char *) const;
109 unsigned create_speed_func_command(unsigned, char *) const;
129 ControlledVariable<unsigned> state;
131 Msp::Time::TimeDelta active_time;
133 Accessory(Kind, unsigned, unsigned);
135 unsigned create_state_command(unsigned, bool, char *) const;
146 ControlledVariable<bool> state;
151 class ControlThread: public Msp::Thread
154 ArduControl &control;
158 ControlThread(ArduControl &);
169 unsigned char length;
172 QueuedCommand(GeneralCommand);
173 QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0);
174 QueuedCommand(Accessory &, Accessory::Command, unsigned = 0);
177 typedef std::map<unsigned, Locomotive> LocomotiveMap;
178 typedef std::list<Locomotive *> LocomotivePtrList;
179 typedef std::map<unsigned, Accessory> AccessoryMap;
180 typedef std::list<Accessory *> AccessoryPtrList;
181 typedef std::map<unsigned, Sensor> SensorMap;
183 Msp::IO::Serial serial;
186 ControlledVariable<bool> power;
188 LocomotiveMap locomotives;
189 LocomotivePtrList refresh_cycle;
190 LocomotivePtrList::iterator next_refresh;
191 unsigned refresh_counter;
192 AccessoryMap accessories;
193 AccessoryPtrList accessory_queue;
194 Accessory *active_accessory;
195 Msp::Time::TimeStamp off_timeout;
196 std::list<QueuedCommand> command_queue;
197 std::list<Tag> completed_commands;
200 unsigned n_s88_octets;
203 ControlThread thread;
206 ArduControl(const std::string &);
209 virtual void set_power(bool);
210 virtual bool get_power() const { return power; }
211 virtual void halt(bool);
212 virtual bool is_halted() const { return false; }
214 virtual const char *enumerate_protocols(unsigned) const;
216 static Protocol map_protocol(const std::string &);
218 virtual unsigned get_protocol_speed_steps(const std::string &) const;
220 virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
221 virtual void remove_loco(unsigned);
222 virtual void set_loco_speed(unsigned, unsigned);
223 virtual void set_loco_reverse(unsigned, bool);
224 virtual void set_loco_function(unsigned, unsigned, bool);
226 void add_loco_to_refresh(Locomotive &);
227 void remove_loco_from_refresh(Locomotive &);
228 Locomotive *get_loco_to_refresh();
229 void advance_next_refresh();
232 virtual unsigned add_turnout(unsigned, const TrackType &);
233 virtual void remove_turnout(unsigned);
234 virtual void set_turnout(unsigned, unsigned);
235 virtual unsigned get_turnout(unsigned) const;
237 virtual unsigned add_signal(unsigned, const SignalType &);
238 virtual void remove_signal(unsigned);
239 virtual void set_signal(unsigned, unsigned);
240 virtual unsigned get_signal(unsigned) const;
243 unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
244 void remove_accessory(Accessory::Kind, unsigned);
245 void set_accessory(Accessory::Kind, unsigned, unsigned);
246 unsigned get_accessory(Accessory::Kind, unsigned) const;
249 virtual unsigned add_sensor(unsigned);
250 virtual void remove_sensor(unsigned);
251 virtual void set_sensor(unsigned, bool) { }
252 virtual bool get_sensor(unsigned) const;
255 virtual void flush();
258 void push_command(const QueuedCommand &);
259 bool pop_command(QueuedCommand &);
260 void push_completed_tag(const Tag &);
261 Tag pop_completed_tag();