Browse Source

Work in progress...

Thomas Buck 8 years ago
parent
commit
de30f9e8d7
4 changed files with 184 additions and 19 deletions
  1. 29
    8
      Saitek-X52-PPM.ino
  2. 9
    4
      hid_parser.cpp
  3. 136
    7
      joystick_events.cpp
  4. 10
    0
      joystick_events.h

+ 29
- 8
Saitek-X52-PPM.ino View File

17
 #include "hid_parser.h"
17
 #include "hid_parser.h"
18
 #include "x52.h"
18
 #include "x52.h"
19
 
19
 
20
+#define ENABLE_SERIAL_PORT
21
+#define DEBUG_OUTPUT
22
+
20
 USB usb;
23
 USB usb;
21
 USBHub hub(&usb);
24
 USBHub hub(&usb);
22
 HIDUniversal hid(&usb);
25
 HIDUniversal hid(&usb);
24
 JoystickEvents joyevents;
27
 JoystickEvents joyevents;
25
 JoystickReportParser joy(&joyevents);
28
 JoystickReportParser joy(&joyevents);
26
 
29
 
27
-void setup() {  
30
+void setup() {
31
+#ifdef ENABLE_SERIAL_PORT
28
     Serial.begin(115200);
32
     Serial.begin(115200);
33
+#endif
34
+
35
+#ifdef DEBUG_OUTPUT
29
     Serial.println("Start");
36
     Serial.println("Start");
37
+#endif
30
 
38
 
31
     if (usb.Init() == -1) {
39
     if (usb.Init() == -1) {
40
+#ifdef DEBUG_OUTPUT
32
         Serial.println("OSC did not start.");
41
         Serial.println("OSC did not start.");
42
+#endif
33
     }
43
     }
34
 
44
 
35
     delay(200);
45
     delay(200);
39
     }
49
     }
40
 }
50
 }
41
 
51
 
52
+void init_joystick() {
53
+    x52.setLEDBrightness(2);
54
+    x52.setMFDBrightness(2);
55
+    x52.setShift(0);
56
+    x52.setBlink(0);
57
+    x52.setMFDText(0, "Arduino X52 Host");
58
+    x52.setMFDText(1, "    has been    ");
59
+    x52.setMFDText(2, "  initialized!  ");
60
+}
61
+
42
 void loop() {
62
 void loop() {
43
     usb.Task();
63
     usb.Task();
44
 
64
 
45
     static unsigned long lastTime = 0;
65
     static unsigned long lastTime = 0;
46
-    static uint8_t d = 0;
47
-    if ((millis() - lastTime) >= 500) {
48
-        //x52.setDate(d, d, d);
49
-        d++;
66
+    static uint8_t initialized = 0;
67
+    if ((millis() - lastTime) >= 1000) {
50
         lastTime = millis();
68
         lastTime = millis();
51
-
52
-        String tmp = String(d);
53
-        //x52.setMFDText(0, tmp.c_str());
69
+        if (!initialized) {
70
+            init_joystick();
71
+            initialized = 1;
72
+        }
54
     }
73
     }
55
 
74
 
75
+#ifdef DEBUG_OUTPUT
56
     if (Serial.available()) {
76
     if (Serial.available()) {
57
         char c = Serial.read();
77
         char c = Serial.read();
58
         if (c == 't') {
78
         if (c == 't') {
89
             Serial.println("Unknown command!");
109
             Serial.println("Unknown command!");
90
         }
110
         }
91
     }
111
     }
112
+#endif
92
 }
113
 }
93
 
114
 

+ 9
- 4
hid_parser.cpp View File

12
 
12
 
13
 #include "hid_parser.h"
13
 #include "hid_parser.h"
14
 
14
 
15
+//#define DEBUG_OUTPUT
16
+
15
 JoystickReportParser::JoystickReportParser(JoystickEvents* evt)
17
 JoystickReportParser::JoystickReportParser(JoystickEvents* evt)
16
-        : joyEvents(evt), oldHat(0), oldButtons(0), oldMouse(0) {
18
+        : joyEvents(evt), oldHat(0), oldButtons(0), oldMouse(0), buffer(0) {
17
     for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) {
19
     for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) {
18
         oldPad[i] = 0;
20
         oldPad[i] = 0;
19
     }
21
     }
20
 }
22
 }
21
 
23
 
22
 void JoystickReportParser::Parse(HID* hid, bool is_rpt_id, uint8_t len, uint8_t* bufPart) {
24
 void JoystickReportParser::Parse(HID* hid, bool is_rpt_id, uint8_t len, uint8_t* bufPart) {
25
+    // Ugly hack for too small packet size in USB Host library...
23
     if (len == 8) {
26
     if (len == 8) {
24
         // First part of buffer, store and do nothing
27
         // First part of buffer, store and do nothing
25
         for (uint8_t i = 0; i < 8; i++) {
28
         for (uint8_t i = 0; i < 8; i++) {
33
         }
36
         }
34
     }
37
     }
35
 
38
 
36
-    /*
39
+    // Dump whole USB HID packet for debugging purposes
40
+#ifdef DEBUG_OUTPUT
37
     Serial.println("");
41
     Serial.println("");
38
     Serial.print("Packet: ");
42
     Serial.print("Packet: ");
39
     for (uint8_t i = 0; i < (8 + len); i++) {
43
     for (uint8_t i = 0; i < (8 + len); i++) {
42
     }
46
     }
43
     Serial.println("");
47
     Serial.println("");
44
     Serial.println("");
48
     Serial.println("");
45
-    */
49
+#endif
46
 
50
 
47
     // Checking if there are changes in report since the method was last called
51
     // Checking if there are changes in report since the method was last called
48
     bool match = true;
52
     bool match = true;
70
         }
74
         }
71
     }
75
     }
72
 
76
 
73
-    // Calling Hat Switch event handler`
77
+    // Calling Hat Switch event handler
74
     uint8_t hat = (buf[12] & 0xF0) >> 4;
78
     uint8_t hat = (buf[12] & 0xF0) >> 4;
75
     if (hat != oldHat && joyEvents) {
79
     if (hat != oldHat && joyEvents) {
76
         joyEvents->OnHatSwitch(hat);
80
         joyEvents->OnHatSwitch(hat);
103
         oldButtons = buttons;
107
         oldButtons = buttons;
104
     }
108
     }
105
 
109
 
110
+    // Calling Mouse Event Handler if state has changed
106
     if (oldMouse != buf[13] && joyEvents) {
111
     if (oldMouse != buf[13] && joyEvents) {
107
         oldMouse = buf[13];
112
         oldMouse = buf[13];
108
         joyEvents->OnMouseMoved((buf[13] & 0xF0) >> 4, buf[13] & 0x0F);
113
         joyEvents->OnMouseMoved((buf[13] & 0xF0) >> 4, buf[13] & 0x0F);

+ 136
- 7
joystick_events.cpp View File

12
 
12
 
13
 #include "hid_parser.h"
13
 #include "hid_parser.h"
14
 
14
 
15
+//#define DEBUG_OUTPUT_RAW
16
+#define DEBUG_OUTPUT
17
+
18
+
19
+/*
20
+ * Uuuhhh TODO!!
21
+ * This is not a deadzone, this is something like sensitivity.
22
+ * For deadzone, we need to only apply it from the center outwards.
23
+ */
24
+
25
+
26
+JoystickEvents::JoystickEvents()
27
+        : lastData(0), lastMouseX(0), lastMouseY(0), deadZone(0) {
28
+    deadZone.X = 0;
29
+    deadZone.Y = 0;
30
+    deadZone.Z = 0;
31
+    deadZone.Rx = 0;
32
+    deadZone.Ry = 0;
33
+    deadZone.Rz = 50;
34
+    deadZoneMouseX = 0;
35
+    deadZoneMouseY = 0;
36
+}
37
+
15
 void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
38
 void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
39
+#ifdef DEBUG_OUTPUT_RAW
16
     Serial.print("X: ");
40
     Serial.print("X: ");
17
     PrintHex<uint16_t > (evt->X, 0x80);
41
     PrintHex<uint16_t > (evt->X, 0x80);
18
-    Serial.print("\tY: ");
42
+    Serial.print(" Y: ");
19
     PrintHex<uint16_t > (evt->Y, 0x80);
43
     PrintHex<uint16_t > (evt->Y, 0x80);
20
-    Serial.print("\tZ: ");
44
+    Serial.print(" Z: ");
21
     PrintHex<uint8_t > (evt->Z, 0x80);
45
     PrintHex<uint8_t > (evt->Z, 0x80);
22
-    Serial.print("\tRx: ");
46
+    Serial.print(" Rx: ");
23
     PrintHex<uint8_t > (evt->Rx, 0x80);
47
     PrintHex<uint8_t > (evt->Rx, 0x80);
24
-    Serial.print("\tRy: ");
48
+    Serial.print(" Ry: ");
25
     PrintHex<uint8_t > (evt->Ry, 0x80);
49
     PrintHex<uint8_t > (evt->Ry, 0x80);
26
-    Serial.print("\tRz: ");
50
+    Serial.print(" Rz: ");
27
     PrintHex<uint16_t > (evt->Rz, 0x80);
51
     PrintHex<uint16_t > (evt->Rz, 0x80);
28
-    Serial.print("\tSlider: ");
52
+    Serial.print(" S: ");
29
     PrintHex<uint8_t > (evt->Slider, 0x80);
53
     PrintHex<uint8_t > (evt->Slider, 0x80);
30
     Serial.println("");
54
     Serial.println("");
55
+#endif
56
+
57
+    GamePadEventData newData = lastData;
58
+    uint8_t updated = 0;
59
+
60
+    if ((evt->X > (lastData.X + deadZone.X))
61
+            || (evt->X < (lastData.X - deadZone.X))) {
62
+        newData.X = evt->X;
63
+        updated = 1;
64
+    }
65
+
66
+    if ((evt->Y > (lastData.Y + deadZone.Y))
67
+            || (evt->Y < (lastData.Y - deadZone.Y))) {
68
+        newData.Y = evt->Y;
69
+        updated = 1;
70
+    }
71
+
72
+    if ((evt->Z > (lastData.Z + deadZone.Z))
73
+            || (evt->Z < (lastData.Z - deadZone.Z))) {
74
+        newData.Z = evt->Z;
75
+        updated = 1;
76
+    }
77
+
78
+    if ((evt->Rx > (lastData.Rx + deadZone.Rx))
79
+            || (evt->Rx < (lastData.Rx - deadZone.Rx))) {
80
+        newData.Rx = evt->Rx;
81
+        updated = 1;
82
+    }
83
+
84
+    if ((evt->Ry > (lastData.Ry + deadZone.Ry))
85
+            || (evt->Ry < (lastData.Ry - deadZone.Ry))) {
86
+        newData.Ry = evt->Ry;
87
+        updated = 1;
88
+    }
89
+
90
+    if ((evt->Rz > (lastData.Rz + deadZone.Rz))
91
+            || (evt->Rz < (lastData.Rz - deadZone.Rz))) {
92
+        newData.Rz = evt->Rz;
93
+        updated = 1;
94
+    }
95
+
96
+    if ((evt->Slider > (lastData.Slider + deadZone.Slider))
97
+            || (evt->Slider < (lastData.Slider - deadZone.Slider))) {
98
+        newData.Slider = evt->Slider;
99
+        updated = 1;
100
+    }
101
+
102
+    if (updated) {
103
+#ifdef DEBUG_OUTPUT
104
+        Serial.print("X: ");
105
+        PrintHex<uint16_t > (newData.X, 0x80);
106
+        Serial.print(" Y: ");
107
+        PrintHex<uint16_t > (newData.Y, 0x80);
108
+        Serial.print(" Z: ");
109
+        PrintHex<uint8_t > (newData.Z, 0x80);
110
+        Serial.print(" Rx: ");
111
+        PrintHex<uint8_t > (newData.Rx, 0x80);
112
+        Serial.print(" Ry: ");
113
+        PrintHex<uint8_t > (newData.Ry, 0x80);
114
+        Serial.print(" Rz: ");
115
+        PrintHex<uint16_t > (newData.Rz, 0x80);
116
+        Serial.print(" S: ");
117
+        PrintHex<uint8_t > (newData.Slider, 0x80);
118
+        Serial.println("");
119
+#endif
120
+    }
121
+
122
+    lastData = *evt;
31
 }
123
 }
32
 
124
 
33
 void JoystickEvents::OnHatSwitch(uint8_t hat) {
125
 void JoystickEvents::OnHatSwitch(uint8_t hat) {
126
+#ifdef DEBUG_OUTPUT
34
     Serial.print("Hat Switch: ");
127
     Serial.print("Hat Switch: ");
35
     PrintHex<uint8_t > (hat, 0x80);
128
     PrintHex<uint8_t > (hat, 0x80);
36
     Serial.println("");
129
     Serial.println("");
130
+#endif
37
 }
131
 }
38
 
132
 
39
 void JoystickEvents::OnButtonUp(uint8_t but_id) {
133
 void JoystickEvents::OnButtonUp(uint8_t but_id) {
134
+#ifdef DEBUG_OUTPUT
40
     Serial.print("Up: ");
135
     Serial.print("Up: ");
41
     Serial.println(but_id, DEC);
136
     Serial.println(but_id, DEC);
137
+#endif
42
 }
138
 }
43
 
139
 
44
 void JoystickEvents::OnButtonDn(uint8_t but_id) {
140
 void JoystickEvents::OnButtonDn(uint8_t but_id) {
45
-    Serial.print("Dn: ");
141
+#ifdef DEBUG_OUTPUT
142
+    Serial.print("Down: ");
46
     Serial.println(but_id, DEC);
143
     Serial.println(but_id, DEC);
144
+#endif
47
 }
145
 }
48
 
146
 
49
 void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
147
 void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
148
+#ifdef DEBUG_OUTPUT_RAW
50
     Serial.print("Mouse X: ");
149
     Serial.print("Mouse X: ");
51
     PrintHex<uint8_t >(x, 0x80);
150
     PrintHex<uint8_t >(x, 0x80);
52
     Serial.print("\tY: ");
151
     Serial.print("\tY: ");
53
     PrintHex<uint8_t >(y, 0x80);
152
     PrintHex<uint8_t >(y, 0x80);
54
     Serial.println("");
153
     Serial.println("");
154
+#endif
155
+
156
+    uint8_t newX = lastMouseX;
157
+    uint8_t newY = lastMouseY;
158
+    uint8_t updated = 0;
159
+    
160
+    if ((x > (lastMouseX + deadZoneMouseX))
161
+            || (x < (lastMouseX - deadZoneMouseX))) {
162
+        newX = x;
163
+        updated = 1;
164
+    }
165
+
166
+    if ((y > (lastMouseY + deadZoneMouseY))
167
+            || (y < (lastMouseY - deadZoneMouseY))) {
168
+        newY = y;
169
+        updated = 1;
170
+    }
171
+
172
+    if (updated) {
173
+#ifdef DEBUG_OUTPUT
174
+        Serial.print("Mouse X: ");
175
+        PrintHex<uint8_t >(newX, 0x80);
176
+        Serial.print("\tY: ");
177
+        PrintHex<uint8_t >(newY, 0x80);
178
+        Serial.println("");
179
+#endif
180
+    }
181
+
182
+    lastMouseX = x;
183
+    lastMouseY = y;
55
 }
184
 }
56
 
185
 

+ 10
- 0
joystick_events.h View File

14
 #define __JOYSTICK_EVENTS_H__
14
 #define __JOYSTICK_EVENTS_H__
15
 
15
 
16
 struct GamePadEventData {
16
 struct GamePadEventData {
17
+    GamePadEventData(uint16_t v) : X(v), Y(v), Z(v), Rx(v), Ry(v), Rz(v) { }
18
+    
17
     uint16_t X, Y, Rz; // 11bit, 11bit, 10bit
19
     uint16_t X, Y, Rz; // 11bit, 11bit, 10bit
18
     uint8_t Z, Rx, Ry, Slider;
20
     uint8_t Z, Rx, Ry, Slider;
19
 };
21
 };
20
 
22
 
21
 class JoystickEvents {
23
 class JoystickEvents {
22
   public:
24
   public:
25
+    JoystickEvents();
26
+
23
     virtual void OnGamePadChanged(const GamePadEventData *evt);
27
     virtual void OnGamePadChanged(const GamePadEventData *evt);
24
     virtual void OnHatSwitch(uint8_t hat);
28
     virtual void OnHatSwitch(uint8_t hat);
25
     virtual void OnButtonUp(uint8_t but_id);
29
     virtual void OnButtonUp(uint8_t but_id);
26
     virtual void OnButtonDn(uint8_t but_id);
30
     virtual void OnButtonDn(uint8_t but_id);
27
     virtual void OnMouseMoved(uint8_t x, uint8_t y);
31
     virtual void OnMouseMoved(uint8_t x, uint8_t y);
32
+
33
+  protected:
34
+    GamePadEventData lastData;
35
+    GamePadEventData deadZone;
36
+    uint8_t lastMouseX, lastMouseY;
37
+    uint8_t deadZoneMouseX, deadZoneMouseY;
28
 };
38
 };
29
 
39
 
30
 #endif // __JOYSTICK_EVENTS_H__
40
 #endif // __JOYSTICK_EVENTS_H__

Loading…
Cancel
Save