optimise mavlink SS packet size (#3029)
[ExpressLRS.git] / src / lib / BUTTON / devButton.cpp
blobf348e0d66af35c5ec120b6ac646518fef2f04f77
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 int start()
64 if (GPIO_PIN_BUTTON == UNDEF_PIN && GPIO_PIN_BUTTON2 == UNDEF_PIN)
66 return DURATION_NEVER;
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 (GPIO_PIN_BUTTON == UNDEF_PIN && GPIO_PIN_BUTTON2 == UNDEF_PIN)
89 return DURATION_NEVER;
91 #if defined(TARGET_TX)
92 if (handset->IsArmed())
94 return DURATION_NEVER;
96 #else
97 if (connectionState == connected)
99 return DURATION_NEVER;
101 #endif
102 return DURATION_IMMEDIATELY;
105 static int timeout()
107 int timeout = DURATION_NEVER;
108 if (GPIO_PIN_BUTTON != UNDEF_PIN)
110 timeout = button1.update();
112 if (GPIO_PIN_BUTTON2 != UNDEF_PIN)
114 timeout = button2.update();
116 return timeout;
119 device_t Button_device = {
120 .initialize = nullptr,
121 .start = start,
122 .event = event,
123 .timeout = timeout