Skip to content

Commit 9cb1e58

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

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3+
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4+
*/
5+
6+
#ifndef TCMENU_TWOBUTTONSWITCHCONTROL_H
7+
#define TCMENU_TWOBUTTONSWITCHCONTROL_H
8+
9+
/**
10+
* @file TwoButtonSwitchControl.h
11+
* @brief contains a class that can be used to set up basic two button control of a menu
12+
*/
13+
14+
#include <tcMenu.h>
15+
#include <IoAbstraction.h>
16+
17+
/**
18+
* Use this class to control a menu application with only two buttons. The up/down buttons double up as back and
19+
* select when held. This means that repeat operation is not possible, keep integer and choice value ranges small
20+
* so as to avoid needing many presses.
21+
*/
22+
class TwoButtonSwitchEncoder : public RotaryEncoder, public SwitchListener {
23+
private:
24+
pinid_t upPin;
25+
pinid_t downPin;
26+
27+
public:
28+
/**
29+
* Create a two button switch given an up and down button along with the callback function for the encoder.
30+
* @param up the up button (doubles as back when held)
31+
* @param down the down button (doubles as OK when held)
32+
* @param callbackFn the encoder change callback.
33+
*/
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+
}
39+
40+
void onPressed(pinid_t pin, bool held) override {
41+
// ignored as we need to only handle once the button is released.
42+
}
43+
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+
}
62+
};
63+
64+
#endif //TCMENU_TWOBUTTONSWITCHCONTROL_H

src/tcMenu.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ScrollChoiceMenuItem.h"
1010
#include "MenuIterator.h"
1111
#include "SecuredMenuPopup.h"
12+
#include "extras/TwoButtonSwitchEncoder.h"
1213
#include <IoAbstraction.h>
1314
#include <BaseDialog.h>
1415

@@ -59,6 +60,12 @@ void MenuManager::initFor4WayJoystick(MenuRenderer* renderer, MenuItem* root, pi
5960
renderer->initialise();
6061
}
6162

63+
void MenuManager::initForTwoButton(MenuRenderer *r, MenuItem *root, pinid_t upPin, pinid_t downPin) {
64+
this->renderer = r;
65+
navigator.setRootItem(root);
66+
switches.setEncoder(new TwoButtonSwitchEncoder(upPin, downPin, [](int v) { menuMgr.valueChanged(v); }));
67+
}
68+
6269
void MenuManager::initForUpDownOk(MenuRenderer* renderer, MenuItem* root, pinid_t pinDown, pinid_t pinUp, pinid_t pinOk, int speed) {
6370
this->renderer = renderer;
6471
navigator.setRootItem(root);

src/tcMenu.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ class MenuManager {
218218
void initFor4WayJoystick(MenuRenderer* renderer, MenuItem* root, pinid_t downPin, pinid_t upPin, pinid_t leftPin,
219219
pinid_t rightPin, pinid_t okPin, int speed=20);
220220

221+
/**
222+
* Initialise for up a 2 button joystick where the up doubles as back, and the down doubles as OK when held.
223+
* @param renderer the renderer used for drawing
224+
* @param root the first menu item
225+
* @param upPin the button on up
226+
* @param downPin the button for down
227+
*/
228+
void initForTwoButton(MenuRenderer* renderer, MenuItem* root, pinid_t upPin, pinid_t downPin);
229+
221230
/**
222231
* Initialise in situations where local input is not needed or where a custom type of input is needed
223232
* that is not one of the common types.

0 commit comments

Comments
 (0)