RSeries astromech firmware
ResistorLadderButtons.h
Go to the documentation of this file.
1 #ifndef ResistorLadderButtons_h
2 #define ResistorLadderButtons_h
3 
4 #include "ReelTwo.h"
5 
20 template <byte kButtonPin, unsigned int... buttonVal>
22 {
23 public:
24  virtual void setup() override
25  {
26  pinMode(kButtonPin, INPUT);
27  fPrevButtonValue = analogRead(kButtonPin);
28  }
29 
30  virtual void buttonPressed(unsigned buttonNum, unsigned buttonTime)
31  {
32  DEBUG_PRINT("BUTTON: "); DEBUG_PRINT(buttonNum); DEBUG_PRINT(" ms: "); DEBUG_PRINTLN(buttonTime);
33  }
34 
35  virtual void animate() override
36  {
37  uint32_t timeNow = millis();
38  unsigned buttonValue = analogRead(kButtonPin);
39  unsigned buttonReleased = kNumberOfButtons+1;
41  {
42  for (byte i = 0; i < kNumberOfButtons; i++)
43  {
45  {
46  if (!fButtonState[i])
47  {
48  fButtonState[i] = true;
49  fButtonTime[i] = timeNow;
50  }
51  }
52  else
53  {
54  fButtonState[i] = false;
55  if (fPrevButtonState[i])
56  {
57  //okay, so a button was pressed and we're pretty sure which one it was
58  //but because of our ladder setup we could be seeing a false reading for a teeny moment
59  //so disregard any button presses that are shorter than 30ms
60  if (timeNow - fButtonTime[i] > 30)
61  {
62  buttonReleased = i;
63  }
64  }
65  }
67  }
68  }
69  fPrevButtonValue = buttonValue;
70 
71  if (buttonReleased < kNumberOfButtons+1)
72  {
73  buttonPressed(buttonReleased, fButtonTime[buttonReleased]);
74  }
75  }
76 
77 protected:
78  static constexpr unsigned kButtonVariance = 200;
79  static constexpr unsigned kNumberOfButtons = sizeof...(buttonVal);
80 
81  unsigned getButtonValue(unsigned i)
82  {
83  static constexpr unsigned int kButtonValues[] = { buttonVal... };
84  return (i < kNumberOfButtons) ? kButtonValues[i] : 0;
85  }
86 
87  unsigned fPrevButtonValue;
88  bool fButtonState[kNumberOfButtons]; //state of each button
90  uint32_t fButtonTime[kNumberOfButtons+1]; //when did state of each button last change
91 
92  static bool inRange(int val, int minimum, int maximum)
93  {
94  return ((minimum <= val) && (val <= maximum));
95  }
96 };
97 
98 #endif
ResistorLadderButtons
Push buttons connected using a resistor ladder.
Definition: ResistorLadderButtons.h:21
ResistorLadderButtons::buttonPressed
virtual void buttonPressed(unsigned buttonNum, unsigned buttonTime)
Definition: ResistorLadderButtons.h:30
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
SetupEvent
Base class for all devices that require setup that cannot happen in the constructor....
Definition: SetupEvent.h:15
ResistorLadderButtons::fButtonTime
uint32_t fButtonTime[kNumberOfButtons+1]
Definition: ResistorLadderButtons.h:90
ResistorLadderButtons::kButtonVariance
static constexpr unsigned kButtonVariance
Definition: ResistorLadderButtons.h:78
ResistorLadderButtons::fButtonState
bool fButtonState[kNumberOfButtons]
Definition: ResistorLadderButtons.h:88
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
ResistorLadderButtons::getButtonValue
unsigned getButtonValue(unsigned i)
Definition: ResistorLadderButtons.h:81
ResistorLadderButtons::kNumberOfButtons
static constexpr unsigned kNumberOfButtons
Definition: ResistorLadderButtons.h:79
ResistorLadderButtons::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: ResistorLadderButtons.h:35
ResistorLadderButtons::setup
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: ResistorLadderButtons.h:24
ResistorLadderButtons::fPrevButtonValue
unsigned fPrevButtonValue
Definition: ResistorLadderButtons.h:87
ResistorLadderButtons::fPrevButtonState
bool fPrevButtonState[kNumberOfButtons]
Definition: ResistorLadderButtons.h:89
ResistorLadderButtons::inRange
static bool inRange(int val, int minimum, int maximum)
Definition: ResistorLadderButtons.h:92