forked from sle118/FreeTouchDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawHelper.cpp
367 lines (343 loc) · 9.11 KB
/
DrawHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "DrawHelper.h"
#include "globals.hpp"
#include "Menu.h"
#include "MenuNavigation.h"
#include <stdarg.h>
#include "esp_bt_main.h" // Additional BLE functionaity
#include "esp_bt_device.h" // Additional BLE functionaity
#include "Tiny3x3a2pt7b.h"
#include "Storage.h"
#include "ImageCache.h"
namespace FreeTouchDeck
{
using namespace fs;
TFT_eSPI tft = TFT_eSPI();
/* ------------- Print an error message the TFT screen ----------------
Purpose: This function prints an message to the TFT screen on a black
background.
Input : const char * message
const char * module
Output : none
Note : none
*/
std::vector<std::string> Messages;
void drawErrorMessage(bool stop, const char *module, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char *message = NULL;
const char *printmsg = fmt;
size_t msg_size = 0;
msg_size = vsnprintf(NULL, 0, fmt, args) + 4;
message = (char *)malloc_fn(msg_size);
if (!message)
{
ESP_LOGE(module, "Could not allocate %d bytes of memory to display message on screen", msg_size);
stop = true;
tft.println(fmt);
}
else
{
vsprintf(message, fmt, args);
printmsg = message;
LOC_LOGE(module, "%s", message);
displayInit();
ClearScreen();
tft.println(printmsg);
FREE_AND_NULL(message);
}
va_end(args);
TFTPrintMemInfo();
while (stop)
{
if (isTouched())
{
tft.println("Restarting...");
delay(1000);
ESP.restart();
}
yield();
}
// hold a bit to let user read message
// if it's not a full stop
delay(5000);
}
/* ------------- Print an error message the TFT screen ----------------
Purpose: This function prints an message to the TFT screen on a black
background.
Input : String message
Output : none
Note : none
*/
void drawErrorMessage(String message)
{
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 20);
SetSmallestFont(1);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println(message);
}
/**
* @brief This function gets the Bluetooth device address and prints it to the serial monitor
and the TFT screen in a 6 byte format, seperating each byte by ":".
*
* @param none
*
* @return none
*
* @note e.g. 00:11:22:33:22:EE
*/
void printDeviceAddress()
{
const uint8_t *point = esp_bt_dev_get_address();
for (int i = 0; i < 6; i++)
{
char str[3];
sprintf(str, "%02X", (int)point[i]);
//Serial.print(str);
tft.print(str);
if (i < 5)
{
// Serial.print(":");
tft.print(":");
}
}
}
std::vector<const GFXfont *> FontsList;
const GFXfont *CurrentFont = LABEL_FONT;
const GFXfont *DefaultFont = LABEL_FONT;
const GFXfont *GetCurrentFont()
{
return CurrentFont;
}
bool SetFont(const GFXfont *newFont)
{
CurrentFont = newFont;
tft.setFreeFont(newFont);
}
bool SetDefaultFont()
{
SetFont(DefaultFont);
}
void InitFontsTable()
{
// FontsList.push_back(&Tiny3x3a2pt7b);
FontsList.push_back(&TomThumb);
LOC_LOGV(module, "Pushed font TomThumb to list with address %d ", &TomThumb);
// FontsList.push_back(&FreeSans9pt7b);
// LOC_LOGV(module,"Pushed font FreeSans9pt7b to list with address %d ",&FreeSans9pt7b);
FontsList.push_back(&FreeSansBold9pt7b);
LOC_LOGV(module, "Pushed font FreeSansBold9pt7b to list with address %d ", &FreeSansBold9pt7b);
// FontsList.push_back(&FreeSans12pt7b);
// LOC_LOGV(module,"Pushed font FreeSans12pt7b to list with address %d ",&FreeSans12pt7b);
FontsList.push_back(&FreeSansBold12pt7b);
LOC_LOGV(module, "Pushed font FreeSansBold12pt7b to list with address %d ", &FreeSansBold12pt7b);
SetDefaultFont();
}
bool SetSmallestFont(int whichOne)
{
std::vector<const GFXfont *>::iterator it = FontsList.begin();
std::advance(it, whichOne);
if (it != FontsList.end())
{
SetFont(*it);
}
}
bool SetLargestFont()
{
SetFont(FontsList.back());
}
template <typename iter, typename t>
iter iterator_from_ptr(iter first, iter last, t ptr)
{
while (first != last)
{
if (*first == ptr)
{
LOC_LOGV(module, "%d == %d. Returning iterator at this position", (uint32_t)*first, (uint32_t)ptr);
return first;
}
LOC_LOGV(module, "%d != %d. Getting next", (uint32_t)*first, (uint32_t)ptr);
++first;
}
LOC_LOGV(module, "Returning last pointer ");
return last;
}
bool SetSmallerFont()
{
auto newFont = iterator_from_ptr(FontsList.begin(), FontsList.end(), CurrentFont);
if (newFont != FontsList.end())
{
LOC_LOGV(module, "Found current font. Getting the next one");
std::advance(newFont, -1);
}
if (newFont != FontsList.end())
{
LOC_LOGD(module, "Setting new font");
SetFont(*newFont);
return true;
}
else
{
LOC_LOGD(module, "Already using smallest font");
}
return false;
}
bool SetLargerFont()
{
auto newFont = iterator_from_ptr(FontsList.begin(), FontsList.end(), CurrentFont);
if (newFont != FontsList.end())
{
LOC_LOGV(module, "Found current font. Getting the next one");
newFont++;
}
if (newFont != FontsList.end())
{
LOC_LOGD(module, "Setting new font");
SetFont(*newFont);
return true;
}
else
{
LOC_LOGD(module, "Already using largest font");
}
return false;
}
void ClearScreen()
{
SetSmallestFont(1);
tft.setCursor(0, tft.fontHeight() + 1);
tft.fillScreen(generalconfig.backgroundColour);
tft.setTextColor(generalconfig.DefaultTextColor, generalconfig.backgroundColour);
}
void displayInit()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
// --------------- Init Display -------------------------
LOC_LOGI(module, "Initializing display");
// Initialise the TFT screen
tft.init();
// Set the rotation
tft.setRotation(generalconfig.screenrotation);
// Clear the screen
// Setup the Font used for plain text
InitFontsTable();
ClearScreen();
powerInit();
LOC_LOGI(module, "Screen size is %dx%d", tft.width(), tft.height());
}
void PrintScreenMessage(bool clear, const char *message, ...)
{
static std::list<std::string> screenbuffer;
displayInit();
char *formatted_message = NULL;
size_t msg_size = 0;
va_list args;
va_start(args, message);
msg_size = vsnprintf(NULL, 0, message, args) + 1;
formatted_message = (char *)malloc_fn(msg_size);
if (!formatted_message)
{
ESP_LOGE(module, "Could not allocate %d bytes of memory to display message on screen", msg_size);
tft.printf("Could not allocate %d bytes of memory to display message on screen\n", msg_size);
tft.println(message);
return;
}
vsprintf(formatted_message, message, args);
LOC_LOGI(module, "%s", formatted_message);
va_end(args);
if (clear)
{
screenbuffer.clear();
ClearScreen();
}
screenbuffer.push_back(formatted_message);
FREE_AND_NULL(formatted_message);
if ((tft.getCursorY() + tft.fontHeight() ) > tft.height())
{
// This is where we start removing top lines
ClearScreen();
screenbuffer.pop_front();
for (auto m : screenbuffer)
{
tft.println(m.c_str());
}
}
else
{
tft.println(screenbuffer.back().c_str());
}
}
void DrawSplash()
{
LOC_LOGD(module, "Loading splash screen bitmap.");
auto splash = ImageCache::GetImage("freetouchdeck_logo.jpg");
if (splash->valid)
{
LOC_LOGD(module, "splash screen bitmap loaded. Drawing");
splash->Draw(tft.width() / 2, tft.height() / 2, false);
}
else
{
LOC_LOGW(module, "Unable to draw the splash screen.");
}
}
/**
* @brief This functions accepts a HTML including the # colour code
(eg. #FF00FF) and returns it in RGB888 format.
*
* @param *html char (including #)
*
* @return unsigned long
*
* @note none
*/
unsigned long convertHTMLtoRGB888(const char *html)
{
const char *hex = html; // remove the #
if (ISNULLSTRING(html))
{
LOC_LOGE(module, "Invalid color. Null string passed");
return 0;
}
if (html[0] == '#')
{
hex += 1;
}
return (unsigned long)strtoul(hex, NULL, 16);
}
/**
* @brief This function converts RGB888 to RGB565.
*
* @param rgb unsigned long
*
* @return unsigned int
*
* @note none
*/
unsigned int convertHTMLRGB888ToRGB565(const char *html)
{
unsigned long rgb = convertHTMLtoRGB888(html);
return convertRGB888ToRGB565(rgb);
}
unsigned long convertRGB656ToRGB888(unsigned long rgb565)
{
return tft.color16to24(rgb565);
}
const char *convertRGB656ToHTMLRGB888(unsigned long rgb565)
{
static char buffer[8] = {0};
snprintf(buffer, sizeof(buffer), "#%06x", convertRGB656ToRGB888(rgb565));
return buffer;
}
const char *convertRGB888oHTMLRGB888(unsigned long rgb888)
{
static char buffer[8] = {0};
snprintf(buffer, sizeof(buffer), "#%06x", rgb888);
return buffer;
}
}