AceButton  1.10.0
An adjustable, compact, event-driven button library for Arduino.
EncodedButtonConfig.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 "EncodedButtonConfig.h"
26 #include "AceButton.h"
27 
28 namespace ace_button {
29 
31  uint8_t numPins, const uint8_t pins[], uint8_t numButtons,
32  AceButton* const buttons[], uint8_t defaultReleasedState):
33  mNumPins(numPins),
34  mNumButtons(numButtons),
35  mPressedState(defaultReleasedState ^ 0x1),
36  mPins(pins),
37  mButtons(buttons) {
38  for (uint8_t i = 0; i < mNumButtons; i++) {
39  AceButton* button = mButtons[i];
40  button->setButtonConfig(this);
41  }
42 }
43 
45  uint8_t virtualPin = getVirtualPin();
46  return (virtualPin == pin) ? mPressedState : (mPressedState ^ 0x1);
47 }
48 
50  uint8_t virtualPin = getVirtualPin();
51  for (uint8_t i = 0; i < mNumButtons; i++) {
52  AceButton* button = mButtons[i];
53  if (button == nullptr) continue;
54 
55  // For each button, call checkState() to allow it to figure out which
56  // state it should move it.
57  uint8_t buttonPin = button->getPin();
58  uint8_t buttonState = (buttonPin == virtualPin)
59  ? mPressedState : (mPressedState ^ 0x1);
60  button->checkState(buttonState);
61  }
62 }
63 
65  uint8_t virtualPin = 0;
66  for (uint8_t i = 0; i < mNumPins; i++) {
67  uint8_t pin = mPins[i];
68  int s = digitalRead(pin);
69  virtualPin |= (s == mPressedState) << i;
70  }
71  return virtualPin;
72 }
73 
74 }
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:54
void setButtonConfig(ButtonConfig *buttonConfig)
Set the ButtonConfig associated with this Button.
Definition: AceButton.h:232
void checkState(uint8_t buttonState)
Version of check() used by EncodedButtonConfig.
Definition: AceButton.cpp:121
uint8_t getPin() const
Get the button's pin number.
Definition: AceButton.h:271
int readButton(uint8_t pin) override
Return state of the virtual (i.e.
EncodedButtonConfig(uint8_t numPins, const uint8_t pins[], uint8_t numButtons, AceButton *const buttons[], uint8_t defaultReleasedState=HIGH)
Constructor.
void checkButtons() const
Read the pins once, obtain the virtual pin number, then call each button's checkState() method to tri...
virtual uint8_t getVirtualPin() const
Return the virtual pin number corresponding to the combinatorial states of the actual pins.