]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/arducontrol.h
Add driver for my custom Arduino-based control device
[r2c2.git] / source / libr2c2 / arducontrol.h
1 #ifndef LIBR2C2_ARDUCONTROL_H_
2 #define LIBR2C2_ARDUCONTROL_H_
3
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>
9 #include "driver.h"
10
11 namespace R2C2 {
12
13 class ArduControl: public Driver
14 {
15 private:
16         enum Command
17         {
18                 POWER_ON = 0x01,
19                 POWER_OFF = 0x02,
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,
28                 S88_READ = 0x30,
29                 COMMAND_OK = 0x80,
30                 RECEIVE_OVERRUN = 0x81,
31                 FRAMING_ERROR = 0x82,
32                 INVALID_COMMAND = 0x83,
33                 LENGTH_ERROR = 0x84,
34                 INVALID_VALUE = 0x85,
35                 OVERCURRENT = 0xA0,
36                 TRACK_CURRENT = 0xC0,
37                 INPUT_VOLTAGE = 0xC1,
38                 S88_DATA = 0xD0
39         };
40
41         struct Tag
42         {
43                 enum Type
44                 {
45                         NONE,
46                         GENERAL,
47                         LOCOMOTIVE,
48                         ACCESSORY,
49                         SENSOR
50                 };
51
52                 Type type;
53                 unsigned char command;
54                 unsigned short serial;
55                 unsigned address;
56
57                 Tag();
58
59                 operator bool() const { return type!=NONE; }
60         };
61
62         enum GeneralCommand
63         {
64                 POWER
65         };
66
67         enum Protocol
68         {
69                 NONE,
70                 MM
71         };
72
73         template<typename T>
74         struct ControlledVariable
75         {
76                 T current;
77                 T pending;
78                 unsigned short serial;
79
80                 ControlledVariable(): current(), pending(), serial(0) { }
81                 ControlledVariable(T v): current(v), pending(v), serial(0) { }
82
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; }
85
86                 operator T() const { return current; }
87         };
88
89         struct Locomotive
90         {
91                 enum Command
92                 {
93                         SPEED,
94                         REVERSE,
95                         FUNCTIONS
96                 };
97
98                 Protocol proto;
99                 unsigned address;
100                 ControlledVariable<unsigned> speed;
101                 ControlledVariable<bool> reverse;
102                 ControlledVariable<unsigned> funcs;
103                 unsigned last_change_age;
104
105                 Locomotive(Protocol, unsigned);
106
107                 unsigned create_speed_dir_command(char *) const;
108                 unsigned create_speed_func_command(unsigned, char *) const;
109         };
110
111         struct Accessory
112         {
113                 enum Kind
114                 {
115                         TURNOUT,
116                         SIGNAL
117                 };
118
119                 enum Command
120                 {
121                         ACTIVATE,
122                         DEACTIVATE
123                 };
124
125                 Kind kind;
126                 unsigned address;
127                 unsigned bits;
128                 ControlledVariable<unsigned> state;
129                 unsigned target;
130                 Msp::Time::TimeDelta active_time;
131
132                 Accessory(Kind, unsigned, unsigned);
133
134                 unsigned create_state_command(unsigned, bool, char *) const;
135         };
136
137         struct Sensor
138         {
139                 enum Command
140                 {
141                         STATE
142                 };
143
144                 unsigned address;
145                 ControlledVariable<bool> state;
146
147                 Sensor(unsigned);
148         };
149
150         class ControlThread: public Msp::Thread
151         {
152         private:
153                 ArduControl &control;
154                 bool done;
155
156         public:
157                 ControlThread(ArduControl &);
158
159                 void exit();
160         private:
161                 virtual void main();
162         };
163
164         struct QueuedCommand
165         {
166                 Tag tag;
167                 char command[15];
168                 unsigned char length;
169
170                 QueuedCommand();
171                 QueuedCommand(GeneralCommand);
172                 QueuedCommand(Locomotive &, Locomotive::Command, unsigned = 0);
173                 QueuedCommand(Accessory &, Accessory::Command, unsigned = 0);
174         };
175
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;
181
182         Msp::IO::Serial serial;
183         bool debug;
184
185         ControlledVariable<bool> power;
186
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;
197
198         SensorMap sensors;
199         unsigned n_s88_octets;
200
201         Msp::Mutex mutex;
202         ControlThread thread;
203
204 public:
205         ArduControl(const std::string &);
206         ~ArduControl();
207
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; }
212
213         virtual const char *enumerate_protocols(unsigned) const;
214 private:
215         static Protocol map_protocol(const std::string &);
216 public:
217         virtual unsigned get_protocol_speed_steps(const std::string &) const;
218
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);
224 private:
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();
229
230 public:
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;
235
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;
240
241 private:
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;
246
247 public:
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;
252
253         virtual void tick();
254         virtual void flush();
255
256 private:
257         void push_command(const QueuedCommand &);
258         bool pop_command(QueuedCommand &);
259         void push_completed_tag(const Tag &);
260         Tag pop_completed_tag();
261 };
262
263 } // namespace R2C2
264
265 #endif