AceButton 1.10.0 An adjustable, compact, event-driven button library for Arduino.

LadderButtonConfig.cpp
1 /*
2 MIT License
3
4 Copyright (c) 2020 Brian T. Park
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "LadderButtonConfig.h"
26 #include "AceButton.h"
27
28 namespace ace_button {
29
30 LadderButtonConfig::LadderButtonConfig(
31 uint8_t pin,
32 uint8_t numLevels,
33 const uint16_t levels[],
34 uint8_t numButtons,
35 AceButton* const buttons[],
36 uint8_t defaultReleasedState
37 ):
38 mPin(pin),
39 mNumLevels(numLevels),
40 mNumButtons(numButtons),
41 mPressedState(defaultReleasedState ^ 0x1),
42 mLevels(levels),
43 mButtons(buttons)
44 {
45 for (uint8_t i = 0; i < mNumButtons; i++) {
46 AceButton* button = mButtons[i];
47 button->setButtonConfig(this);
48 }
49
50 // TODO: Verify that the levels[] are monotonically increasing.
51 }
52
53 int LadderButtonConfig::readButton(uint8_t pin) {
54 uint8_t virtualPin = getVirtualPin();
55 return (virtualPin == pin) ? mPressedState : (mPressedState ^ 0x1);
56 }
57
58 void LadderButtonConfig::checkButtons() const {
59 uint8_t virtualPin = getVirtualPin();
60
61 for (uint8_t i = 0; i < mNumButtons; i++) {
62 AceButton* button = mButtons[i];
63 if (button == nullptr) continue;
64
65 // For each button, call checkState() to allow it to figure out which
66 // state it should move to.
67 uint8_t buttonPin = button->getPin();
68 uint8_t buttonState = (buttonPin == virtualPin)
69 ? mPressedState : (mPressedState ^ 0x1);
70 button->checkState(buttonState);
71 }
72 }
73
74 uint8_t LadderButtonConfig::getVirtualPin() const {
75 uint16_t level = analogRead(mPin);
76 return extractIndex(mNumLevels, mLevels, level);
77 }
78
79 uint8_t LadderButtonConfig::extractIndex(uint8_t numLevels,
80 uint16_t const levels[], uint16_t level) {
81
82 uint8_t i;
83 for (i = 0; i < numLevels - 1; i++) {
84
85 // NOTE(brian): This will overflow a 16-bit ADC. If we need to support that,
86 // a possible formula that avoids uint32_t instructions might be something
87 // like:
88 // threshold = levels[i]/2 + levels[i+1]/2
89 // + (((levels[i] & 0x1) + (levels[i+1] & 0x1)) / 2)
90 uint16_t threshold = (levels[i] + levels[i+1]) / 2;
91
92 if (level < threshold) return i;
93 }
94
95 // Return the index of the last level (i.e. numLevels - 1), to indicate
96 // "nothing found".
97 return i;
98 }
99
100 }
ace_button::AceButton
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:54
ace_button::AceButton::setButtonConfig
void setButtonConfig(ButtonConfig *buttonConfig)
Set the ButtonConfig associated with this Button.
Definition: AceButton.h:232
ace_button::AceButton::checkState
void checkState(uint8_t buttonState)
Version of check() used by EncodedButtonConfig.
Definition: AceButton.cpp:121
ace_button::AceButton::getPin
uint8_t getPin() const
Get the button's pin number.
Definition: AceButton.h:271
ace_button::LadderButtonConfig::LadderButtonConfig
LadderButtonConfig(uint8_t pin, uint8_t numLevels, const uint16_t levels[], uint8_t numButtons, AceButton *const buttons[], uint8_t defaultReleasedState=HIGH)
Constructor.
Definition: LadderButtonConfig.cpp:30
ace_button::LadderButtonConfig::readButton
int readButton(uint8_t pin) override
Return state of the button corresponding to the virtual 'pin' number.
Definition: LadderButtonConfig.cpp:53
ace_button::LadderButtonConfig::getVirtualPin
virtual uint8_t getVirtualPin() const
Return the virtual pin number corresponding to current state of the ADC as returned by the analogRead...
Definition: LadderButtonConfig.cpp:74
ace_button::LadderButtonConfig::checkButtons
void checkButtons() const
Read the single mPin once, calculate the virtual pin number of the pressed button (if any),...
Definition: LadderButtonConfig.cpp:58