Skip to content

Commit 40fe481

Browse files
committed
#203 two button support
1 parent 9cb1e58 commit 40fe481

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include "TwoButtonSwitchEncoder.h"
3+
4+
TwoButtonSwitchEncoder::TwoButtonSwitchEncoder(pinid_t up, pinid_t down, EncoderCallbackFn callbackFn)
5+
: RotaryEncoder(callbackFn), upPin(up), downPin(down) {
6+
switches.addSwitchListener(up, this, NO_REPEAT);
7+
switches.addSwitchListener(down, this, NO_REPEAT);
8+
}
9+
10+
void TwoButtonSwitchEncoder::onReleased(pinid_t pin, bool held) {
11+
auto invert = intent == SCROLL_THROUGH_ITEMS;
12+
if(pin == upPin) {
13+
if(held) {
14+
menuMgr.performDirectionMove(true);
15+
} else {
16+
int8_t dir = invert ? -stepSize : stepSize;
17+
increment(dir);
18+
}
19+
} else if(pin == downPin) {
20+
if(held) {
21+
menuMgr.onMenuSelect(false);
22+
} else {
23+
int8_t dir = invert ? stepSize : -stepSize;
24+
increment(dir);
25+
}
26+
}
27+
}
28+
29+
void TwoButtonSwitchEncoder::onPressed(pinid_t pin, bool held) {
30+
// ignored for two button case
31+
}

src/extras/TwoButtonSwitchEncoder.h

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,12 @@ class TwoButtonSwitchEncoder : public RotaryEncoder, public SwitchListener {
3131
* @param down the down button (doubles as OK when held)
3232
* @param callbackFn the encoder change callback.
3333
*/
34-
TwoButtonSwitchEncoder(pinid_t up, pinid_t down, EncoderCallbackFn callbackFn)
35-
: RotaryEncoder(callbackFn), upPin(up), downPin(down) {
36-
//switches.addSwitchListener(up, this, NO_REPEAT);
37-
//switches.addSwitchListener(down, this, NO_REPEAT);
38-
}
34+
TwoButtonSwitchEncoder(pinid_t up, pinid_t down, EncoderCallbackFn callbackFn);
3935

40-
void onPressed(pinid_t pin, bool held) override {
41-
// ignored as we need to only handle once the button is released.
42-
}
36+
void onPressed(pinid_t pin, bool held) override;
37+
38+
void onReleased(pinid_t pin, bool held) override;
4339

44-
void onReleased(pinid_t pin, bool held) override {
45-
auto invert = intent == SCROLL_THROUGH_ITEMS;
46-
if(pin == upPin) {
47-
if(held) {
48-
menuMgr.performDirectionMove(true);
49-
} else {
50-
int8_t dir = invert ? -stepSize : stepSize;
51-
increment(dir);
52-
}
53-
} else if(pin == downPin) {
54-
if(held) {
55-
menuMgr.onMenuSelect(false);
56-
} else {
57-
int8_t dir = invert ? stepSize : -stepSize;
58-
increment(dir);
59-
}
60-
}
61-
}
6240
};
6341

6442
#endif //TCMENU_TWOBUTTONSWITCHCONTROL_H

src/tcMenu.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void MenuManager::initForTwoButton(MenuRenderer *r, MenuItem *root, pinid_t upPi
6464
this->renderer = r;
6565
navigator.setRootItem(root);
6666
switches.setEncoder(new TwoButtonSwitchEncoder(upPin, downPin, [](int v) { menuMgr.valueChanged(v); }));
67+
renderer->initialise();
6768
}
6869

6970
void MenuManager::initForUpDownOk(MenuRenderer* renderer, MenuItem* root, pinid_t pinDown, pinid_t pinUp, pinid_t pinOk, int speed) {

0 commit comments

Comments
 (0)