RSeries astromech firmware
AnoRotaryEncoder.h
Go to the documentation of this file.
1 #ifndef AnoRotaryEncoder_h
2 #define AnoRotaryEncoder_h
3 
4 #include "core/PinManager.h"
6 
8 {
9 public:
11  PinManager& pinManager,
12  byte encoderA,
13  byte encoderB,
14  byte buttonUp,
15  byte buttonLeft,
16  byte buttonDown,
17  byte buttonRight,
18  byte buttonIn,
19  bool useInterrupt = true) :
20  RotaryEncoder(encoderA, encoderB, LatchMode::kTwo03, useInterrupt),
21  fPinManager(pinManager)
22  {
23  fButtonPin[0] = buttonUp;
24  fButtonPin[1] = buttonLeft;
25  fButtonPin[2] = buttonDown;
26  fButtonPin[3] = buttonRight;
27  fButtonPin[4] = buttonIn;
28  memset(&fButtonState, '\0', sizeof(fButtonState));
29  memset(&fButtonOldState, '\0', sizeof(fButtonOldState));
30  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
31  fPinManager.pinMode(fButtonPin[i], INPUT_PULLUP);
32  }
33 
34  void setButtonNotify(byte pin, void (*notify)(bool))
35  {
36  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
37  {
38  if (fButtonPin[i] == pin)
39  {
40  fButtonNotify[i] = notify;
41  }
42  }
43  }
44 
45  bool hasButtonStateChanged() const
46  {
47  return fButtonStateChanged;
48  }
49 
50  uint8_t getButtonPressedMask() const
51  {
52  uint8_t mask = 0;
53  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
54  {
55  if (fButtonState[i])
56  mask |= (1<<i);
57  }
58  return mask;
59  }
60 
61  bool isButtonPressed(byte pin) const
62  {
63  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
64  {
65  if (fButtonPin[i] == pin)
66  return fButtonState[i];
67  }
68  return false;
69  }
70 
71  bool isButtonReleased(byte pin) const
72  {
73  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
74  {
75  if (fButtonPin[i] == pin)
76  {
77  return fButtonOldState[i] && !fButtonState[i];
78  }
79  }
80  return false;
81  }
82 
83  virtual void animate() override
84  {
85  fButtonStateChanged = false;
87  for (unsigned i = 0; i < sizeof(fButtonPin); i++)
88  {
89  bool newState = !fPinManager.digitalRead(fButtonPin[i]);
90  fButtonOldState[i] = fButtonState[i];
91  if (newState != fButtonState[i])
92  {
93  fButtonState[i] = newState;
94  fButtonStateChanged = true;
95  if (fButtonNotify[i] != nullptr)
96  {
97  fButtonNotify[i](newState);
98  }
99  }
100  }
101  }
102 
103 private:
104  typedef void (*ButtonNotifyProc)(bool pressed);
105  byte fButtonPin[5];
106  bool fButtonState[sizeof(fButtonPin)];
107  bool fButtonOldState[sizeof(fButtonPin)];
108  ButtonNotifyProc fButtonNotify[sizeof(fButtonPin)];
109  bool fButtonStateChanged = false;
110  PinManager &fPinManager;
111 };
112 
113 #endif
RotaryEncoder::LatchMode
LatchMode
Definition: RotaryEncoder.h:29
PinManager::pinMode
virtual void pinMode(uint8_t pin, uint8_t mode)
Definition: PinManager.h:20
AnoRotaryEncoder::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: AnoRotaryEncoder.h:83
AnoRotaryEncoder::getButtonPressedMask
uint8_t getButtonPressedMask() const
Definition: AnoRotaryEncoder.h:50
AnoRotaryEncoder
Definition: AnoRotaryEncoder.h:7
AnoRotaryEncoder::isButtonReleased
bool isButtonReleased(byte pin) const
Definition: AnoRotaryEncoder.h:71
AnoRotaryEncoder::setButtonNotify
void setButtonNotify(byte pin, void(*notify)(bool))
Definition: AnoRotaryEncoder.h:34
PinManager.h
RotaryEncoder::animate
virtual void animate() override
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: RotaryEncoder.h:62
AnoRotaryEncoder::AnoRotaryEncoder
AnoRotaryEncoder(PinManager &pinManager, byte encoderA, byte encoderB, byte buttonUp, byte buttonLeft, byte buttonDown, byte buttonRight, byte buttonIn, bool useInterrupt=true)
Definition: AnoRotaryEncoder.h:10
PinManager
Definition: PinManager.h:6
AnoRotaryEncoder::isButtonPressed
bool isButtonPressed(byte pin) const
Definition: AnoRotaryEncoder.h:61
PinManager::digitalRead
virtual bool digitalRead(uint8_t pin)
Definition: PinManager.h:12
RotaryEncoder.h
RotaryEncoder
Decodes the signals from a rotary encoder (quadrature pulses) and translates them into a counter posi...
Definition: RotaryEncoder.h:19
AnoRotaryEncoder::hasButtonStateChanged
bool hasButtonStateChanged() const
Definition: AnoRotaryEncoder.h:45