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