|
| 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 |
0 commit comments