RSeries astromech firmware
CommandScreenTouchDisplay.h
Go to the documentation of this file.
1 #ifndef CommandScreenTouchHandler_h
2 #define CommandScreenTouchHandler_h
3 
4 #include "ReelTwo.h"
5 #include "menus/CommandScreen.h"
6 
8 //
9 // Command screen handler for SSD 1306 using a rotary dial for events
10 //
12 
13 template <typename COMMAND_DISPLAY>
15 {
16 public:
17  CommandScreenTouchDisplay(COMMAND_DISPLAY& display, bool (*init)(void) = nullptr) :
19  fInitProc(init)
20  {
21  resetDisplay();
22  }
23 
24  bool begin()
25  {
26  return (fInitProc != nullptr) ? fInitProc() : true;
27  }
28 
29  virtual void wakeDevice() override
30  {
31  fDisplay.wakeDevice();
32  }
33 
34  virtual void sleepDevice() override
35  {
36  fDisplay.sleepDevice();
37  invertDisplay(false);
38  clearDisplay();
39  display();
40  switchToScreen(kMainScreen);
41  }
42 
43  inline void invertDisplay(bool invert)
44  {
45  #ifdef USE_SMQ
46  fInvert = invert;
47  #endif
48  fDisplay.invertDisplay(invert);
49  }
50 
51  inline void clearDisplay()
52  {
53  #ifdef USE_SMQ
54  fClearDisplay = true;
55  #endif
56  fDisplay.clearDisplay();
57  }
58 
59  inline void setTextSize(int siz)
60  {
61  #ifdef USE_SMQ
62  fTextSize = siz;
63  #endif
64  fDisplay.setTextSize(siz);
65  }
66 
67  inline void getTextBounds(const char *string, int16_t x, int16_t y,
68  int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
69  {
70  fDisplay.getTextBounds(string, x, y, x1, y1, w, h);
71  }
72 
73  void setRotation(uint8_t r)
74  {
75  fDisplay.setRotation(r);
76  }
77 
78  void setTextColor(uint16_t c)
79  {
80  fDisplay.setTextColor(c);
81  }
82 
83  void setCursor(uint8_t x, uint8_t y)
84  {
85  #ifdef USE_SMQ
86  fX = x;
87  fY = y;
88  #endif
89  fDisplay.setCursor(x, y);
90  }
91 
92  void print(String text)
93  {
94  #ifdef USE_SMQ
95  fString += text;
96  #endif
97  fDisplay.print(text);
98  }
99 
100  void println(unsigned val)
101  {
102  #ifdef USE_SMQ
103  fString += String("\n") + String(val);
104  #endif
105  fDisplay.println(val);
106  }
107 
108  void println(String text)
109  {
110  #ifdef USE_SMQ
111  fString += String("\n") + text;
112  #endif
113  fDisplay.println(text);
114  }
115 
116  void display()
117  {
118  fDisplay.display();
119  // #ifdef USE_SMQ
120  // if (SMQ::sendTopic("LCD", "Remote"))
121  // {
122  // SMQ::send_uint8("x", fX);
123  // SMQ::send_uint8("y", fY);
124  // SMQ::send_boolean("invert", fInvert);
125  // SMQ::send_boolean("centered", fCentered);
126  // SMQ::send_uint8("size", fTextSize);
127  // SMQ::send_string("text", fString.c_str());
128  // SMQ::sendEnd();
129  // resetDisplay();
130  // }
131  // #endif
132  }
133 
134  void drawTextCentered(String text)
135  {
136  int16_t x1, y1;
137  uint16_t w = 0, h;
138  fDisplay.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
139  fDisplay.setCursor(SCREEN_WIDTH / 2 - w / 2, 0);
140  fDisplay.print(text);
141  #ifdef USE_SMQ
142  fCentered = true;
143  fString = text;
144  #endif
145  }
146 
147  void remoteDialEvent(long newValue, long oldValue)
148  {
149  #ifdef USE_SMQ
150  fNewDialValue = newValue;
151  #endif
152  }
153 
154  void remoteButtonEvent(uint8_t id, bool pressed, bool repeat)
155  {
156  #ifdef USE_SMQ
157  fButtonID = id;
158  fButtonPressed = pressed;
159  fButtonRepeat = repeat;
160  #endif
161  }
162 
164  {
165  #ifdef USE_SMQ
166  setEnabled(true);
167  fRemoteActive = true;
168  #endif
169  }
170 
171  virtual bool handleEvent() override
172  {
173  bool ret = false;
174  #ifdef USE_SMQ
175  if (fRemoteActive)
176  {
177  switchToScreen(kMainScreen);
178  fRemoteActive = false;
179  }
180  #endif
181  CommandScreen* currentScr = current();
182  if (currentScr != nullptr)
183  {
184  #ifdef USE_SMQ
185  if (fDialValue != fNewDialValue || fButtonID != 0)
186  {
187  // Reset the screen blanking timer if the encoder changed
188  // or if any of the buttons are currently pressed
189  ret = true;
190  }
191  if (fDialValue != fNewDialValue)
192  {
193  currentScr->buttonDial(fNewDialValue, fDialValue);
194  fDialValue = fNewDialValue;
195  }
196  if (fButtonID)
197  {
198  if (fButtonID == 1 && fButtonPressed)
199  currentScr->buttonUpPressed();
200  if (fButtonID == 2 && fButtonPressed)
201  currentScr->buttonLeftPressed();
202  if (fButtonID == 3 && fButtonPressed)
203  currentScr->buttonDownPressed();
204  if (fButtonID == 4 && fButtonPressed)
205  currentScr->buttonRightPressed();
206  if (fButtonID == 5 && fButtonPressed)
207  currentScr->buttonInPressed();
208  if (fLastScreenID == currentScr->ID())
209  {
210  if (fButtonID == 1 && !fButtonPressed)
211  currentScr->buttonUpReleased();
212  if (fButtonID == 2 && !fButtonPressed)
213  currentScr->buttonLeftReleased();
214  if (fButtonID == 3 && !fButtonPressed)
215  currentScr->buttonDownReleased();
216  if (fButtonID == 4 && !fButtonPressed)
217  currentScr->buttonRightReleased();
218  if (fButtonID == 5 && !fButtonPressed)
219  currentScr->buttonInReleased();
220  }
221  fLastScreenID = currentScr->ID();
222  }
223  else if (fLastScreenID == currentScr->ID())
224  {
225  if (fButtonID == 1 && fButtonPressed && fButtonRepeat)
226  {
227  currentScr->buttonUpPressed(true);
228  }
229  else if (fButtonID == 2 && fButtonPressed && fButtonRepeat)
230  {
231  currentScr->buttonLeftPressed(true);
232  }
233  else if (fButtonID == 3 && fButtonPressed && fButtonRepeat)
234  {
235  currentScr->buttonDownPressed(true);
236  }
237  else if (fButtonID == 4 && fButtonPressed && fButtonRepeat)
238  {
239  currentScr->buttonRightPressed(true);
240  }
241  else if (fButtonID == 5 && fButtonPressed && fButtonRepeat)
242  {
243  currentScr->buttonInPressed(true);
244  }
245  }
246  #endif
247  }
248  #ifdef USE_SMQ
249  fButtonID = 0;
250  fButtonPressed = false;
251  fButtonRepeat = false;
252  fDialValue = fNewDialValue;
253  #endif
254  return ret;
255  }
256 
257  COMMAND_DISPLAY& fDisplay;
258  bool (*fInitProc)(void);
259  long fDialValue = 0;
260  uint32_t fLastKeyEvent = 0;
261  ScreenID fLastScreenID = kInvalid;
262  bool fSkipButtonReleased = false;
263 #ifdef USE_SMQ
264  long fNewDialValue = 0;
265  uint8_t fButtonID = 0;
266  bool fRemoteActive = false;
267  bool fButtonPressed = false;
268  bool fButtonRepeat = false;
269  bool fInvert;
270  bool fClearDisplay;
271  bool fCentered;
272  String fString;
273  uint8_t fX;
274  uint8_t fY;
275  uint8_t fTextSize;
276 #endif
277  inline void resetDisplay()
278  {
279  #ifdef USE_SMQ
280  fInvert = false;
281  fClearDisplay = false;
282  fTextSize = 0;
283  fCentered = false;
284  fString = "";
285  fX = 0;
286  fY = 0;
287  #endif
288  }
289 };
290 #endif
CommandScreen
Definition: CommandScreen.h:273
CommandScreen::buttonLeftPressed
virtual void buttonLeftPressed(bool repeat=false)
Definition: CommandScreen.h:328
CommandScreen.h
CommandScreenTouchDisplay::println
void println(String text)
Definition: CommandScreenTouchDisplay.h:108
CommandScreenTouchDisplay::clearDisplay
void clearDisplay()
Definition: CommandScreenTouchDisplay.h:51
CommandScreenTouchDisplay::println
void println(unsigned val)
Definition: CommandScreenTouchDisplay.h:100
CommandScreenHandler::switchToScreen
void switchToScreen(ScreenID id, bool popStack=true)
Definition: CommandScreen.h:175
ReelTwo.h
CommandScreenHandler::current
CommandScreen * current()
Definition: CommandScreen.h:154
CommandScreenTouchDisplay::setTextSize
void setTextSize(int siz)
Definition: CommandScreenTouchDisplay.h:59
CommandScreen::buttonInReleased
virtual void buttonInReleased()
Definition: CommandScreen.h:336
CommandScreenTouchDisplay::fLastScreenID
ScreenID fLastScreenID
Definition: CommandScreenTouchDisplay.h:261
CommandScreenTouchDisplay::sleepDevice
virtual void sleepDevice() override
Definition: CommandScreenTouchDisplay.h:34
CommandScreenTouchDisplay::display
void display()
Definition: CommandScreenTouchDisplay.h:116
CommandScreenTouchDisplay::drawTextCentered
void drawTextCentered(String text)
Definition: CommandScreenTouchDisplay.h:134
CommandScreenTouchDisplay
Definition: CommandScreenTouchDisplay.h:14
CommandScreenTouchDisplay::wakeDevice
virtual void wakeDevice() override
Definition: CommandScreenTouchDisplay.h:29
CommandScreenTouchDisplay::fLastKeyEvent
uint32_t fLastKeyEvent
Definition: CommandScreenTouchDisplay.h:260
CommandScreenHandler
Definition: CommandScreen.h:147
CommandScreenTouchDisplay::fDisplay
COMMAND_DISPLAY & fDisplay
Definition: CommandScreenTouchDisplay.h:257
CommandScreenHandler::setEnabled
void setEnabled(bool enabled)
Definition: CommandScreen.h:236
CommandScreenTouchDisplay::remoteButtonEvent
void remoteButtonEvent(uint8_t id, bool pressed, bool repeat)
Definition: CommandScreenTouchDisplay.h:154
CommandScreenTouchDisplay::resetDisplay
void resetDisplay()
Definition: CommandScreenTouchDisplay.h:277
CommandScreen::buttonRightPressed
virtual void buttonRightPressed(bool repeat=false)
Definition: CommandScreen.h:330
CommandScreen::buttonLeftReleased
virtual void buttonLeftReleased()
Definition: CommandScreen.h:333
CommandScreenTouchDisplay::fSkipButtonReleased
bool fSkipButtonReleased
Definition: CommandScreenTouchDisplay.h:262
CommandScreen::ID
ScreenID ID() const
Definition: CommandScreen.h:286
CommandScreenTouchDisplay::getTextBounds
void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
Definition: CommandScreenTouchDisplay.h:67
CommandScreenTouchDisplay::CommandScreenTouchDisplay
CommandScreenTouchDisplay(COMMAND_DISPLAY &display, bool(*init)(void)=nullptr)
Definition: CommandScreenTouchDisplay.h:17
CommandScreenTouchDisplay::remoteActive
void remoteActive()
Definition: CommandScreenTouchDisplay.h:163
CommandScreen::buttonDial
virtual void buttonDial(long newValue, long oldValue=0)
Definition: CommandScreen.h:337
CommandScreen::buttonUpPressed
virtual void buttonUpPressed(bool repeat=false)
Definition: CommandScreen.h:327
CommandScreenTouchDisplay::setCursor
void setCursor(uint8_t x, uint8_t y)
Definition: CommandScreenTouchDisplay.h:83
CommandScreenTouchDisplay::fInitProc
bool(* fInitProc)(void)
Definition: CommandScreenTouchDisplay.h:258
CommandScreenTouchDisplay::begin
bool begin()
Definition: CommandScreenTouchDisplay.h:24
CommandScreenTouchDisplay::handleEvent
virtual bool handleEvent() override
Definition: CommandScreenTouchDisplay.h:171
CommandScreenTouchDisplay::print
void print(String text)
Definition: CommandScreenTouchDisplay.h:92
CommandScreenTouchDisplay::fDialValue
long fDialValue
Definition: CommandScreenTouchDisplay.h:259
CommandScreen::buttonDownReleased
virtual void buttonDownReleased()
Definition: CommandScreen.h:334
CommandScreen::buttonUpReleased
virtual void buttonUpReleased()
Definition: CommandScreen.h:332
CommandScreen::buttonRightReleased
virtual void buttonRightReleased()
Definition: CommandScreen.h:335
CommandScreenTouchDisplay::invertDisplay
void invertDisplay(bool invert)
Definition: CommandScreenTouchDisplay.h:43
CommandScreenTouchDisplay::setTextColor
void setTextColor(uint16_t c)
Definition: CommandScreenTouchDisplay.h:78
CommandScreen::buttonInPressed
virtual void buttonInPressed(bool repeat=false)
Definition: CommandScreen.h:331
CommandScreen::buttonDownPressed
virtual void buttonDownPressed(bool repeat=false)
Definition: CommandScreen.h:329
CommandScreenTouchDisplay::setRotation
void setRotation(uint8_t r)
Definition: CommandScreenTouchDisplay.h:73
CommandScreenTouchDisplay::remoteDialEvent
void remoteDialEvent(long newValue, long oldValue)
Definition: CommandScreenTouchDisplay.h:147