LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
core.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2021, Alexey Dynda
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 */
28 #ifndef _NANO_ENGINE_CORE_H_
29 #define _NANO_ENGINE_CORE_H_
30 
31 #include "tiler.h"
32 #include "canvas/canvas.h"
33 #include <stdint.h>
34 
41 #define ENGINE_DEFAULT_FPS (15)
42 
44 typedef uint8_t (*TNanoEngineGetButtons)(void);
45 
47 typedef void (*TLoopCallback)(void);
48 
52 
53 enum
54 {
55  BUTTON_NONE = 0B00000000,
56  BUTTON_DOWN = 0B00000001,
57  BUTTON_LEFT = 0B00000010,
58  BUTTON_RIGHT = 0B00000100,
59  BUTTON_UP = 0B00001000,
60  BUTTON_A = 0B00010000,
61  BUTTON_B = 0B00100000,
62  BUTTON_C = 0B01000000,
63  BUTTON_CENTER = 0B10000000,
64 };
65 
70 {
71 protected:
76 
77 public:
84  static bool pressed(uint8_t buttons);
85 
92  static bool notPressed(uint8_t buttons);
93 
100  static bool clicked(uint8_t buttons);
101 
110  static uint8_t buttonsState()
111  {
112  return m_onButtons();
113  }
114 
119  static void connectCustomKeys(TNanoEngineGetButtons handler);
120 
126  static void connectZKeypad(uint8_t analogPin);
127 
132  static void connectArduboyKeys();
133 
142  static void connectKY40encoder(uint8_t pina_clk, uint8_t pinb_dt, int8_t pinc_sw = -1);
143 
157  static void connectGpioKeypad(const uint8_t *gpioKeys);
158 
164  static void connectWioKeypad();
165 
166 protected:
169 
171  static uint8_t m_lastButtons;
172 
174  static uint8_t m_newButtons;
175 
177  static void resetButtonsCache();
178 
179 private:
180  static uint8_t s_zkeypadPin;
181  static const uint8_t *s_gpioKeypadPins;
182  static uint8_t s_ky40_clk;
183  static uint8_t s_ky40_dt;
184  static uint8_t s_ky40_sw;
185  static uint8_t zkeypadButtons();
186  static uint8_t arduboyButtons();
187  static uint8_t gpioButtons();
188 #ifndef SDL_EMULATION
189  static uint8_t wioButtons();
190 #endif
191  static uint8_t ky40Buttons();
192 };
193 
197 
202 {
203 protected:
205  : NanoEngineInputs(){};
206 
210  void beginCore();
211 
212 public:
217  void setFrameRate(uint8_t fps);
218 
222  uint8_t getFrameRate()
223  {
224  return m_fps;
225  }
226 
233  uint8_t getCpuLoad()
234  {
235  return m_cpuLoad;
236  }
237 
241  bool nextFrame();
242 
248  {
249  m_loop = callback;
250  }
251 
252 protected:
254  uint16_t m_frameDurationMs = 1000 / ENGINE_DEFAULT_FPS;
256  uint8_t m_fps = ENGINE_DEFAULT_FPS;
258  uint8_t m_cpuLoad = 0;
260  uint32_t m_lastFrameTs = 0;
262  TLoopCallback m_loop = {nullptr};
263 };
264 
268 template <class C, class D> class NanoEngine: public NanoEngineCore, public NanoEngineTiler<C, D>
269 {
270 public:
274  explicit NanoEngine(D &display);
275 
282  void display();
283 
288  void begin();
289 
295  void notify(const char *str);
296 
297 protected:
298 };
299 
300 template <class C, class D>
302  : NanoEngineCore()
303  , NanoEngineTiler<C, D>(display)
304 {
305 }
306 
307 template <class C, class D> void NanoEngine<C, D>::display()
308 {
313 }
314 
315 template <class C, class D> void NanoEngine<C, D>::begin()
316 {
318 }
319 
320 template <class C, class D> void NanoEngine<C, D>::notify(const char *str)
321 {
323  lcd_delay(1000);
326 }
327 
332 #endif
static void resetButtonsCache()
resets buttons cache.
uint8_t getFrameRate()
Returns current frame rate.
Definition: core.h:222
static uint8_t m_lastButtons
State of last pressed buttons.
Definition: core.h:171
Class for keys processing functionality.
Definition: core.h:69
static void connectZKeypad(uint8_t analogPin)
Enables engine to use Z-Keypad.
void displayBuffer() __attribute__((noinline))
refreshes content on oled display.
Definition: tiler.h:487
Base class for NanoEngine.
Definition: core.h:268
static void connectKY40encoder(uint8_t pina_clk, uint8_t pinb_dt, int8_t pinc_sw=-1)
Configures NanoEngine to use KY40 Rotary Encoder.
static bool clicked(uint8_t buttons)
Returns true if button was clicked and released.
Nano Engine Core class, contains generic frame-rate control functions.
Definition: core.h:201
static void connectArduboyKeys()
Configures NanoEngine8 to use Arduboy keys layout.
static bool pressed(uint8_t buttons)
Returns true if button or specific combination of buttons is pressed.
void display()
refreshes content on oled display.
Definition: core.h:307
NanoEngine(D &display)
Initializes Nano Engine Base object.
Definition: core.h:301
Drawing in memory buffer.
void loopCallback(TLoopCallback callback)
Sets user-defined loop callback.
Definition: core.h:247
static void connectCustomKeys(TNanoEngineGetButtons handler)
Configures NanoEngine8 to use custom key handler.
uint16_t m_frameDurationMs
Duration between frames in milliseconds.
Definition: core.h:254
void begin()
Initializes internal timestamps, engine state, and switches oled display to required mode (see ssd130...
Definition: core.h:315
void displayPopup(const char *msg)
prints popup message over display content prints popup message over display content ...
Definition: tiler.h:518
void refresh()
Marks all tiles for update.
Definition: tiler.h:201
uint8_t m_cpuLoad
Current cpu load in percents.
Definition: core.h:258
uint8_t getCpuLoad()
Returns cpu load in percents [0-255].
Definition: core.h:233
uint32_t m_lastFrameTs
Last timestamp in milliseconds the frame was updated on oled display.
Definition: core.h:260
static uint8_t buttonsState()
Returns bits of all pressed buttons.
Definition: core.h:110
void beginCore()
Initializes internal timestamps.
This class template is responsible for holding and updating data about areas to be refreshed on LCD d...
Definition: tiler.h:79
uint32_t lcd_millis(void)
returns 32-bit timestamp from system power-up in milliseconds
void notify(const char *str)
shows notification to a user for 1 seconds Shows notification to a user for 1 seconds ...
Definition: core.h:320
static bool notPressed(uint8_t buttons)
Returns true if button or specific combination of buttons is not pressed.
NanoEngineInputs()
Initializes Nano Engine Inputs object.
Definition: core.h:75
#define ENGINE_DEFAULT_FPS
Defaut frame rate for all engines.
Definition: core.h:41
uint8_t(* TNanoEngineGetButtons)(void)
Type of user-specified keyboard callback.
Definition: core.h:44
static void connectGpioKeypad(const uint8_t *gpioKeys)
Enables engine to use GPIO keys.
Tiler helper for graphics processing.
static void connectWioKeypad()
Connects Wio keys to NanoEngine.
void(* TLoopCallback)(void)
Type of user-specified loop callback.
Definition: core.h:47
void lcd_delay(unsigned long ms)
Forces current thread to sleeps for specified number of milliseconds.
static uint8_t m_newButtons
State of last pressed buttons.
Definition: core.h:174
static TNanoEngineGetButtons m_onButtons
Callback to call if buttons state needs to be updated.
Definition: core.h:168