Skip to content

Commit 74722c2

Browse files
author
Scott Powell
committed
* MomentaryButton: added support for analog button (with threshold)
* RAK: support for PIN_USER_BTN_ANA
1 parent b8223e9 commit 74722c2

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

examples/companion_radio/ui-new/UITask.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* no
354354
#if defined(PIN_USER_BTN)
355355
user_btn.begin();
356356
#endif
357+
#if defined(PIN_USER_BTN_ANA)
358+
analog_btn.begin();
359+
#endif
357360

358361
_node_prefs = node_prefs;
359362
if (_display != NULL) {
@@ -508,6 +511,14 @@ void UITask::loop() {
508511
c = handleLongPress(KEY_RIGHT);
509512
}
510513
#endif
514+
#if defined(PIN_USER_BTN_ANA)
515+
ev = analog_btn.check();
516+
if (ev == BUTTON_EVENT_CLICK) {
517+
c = checkDisplayOn(KEY_SELECT);
518+
} else if (ev == BUTTON_EVENT_LONG_PRESS) {
519+
c = handleLongPress(KEY_ENTER);
520+
}
521+
#endif
511522

512523
if (c != 0 && curr) {
513524
curr->handleInput(c);

src/helpers/ui/MomentaryButton.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,39 @@ MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse
88
prev = _reverse ? HIGH : LOW;
99
cancel = 0;
1010
_long_millis = long_press_millis;
11+
_threshold = 0;
12+
}
13+
14+
MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, int analog_threshold) {
15+
_pin = pin;
16+
_reverse = false;
17+
_pull = false;
18+
down_at = 0;
19+
prev = LOW;
20+
cancel = 0;
21+
_long_millis = long_press_millis;
22+
_threshold = analog_threshold;
1123
}
1224

1325
void MomentaryButton::begin() {
14-
if (_pin >= 0) {
26+
if (_pin >= 0 && _threshold == 0) {
1527
pinMode(_pin, _pull ? (_reverse ? INPUT_PULLUP : INPUT_PULLDOWN) : INPUT);
1628
}
1729
}
1830

1931
bool MomentaryButton::isPressed() const {
20-
return isPressed(digitalRead(_pin));
32+
int btn = _threshold > 0 ? (analogRead(_pin) < _threshold) : digitalRead(_pin);
33+
return isPressed(btn);
2134
}
2235

2336
void MomentaryButton::cancelClick() {
2437
cancel = 1;
2538
}
2639

2740
bool MomentaryButton::isPressed(int level) const {
41+
if (_threshold > 0) {
42+
return level;
43+
}
2844
if (_reverse) {
2945
return level == LOW;
3046
} else {
@@ -36,7 +52,7 @@ int MomentaryButton::check(bool repeat_click) {
3652
if (_pin < 0) return BUTTON_EVENT_NONE;
3753

3854
int event = BUTTON_EVENT_NONE;
39-
int btn = digitalRead(_pin);
55+
int btn = _threshold > 0 ? (analogRead(_pin) < _threshold) : digitalRead(_pin);
4056
if (btn != prev) {
4157
if (isPressed(btn)) {
4258
down_at = millis();

src/helpers/ui/MomentaryButton.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ class MomentaryButton {
1111
int8_t prev, cancel;
1212
bool _reverse, _pull;
1313
int _long_millis;
14+
int _threshold; // analog mode
1415
unsigned long down_at;
1516

1617
bool isPressed(int level) const;
1718

1819
public:
1920
MomentaryButton(int8_t pin, int long_press_mills=0, bool reverse=false, bool pulldownup=false);
21+
MomentaryButton(int8_t pin, int long_press_mills, int analog_threshold);
2022
void begin();
2123
int check(bool repeat_click=false); // returns one of BUTTON_EVENT_*
2224
void cancelClick(); // suppress next BUTTON_EVENT_CLICK (if already in DOWN state)

variants/rak4631/target.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ RAK4631Board board;
1111
#ifdef DISPLAY_CLASS
1212
DISPLAY_CLASS display;
1313
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
14+
15+
#if defined(PIN_USER_BTN_ANA)
16+
MomentaryButton analog_btn(PIN_USER_BTN_ANA, 1000, 20);
17+
#endif
1418
#endif
1519

1620
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);

variants/rak4631/target.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
extern DISPLAY_CLASS display;
1414
#include <helpers/ui/MomentaryButton.h>
1515
extern MomentaryButton user_btn;
16+
#if defined(PIN_USER_BTN_ANA)
17+
extern MomentaryButton analog_btn;
18+
#endif
1619
#endif
1720

1821
extern RAK4631Board board;

0 commit comments

Comments
 (0)