RSeries astromech firmware
SiKRadioController.h
Go to the documentation of this file.
1 #ifndef SiKRadioController_h
2 #define SiKRadioController_h
3 
4 #include "JoystickController.h"
5 
6 #ifdef USE_RADIO
7 #include <OSCMessage.h>
8 #include <SLIPEncodedSerial.h>
9 
10 class SiKRadioController : public JoystickController
11 {
12 public:
13  SiKRadioController(HardwareSerial &serial) :
14  fSLIP(serial)
15  {
16  memset(&state, '\0', sizeof(state));
17  memset(&event, '\0', sizeof(event));
18  // serial.setTimeout(100);
19  }
20 
21  void start()
22  {
23  xTaskCreatePinnedToCore(
24  joyLoopTask,
25  "joy",
26  4000,
27  this,
28  1,
29  &fTask,
30  0);
31  }
32 
33 private:
34  SLIPEncodedSerial fSLIP;
35  TaskHandle_t fTask;
36 
37  void loop()
38  {
39  uint32_t lastEvent = 0;
40  fConnecting = true;
41  for (;;)
42  {
43  OSCMessage msg;
44  if (fSLIP.available())
45  {
46  while (!fSLIP.endofPacket())
47  {
48  while (fSLIP.available())
49  {
50  msg.fill(fSLIP.read());
51  if (fConnecting)
52  {
53  Serial.println("RADIO CONNECTED");
54  fConnecting = false;
55  }
56  }
57  }
58  }
59  if (!msg.hasError())
60  {
61  state.analog.stick.lx = map(msg.getInt(0), 0, 1024, 127, -128);
62  state.analog.stick.ly = map(msg.getInt(1), 0, 1024, 127, -128);
63  state.analog.button.l2 = 255;
64  int32_t b = msg.getInt(2);
65  state.button.left = ((b & (1<<1)) != 0);
66  state.button.up = ((b & (1<<2)) != 0);
67  state.button.right = ((b & (1<<3)) != 0);
68  state.button.down = ((b & (1<<4)) != 0);
69  state.button.select= ((b & (1<<0)) != 0);
70  fConnected = true;
71  lastEvent = millis();
72  // Serial.println(String(state.analog.stick.lx)+","+String(state.analog.stick.ly));
73  }
74  if (fConnected && lastEvent + 200 < millis())
75  {
76  Serial.println("NO RADIO DATA for 200ms");
77  fConnected = false;
78  }
79  vTaskDelay(1);
80  }
81  }
82 
83  static void joyLoopTask(void* arg)
84  {
85  RadioController* ctrl = (RadioController*)arg;
86  ctrl->loop();
87  }
88 
89  static int map(int x, int in_min, int in_max, int out_min, int out_max)
90  {
91  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
92  }
93 };
94 #endif
95 
96 #endif
JoystickController.h
JoystickController
Definition: JoystickController.h:4