SSD1306 OLED display driver  1.5.0
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
core.cpp
1 /*
2  MIT License
3 
4  Copyright (c) 2018, 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 */
24 
25 #include "core.h"
26 
30 
33 
34 bool NanoEngineInputs::pressed(uint8_t buttons)
35 {
36  return (m_onButtons() & buttons) == buttons;
37 }
38 
39 bool NanoEngineInputs::notPressed(uint8_t buttons)
40 {
41  return (m_onButtons() & buttons) == 0;
42 }
43 
45 {
46  m_onButtons = handler;
47 }
48 
50 {
51  m_onButtons = arduboyButtons;
52 }
53 
54 uint8_t NanoEngineInputs::s_zkeypadPin;
55 
56 uint8_t NanoEngineInputs::zkeypadButtons()
57 {
58  int buttonValue = analogRead(s_zkeypadPin);
59  if (buttonValue < 100) return BUTTON_RIGHT;
60  if (buttonValue < 200) return BUTTON_UP;
61  if (buttonValue < 400) return BUTTON_DOWN;
62  if (buttonValue < 600) return BUTTON_LEFT;
63  if (buttonValue < 800) return BUTTON_A;
65  return BUTTON_NONE;
66 }
67 
68 void NanoEngineInputs::connectZKeypad(uint8_t analogPin)
69 {
70  NanoEngineInputs::s_zkeypadPin = analogPin;
71  m_onButtons = zkeypadButtons;
72 }
73 
74 uint8_t NanoEngineInputs::arduboyButtons()
75 {
76  uint8_t buttons;
77  /* Arduboy buttons available only for Atmega32U4 platform */
78  #if defined(__AVR_ATmega32U4__)
79  // down, up, left right
80  buttons = (((~PINF) & 0B11110000)>>4);
81  // A (left)
82  buttons |= (((~PINE) & 0B01000000) >> 2);
83  // B (right)
84  buttons |= (((~PINB) & 0B00010000) << 1);
85  #else
86  buttons = 0;
87  #endif
88  return buttons;
89 }
90 
94 
96 static const uint8_t ENGINE_DEFAULT_FPS = 30;
97 
99 uint8_t NanoEngineCore::m_frameDurationMs = 1000/ENGINE_DEFAULT_FPS;
101 uint8_t NanoEngineCore::m_fps = ENGINE_DEFAULT_FPS;
103 uint8_t NanoEngineCore::m_cpuLoad = 0;
108 
109 
111 {
112  m_lastFrameTs = millis();
113 }
114 
116 {
117  m_fps = fps;
118  m_frameDurationMs = 1000/fps;
119 }
120 
122 {
123  bool needUpdate = (uint32_t)(millis() - m_lastFrameTs) >= m_frameDurationMs;
124  if (needUpdate && m_loop) m_loop();
125  return needUpdate;
126 }
127 
void(* TLoopCallback)(void)
Definition: core.h:39
static TLoopCallback m_loop
Definition: core.h:181
static bool notPressed(uint8_t buttons)
Returns true if button or specific combination of buttons is not pressed. Returns true if button or s...
Definition: core.cpp:39
static void connectArduboyKeys()
Configures NanoEngine8 to use Arduboy keys layout. Configures NanoEngine8 to use Arduboy keys layout...
Definition: core.cpp:49
static bool pressed(uint8_t buttons)
Returns true if button or specific combination of buttons is not pressed. Returns true if button or s...
Definition: core.cpp:34
static void setFrameRate(uint8_t fps)
Definition: core.cpp:115
static uint8_t m_fps
Definition: core.h:175
uint8_t(* TNanoEngineGetButtons)(void)
Definition: core.h:36
static uint8_t m_cpuLoad
Definition: core.h:177
static uint32_t m_lastFrameTs
Definition: core.h:179
static void begin()
Definition: core.cpp:110
static void connectCustomKeys(TNanoEngineGetButtons handler)
Definition: core.cpp:44
static bool nextFrame()
Definition: core.cpp:121
static void connectZKeypad(uint8_t analogPin)
Enables engine to use Z-Keypad. Enables engine to use Z-Keypad. Please refer to arkanoid example for ...
Definition: core.cpp:68
static uint8_t m_frameDurationMs
Definition: core.h:168
static TNanoEngineGetButtons m_onButtons
Definition: core.h:113