Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/ESP_Code/DisplayHal.h
925 views
1
// Abstraction layer for handling various types of screens
2
// See Settings.h for enabling the screen of your choice
3
#ifndef DISPLAY_HAL_H
4
#define DISPLAY_HAL_H
5
6
// Abstraction layer: custom fonts, images, etc.
7
#if defined(DISPLAY_SSD1306)
8
static const unsigned char image_check_contour_bits[] U8X8_PROGMEM = {0x00,0x04,0x00,0x0a,0x04,0x11,0x8a,0x08,0x51,0x04,0x22,0x02,0x04,0x01,0x88,0x00,0x50,0x00,0x20,0x00};
9
static const unsigned char image_network_1_bar_bits[] U8X8_PROGMEM = {0x00,0x70,0x00,0x50,0x00,0x50,0x00,0x50,0x00,0x57,0x00,0x55,0x00,0x55,0x00,0x55,0x70,0x55,0x50,0x55,0x50,0x55,0x50,0x55,0x57,0x55,0x57,0x55,0x77,0x77,0x00,0x00};
10
static const unsigned char image_network_2_bars_bits[] U8X8_PROGMEM = {0x00,0x70,0x00,0x50,0x00,0x50,0x00,0x50,0x00,0x57,0x00,0x55,0x00,0x55,0x00,0x55,0x70,0x55,0x70,0x55,0x70,0x55,0x70,0x55,0x77,0x55,0x77,0x55,0x77,0x77,0x00,0x00};
11
static const unsigned char image_network_3_bars_bits[] U8X8_PROGMEM = {0x00,0x70,0x00,0x50,0x00,0x50,0x00,0x50,0x00,0x57,0x00,0x57,0x00,0x57,0x00,0x57,0x70,0x57,0x70,0x57,0x70,0x57,0x70,0x57,0x77,0x57,0x77,0x57,0x77,0x77,0x00,0x00};
12
static const unsigned char image_network_4_bars_bits[] U8X8_PROGMEM = {0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,0x77,0x00,0x77,0x00,0x77,0x00,0x77,0x70,0x77,0x70,0x77,0x70,0x77,0x70,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x00,0x00};
13
static const unsigned char image_duco_logo_bits[] U8X8_PROGMEM = {0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0xff,0x00,0xc0,0x01,0x9f,0x01,0x20,0x01,0x20,0x01,0x20,0x01,0x9f,0x01,0xc0,0x01,0xff,0x00,0x7f,0x00};
14
static const unsigned char image_duco_logo_big_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0x0f,0x00,0x00,0xfc,0xff,0xff,0x3f,0x00,0x00,0xfc,0xff,0xff,0x7f,0x00,0x00,0xfc,0xff,0xff,0xff,0x00,0x00,0xfc,0xff,0xff,0xff,0x01,0x00,0xfc,0xff,0xff,0xff,0x03,0x00,0xf8,0xff,0xff,0xff,0x07,0x00,0x00,0x00,0x00,0xfe,0x0f,0x00,0xfc,0xff,0x03,0xf8,0x0f,0x00,0xfc,0xff,0x0f,0xf0,0x1f,0x00,0xfc,0xff,0x1f,0xe0,0x1f,0x00,0xfc,0xff,0x3f,0xe0,0x3f,0x00,0xfc,0xff,0x7f,0xc0,0x3f,0x00,0xfc,0xff,0xff,0xc0,0x7f,0x00,0xf8,0xff,0xff,0x80,0x7f,0x00,0x00,0x00,0xfe,0x80,0x7f,0x00,0x00,0x00,0xfc,0x81,0x7f,0x00,0x00,0x00,0xfc,0x01,0x7f,0x00,0x00,0x00,0xfc,0x01,0xff,0x00,0x00,0x00,0xfc,0x01,0xff,0x00,0x00,0x00,0xfc,0x01,0xff,0x00,0x00,0x00,0xfc,0x01,0xff,0x00,0x00,0x00,0xfc,0x01,0xff,0x00,0x00,0x00,0xfc,0x81,0x7f,0x00,0x00,0x00,0xfe,0x81,0x7f,0x00,0x00,0x80,0xff,0x80,0x7f,0x00,0xfc,0xff,0xff,0xc0,0x7f,0x00,0xfc,0xff,0xff,0xc0,0x7f,0x00,0xfc,0xff,0x7f,0xe0,0x3f,0x00,0xfc,0xff,0x3f,0xf0,0x3f,0x00,0xfc,0xff,0x1f,0xf8,0x1f,0x00,0xfc,0xff,0x0f,0xfc,0x1f,0x00,0xf8,0xff,0x01,0xfe,0x0f,0x00,0x00,0x00,0xc0,0xff,0x0f,0x00,0xfc,0xff,0xff,0xff,0x07,0x00,0xfc,0xff,0xff,0xff,0x03,0x00,0xfc,0xff,0xff,0xff,0x01,0x00,0xfc,0xff,0xff,0xff,0x00,0x00,0xfc,0xff,0xff,0x7f,0x00,0x00,0xfc,0xff,0xff,0x1f,0x00,0x00,0xfc,0xff,0xff,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
15
#endif
16
17
#if defined(DISPLAY_16X2)
18
static byte duco_logo[] = {0x1E, 0x01, 0x1D, 0x05, 0x1D, 0x01, 0x1E, 0x00};
19
static byte check_mark[] = {0x00, 0x00, 0x00, 0x01, 0x02,0x14, 0x08, 0x00};
20
static byte kh[] = {0x08, 0x0A, 0x0C, 0x0A, 0x00, 0x0A, 0x0E, 0x0A};
21
static byte msec[] = {0x0A, 0x15, 0x11, 0x06, 0x08, 0x04, 0x02, 0x0C};
22
#endif
23
24
#if defined(DISPLAY_SSD1306)
25
void drawStrMultiline(const char *msg, int xloc, int yloc) {
26
//https://github.com/olikraus/u8g2/discussions/1479
27
int dspwidth = u8g2.getDisplayWidth();
28
int strwidth = 0;
29
char glyph[2]; glyph[1] = 0;
30
31
for (const char *ptr = msg, *lastblank = NULL; *ptr; ++ptr) {
32
while (xloc == 0 && *msg == ' ')
33
if (ptr == msg++) ++ptr;
34
35
glyph[0] = *ptr;
36
strwidth += u8g2.getStrWidth(glyph);
37
if (*ptr == ' ') lastblank = ptr;
38
else ++strwidth;
39
40
if (xloc + strwidth > dspwidth) {
41
int starting_xloc = xloc;
42
while (msg < (lastblank ? lastblank : ptr)) {
43
glyph[0] = *msg++;
44
xloc += u8g2.drawStr(xloc, yloc, glyph);
45
}
46
47
strwidth -= xloc - starting_xloc;
48
yloc += u8g2.getMaxCharHeight();
49
xloc = 0; lastblank = NULL;
50
}
51
}
52
while (*msg) {
53
glyph[0] = *msg++;
54
xloc += u8g2.drawStr(xloc, yloc, glyph);
55
}
56
}
57
#endif
58
59
#if defined(DISPLAY_SSD1306) || defined(DISPLAY_16X2)
60
void screen_setup() {
61
// Ran during setup()
62
// Abstraction layer: screen initialization
63
64
#if defined(DISPLAY_SSD1306)
65
u8g2.begin();
66
u8g2.clearBuffer();
67
u8g2.setFontMode(1);
68
u8g2.setBitmapMode(1);
69
u8g2.sendBuffer();
70
#endif
71
72
#if defined(DISPLAY_16X2)
73
lcd.begin(16, 2);
74
lcd.createChar(0, duco_logo);
75
lcd.createChar(1, check_mark);
76
lcd.createChar(2, kh);
77
lcd.createChar(3, msec);
78
lcd.home();
79
lcd.clear();
80
#endif
81
}
82
83
84
void display_boot() {
85
// Abstraction layer: compilation time, features, etc.
86
87
#if defined(DISPLAY_16X2)
88
lcd.clear();
89
#if defined(ESP8266)
90
lcd.print("ESP8266 ");
91
#elif defined(CONFIG_FREERTOS_UNICORE)
92
lcd.print("ESP32S2 ");
93
#else
94
lcd.print("ESP32 ");
95
#endif
96
#if defined(ESP8266)
97
lcd.print(String(ESP.getCpuFreqMHz()).c_str());
98
#else
99
lcd.print(String(getCpuFrequencyMhz()).c_str());
100
#endif
101
lcd.print(" MHz");
102
103
lcd.setCursor(0, 1);
104
lcd.print(__DATE__);
105
#endif
106
107
#if defined(DISPLAY_SSD1306)
108
u8g2.clearBuffer();
109
110
u8g2.setFont(u8g2_font_profont15_tr);
111
u8g2.setCursor(2, 13);
112
#if defined(ESP8266)
113
u8g2.print("ESP8266 ");
114
#elif defined(CONFIG_FREERTOS_UNICORE)
115
u8g2.print("ESP32S2/C3 ");
116
#else
117
u8g2.print("ESP32 ");
118
#endif
119
120
#if defined(ESP8266)
121
u8g2.print(String(ESP.getCpuFreqMHz()).c_str());
122
#else
123
u8g2.print(String(getCpuFrequencyMhz()).c_str());
124
#endif
125
u8g2.print(" MHz");
126
127
u8g2.setFont(u8g2_font_profont10_tr);
128
u8g2.drawLine(1, 27, 126, 27);
129
u8g2.setCursor(2, 24);
130
u8g2.print("Compiled ");
131
u8g2.print(__DATE__);
132
133
134
u8g2.drawStr(2, 37, "Features:");
135
u8g2.setCursor(2, 46);
136
String features_str = "OTA ";
137
#if defined(USE_LAN)
138
features_str += "LAN ";
139
#endif
140
#if defined(LED_BLINKING)
141
features_str += "Blink ";
142
#endif
143
#if defined(SERIAL_PRINTING)
144
features_str += "Serial ";
145
#endif
146
#if defined(WEB_DASHBOARD)
147
features_str += "Webserver ";
148
#endif
149
#if defined(DISPLAY_16X2)
150
features_str += "LCD16X2 ";
151
#endif
152
#if defined(DISPLAY_SSD1306)
153
features_str += "SSD1306 ";
154
#endif
155
#if defined(USE_INTERNAL_SENSOR)
156
features_str += "Int. sensor ";
157
#endif
158
#if defined(USE_DS18B20)
159
features_str += "DS18B20 ";
160
#endif
161
#if defined(USE_DHT)
162
features_str += "DHT ";
163
#endif
164
#if defined(USE_HSU07M)
165
features_str += "HSU07M ";
166
#endif
167
drawStrMultiline(features_str.c_str(), 2, 46);
168
u8g2.sendBuffer();
169
#endif
170
}
171
172
void display_info(String message) {
173
// Abstraction layer: info screens (setups)
174
175
#if defined(DISPLAY_SSD1306)
176
u8g2.clearBuffer();
177
u8g2.drawXBMP(-1, 3, 41, 45, image_duco_logo_big_bits);
178
u8g2.setFont(u8g2_font_t0_16b_tr);
179
#if defined(ESP8266)
180
u8g2.drawStr(42, 27, "ESP8266");
181
#elif defined(CONFIG_FREERTOS_UNICORE)
182
u8g2.drawStr(42, 27, "ESP32S2/C3");
183
#else
184
u8g2.drawStr(42, 27, "ESP32");
185
#endif
186
u8g2.setFont(u8g2_font_t0_13b_tr);
187
u8g2.drawStr(41, 14, "Duino-Coin");
188
u8g2.setFont(u8g2_font_6x10_tr);
189
u8g2.drawStr(98, 36, "MINER");
190
u8g2.setFont(u8g2_font_6x13_tr);
191
u8g2.drawStr(1, 60, message.c_str());
192
u8g2.setFont(u8g2_font_5x8_tr);
193
u8g2.drawStr(42, 46, "www.duinocoin.com");
194
u8g2.setFont(u8g2_font_4x6_tr);
195
u8g2.drawStr(116, 14, String(SOFTWARE_VERSION).c_str());
196
u8g2.sendBuffer();
197
#endif
198
199
#if defined(DISPLAY_16X2)
200
lcd.clear();
201
lcd.setCursor(0, 0);
202
lcd.write(0);
203
lcd.print(" Duino-Coin ");
204
lcd.print(SOFTWARE_VERSION);
205
lcd.setCursor(0, 1);
206
lcd.print(message);
207
#endif
208
}
209
210
211
void display_mining_results(String hashrate, String accepted_shares, String total_shares, String uptime, String node,
212
String difficulty, String sharerate, String ping, String accept_rate) {
213
// Ran after each found share
214
// Abstraction layer: displaying mining results
215
Serial.println("Displaying mining results");
216
217
#if defined(DISPLAY_SSD1306)
218
u8g2.clearBuffer();
219
u8g2.setFont(u8g2_font_profont10_tr);
220
u8g2.drawStr(67, 26, "kH");
221
if (hashrate.toFloat() < 100.0) {
222
u8g2.setFont(u8g2_font_profont29_tr);
223
u8g2.drawStr(2, 36, hashrate.c_str());
224
} else {
225
u8g2.setFont(u8g2_font_profont22_tr);
226
u8g2.drawStr(3, 35, hashrate.c_str());
227
}
228
229
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
230
u8g2.drawStr(52, 12, node.c_str());
231
232
u8g2.setFont(u8g2_font_t0_11_tr);
233
u8g2.drawStr(17, 47, (accepted_shares + "/" + total_shares).c_str());
234
u8g2.setFont(u8g2_font_5x7_tr);
235
u8g2.drawStr(88, 47, ("(" + accept_rate + "%)").c_str());
236
237
u8g2.setFont(u8g2_font_profont12_tr);
238
u8g2.drawStr(20, 12, (ping + "ms").c_str());
239
u8g2.drawStr(69, 36, "s");
240
241
u8g2.setFont(u8g2_font_6x13_tr);
242
u8g2.drawStr(125-u8g2.getStrWidth(uptime.c_str()), 61, uptime.c_str());
243
244
u8g2.drawStr(85, 38, sharerate.c_str());
245
u8g2.drawStr(85, 27, difficulty.c_str());
246
u8g2.drawLine(67, 28, 75, 28);
247
248
u8g2.drawXBMP(2, 38, 13, 10, image_check_contour_bits);
249
250
if (WiFi.RSSI() > -40) {
251
u8g2.drawXBMP(1, 0, 15, 16, image_network_4_bars_bits);
252
} else if (WiFi.RSSI() > -60) {
253
u8g2.drawXBMP(1, 0, 15, 16, image_network_3_bars_bits);
254
} else if (WiFi.RSSI() > -75) {
255
u8g2.drawXBMP(1, 0, 15, 16, image_network_2_bars_bits);
256
} else {
257
u8g2.drawXBMP(1, 0, 15, 16, image_network_1_bar_bits);
258
}
259
260
u8g2.setFont(u8g2_font_4x6_tr);
261
u8g2.drawStr(14, 61, String(WiFi.localIP().toString()).c_str());
262
u8g2.drawStr(14, 55, ("Duino-Coin " + String(SOFTWARE_VERSION)).c_str());
263
u8g2.drawXBMP(2, 11, 9, 50, image_duco_logo_bits);
264
u8g2.drawStr(111, 27, "diff");
265
u8g2.drawStr(107, 38, "shr/s");
266
267
u8g2.sendBuffer();
268
#endif
269
270
#if defined(DISPLAY_16X2)
271
lcd.clear();
272
lcd.setCursor(0,0);
273
lcd.print(hashrate);
274
lcd.setCursor(4,0);
275
lcd.write(2); // kh
276
277
lcd.setCursor(7, 0);
278
lcd.print(difficulty);
279
lcd.print(" diff");
280
281
lcd.setCursor(0, 1);
282
lcd.write(1); // checkmark
283
lcd.print(accepted_shares);
284
285
lcd.setCursor(7, 1);
286
lcd.print(ping);
287
lcd.write(3); // ms
288
289
lcd.setCursor(12, 1);
290
lcd.print(sharerate);
291
lcd.print("s");
292
#endif
293
}
294
#endif
295
296
#endif
297
298