RSeries astromech firmware
ProgrammableController.h
Go to the documentation of this file.
1 #ifndef ProgrammableController_h
2 #define ProgrammableController_h
3 
4 #include "ReelTwo.h"
5 #include "JoystickController.h"
6 
8 {
9 public:
10  enum {
11  kHome = -50,
12  kPageUp = -53,
13  kPageDown = -54,
14  kEnd = -70
15  };
16 
18  {
19  memset(&state, '\0', sizeof(state));
20  memset(&event, '\0', sizeof(event));
21  memset(&longpress, '\0', sizeof(longpress));
22  fConnecting = true;
23  fConnected = false;
24  }
25 
26  uint16_t y;
27  uint16_t x;
28  uint16_t w1;
29  uint16_t w2;
30  bool button[5];
31  struct LongPress {
32  uint32_t pressTime;
33  bool longPress;
34  };
35  struct {
41  } longpress;
42  uint32_t lastPacket;
43 
44  inline float getSpeed()
45  {
46  return fSpeed;
47  }
48 
49  inline void setSpeed(float speed)
50  {
51  fSpeed = max(min(speed, 1.0), 0.0);
52  }
53 
54  inline void increaseSpeed()
55  {
56  setSpeed(fSpeed + 0.1);
57  }
58 
59  inline void decreaseSpeed()
60  {
61  setSpeed(fSpeed - 0.1);
62  }
63 
64  bool connect()
65  {
66  if (!fConnected)
67  {
68  fConnecting = true;
69  }
70  }
71 
72  bool disconnect()
73  {
74  fConnected = false;
75  fConnecting = false;
76  }
77 
78  void update()
79  {
80  Event evt = {};
81  State prev = state;
82  state.analog.stick.lx = map(x, 0, 1024, 127, -128);
83  state.analog.stick.ly = map(y, 0, 1024, 127, -128);
86  state.analog.button.l1 = map(w2, 0, 1024, 255, 0);
87  state.analog.button.l2 = map(w1, 0, 1024, 255, 0);
92  state.button.cross = button[2];
94  state.button.l3 = button[4];
95 
96  // for (int i = 0; i < SizeOfArray(button); i++)
97  // {
98  // if (button[i])
99  // {
100  // CONSOLE_SERIAL.print("BUTTON_DOWN ");
101  // CONSOLE_SERIAL.println(i);
102  // }
103  // }
104  #define CHECK_BUTTON_DOWN(b) evt.button_down.b = (!prev.button.b && state.button.b)
110  #define CHECK_BUTTON_UP(b) evt.button_up.b = (prev.button.b && !state.button.b)
116  #define CHECK_BUTTON_LONGPRESS(b) \
117  { \
118  evt.long_button_up.b = false; \
119  if (evt.button_down.b) \
120  { \
121  longpress.b.pressTime = millis(); \
122  longpress.b.longPress = false; \
123  } \
124  else if (evt.button_up.b) \
125  { \
126  longpress.b.pressTime = 0; \
127  if (longpress.b.longPress) \
128  evt.button_up.b = false; \
129  longpress.b.longPress = false; \
130  } \
131  else if (longpress.b.pressTime != 0 && state.button.b) \
132  { \
133  if (longpress.b.pressTime + SERIAL_CONSOLE_LONG_PRESS_TIME < millis()) \
134  { \
135  longpress.b.pressTime = 0; \
136  longpress.b.longPress = true; \
137  evt.long_button_up.b = true; \
138  } \
139  } \
140  }
146 
147  /* Analog events */
152  if (fConnecting)
153  {
154  fConnecting = false;
155  fConnected = true;
156  onConnect();
157  }
158  if (fConnected)
159  {
160  event = evt;
161  notify();
162  }
163  }
164 
165  void updateState(float upDown, float leftRight)
166  {
167 
168  }
169 
170 protected:
171  uint32_t fLastTime = 0;
172  float fSpeed = 0.5;
173 
174  static uint16_t updateDirection(float speed)
175  {
176  return 512 + 512 * speed;
177  }
178 
179  int read()
180  {
181  int ret = 0;
182  if (fSerial->available())
183  {
184  int ch = fSerial->read();
185  switch (ch)
186  {
187  case 27:
188  switch (readCharBlocking())
189  {
190  case 79:
191  switch (readCharBlocking())
192  {
193  case 80:
194  /* F1 */
195  break;
196  case 81:
197  /* F2 */
198  break;
199  case 82:
200  /* F3 */
201  break;
202  case 84:
203  /* F4 */
204  break;
205  }
206  break;
207  case 91:
208  switch (readCharBlocking())
209  {
210  case 65:
211  /* Up arrow */
212  y = updateDirection(-fSpeed);//512 - 256;
213  w1 = updateDirection(0);
214  update();
215  break;
216  case 66:
217  /* Down arrow */
218  y = updateDirection(fSpeed);//512 + 256;
219  w1 = updateDirection(0);
220  update();
221  break;
222  case 67:
223  /* Right arrow */
224  x = updateDirection(fSpeed); //512 + 256;
225  w1 = updateDirection(0);
226  update();
227  break;
228  case 68:
229  /* Left arrow */
230  x = updateDirection(-fSpeed);//512 - 256;
231  w1 = updateDirection(0); //512;
232  update();
233  break;
234  case 50:
235  switch (Serial.read())
236  {
237  case 126:
238  /* Home */
239  ret = kHome;
240  break;
241  }
242  break;
243  case 53:
244  switch (readCharBlocking())
245  {
246  case 126:
247  /* Page up */
248  ret = kPageUp;
249  break;
250  }
251  break;
252  case 54:
253  switch (readCharBlocking())
254  {
255  case 126:
256  /* Page down */
257  ret = kPageDown;
258  break;
259  }
260  break;
261  case 70:
262  /* End */
263  ret = kEnd;
264  break;
265  }
266  break;
267  }
268  break;
269  default:
270  ret = ch;
271  break;
272  }
273  fLastTime = millis();
274  }
275  else if (fLastTime + 500 < millis())
276  {
277  if (x != 512 || y != 512)
278  {
279  x = updateDirection(0);//512;
280  y = updateDirection(0);//512;
281  update();
282  // DEBUG_PRINTLN("IDLE");
283  }
284  }
285  return ret;
286  }
287 };
288 #endif
JoystickController::Event
Definition: JoystickController.h:120
ProgrammableController::disconnect
bool disconnect()
Definition: ProgrammableController.h:72
CHECK_BUTTON_DOWN
#define CHECK_BUTTON_DOWN(b)
JoystickController::state
State state
Definition: JoystickController.h:136
JoystickController::AnalogButton::l2
uint8_t l2
Definition: JoystickController.h:22
JoystickController::Button::cross
uint8_t cross
Definition: JoystickController.h:63
ProgrammableController::update
void update()
Definition: ProgrammableController.h:78
JoystickController::AnalogStick::lx
int8_t lx
Definition: JoystickController.h:9
ReelTwo.h
ProgrammableController::LongPress::longPress
bool longPress
Definition: ProgrammableController.h:33
ProgrammableController
Definition: ProgrammableController.h:7
ProgrammableController::setSpeed
void setSpeed(float speed)
Definition: ProgrammableController.h:49
ProgrammableController::longpress
struct ProgrammableController::@38 longpress
ProgrammableController::w2
uint16_t w2
Definition: ProgrammableController.h:29
JoystickController::notify
virtual void notify()
Definition: JoystickController.h:152
JoystickController::State
Definition: JoystickController.h:128
JoystickController::State::analog
Analog analog
Definition: JoystickController.h:130
ProgrammableController::kPageDown
@ kPageDown
Definition: ProgrammableController.h:13
ProgrammableController::kEnd
@ kEnd
Definition: ProgrammableController.h:14
ProgrammableController::read
int read()
Definition: ProgrammableController.h:179
ProgrammableController::square
LongPress square
Definition: ProgrammableController.h:40
ProgrammableController::l3
LongPress l3
Definition: ProgrammableController.h:36
JoystickController::State::button
Button button
Definition: JoystickController.h:131
JoystickController::AnalogStick::rx
int8_t rx
Definition: JoystickController.h:11
ProgrammableController::kHome
@ kHome
Definition: ProgrammableController.h:11
ProgrammableController::updateDirection
static uint16_t updateDirection(float speed)
Definition: ProgrammableController.h:174
ProgrammableController::x
uint16_t x
Definition: ProgrammableController.h:27
JoystickController::Analog::stick
AnalogStick stick
Definition: JoystickController.h:35
ProgrammableController::fLastTime
uint32_t fLastTime
Definition: ProgrammableController.h:171
CHECK_BUTTON_LONGPRESS
#define CHECK_BUTTON_LONGPRESS(b)
JoystickController::AnalogButton::l1
uint8_t l1
Definition: JoystickController.h:24
JoystickController::AnalogStick::ly
int8_t ly
Definition: JoystickController.h:10
ProgrammableController::updateState
void updateState(float upDown, float leftRight)
Definition: ProgrammableController.h:165
JoystickController::Button::square
uint8_t square
Definition: JoystickController.h:64
ProgrammableController::button
bool button[5]
Definition: ProgrammableController.h:30
ProgrammableController::decreaseSpeed
void decreaseSpeed()
Definition: ProgrammableController.h:59
ProgrammableController::increaseSpeed
void increaseSpeed()
Definition: ProgrammableController.h:54
JoystickController::Button::circle
uint8_t circle
Definition: JoystickController.h:62
JoystickController.h
ProgrammableController::ProgrammableController
ProgrammableController()
Definition: ProgrammableController.h:17
CHECK_BUTTON_UP
#define CHECK_BUTTON_UP(b)
JoystickController::Analog::button
AnalogButton button
Definition: JoystickController.h:36
JoystickController::Button::l3
uint8_t l3
Definition: JoystickController.h:42
ProgrammableController::kPageUp
@ kPageUp
Definition: ProgrammableController.h:12
JoystickController::event
Event event
Definition: JoystickController.h:137
ProgrammableController::getSpeed
float getSpeed()
Definition: ProgrammableController.h:44
JoystickController::onConnect
virtual void onConnect()
Definition: JoystickController.h:153
JoystickController::AnalogButton::r1
uint8_t r1
Definition: JoystickController.h:25
JoystickController::AnalogStick::ry
int8_t ry
Definition: JoystickController.h:12
ProgrammableController::connect
bool connect()
Definition: ProgrammableController.h:64
ProgrammableController::y
uint16_t y
Definition: ProgrammableController.h:26
JoystickController::fConnected
bool fConnected
Definition: JoystickController.h:161
ProgrammableController::lastPacket
uint32_t lastPacket
Definition: ProgrammableController.h:42
JoystickController
Definition: JoystickController.h:4
ProgrammableController::cross
LongPress cross
Definition: ProgrammableController.h:39
JoystickController::Button::triangle
uint8_t triangle
Definition: JoystickController.h:61
ProgrammableController::LongPress::pressTime
uint32_t pressTime
Definition: ProgrammableController.h:32
ProgrammableController::circle
LongPress circle
Definition: ProgrammableController.h:38
JoystickController::Event::analog_changed
Analog analog_changed
Definition: JoystickController.h:125
JoystickController::AnalogButton::r2
uint8_t r2
Definition: JoystickController.h:23
JoystickController::fConnecting
bool fConnecting
Definition: JoystickController.h:162
ProgrammableController::LongPress
Definition: ProgrammableController.h:31
ProgrammableController::fSpeed
float fSpeed
Definition: ProgrammableController.h:172
ProgrammableController::triangle
LongPress triangle
Definition: ProgrammableController.h:37
ProgrammableController::w1
uint16_t w1
Definition: ProgrammableController.h:28