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