瀏覽代碼

Work in progress...

Thomas Buck 7 年之前
父節點
當前提交
de30f9e8d7
共有 4 個文件被更改,包括 184 次插入19 次删除
  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 查看文件

@@ -17,6 +17,9 @@
17 17
 #include "hid_parser.h"
18 18
 #include "x52.h"
19 19
 
20
+#define ENABLE_SERIAL_PORT
21
+#define DEBUG_OUTPUT
22
+
20 23
 USB usb;
21 24
 USBHub hub(&usb);
22 25
 HIDUniversal hid(&usb);
@@ -24,12 +27,19 @@ X52 x52(&usb, &hid);
24 27
 JoystickEvents joyevents;
25 28
 JoystickReportParser joy(&joyevents);
26 29
 
27
-void setup() {  
30
+void setup() {
31
+#ifdef ENABLE_SERIAL_PORT
28 32
     Serial.begin(115200);
33
+#endif
34
+
35
+#ifdef DEBUG_OUTPUT
29 36
     Serial.println("Start");
37
+#endif
30 38
 
31 39
     if (usb.Init() == -1) {
40
+#ifdef DEBUG_OUTPUT
32 41
         Serial.println("OSC did not start.");
42
+#endif
33 43
     }
34 44
 
35 45
     delay(200);
@@ -39,20 +49,30 @@ void setup() {
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 62
 void loop() {
43 63
     usb.Task();
44 64
 
45 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 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 76
     if (Serial.available()) {
57 77
         char c = Serial.read();
58 78
         if (c == 't') {
@@ -89,5 +109,6 @@ void loop() {
89 109
             Serial.println("Unknown command!");
90 110
         }
91 111
     }
112
+#endif
92 113
 }
93 114
 

+ 9
- 4
hid_parser.cpp 查看文件

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

+ 136
- 7
joystick_events.cpp 查看文件

@@ -12,45 +12,174 @@
12 12
 
13 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 38
 void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
39
+#ifdef DEBUG_OUTPUT_RAW
16 40
     Serial.print("X: ");
17 41
     PrintHex<uint16_t > (evt->X, 0x80);
18
-    Serial.print("\tY: ");
42
+    Serial.print(" Y: ");
19 43
     PrintHex<uint16_t > (evt->Y, 0x80);
20
-    Serial.print("\tZ: ");
44
+    Serial.print(" Z: ");
21 45
     PrintHex<uint8_t > (evt->Z, 0x80);
22
-    Serial.print("\tRx: ");
46
+    Serial.print(" Rx: ");
23 47
     PrintHex<uint8_t > (evt->Rx, 0x80);
24
-    Serial.print("\tRy: ");
48
+    Serial.print(" Ry: ");
25 49
     PrintHex<uint8_t > (evt->Ry, 0x80);
26
-    Serial.print("\tRz: ");
50
+    Serial.print(" Rz: ");
27 51
     PrintHex<uint16_t > (evt->Rz, 0x80);
28
-    Serial.print("\tSlider: ");
52
+    Serial.print(" S: ");
29 53
     PrintHex<uint8_t > (evt->Slider, 0x80);
30 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 125
 void JoystickEvents::OnHatSwitch(uint8_t hat) {
126
+#ifdef DEBUG_OUTPUT
34 127
     Serial.print("Hat Switch: ");
35 128
     PrintHex<uint8_t > (hat, 0x80);
36 129
     Serial.println("");
130
+#endif
37 131
 }
38 132
 
39 133
 void JoystickEvents::OnButtonUp(uint8_t but_id) {
134
+#ifdef DEBUG_OUTPUT
40 135
     Serial.print("Up: ");
41 136
     Serial.println(but_id, DEC);
137
+#endif
42 138
 }
43 139
 
44 140
 void JoystickEvents::OnButtonDn(uint8_t but_id) {
45
-    Serial.print("Dn: ");
141
+#ifdef DEBUG_OUTPUT
142
+    Serial.print("Down: ");
46 143
     Serial.println(but_id, DEC);
144
+#endif
47 145
 }
48 146
 
49 147
 void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
148
+#ifdef DEBUG_OUTPUT_RAW
50 149
     Serial.print("Mouse X: ");
51 150
     PrintHex<uint8_t >(x, 0x80);
52 151
     Serial.print("\tY: ");
53 152
     PrintHex<uint8_t >(y, 0x80);
54 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 查看文件

@@ -14,17 +14,27 @@
14 14
 #define __JOYSTICK_EVENTS_H__
15 15
 
16 16
 struct GamePadEventData {
17
+    GamePadEventData(uint16_t v) : X(v), Y(v), Z(v), Rx(v), Ry(v), Rz(v) { }
18
+    
17 19
     uint16_t X, Y, Rz; // 11bit, 11bit, 10bit
18 20
     uint8_t Z, Rx, Ry, Slider;
19 21
 };
20 22
 
21 23
 class JoystickEvents {
22 24
   public:
25
+    JoystickEvents();
26
+
23 27
     virtual void OnGamePadChanged(const GamePadEventData *evt);
24 28
     virtual void OnHatSwitch(uint8_t hat);
25 29
     virtual void OnButtonUp(uint8_t but_id);
26 30
     virtual void OnButtonDn(uint8_t but_id);
27 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 40
 #endif // __JOYSTICK_EVENTS_H__

Loading…
取消
儲存