ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SSD1306.h
Go to the documentation of this file.
1/**
2 * @file SSD1306.h
3 * @brief Declares the HeadsUpDisplay class for rendering and displaying information on SSD1306/SH1106 displays.
4 *
5 * This file defines the HeadsUpDisplay class, which extends the Effect interface to capture and process
6 * rendered frames efficiently. It supports SSD1306 displays and provides an untested option for SH1106 displays.
7 *
8 * @date 22/12/2024
9 * @author Coela Can't
10 */
12#pragma once
13
14#include <Arduino.h> // Include for Arduino compatibility.
15#include <Wire.h> // Include for I2C communication.
16#include <Adafruit_GFX.h> // Include for Adafruit GFX library.
17#include <Fonts/Picopixel.h> // Include for additional font support.
18
19#include "../../Examples/UserConfiguration.h" // Include for user-specific configurations.
20#include "../InputDevices/Menu/Menu.h" // Include for menu input handling.
21#include "../../Utils/Math/Mathematics.h" // Include for mathematical utilities.
22#include "../../Scene/Screenspace/Effect.h" // Include for effect interface.
23#include "../../Utils/Time/TimeStep.h" // Include for timestep utility.
24
25#ifdef SH1106
26#include "Adafruit_SH1106.h" // Include for SH1106 display (untested).
27#else
28#include <Adafruit_SSD1306.h> // Include for SSD1306 display.
29#endif
30
31/**
32 * @class HeadsUpDisplay
33 * @brief Renders and displays information on an SSD1306/SH1106 display.
34 *
35 * The HeadsUpDisplay class acts as an Effect, allowing it to intercept and process rendered
36 * frames efficiently for display on OLED screens. It supports a variety of pre-configured
37 * menus, effects, and face animations.
38 */
39class HeadsUpDisplay : public Effect {
40private:
41 static const uint16_t SCREEN_WIDTH = 128; ///< Width of the OLED screen.
42 static const uint16_t SCREEN_HEIGHT = 64; ///< Height of the OLED screen.
43 static const int8_t OLED_RESET = -1; ///< Reset pin for the display.
44 static const uint8_t faceXMinPosition = 66; ///< Minimum X position for face rendering.
45 static const uint8_t faceXMaxPosition = 124; ///< Maximum X position for face rendering.
46 static const uint8_t faceYMinPosition = 35; ///< Minimum Y position for face rendering.
47 static const uint8_t faceYMaxPosition = 60; ///< Maximum Y position for face rendering.
48 static const uint32_t splashTime = 2500; ///< Duration for splash screen in milliseconds.
49 static const int bufferSize = 2048; ///< Buffer size for display rendering.
50
51 const __FlashStringHelper* percentArray[10] = {F("10%"), F("20%"), F("30%"), F("40%"), F("50%"), F("60%"), F("70%"), F("80%"), F("90%"), F("100%")};//!< Battery percentages.
52 const __FlashStringHelper* effectArray[10] = {F("NONE"), F("PHASEY"), F("PHASEX"), F("PHASER"), F("GLITCHX"), F("MAGNET"), F("FISHEYE"), F("BLURH"), F("BLURV"), F("BLURR")};
53 const __FlashStringHelper* colorArray[10] = {F("GRDIENT"), F("YELLOW"), F("ORANGE"), F("WHITE"), F("GREEN"), F("PURPLE"), F("RED"), F("BLUE"), F("RAINBOW"), F("NOISE")};
54 const __FlashStringHelper* onOffArray[2] = {F("OFFLINE"), F("ONLINE")};//!< Online/Offline statuses.
55 const __FlashStringHelper* hueArray[10] = {F("0 DEG"), F("36 DEG"), F("72 DEG"), F("108 DEG"), F("144 DEG"), F("180 DEG"), F("216 DEG"), F("252 DEG"), F("288 DEG"), F("324 DEG")};//!< Hue options.
56 const __FlashStringHelper* faceArray[10] = {F("DEFAULT"), F("ANGRY"), F("DOUBT"), F("FROWN"), F("LOOKUP"), F("SAD"), F("AUDIO1"), F("AUDIO2"), F("AUDIO3")}; //!< Face names.
57
58 const __FlashStringHelper** faceNames; ///< Names of each face to be displayed on the screen
59 bool useExternalFace = false; ///< To decide if it uses faceArray or faceNames for the face
60
61 Effect* subEffect; ///< Used to capture the complete rendered frame
62 TimeStep timeStep = TimeStep(5); ///< Limits the display to update 5 times per second
63 bool didBegin = false; ///< True if the I2C interface started correctly
64 bool splashFinished = false; ///< True when the splash startup screen is finished
65 Vector2D faceMin; ///< Minimum coordinate for face on display
66 Vector2D faceMax; ///< Maximum coordinate for face on display
67 uint32_t startMillis; ///< Start time of the display for the splash screen
68
69#ifdef SH1106
70 static Adafruit_SH1106 display;
71#else
72 static Adafruit_SSD1306 display;
73#endif
74 static uint8_t faceBitmap[bufferSize]; ///< Background template for displaying the grids
75
76 static const uint8_t CoelaSplash[];
77 static const uint8_t PrototracerSplash[];
78 static const uint8_t FaceTemplate[];
79
81
82 /**
83 * @brief Resets the display buffer to a blank state.
84 */
85 void ResetDisplayBuffer();
86
87 /**
88 * @brief Enables rendering for a bitmap face within specified coordinates.
89 *
90 * @param xIn X-coordinate input for face rendering.
91 * @param yIn Y-coordinate input for face rendering.
92 */
93 void EnableBitFaceRender(float xIn, float yIn);
94
95 /**
96 * @brief Updates face rendering information.
97 */
99
100public:
101 /**
102 * @brief Constructs a HeadsUpDisplay with face rendering boundaries.
103 *
104 * @param faceMin Minimum coordinates for face rendering.
105 * @param faceMax Maximum coordinates for face rendering.
106 */
108
109 /**
110 * @brief Sets the array of face names.
111 *
112 * @param faceNames Pointer to an array of face names.
113 */
114 void SetFaceArray(const __FlashStringHelper** faceNames);
115
116 /**
117 * @brief Sets the minimum face rendering coordinates.
118 *
119 * @param faceMin Minimum coordinates for face rendering.
120 */
122
123 /**
124 * @brief Sets the maximum face rendering coordinates.
125 *
126 * @param faceMax Maximum coordinates for face rendering.
127 */
129
130 /**
131 * @brief Initializes the display and related components.
132 */
133 void Initialize();
134
135 /**
136 * @brief Resets the I2C bus in case of communication errors.
137 */
138 void ResetI2CBus();
139
140 /**
141 * @brief Updates the display content based on the current state.
142 */
143 void Update();
144
145 /**
146 * @brief Sets the sub-effect to be applied to the display.
147 *
148 * @param effect Pointer to the Effect to be applied.
149 */
150 void SetEffect(Effect* effect);
151
152 /**
153 * @brief Applies the effect to the specified pixel group.
154 *
155 * @param pixelGroup Pointer to the pixel group to process.
156 */
157 void ApplyEffect(IPixelGroup* pixelGroup);
158
159 /**
160 * @brief Prints text conditionally inverted based on menu selection.
161 *
162 * @param x X-coordinate for the text.
163 * @param y Y-coordinate for the text.
164 * @param menu Current menu index.
165 * @param str The string to print.
166 */
167 void CheckInvertPrintText(int16_t x, int16_t y, uint8_t menu, const String& str);
168};
Abstract base class for applying visual effects to pixel groups.
Definition Effect.h:26
Renders and displays information on an SSD1306/SH1106 display.
Definition SSD1306.h:39
void SetEffect(Effect *effect)
Sets the sub-effect to be applied to the display.
Definition SSD1306.cpp:315
static const uint8_t faceYMaxPosition
Maximum Y position for face rendering.
Definition SSD1306.h:47
static const uint16_t SCREEN_HEIGHT
Height of the OLED screen.
Definition SSD1306.h:42
static const uint8_t faceXMinPosition
Minimum X position for face rendering.
Definition SSD1306.h:44
void ApplyEffect(IPixelGroup *pixelGroup)
Applies the effect to the specified pixel group.
Definition SSD1306.cpp:319
void UpdateFaceInformation()
Updates face rendering information.
Definition SSD1306.cpp:352
const __FlashStringHelper * hueArray[10]
Hue options.
Definition SSD1306.h:55
bool useExternalFace
To decide if it uses faceArray or faceNames for the face.
Definition SSD1306.h:59
static uint8_t faceBitmap[bufferSize]
Background template for displaying the grids.
Definition SSD1306.h:74
static const int bufferSize
Buffer size for display rendering.
Definition SSD1306.h:49
static const uint8_t faceYMinPosition
Minimum Y position for face rendering.
Definition SSD1306.h:46
bool splashFinished
True when the splash startup screen is finished.
Definition SSD1306.h:64
const __FlashStringHelper * colorArray[10]
Definition SSD1306.h:53
const __FlashStringHelper * faceArray[10]
Face names.
Definition SSD1306.h:56
TimeStep timeStep
Limits the display to update 5 times per second.
Definition SSD1306.h:62
const IPixelGroup * facePixels
Definition SSD1306.h:80
const __FlashStringHelper * effectArray[10]
Definition SSD1306.h:52
static const uint16_t SCREEN_WIDTH
Width of the OLED screen.
Definition SSD1306.h:41
void SetFaceMin(Vector2D faceMin)
Sets the minimum face rendering coordinates.
Definition SSD1306.cpp:226
void Initialize()
Initializes the display and related components.
Definition SSD1306.cpp:234
Effect * subEffect
Used to capture the complete rendered frame.
Definition SSD1306.h:61
static const uint8_t CoelaSplash[]
Definition SSD1306.h:11
static const uint8_t faceXMaxPosition
Maximum X position for face rendering.
Definition SSD1306.h:45
void EnableBitFaceRender(float xIn, float yIn)
Enables rendering for a bitmap face within specified coordinates.
Definition SSD1306.cpp:340
const __FlashStringHelper ** faceNames
Names of each face to be displayed on the screen.
Definition SSD1306.h:58
void SetFaceMax(Vector2D faceMax)
Sets the maximum face rendering coordinates.
Definition SSD1306.cpp:230
static Adafruit_SSD1306 display
Definition SSD1306.h:72
static const int8_t OLED_RESET
Reset pin for the display.
Definition SSD1306.h:43
static const uint8_t FaceTemplate[]
Definition SSD1306.h:145
const __FlashStringHelper * onOffArray[2]
Online/Offline statuses.
Definition SSD1306.h:54
void CheckInvertPrintText(int16_t x, int16_t y, uint8_t menu, const String &str)
Prints text conditionally inverted based on menu selection.
Definition SSD1306.cpp:386
uint32_t startMillis
Start time of the display for the splash screen.
Definition SSD1306.h:67
Vector2D faceMax
Maximum coordinate for face on display.
Definition SSD1306.h:66
static const uint32_t splashTime
Duration for splash screen in milliseconds.
Definition SSD1306.h:48
void ResetDisplayBuffer()
Resets the display buffer to a blank state.
Definition SSD1306.cpp:335
void ResetI2CBus()
Resets the I2C bus in case of communication errors.
Definition SSD1306.cpp:279
const __FlashStringHelper * percentArray[10]
Battery percentages.
Definition SSD1306.h:51
static const uint8_t PrototracerSplash[]
Definition SSD1306.h:78
bool didBegin
True if the I2C interface started correctly.
Definition SSD1306.h:63
Vector2D faceMin
Minimum coordinate for face on display.
Definition SSD1306.h:65
void Update()
Updates the display content based on the current state.
Definition SSD1306.cpp:285
void SetFaceArray(const __FlashStringHelper **faceNames)
Sets the array of face names.
Definition SSD1306.cpp:217
Interface for managing and interacting with a collection of pixels.
Definition IPixelGroup.h:25
Provides a mechanism to trigger actions at a specified frequency.
Definition TimeStep.h:18
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27