Silence unused-variable warning (#2872)
[ExpressLRS.git] / src / lib / BUTTON / devButton.cpp
blob89f143fbd9ffcac3cbbd97eefd8eb0542862ace9
1 #include "devButton.h"
3 #if defined(GPIO_PIN_BUTTON)
4 #include "logging.h"
5 #include "button.h"
6 #include "config.h"
7 #include "helpers.h"
8 #include "handset.h"
10 #ifndef GPIO_BUTTON_INVERTED
11 #define GPIO_BUTTON_INVERTED false
12 #endif
13 #ifndef GPIO_BUTTON2_INVERTED
14 #define GPIO_BUTTON2_INVERTED false
15 #endif
16 #if !defined(GPIO_PIN_BUTTON2)
17 #define GPIO_PIN_BUTTON2 UNDEF_PIN
18 #endif
20 static Button button1;
21 #if defined(GPIO_PIN_BUTTON2)
22 static Button button2;
23 #endif
25 // only check every second if the device is in-use, i.e. RX connected, or TX is armed
26 static constexpr int MS_IN_USE = 1000;
28 #if defined(TARGET_RX)
29 static constexpr struct {
30 bool pressType;
31 uint8_t count;
32 action_e action;
33 } button_actions[] = {
34 // half second durations + 1 (i.e. 2=1.5s)
35 {true, 2, ACTION_BIND}, // 1.5s
36 {true, 9, ACTION_START_WIFI}, // 5.0s
37 {true, 23, ACTION_RESET_REBOOT} // 12.0s
39 #endif
41 static ButtonAction_fn actions[ACTION_LAST] = { nullptr };
43 void registerButtonFunction(action_e action, ButtonAction_fn function)
45 actions[action] = function;
48 size_t button_GetActionCnt()
50 #if defined(TARGET_RX)
51 return ARRAY_SIZE(button_actions);
52 #else
53 return CONFIG_TX_BUTTON_ACTION_CNT;
54 #endif
57 static void handlePress(uint8_t button, bool longPress, uint8_t count)
59 DBGLN("handlePress(%u, %u, %u)", button, (uint8_t)longPress, count);
60 #if defined(TARGET_TX)
61 const button_action_t *button_actions = config.GetButtonActions(button)->val.actions;
62 #endif
63 for (unsigned i=0 ; i<button_GetActionCnt() ; i++)
65 if (button_actions[i].action != ACTION_NONE && button_actions[i].pressType == longPress && button_actions[i].count == count-1)
67 if (actions[button_actions[i].action])
69 actions[button_actions[i].action]();
75 static int start()
77 if (GPIO_PIN_BUTTON == UNDEF_PIN)
79 return DURATION_NEVER;
82 if (GPIO_PIN_BUTTON != UNDEF_PIN)
84 button1.init(GPIO_PIN_BUTTON, GPIO_BUTTON_INVERTED);
85 button1.OnShortPress = [](){ handlePress(0, false, button1.getCount()); };
86 button1.OnLongPress = [](){ handlePress(0, true, button1.getLongCount()+1); };
88 #if defined(TARGET_TX)
89 if (GPIO_PIN_BUTTON2 != UNDEF_PIN)
91 button2.init(GPIO_PIN_BUTTON2, GPIO_BUTTON_INVERTED);
92 button2.OnShortPress = [](){ handlePress(1, false, button2.getCount()); };
93 button2.OnLongPress = [](){ handlePress(1, true, button2.getLongCount()+1); };
95 #endif
97 return DURATION_IMMEDIATELY;
100 static int timeout()
102 if (GPIO_PIN_BUTTON == UNDEF_PIN)
104 return DURATION_NEVER;
106 #if defined(TARGET_TX)
107 if (handset->IsArmed())
108 return MS_IN_USE;
109 #else
110 if (connectionState == connected)
111 return MS_IN_USE;
112 #endif
114 #if defined(GPIO_PIN_BUTTON2)
115 if (GPIO_PIN_BUTTON2 != UNDEF_PIN)
117 button2.update();
119 #endif
120 return button1.update();
123 device_t Button_device = {
124 .initialize = nullptr,
125 .start = start,
126 .event = nullptr,
127 .timeout = timeout
130 #endif