Double MSP (TLM and MAVLink) throughput for Gemini hardware (#3037)
[ExpressLRS.git] / src / lib / BUTTON / devButton.cpp
blobc7fa93d31ea2c7163e8b8c9e8dfa378688dbd0ed
1 #include "devButton.h"
3 #include "logging.h"
4 #include "button.h"
5 #include "config.h"
6 #include "helpers.h"
7 #include "handset.h"
9 static Button button1;
10 static Button button2;
12 // only check every second if the device is in-use, i.e. RX connected, or TX is armed
13 static constexpr int MS_IN_USE = 1000;
15 #if defined(TARGET_RX)
16 static constexpr struct {
17 bool pressType;
18 uint8_t count;
19 action_e action;
20 } button_actions[] = {
21 // half second durations + 1 (i.e. 2=1.5s)
22 {true, 2, ACTION_BIND}, // 1.5s
23 {true, 9, ACTION_START_WIFI}, // 5.0s
24 {true, 23, ACTION_RESET_REBOOT} // 12.0s
26 #endif
28 static ButtonAction_fn actions[ACTION_LAST] = { nullptr };
30 void registerButtonFunction(action_e action, ButtonAction_fn function)
32 actions[action] = function;
35 size_t button_GetActionCnt()
37 #if defined(TARGET_RX)
38 return ARRAY_SIZE(button_actions);
39 #else
40 return CONFIG_TX_BUTTON_ACTION_CNT;
41 #endif
44 static void handlePress(uint8_t button, bool longPress, uint8_t count)
46 DBGLN("handlePress(%u, %u, %u)", button, (uint8_t)longPress, count);
47 #if defined(TARGET_TX)
48 const button_action_t *button_actions = config.GetButtonActions(button)->val.actions;
49 #endif
50 for (unsigned i=0 ; i<button_GetActionCnt() ; i++)
52 if (button_actions[i].action != ACTION_NONE && button_actions[i].pressType == longPress && button_actions[i].count == count-1)
54 if (actions[button_actions[i].action])
56 actions[button_actions[i].action]();
62 static bool initialize()
64 return GPIO_PIN_BUTTON != UNDEF_PIN || GPIO_PIN_BUTTON2 != UNDEF_PIN;
67 static int start()
69 if (GPIO_PIN_BUTTON != UNDEF_PIN)
71 button1.init(GPIO_PIN_BUTTON);
72 button1.OnShortPress = [](){ handlePress(0, false, button1.getCount()); };
73 button1.OnLongPress = [](){ handlePress(0, true, button1.getLongCount()+1); };
75 if (GPIO_PIN_BUTTON2 != UNDEF_PIN)
77 button2.init(GPIO_PIN_BUTTON2);
78 button2.OnShortPress = [](){ handlePress(1, false, button2.getCount()); };
79 button2.OnLongPress = [](){ handlePress(1, true, button2.getLongCount()+1); };
82 return DURATION_IMMEDIATELY;
85 static int event()
87 #if defined(TARGET_TX)
88 if (handset->IsArmed())
90 return DURATION_NEVER;
92 #else
93 if (connectionState == connected)
95 return DURATION_NEVER;
97 #endif
98 return DURATION_IMMEDIATELY;
101 static int timeout()
103 int timeout = DURATION_NEVER;
104 if (GPIO_PIN_BUTTON != UNDEF_PIN)
106 timeout = button1.update();
108 if (GPIO_PIN_BUTTON2 != UNDEF_PIN)
110 timeout = button2.update();
112 return timeout;
115 device_t Button_device = {
116 .initialize = initialize,
117 .start = start,
118 .event = event,
119 .timeout = timeout,
120 .subscribe = EVENT_ARM_FLAG_CHANGED | EVENT_CONNECTION_CHANGED