Line data Source code
1 : #pragma once 2 : 3 : #include "Selector.hpp" 4 : #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp> 5 : #include <Def/Def.hpp> 6 : 7 : BEGIN_CS_NAMESPACE 8 : 9 : template <setting_t N, class Callback = EmptySelectorCallback> 10 3 : class GenericManyButtonsSelector : public GenericSelector<N, Callback> { 11 : using Parent = GenericSelector<N, Callback>; 12 : 13 : public: 14 3 : GenericManyButtonsSelector(Selectable<N> &selectable, 15 : const Callback &callback, 16 : const PinList<N> &buttonPins) 17 9 : : GenericSelector<N, Callback>{selectable, callback}, buttonPins{ 18 12 : buttonPins} {} 19 : 20 1 : void begin() override { 21 1 : Parent::begin(); 22 5 : for (const pin_t &pin : buttonPins) 23 4 : AH::ExtIO::pinMode(pin, INPUT_PULLUP); 24 1 : } 25 : 26 1 : void update() override { 27 1 : Parent::update(); 28 4 : for (setting_t i = 0; i < N; i++) 29 : // TODO: invert? 30 4 : if (AH::ExtIO::digitalRead(buttonPins[i]) == LOW) { 31 1 : if (AH::ExtIO::digitalRead(buttonPins[this->get()]) != LOW) 32 1 : this->set(i); 33 1 : break; 34 : } 35 1 : } 36 : 37 : private: 38 : PinList<N> buttonPins; 39 : }; 40 : 41 : // -------------------------------------------------------------------------- // 42 : 43 : /** 44 : * @brief Selector that reads from @f$ N @f$ buttons. 45 : * 46 : * Pressing the @f$ n @f$-th button selects the @f$ n @f$-th setting. 47 : * 48 : * @htmlonly 49 : * <object type="image/svg+xml" data="../../selector-multiple-momentary-switches-LED.svg"></object> 50 : * @endhtmlonly 51 : * 52 : * @ingroup Selectors 53 : * 54 : * @tparam N 55 : * The number of settings. 56 : */ 57 : template <setting_t N> 58 2 : class ManyButtonsSelector : public GenericManyButtonsSelector<N> { 59 : public: 60 2 : ManyButtonsSelector(Selectable<N> &selectable, const PinList<N> &buttonPins) 61 2 : : GenericManyButtonsSelector<N>{ 62 2 : selectable, 63 : {}, 64 2 : buttonPins, 65 4 : } {} 66 : }; 67 : 68 : END_CS_NAMESPACE