RSeries astromech firmware
CommandScreenHandlerSSD1306.h
Go to the documentation of this file.
1 #ifndef CommandScreenHandlerSSD1306_h
2 #define CommandScreenHandlerSSD1306_h
3 
4 #include "ReelTwo.h"
5 #include <Adafruit_GFX.h>
6 #include <Adafruit_SSD1306.h>
8 
10 //
11 // Command screen handler for SSD 1306 using a rotary dial for events
12 //
14 
15 class CommandScreenHandlerSSD1306: public Adafruit_SSD1306, public CommandScreenHandler
16 {
17 public:
19  Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1),
20  fDial(pinManager, PIN_ENCODER_A,
21  PIN_ENCODER_B,
22  BUTTON_UP,
23  BUTTON_LEFT,
24  BUTTON_DOWN,
25  BUTTON_RIGHT,
26  BUTTON_IN,
27  // TODO: Investigate. Performs significantly
28  // better without interrupts.
29  false)
30  {
31  }
32 
33  virtual void sleepDevice() override
34  {
35  invertDisplay(false);
36  clearDisplay();
37  display();
38  switchToScreen(kMainScreen);
39  }
40 
41  void drawTextCentered(String text)
42  {
43  int16_t x1, y1;
44  uint16_t w = 0, h;
45  getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
46  setCursor(SCREEN_WIDTH / 2 - w / 2, 0);
47  print(text);
48  }
49 
50  void remoteDialEvent(long newValue, long oldValue)
51  {
52  // TODO
53  // fNewDialValue = newValue;
54  }
55 
56  void remoteButtonEvent(uint8_t id, bool pressed, bool repeat)
57  {
58  // TODO
59  // fButtonID = id;
60  // fButtonPressed = pressed;
61  // fButtonRepeat = repeat;
62  }
63 
64  void remoteActive()
65  {
66  // TODO
67  }
68 
69  virtual bool handleEvent() override
70  {
71  bool ret = false;
72  CommandScreen* currentScr = current();
73  if (currentScr == nullptr)
74  return ret;
75  unsigned keyRepeatRate = currentScr->getKeyRepeatRate();
76  if (keyRepeatRate == 0)
77  keyRepeatRate = KEY_REPEAT_RATE_MS;
79  {
80  // Reset the screen blanking timer if the encoder changed
81  // or if any of the buttons are currently pressed
82  if (isSleeping())
83  {
84  fSkipButtonReleased = true;
85  return true;
86  }
87  ret = true;
88  }
89  if (fDial.hasChanged())
90  {
91  long dialValue = -fDial.getValue();
92  currentScr->buttonDial(dialValue, fDialValue);
93  fDialValue = dialValue;
94  }
96  {
98  fSkipButtonReleased = false;
99  return true;
100  }
102  {
103  if (fDial.isButtonPressed(BUTTON_UP))
104  currentScr->buttonUpPressed();
105  if (fDial.isButtonPressed(BUTTON_LEFT))
106  currentScr->buttonLeftPressed();
107  if (fDial.isButtonPressed(BUTTON_DOWN))
108  currentScr->buttonDownPressed();
109  if (fDial.isButtonPressed(BUTTON_RIGHT))
110  currentScr->buttonRightPressed();
111  if (fDial.isButtonPressed(BUTTON_IN))
112  currentScr->buttonInPressed();
113  if (fLastScreenID == currentScr->ID())
114  {
115  if (fDial.isButtonReleased(BUTTON_UP))
116  currentScr->buttonUpReleased();
117  if (fDial.isButtonReleased(BUTTON_LEFT))
118  currentScr->buttonLeftReleased();
119  if (fDial.isButtonReleased(BUTTON_DOWN))
120  currentScr->buttonDownReleased();
121  if (fDial.isButtonReleased(BUTTON_RIGHT))
122  currentScr->buttonRightReleased();
123  if (fDial.isButtonReleased(BUTTON_IN))
124  currentScr->buttonInReleased();
125  }
126  fLastKeyEvent = millis();
127  fLastScreenID = currentScr->ID();
128  }
129  else if (fLastScreenID == currentScr->ID())
130  {
131  if (fDial.isButtonPressed(BUTTON_UP))
132  {
133  if (fLastKeyEvent + keyRepeatRate < millis())
134  {
135  currentScr->buttonUpPressed(true);
136  fLastKeyEvent = millis();
137  }
138  }
139  else if (fDial.isButtonPressed(BUTTON_LEFT))
140  {
141  if (fLastKeyEvent + keyRepeatRate < millis())
142  {
143  currentScr->buttonLeftPressed(true);
144  fLastKeyEvent = millis();
145  }
146  }
147  else if (fDial.isButtonPressed(BUTTON_DOWN))
148  {
149  if (fLastKeyEvent + keyRepeatRate < millis())
150  {
151  currentScr->buttonDownPressed(true);
152  fLastKeyEvent = millis();
153  }
154  }
155  else if (fDial.isButtonPressed(BUTTON_RIGHT))
156  {
157  if (fLastKeyEvent + keyRepeatRate < millis())
158  {
159  currentScr->buttonRightPressed(true);
160  fLastKeyEvent = millis();
161  }
162  }
163  else if (fDial.isButtonPressed(BUTTON_IN))
164  {
165  if (fLastKeyEvent + keyRepeatRate < millis())
166  {
167  currentScr->buttonInPressed(true);
168  fLastKeyEvent = millis();
169  }
170  }
171  }
172  return ret;
173  }
174 
176  long fDialValue = 0;
177  uint32_t fLastKeyEvent = 0;
178  ScreenID fLastScreenID = kInvalid;
179  bool fSkipButtonReleased = false;
180 };
181 #endif
CommandScreen
Definition: CommandScreen.h:260
CommandScreen::buttonLeftPressed
virtual void buttonLeftPressed(bool repeat=false)
Definition: CommandScreen.h:313
CommandScreenHandlerSSD1306::fDialValue
long fDialValue
Definition: CommandScreenHandlerSSD1306.h:176
CommandScreenHandler::switchToScreen
void switchToScreen(ScreenID id, bool popStack=true)
Definition: CommandScreen.h:162
ReelTwo.h
CommandScreenHandlerSSD1306::fSkipButtonReleased
bool fSkipButtonReleased
Definition: CommandScreenHandlerSSD1306.h:179
CommandScreenHandler::current
CommandScreen * current()
Definition: CommandScreen.h:154
AnoRotaryEncoder.h
CommandScreen::buttonInReleased
virtual void buttonInReleased()
Definition: CommandScreen.h:321
CommandScreenHandlerSSD1306::remoteActive
void remoteActive()
Definition: CommandScreenHandlerSSD1306.h:64
CommandScreenHandlerSSD1306::fLastKeyEvent
uint32_t fLastKeyEvent
Definition: CommandScreenHandlerSSD1306.h:177
AnoRotaryEncoder::getButtonPressedMask
uint8_t getButtonPressedMask() const
Definition: AnoRotaryEncoder.h:50
AnoRotaryEncoder
Definition: AnoRotaryEncoder.h:7
CommandScreenHandler
Definition: CommandScreen.h:147
CommandScreenHandlerSSD1306::fDial
AnoRotaryEncoder fDial
Definition: CommandScreenHandlerSSD1306.h:175
AnoRotaryEncoder::isButtonReleased
bool isButtonReleased(byte pin) const
Definition: AnoRotaryEncoder.h:71
CommandScreenHandlerSSD1306::drawTextCentered
void drawTextCentered(String text)
Definition: CommandScreenHandlerSSD1306.h:41
RotaryEncoder::getValue
long getValue()
Returns current value.
Definition: RotaryEncoder.h:102
CommandScreenHandlerSSD1306::remoteButtonEvent
void remoteButtonEvent(uint8_t id, bool pressed, bool repeat)
Definition: CommandScreenHandlerSSD1306.h:56
CommandScreen::buttonRightPressed
virtual void buttonRightPressed(bool repeat=false)
Definition: CommandScreen.h:315
CommandScreenHandlerSSD1306::sleepDevice
virtual void sleepDevice() override
Definition: CommandScreenHandlerSSD1306.h:33
CommandScreenHandlerSSD1306
Definition: CommandScreenHandlerSSD1306.h:15
CommandScreen::buttonLeftReleased
virtual void buttonLeftReleased()
Definition: CommandScreen.h:318
CommandScreenHandlerSSD1306::CommandScreenHandlerSSD1306
CommandScreenHandlerSSD1306(PinManager &pinManager)
Definition: CommandScreenHandlerSSD1306.h:18
CommandScreen::ID
ScreenID ID() const
Definition: CommandScreen.h:273
CommandScreenHandler::isSleeping
bool isSleeping() const
Definition: CommandScreen.h:210
CommandScreenHandlerSSD1306::remoteDialEvent
void remoteDialEvent(long newValue, long oldValue)
Definition: CommandScreenHandlerSSD1306.h:50
CommandScreen::getKeyRepeatRate
unsigned getKeyRepeatRate() const
Definition: CommandScreen.h:304
CommandScreen::buttonDial
virtual void buttonDial(long newValue, long oldValue=0)
Definition: CommandScreen.h:322
CommandScreen::buttonUpPressed
virtual void buttonUpPressed(bool repeat=false)
Definition: CommandScreen.h:312
PinManager
Definition: PinManager.h:6
RotaryEncoder::hasChanged
bool hasChanged()
Returns true if value has changed since last animated event.
Definition: RotaryEncoder.h:94
CommandScreen::buttonDownReleased
virtual void buttonDownReleased()
Definition: CommandScreen.h:319
CommandScreenHandlerSSD1306::fLastScreenID
ScreenID fLastScreenID
Definition: CommandScreenHandlerSSD1306.h:178
CommandScreen::buttonUpReleased
virtual void buttonUpReleased()
Definition: CommandScreen.h:317
AnoRotaryEncoder::isButtonPressed
bool isButtonPressed(byte pin) const
Definition: AnoRotaryEncoder.h:61
CommandScreen::buttonRightReleased
virtual void buttonRightReleased()
Definition: CommandScreen.h:320
CommandScreen::buttonInPressed
virtual void buttonInPressed(bool repeat=false)
Definition: CommandScreen.h:316
CommandScreen::buttonDownPressed
virtual void buttonDownPressed(bool repeat=false)
Definition: CommandScreen.h:314
CommandScreenHandlerSSD1306::handleEvent
virtual bool handleEvent() override
Definition: CommandScreenHandlerSSD1306.h:69
AnoRotaryEncoder::hasButtonStateChanged
bool hasButtonStateChanged() const
Definition: AnoRotaryEncoder.h:45