Browse Source

React to button events

Thomas Buck 7 years ago
parent
commit
432691438a
4 changed files with 103 additions and 10 deletions
  1. 4
    6
      Saitek-X52-PPM.ino
  2. 14
    0
      events.h
  3. 69
    0
      events_buttons.cpp
  4. 16
    4
      events_cppm.cpp

+ 4
- 6
Saitek-X52-PPM.ino View File

@@ -28,7 +28,8 @@ USBHub hub(&usb);
28 28
 HIDUniversal hid(&usb);
29 29
 X52 x52(&usb, &hid);
30 30
 JoystickEventsCPPM joyCPPM;
31
-JoystickEventsDeadZone joyevents((JoystickEvents*)&joyCPPM);
31
+JoystickEventsButtons joyButtons(&x52, (JoystickEvents*)&joyCPPM);
32
+JoystickEventsDeadZone joyevents((JoystickEvents*)&joyButtons);
32 33
 JoystickReportParser joy(&joyevents);
33 34
 
34 35
 void setup() {
@@ -55,12 +56,9 @@ void setup() {
55 56
 
56 57
 void init_joystick() {
57 58
     x52.initialize();
58
-    x52.setLEDBrightness(2);
59
-    x52.setMFDBrightness(2);
60
-    x52.setShift(0);
61
-    x52.setBlink(0);
62 59
     x52.setMFDText(0, "Arduino X52 Host");
63
-    x52.setMFDText(1, "  initialized!");
60
+    x52.setMFDText(1, "  initialized!  ");
61
+    x52.setMFDText(2, "                ");
64 62
 }
65 63
 
66 64
 void loop() {

+ 14
- 0
events.h View File

@@ -16,6 +16,7 @@
16 16
 #include <stdint.h>
17 17
 
18 18
 class GamePadEventData;
19
+class X52;
19 20
 
20 21
 class JoystickEvents {
21 22
   public:
@@ -61,5 +62,18 @@ class JoystickEventsCPPM : public JoystickEvents {
61 62
     uint16_t values[channels];
62 63
 };
63 64
 
65
+class JoystickEventsButtons : public JoystickEvents {
66
+  public:
67
+    JoystickEventsButtons(X52* x = 0, JoystickEvents* client = 0);
68
+    virtual void OnGamePadChanged(const GamePadEventData& evt);
69
+    virtual void OnHatSwitch(uint8_t hat);
70
+    virtual void OnButtonUp(uint8_t but_id);
71
+    virtual void OnButtonDn(uint8_t but_id);
72
+    virtual void OnMouseMoved(uint8_t x, uint8_t y);
73
+
74
+  private:
75
+    X52* x52;
76
+};
77
+
64 78
 #endif // __JOYSTICK_EVENTS_H__
65 79
 

+ 69
- 0
events_buttons.cpp View File

@@ -0,0 +1,69 @@
1
+/*
2
+ * Saitek X52 Arduino USB Host Shield Library.
3
+ * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
4
+ *
5
+ * Based on the USB Host Library HID Joystick example
6
+ * https://www.circuitsathome.com/mcu/hid-joystick-code-sample
7
+ *
8
+ * This program is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU General Public License as
10
+ * published by the Free Software Foundation, version 2.
11
+ */
12
+
13
+#include <Arduino.h>
14
+#include "data.h"
15
+#include "x52.h"
16
+#include "events.h"
17
+
18
+#define DEBUG_BUTTON_MFD
19
+
20
+JoystickEventsButtons::JoystickEventsButtons(X52* x, JoystickEvents* client) : JoystickEvents(client), x52(x) {
21
+
22
+}
23
+
24
+void JoystickEventsButtons::OnGamePadChanged(const GamePadEventData& evt) {
25
+    if (client) {
26
+        client->OnGamePadChanged(evt);
27
+    }
28
+}
29
+
30
+void JoystickEventsButtons::OnHatSwitch(uint8_t hat) {
31
+    if (client) {
32
+        client->OnHatSwitch(hat);
33
+    }
34
+}
35
+
36
+void JoystickEventsButtons::OnButtonUp(uint8_t but_id) {
37
+    if (client) {
38
+        client->OnButtonUp(but_id);
39
+    }
40
+}
41
+
42
+void JoystickEventsButtons::OnButtonDn(uint8_t but_id) {
43
+    if (client) {
44
+        client->OnButtonDn(but_id);
45
+    }
46
+
47
+#ifdef DEBUG_BUTTON_MFD
48
+    String text = "Button " + String(but_id) + " down";
49
+    x52->setMFDText(1, text.c_str());
50
+#endif
51
+
52
+    if (but_id == 23) {
53
+        x52->setLEDBrightness(2);
54
+        x52->setMFDBrightness(2);
55
+    } else if (but_id == 24) {
56
+        x52->setLEDBrightness(1);
57
+        x52->setMFDBrightness(1);
58
+    } else if (but_id == 25) {
59
+        x52->setLEDBrightness(0);
60
+        x52->setMFDBrightness(0);
61
+    }
62
+}
63
+
64
+void JoystickEventsButtons::OnMouseMoved(uint8_t x, uint8_t y) {
65
+    if (client) {
66
+        client->OnMouseMoved(x, y);
67
+    }
68
+}
69
+

+ 16
- 4
events_cppm.cpp View File

@@ -42,21 +42,33 @@ void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
42 42
     values[CHANNEL_AUX2] = map(evt.Slider, 0, 0xFF, 1000, 2000);
43 43
 
44 44
     cppmCopy(values);
45
+
46
+    if (client) {
47
+        client->OnGamePadChanged(evt);
48
+    }
45 49
 }
46 50
 
47 51
 void JoystickEventsCPPM::OnHatSwitch(uint8_t hat) {
48
-
52
+    if (client) {
53
+        client->OnHatSwitch(hat);
54
+    }
49 55
 }
50 56
 
51 57
 void JoystickEventsCPPM::OnButtonUp(uint8_t but_id) {
52
-
58
+    if (client) {
59
+        client->OnButtonUp(but_id);
60
+    }
53 61
 }
54 62
 
55 63
 void JoystickEventsCPPM::OnButtonDn(uint8_t but_id) {
56
-
64
+    if (client) {
65
+        client->OnButtonDn(but_id);
66
+    }
57 67
 }
58 68
 
59 69
 void JoystickEventsCPPM::OnMouseMoved(uint8_t x, uint8_t y) {
60
-
70
+    if (client) {
71
+        client->OnMouseMoved(x, y);
72
+    }
61 73
 }
62 74
 

Loading…
Cancel
Save