Browse Source

example implementation of using buttons for output axes

Thomas Buck 3 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
     CHANNEL_THROTTLE = 2,
28
     CHANNEL_THROTTLE = 2,
29
     CHANNEL_YAW = 3,
29
     CHANNEL_YAW = 3,
30
     CHANNEL_AUX1 = 4,
30
     CHANNEL_AUX1 = 4,
31
-    CHANNEL_AUX2 = 5
31
+    CHANNEL_AUX2 = 5,
32
+    CHANNEL_AUX3 = 6
32
 };
33
 };
33
 
34
 
34
 // Increase string number when struct changes!
35
 // Increase string number when struct changes!

+ 2
- 0
events.h View File

49
   public:
49
   public:
50
     JoystickEventsCPPM(JoystickEvents* client = 0);
50
     JoystickEventsCPPM(JoystickEvents* client = 0);
51
     virtual void OnGamePadChanged(const GamePadEventData& evt);
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
     uint8_t getInvert(uint8_t ch) {
55
     uint8_t getInvert(uint8_t ch) {
54
         if (ch < CHANNELS_MAX) return invert[ch];
56
         if (ch < CHANNELS_MAX) return invert[ch];

+ 26
- 0
events_cppm.cpp View File

36
 }
36
 }
37
 
37
 
38
 void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
38
 void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
39
+    // up to CHANNEL_AUX2 we are using axes
39
     for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) {
40
     for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) {
40
         uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i];
41
         uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i];
41
         values[i] = map(value, 0, getJoystickMax(i),
42
         values[i] = map(value, 0, getJoystickMax(i),
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
 uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) {
78
 uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) {
53
     if (ch == CHANNEL_ROLL) {
79
     if (ch == CHANNEL_ROLL) {
54
         return evt.X;
80
         return evt.X;

Loading…
Cancel
Save