Browse Source

example implementation of using buttons for output axes

Thomas Buck 2 years ago
parent
commit
d58cf17e90
3 changed files with 30 additions and 1 deletions
  1. 2
    1
      config.h
  2. 2
    0
      events.h
  3. 26
    0
      events_cppm.cpp

+ 2
- 1
config.h View File

@@ -28,7 +28,8 @@ enum RxChannels {
28 28
     CHANNEL_THROTTLE = 2,
29 29
     CHANNEL_YAW = 3,
30 30
     CHANNEL_AUX1 = 4,
31
-    CHANNEL_AUX2 = 5
31
+    CHANNEL_AUX2 = 5,
32
+    CHANNEL_AUX3 = 6
32 33
 };
33 34
 
34 35
 // Increase string number when struct changes!

+ 2
- 0
events.h View File

@@ -49,6 +49,8 @@ class JoystickEventsCPPM : public JoystickEvents {
49 49
   public:
50 50
     JoystickEventsCPPM(JoystickEvents* client = 0);
51 51
     virtual void OnGamePadChanged(const GamePadEventData& evt);
52
+    virtual void OnButtonUp(uint8_t but_id);
53
+    virtual void OnButtonDown(uint8_t but_id);
52 54
 
53 55
     uint8_t getInvert(uint8_t ch) {
54 56
         if (ch < CHANNELS_MAX) return invert[ch];

+ 26
- 0
events_cppm.cpp View File

@@ -36,6 +36,7 @@ JoystickEventsCPPM::JoystickEventsCPPM(JoystickEvents* client) : JoystickEvents(
36 36
 }
37 37
 
38 38
 void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
39
+    // up to CHANNEL_AUX2 we are using axes
39 40
     for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) {
40 41
         uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i];
41 42
         values[i] = map(value, 0, getJoystickMax(i),
@@ -49,6 +50,31 @@ void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
49 50
     }
50 51
 }
51 52
 
53
+void JoystickEventsCPPM::OnButtonUp(uint8_t but_id) {
54
+    // if you want to do something when a button is released
55
+    // do it here...
56
+
57
+    if (client) {
58
+        client->OnButtonUp(but_id);
59
+    }
60
+}
61
+
62
+void JoystickEventsCPPM::OnButtonDown(uint8_t but_id) {
63
+    // called when a button is pressed
64
+    if (but_id == 42) { // TODO proper button id
65
+        // set CHANNEL_AUX3 according to button
66
+        values[CHANNEL_AUX3] = 1000;
67
+    } else if (but_id == 23) { // TODO proper button id
68
+        values[CHANNEL_AUX3] = 2000;
69
+    }
70
+
71
+    CPPM::instance().copy(values);
72
+
73
+    if (client) {
74
+        client->OnButtonDown(but_id);
75
+    }
76
+}
77
+
52 78
 uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) {
53 79
     if (ch == CHANNEL_ROLL) {
54 80
         return evt.X;

Loading…
Cancel
Save