1-phase PV router
Loading...
Searching...
No Matches
utils_oled.h
Go to the documentation of this file.
1
11
12#ifndef UTILS_OLED_H
13#define UTILS_OLED_H
14
15#include <Arduino.h>
16#include <U8g2lib.h>
17
18#include "type_traits.hpp"
19#include "types.h"
20
21// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
22// The pins for I2C are defined by the Wire-library.
23// On an arduino UNO: A4(SDA), A5(SCL)
24#define OLED_RESET -1
25#define SCREEN_ADDRESS 0x3C
26
40{
41public:
42 u8x8_fake(uint8_t) {}
43
44 bool begin()
45 {
46 return true;
47 }
48 void clearDisplay() {}
49 void noInverse() {}
50 uint8_t getCols()
51 {
52 return 0;
53 }
54 uint8_t getRows()
55 {
56 return 0;
57 }
58
59 void setFont(const uint8_t *) {}
60 void drawString(uint8_t, uint8_t, const char *) {}
61 void drawGlyph(uint8_t, uint8_t, uint8_t) {}
62
63 void drawTile(uint8_t, uint8_t, uint8_t, uint8_t *) {}
64};
65
67
68inline constexpr uint8_t LOGO_WIDTH{ 72 };
69inline constexpr uint8_t LOGO_HEIGHT{ 64 };
70
71const unsigned char logo_xbm[] U8X8_PROGMEM = {
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xef, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xbf, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x9f, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xfb, 0xcf, 0x9f, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xf3,
76 0xcf, 0x9f, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xdf, 0xdf, 0x3f,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xdf, 0xdf, 0x3f, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xcf, 0xdf, 0xdf, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf,
79 0x9f, 0xcf, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xdf, 0xcf, 0xcf,
80 0xff, 0xff, 0xff, 0xff, 0xf3, 0xbf, 0x9f, 0xcf, 0xef, 0x7f, 0xfe, 0xff,
81 0xff, 0xe7, 0x3f, 0xbf, 0xef, 0xe7, 0x3f, 0xff, 0xff, 0xff, 0xcf, 0x7f,
82 0xbf, 0xef, 0xf7, 0x9f, 0xff, 0xff, 0xff, 0x1f, 0x7f, 0xbe, 0xef, 0xf3,
83 0xc7, 0xff, 0xff, 0xff, 0x3f, 0xfe, 0xbe, 0xef, 0xfb, 0xe7, 0xff, 0xff,
84 0xff, 0xff, 0xfc, 0x3d, 0xe7, 0xf9, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xf9,
85 0x79, 0xf7, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xfb, 0xff, 0x7e,
86 0xfe, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff,
87 0x1f, 0xff, 0xcf, 0xff, 0xff, 0x9f, 0xff, 0xc7, 0xff, 0x3f, 0xf8, 0x9f,
88 0xff, 0xff, 0xef, 0xff, 0xe0, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xff, 0xef,
89 0x3f, 0xfc, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x87, 0xff, 0xff,
90 0xff, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xe1,
91 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
92 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0xff, 0xff, 0xff, 0xff,
93 0xff, 0xff, 0xff, 0x43, 0x0b, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
94 0xc7, 0x3f, 0xfe, 0xff, 0xff, 0xff, 0x07, 0x00, 0xf0, 0xc7, 0x7f, 0x7e,
95 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xfe, 0xff, 0xff, 0xff,
96 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xc7, 0xff, 0xa3, 0x45, 0xce, 0xe2, 0xff, 0xff, 0xff, 0xc7, 0xff, 0x3b,
98 0x32, 0xb4, 0xfa, 0xff, 0xff, 0xff, 0xc7, 0xbf, 0x23, 0x62, 0x92, 0xe2,
99 0xff, 0xff, 0xff, 0xc7, 0x9f, 0xab, 0x14, 0xa4, 0xfa, 0xff, 0xff, 0xff,
100 0x07, 0x80, 0xf7, 0xeb, 0xcd, 0xe6, 0x07, 0x00, 0xe0, 0xc7, 0x8f, 0xff,
101 0xff, 0xff, 0xff, 0xe7, 0x23, 0xfc, 0xc7, 0x9f, 0xe4, 0xff, 0xff, 0xff,
102 0xcf, 0x33, 0xfc, 0xc7, 0x1f, 0x00, 0xfc, 0xff, 0xff, 0x8f, 0x99, 0xf9,
103 0xc7, 0x3f, 0x3c, 0xf8, 0xff, 0xff, 0x9f, 0x99, 0xf1, 0xc7, 0x3f, 0xfc,
104 0xf0, 0xff, 0xff, 0x3f, 0xcc, 0xf3, 0xc7, 0x3f, 0xfc, 0xf0, 0xff, 0xff,
105 0x3f, 0xc4, 0xe7, 0xc7, 0x3f, 0xfc, 0xe1, 0xff, 0xff, 0x07, 0x00, 0xe0,
106 0xc7, 0x3f, 0xfc, 0xe1, 0xff, 0xff, 0x07, 0x00, 0xe8, 0xc7, 0x3f, 0xfc,
107 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0x3f, 0xfc, 0xf1, 0xff, 0xff,
108 0xff, 0xff, 0xff, 0x03, 0x00, 0xfc, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
109 0xff, 0x3f, 0x3c, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00,
110 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x1c, 0xfe, 0xff, 0xff,
111 0xff, 0xff, 0xff, 0xff, 0x3f, 0x7c, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff,
112 0xff, 0x3f, 0x7c, 0xf8, 0xff, 0xff, 0xff, 0x18, 0x2b, 0x22, 0x3e, 0xfc,
113 0xf8, 0xff, 0xff, 0xff, 0x6a, 0x6a, 0x73, 0x3e, 0xfc, 0xf0, 0xff, 0xff,
114 0xff, 0xc8, 0x7a, 0x23, 0x3e, 0xfc, 0xf0, 0xff, 0xff, 0xff, 0x58, 0x6a,
115 0x2b, 0x3e, 0xfc, 0xf1, 0xff, 0xff, 0xff, 0x97, 0x77, 0xeb, 0x3d, 0xfc,
116 0xe1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc, 0xe3, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0x1f, 0xf8, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc,
119 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
120};
121
122#if !defined(__DOXYGEN__)
123inline uint8_t *get_tile_from_xbm(uint8_t, uint8_t, uint8_t, const unsigned char *) __attribute__((always_inline));
124#endif
125
137uint8_t *get_tile_from_xbm(uint8_t tx, uint8_t ty, uint8_t xbm_byte_width, const unsigned char *xbm)
138{
139 uint8_t d[8];
140 const auto *p{ xbm + tx + xbm_byte_width * (ty << 3) };
141
142 // Clear the tile buffer
143 memset(d, 0, sizeof(d));
144
145 for (uint8_t i = 0; i < 8; ++i)
146 {
147 uint8_t b = u8x8_pgm_read(p);
148 for (uint8_t bit = 0; bit < 8; ++bit)
149 {
150 if (b & (1 << bit))
151 {
152 d[bit] |= (1 << i);
153 }
154 }
155 p += xbm_byte_width;
156 }
157 return d;
158}
159
171void u8x8_draw_xbm(uint8_t _tx, uint8_t _ty, uint8_t xbm_width, uint8_t xbm_height, const unsigned char *xbm)
172{
173 for (uint8_t y = 0; y < (xbm_height >> 3); ++y)
174 {
175 for (uint8_t x = 0; x < (xbm_width >> 3); ++x)
176 {
177 const auto tile{ get_tile_from_xbm(x, y, xbm_width >> 3, xbm) };
178 u8x8.drawTile(_tx + x, _ty + y, 1, tile);
179 }
180 }
181}
182
189{
190 if constexpr (TYPE_OF_DISPLAY == DisplayType::OLED)
191 {
192 if (!u8x8.begin())
193 {
194 Serial.println(F("u8x8 allocation failed"));
195 }
196
197 // Clear the buffer
198 u8x8.clearDisplay();
199
200 u8x8.noInverse();
201
202 u8x8_draw_xbm((u8x8.getCols() - (LOGO_WIDTH >> 3)) >> 1, (u8x8.getRows() - (LOGO_HEIGHT >> 3)) >> 1, LOGO_WIDTH, LOGO_HEIGHT, logo_xbm);
203 }
204}
205
212{
213 u8x8.clearDisplay();
214}
215
222{
223 if constexpr (TYPE_OF_DISPLAY == DisplayType::OLED)
224 {
225 static bool watchdogState{ false }; // State of the blinking LED
226
227 // Toggle the watchdog state
228 watchdogState ^= true;
229
230 if (watchdogState)
231 {
232 u8x8.setFont(u8x8_font_open_iconic_embedded_2x2); // Change the font if needed
233 u8x8.drawGlyph(0, 6, 70); // The symbol code
234 }
235 else
236 {
237 u8x8.drawString(0, 6, " ");
238 }
239 }
240}
241
249void updateOLED(uint16_t value)
250{
251 if constexpr (TYPE_OF_DISPLAY == DisplayType::OLED)
252 {
253 static char buffer[6]; // Buffer to hold the formatted string
254
255 // Set the font and format the value as a float with max 3 decimal places and 4 digits wide
256 u8x8.setFont(u8x8_font_inb33_3x6_n);
257 dtostrf(value * 0.001f, 4, 3, buffer);
258 u8x8.drawString(0, 0, buffer);
259
260 // Set the font and draw the unit
261 u8x8.setFont(u8x8_font_7x14B_1x2_r);
262 u8x8.drawString(12, 6, "kWh");
263 }
264}
265
266#endif /* UTILS_OLED_H */
void drawGlyph(uint8_t, uint8_t, uint8_t)
Definition utils_oled.h:61
void setFont(const uint8_t *)
Definition utils_oled.h:59
uint8_t getCols()
Definition utils_oled.h:50
u8x8_fake(uint8_t)
Definition utils_oled.h:42
void noInverse()
Definition utils_oled.h:49
bool begin()
Definition utils_oled.h:44
void drawString(uint8_t, uint8_t, const char *)
Definition utils_oled.h:60
void clearDisplay()
Definition utils_oled.h:48
void drawTile(uint8_t, uint8_t, uint8_t, uint8_t *)
Definition utils_oled.h:63
uint8_t getRows()
Definition utils_oled.h:54
constexpr DisplayType TYPE_OF_DISPLAY
Definition config.h:51
void updateWatchdog()
Update the watchdog on the display.
Definition utils_oled.h:221
void setupOLED()
Setup the OLED display.
Definition utils_oled.h:188
uint8_t * get_tile_from_xbm(uint8_t tx, uint8_t ty, uint8_t xbm_byte_width, const unsigned char *xbm)
Get the tile from xbm object.
Definition utils_oled.h:137
void updateOLED(uint16_t value)
Update the OLED display with the given value.
Definition utils_oled.h:249
void u8x8_draw_xbm(uint8_t _tx, uint8_t _ty, uint8_t xbm_width, uint8_t xbm_height, const unsigned char *xbm)
Draw the xbm on the OLED display.
Definition utils_oled.h:171
void clearDisplay()
Clear the display.
Definition utils_oled.h:211
TrueType type
uint8_t i
Some useful but missing stl functions templates.
Some basics classes/types.
constexpr uint8_t LOGO_WIDTH
Definition utils_oled.h:68
conditional< TYPE_OF_DISPLAY==DisplayType::OLED, U8X8_SSD1306_128X64_NONAME_HW_I2C, u8x8_fake >::type u8x8(U8X8_PIN_NONE)
const unsigned char logo_xbm[] U8X8_PROGMEM
Definition utils_oled.h:71
constexpr uint8_t LOGO_HEIGHT
Definition utils_oled.h:69