FabGL
ESP32 Display Controller and Graphics Library
fabutils.h
Go to the documentation of this file.
1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27
28#pragma once
29
30
39#include "freertos/FreeRTOS.h"
40#include "freertos/semphr.h"
41
42#include <driver/adc.h>
43#include <esp_system.h>
44#include "sdmmc_cmd.h"
45#include "soc/frc_timer_reg.h"
46
47
48namespace fabgl {
49
50
51// manage IDF versioning
52#ifdef ESP_IDF_VERSION
53 #define FABGL_ESP_IDF_VERSION_VAL ESP_IDF_VERSION_VAL
54 #define FABGL_ESP_IDF_VERSION ESP_IDF_VERSION
55#else
56 #define FABGL_ESP_IDF_VERSION_VAL(major, minor, patch) ((major << 16) | (minor << 8) | (patch))
57 #define FABGL_ESP_IDF_VERSION FABGL_ESP_IDF_VERSION_VAL(0, 0, 0)
58#endif
59
60
61
62#define GPIO_UNUSED (GPIO_NUM_MAX)
63#define GPIO_AUTO ((gpio_num_t)(GPIO_NUM_MAX + 1))
64
65
67// PSRAM_HACK
68// ESP32 Revision 1 has following bug: "When the CPU accesses external SRAM through cache, under certain conditions read and write errors occur"
69// A workaround is done by the compiler, so whenever PSRAM is enabled the workaround is automatically applied (-mfix-esp32-psram-cache-issue compiler option).
70// Unfortunately this workaround reduces performance, even when SRAM is not access, like in VGAXController interrupt handler. This is unacceptable for the interrupt routine.
71// In order to confuse the compiler and prevent the workaround from being applied, a "nop" is added between load and store instructions (PSRAM_HACK).
72
73#ifdef ARDUINO
74 #ifdef BOARD_HAS_PSRAM
75 #define FABGL_NEED_PSRAM_DISABLE_HACK
76 #endif
77#else
78 #ifdef CONFIG_SPIRAM_SUPPORT
79 #define FABGL_NEED_PSRAM_DISABLE_HACK
80 #endif
81#endif
82
83#ifdef FABGL_NEED_PSRAM_DISABLE_HACK
84 #define PSRAM_HACK asm(" nop")
85#else
86 #define PSRAM_HACK
87#endif
88
89// ESP32 PSRAM bug workaround (use when the library is NOT compiled with PSRAM hack enabled)
90// Place between a write and a read PSRAM operation (write->ASM_MEMW->read), not viceversa
91#define ASM_MEMW asm(" MEMW");
92
93#define ASM_NOP asm(" NOP");
94
95#define PSRAM_WORKAROUND1 asm(" nop;nop;nop;nop");
96#define PSRAM_WORKAROUND2 asm(" memw");
97
98
99
100
102
103
104#define TORAD(a) ((a) * M_PI / 180.)
105
106
107// Integer square root by Halleck's method, with Legalize's speedup
108int isqrt (int x);
109
110
111template <typename T>
112const T & tmax(const T & a, const T & b)
113{
114 return (a < b) ? b : a;
115}
116
117
118constexpr auto imax = tmax<int>;
119
120
121template <typename T>
122const T & tmin(const T & a, const T & b)
123{
124 return !(b < a) ? a : b;
125}
126
127
128constexpr auto imin = tmin<int>;
129
130
131
132template <typename T>
133const T & tclamp(const T & v, const T & lo, const T & hi)
134{
135 return (v < lo ? lo : (v > hi ? hi : v));
136}
137
138
139constexpr auto iclamp = tclamp<int>;
140
141
142template <typename T>
143const T & twrap(const T & v, const T & lo, const T & hi)
144{
145 return (v < lo ? hi : (v > hi ? lo : v));
146}
147
148
149template <typename T>
150void tswap(T & v1, T & v2)
151{
152 T t = v1;
153 v1 = v2;
154 v2 = t;
155}
156
157
158constexpr auto iswap = tswap<int>;
159
160
161template <typename T>
162T moveItems(T dest, T src, size_t n)
163{
164 T pd = dest;
165 T ps = src;
166 if (pd != ps) {
167 if (ps < pd)
168 for (pd += n, ps += n; n--;)
169 *--pd = *--ps;
170 else
171 while (n--)
172 *pd++ = *ps++;
173 }
174 return dest;
175}
176
177
178void rgb222_to_hsv(int R, int G, int B, double * h, double * s, double * v);
179
180
181inline uint16_t changeEndiannesWord(uint16_t value)
182{
183 return ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
184}
185
186
187inline uint32_t changeEndiannesDWord(uint32_t value)
188{
189 return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | ((value & 0xff000000) >> 24);
190}
191
192
193struct APLLParams {
194 uint8_t sdm0;
195 uint8_t sdm1;
196 uint8_t sdm2;
197 uint8_t o_div;
198};
199
200void APLLCalcParams(double freq, APLLParams * params, uint8_t * a, uint8_t * b, double * out_freq, double * error);
201
202int calcI2STimingParams(int sampleRate, int * outA, int * outB, int * outN, int * outM);
203
204
206
207
213struct Point {
214 int16_t X;
215 int16_t Y;
217 Point() : X(0), Y(0) { }
218 Point(int X_, int Y_) : X(X_), Y(Y_) { }
219
220 Point add(Point const & p) const { return Point(X + p.X, Y + p.Y); }
221 Point sub(Point const & p) const { return Point(X - p.X, Y - p.Y); }
222 Point neg() const { return Point(-X, -Y); }
223 bool operator==(Point const & r) { return X == r.X && Y == r.Y; }
224 bool operator!=(Point const & r) { return X != r.X || Y != r.Y; }
225} __attribute__ ((packed));
226
227
231struct Size {
232 int16_t width;
233 int16_t height;
235 Size() : width(0), height(0) { }
236 Size(int width_, int height_) : width(width_), height(height_) { }
237 bool operator==(Size const & r) { return width == r.width && height == r.height; }
238 bool operator!=(Size const & r) { return width != r.width || height != r.height; }
239} __attribute__ ((packed));
240
241
242
248struct Rect {
249 int16_t X1;
250 int16_t Y1;
251 int16_t X2;
252 int16_t Y2;
254 Rect() : X1(0), Y1(0), X2(0), Y2(0) { }
255 Rect(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) { }
256 Rect(Rect const & r) { X1 = r.X1; Y1 = r.Y1; X2 = r.X2; Y2 = r.Y2; }
257
258 bool operator==(Rect const & r) { return X1 == r.X1 && Y1 == r.Y1 && X2 == r.X2 && Y2 == r.Y2; }
259 bool operator!=(Rect const & r) { return X1 != r.X1 || Y1 != r.Y1 || X2 != r.X2 || Y2 != r.Y2; }
260 Point pos() const { return Point(X1, Y1); }
261 Size size() const { return Size(X2 - X1 + 1, Y2 - Y1 + 1); }
262 int width() const { return X2 - X1 + 1; }
263 int height() const { return Y2 - Y1 + 1; }
264 Rect translate(int offsetX, int offsetY) const { return Rect(X1 + offsetX, Y1 + offsetY, X2 + offsetX, Y2 + offsetY); }
265 Rect translate(Point const & offset) const { return Rect(X1 + offset.X, Y1 + offset.Y, X2 + offset.X, Y2 + offset.Y); }
266 Rect move(Point const & position) const { return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
267 Rect move(int x, int y) const { return Rect(x, y, x + width() - 1, y + height() - 1); }
268 Rect shrink(int value) const { return Rect(X1 + value, Y1 + value, X2 - value, Y2 - value); }
269 Rect hShrink(int value) const { return Rect(X1 + value, Y1, X2 - value, Y2); }
270 Rect vShrink(int value) const { return Rect(X1, Y1 + value, X2, Y2 - value); }
271 Rect resize(int width, int height) const { return Rect(X1, Y1, X1 + width - 1, Y1 + height - 1); }
272 Rect resize(Size size) const { return Rect(X1, Y1, X1 + size.width - 1, Y1 + size.height - 1); }
273 Rect intersection(Rect const & rect) const;
274 bool intersects(Rect const & rect) const { return X1 <= rect.X2 && X2 >= rect.X1 && Y1 <= rect.Y2 && Y2 >= rect.Y1; }
275 bool contains(Rect const & rect) const { return (rect.X1 >= X1) && (rect.Y1 >= Y1) && (rect.X2 <= X2) && (rect.Y2 <= Y2); }
276 bool contains(Point const & point) const { return point.X >= X1 && point.Y >= Y1 && point.X <= X2 && point.Y <= Y2; }
277 bool contains(int x, int y) const { return x >= X1 && y >= Y1 && x <= X2 && y <= Y2; }
278 Rect merge(Rect const & rect) const;
279} __attribute__ ((packed));
280
281
282
287 uint8_t left : 1;
288 uint8_t middle : 1;
289 uint8_t right : 1;
291 MouseButtons() : left(0), middle(0), right(0) { }
292};
293
294
295
300 int16_t X;
301 int16_t Y;
302 int8_t wheelDelta;
305 MouseStatus() : X(0), Y(0), wheelDelta(0) { }
306};
307
308
309
310#define FONTINFOFLAGS_ITALIC 1
311#define FONTINFOFLAGS_UNDERLINE 2
312#define FONTINFODLAFS_STRIKEOUT 4
313#define FONTINFOFLAGS_VARWIDTH 8
314
315
316struct FontInfo {
317 uint8_t pointSize;
318 uint8_t width; // used only for fixed width fonts (FONTINFOFLAGS_VARWIDTH = 0)
319 uint8_t height;
320 uint8_t ascent;
321 uint8_t inleading;
322 uint8_t exleading;
323 uint8_t flags;
324 uint16_t weight;
325 uint16_t charset;
326 // when FONTINFOFLAGS_VARWIDTH = 0:
327 // data[] contains 256 items each one representing a single character
328 // when FONTINFOFLAGS_VARWIDTH = 1:
329 // data[] contains 256 items each one representing a single character. First byte contains the
330 // character width. "chptr" is filled with an array of pointers to the single characters.
331 uint8_t const * data;
332 uint32_t const * chptr; // used only for variable width fonts (FONTINFOFLAGS_VARWIDTH = 1)
333 uint16_t codepage;
334};
335
336
337
338
340// TimeOut
341
342
343struct TimeOut {
344 TimeOut();
345
346 // -1 means "infinite", never times out
347 bool expired(int valueMS);
348
349private:
350 int64_t m_start;
351};
352
353
354
356// Stack
357
358
359template <typename T>
360struct StackItem {
361 StackItem * next;
362 T item;
363 StackItem(StackItem * next_, T const & item_) : next(next_), item(item_) { }
364};
365
366template <typename T>
367class Stack {
368public:
369 Stack() : m_items(nullptr) { }
370 bool isEmpty() { return m_items == nullptr; }
371 void push(T const & value) {
372 m_items = new StackItem<T>(m_items, value);
373 }
374 T pop() {
375 if (m_items) {
376 StackItem<T> * iptr = m_items;
377 m_items = iptr->next;
378 T r = iptr->item;
379 delete iptr;
380 return r;
381 } else
382 return T();
383 }
384 int count() {
385 int r = 0;
386 for (auto i = m_items; i; i = i->next)
387 ++r;
388 return r;
389 }
390private:
391 StackItem<T> * m_items;
392};
393
394
395
397// Delegate
398
399template <typename ...Params>
400struct Delegate {
401
402 // empty constructor
403 Delegate() : m_func(nullptr) {
404 }
405
406 // denied copy
407 Delegate(const Delegate & c) = delete;
408
409 // construct from lambda
410 template <typename Func>
411 Delegate(Func f) : Delegate() {
412 *this = f;
413 }
414
415 ~Delegate() {
416 cleanUp();
417 }
418
419 // assignment operator from Func
420 template <typename Func>
421 void operator=(Func f) {
422 cleanUp();
423 m_closure = [] (void * func, const Params & ...params) -> void { (*(Func *)func)(params...); };
424 m_func = heap_caps_malloc(sizeof(Func), MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
425 moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f, sizeof(Func) / sizeof(uint32_t));
426 }
427
428 // denied assignment from Delegate
429 void operator=(const Delegate&) = delete;
430
431 void operator()(const Params & ...params) {
432 if (m_func)
433 m_closure(m_func, params...);
434 }
435
436private:
437
438 void (*m_closure)(void * func, const Params & ...params);
439 void * m_func;
440
441 void cleanUp() {
442 if (m_func)
443 heap_caps_free(m_func);
444 }
445};
446
447
448
450// StringList
451
452class StringList {
453
454public:
455 StringList();
456 ~StringList();
457 int append(char const * str);
458 int appendFmt(const char *format, ...);
459 void append(char const * strlist[], int count);
460 void appendSepList(char const * strlist, char separator);
461 void insert(int index, char const * str);
462 void set(int index, char const * str);
463 void remove(int index);
464 int count() { return m_count; }
465 char const * get(int index) { return m_items[index]; }
466 void clear();
467 void takeStrings();
468 void select(int index, bool value);
469 void deselectAll();
470 bool selected(int index);
471 int getFirstSelected();
472 void copyFrom(StringList const & src);
473 void copySelectionMapFrom(StringList const & src);
474
475private:
476 void checkAllocatedSpace(int requiredItems);
477
478 char const * * m_items;
479
480 // each 32 bit word can select up to 32 items, one bit per item
481 uint32_t * m_selMap;
482
483 // If true (default is false) all strings added (append/insert/set) are copied.
484 // Strings will be released when no more used (destructor, clear(), etc...).
485 // This flag is permanently switched to True by takeStrings() call.
486 bool m_ownStrings;
487
488 uint16_t m_count; // actual items
489 uint16_t m_allocated; // allocated items
490
491};
492
493
495// LightMemoryPool
496// Each allocated block starts with a two bytes header (int16_t). Bit 15 is allocation flag (0=free, 1=allocated).
497// Bits 14..0 represent the block size.
498// The maximum size of a block is 32767 bytes.
499// free() just marks the block header as free.
500
501class LightMemoryPool {
502public:
503 LightMemoryPool(int poolSize);
504 ~LightMemoryPool();
505 void * alloc(int size);
506 void free(void * mem) { if (mem) markFree((uint8_t*)mem - m_mem - 2); }
507
508 bool memCheck();
509 int totFree(); // get total free memory
510 int totAllocated(); // get total allocated memory
511 int largestFree();
512
513private:
514
515 void mark(int pos, int16_t size, bool allocated);
516 void markFree(int pos) { m_mem[pos + 1] &= 0x7f; }
517 int16_t getSize(int pos);
518 bool isFree(int pos);
519
520 uint8_t * m_mem;
521 int m_poolSize;
522};
523
524
525
527// FileBrowser
528
529
533struct DirItem {
534 bool isDir;
535 char const * name;
536};
537
538
542enum class DriveType {
543 None,
544 SPIFFS,
545 SDCard,
546};
547
548
555public:
556
557 FileBrowser();
558
559 FileBrowser(char const * path);
560
561 ~FileBrowser();
562
570 bool setDirectory(const char * path);
571
577 void changeDirectory(const char * subdir);
578
584 bool reload();
585
591 char const * directory() { return m_dir; }
592
598 int count() { return m_count; }
599
607 DirItem const * get(int index) { return m_items + index; }
608
619 bool exists(char const * name, bool caseSensitive = true);
620
630 bool filePathExists(char const * filepath);
631
639 size_t fileSize(char const * name);
640
654 bool fileCreationDate(char const * name, int * year, int * month, int * day, int * hour, int * minutes, int * seconds);
655
669 bool fileUpdateDate(char const * name, int * year, int * month, int * day, int * hour, int * minutes, int * seconds);
670
684 bool fileAccessDate(char const * name, int * year, int * month, int * day, int * hour, int * minutes, int * seconds);
685
691 void setSorted(bool value);
692
693 void setIncludeHiddenFiles(bool value) { m_includeHiddenFiles = value; }
694
700 void makeDirectory(char const * dirname);
701
709 void remove(char const * name);
710
717 void rename(char const * oldName, char const * newName);
718
727 bool truncate(char const * name, size_t size);
728
734 char * createTempFilename();
735
745 int getFullPath(char const * name, char * outPath = nullptr, int maxlen = 0);
746
755 FILE * openFile(char const * filename, char const * mode);
756
763
771 static DriveType getDriveType(char const * path);
772
790 static bool format(DriveType driveType, int drive);
791
811 static bool mountSDCard(bool formatOnFail, char const * mountPath, size_t maxFiles = 4, int allocationUnitSize = 16 * 1024, int MISO = 16, int MOSI = 17, int CLK = 14, int CS = 13);
812
818 static bool remountSDCard();
819
820 static bool mountedSDCard() { return s_SDCardMounted; }
821
825 static void unmountSDCard();
826
841 static bool mountSPIFFS(bool formatOnFail, char const * mountPath, size_t maxFiles = 4);
842
848 static bool remountSPIFFS();
849
853 static void unmountSPIFFS();
854
877 static bool getFSInfo(DriveType driveType, int drive, int64_t * total, int64_t * used);
878
879private:
880
881 void clear();
882 int countDirEntries(int * namesLength);
883
884 // SPIFFS static infos
885 static bool s_SPIFFSMounted;
886 static char const * s_SPIFFSMountPath;
887 static size_t s_SPIFFSMaxFiles;
888
889 // SD Card static infos
890 static bool s_SDCardMounted;
891 static char const * s_SDCardMountPath;
892 static size_t s_SDCardMaxFiles;
893 static int s_SDCardAllocationUnitSize;
894 static int8_t s_SDCardMISO;
895 static int8_t s_SDCardMOSI;
896 static int8_t s_SDCardCLK;
897 static int8_t s_SDCardCS;
898 static sdmmc_card_t * s_SDCard;
899
900 char * m_dir;
901 int m_count;
902 DirItem * m_items;
903 bool m_sorted;
904 bool m_includeHiddenFiles;
905 char * m_namesStorage;
906};
907
908
909
910
912
913
914
915bool clipLine(int & x1, int & y1, int & x2, int & y2, Rect const & clipRect, bool checkOnly);
916
917
918void removeRectangle(Stack<Rect> & rects, Rect const & mainRect, Rect const & rectToRemove);
919
920
921bool calcParity(uint8_t v);
922
923// why these? this is like heap_caps_malloc with MALLOC_CAP_32BIT. Unfortunately
924// heap_caps_realloc crashes, so we need this workaround.
925void * realloc32(void * ptr, size_t size);
926void free32(void * ptr);
927
928
929inline gpio_num_t int2gpio(int gpio)
930{
931 return gpio == -1 ? GPIO_UNUSED : (gpio_num_t)gpio;
932}
933
934
935// converts 0..9 -> '0'..'9', 10..15 -> 'a'..'f'
936inline char digit2hex(int digit)
937{
938 return digit < 10 ? '0' + digit : 'a' + digit - 10;
939}
940
941
942// converts '0'..'9' -> 0..9, 'a'..'f' -> 10..15
943inline int hex2digit(char hex)
944{
945 return hex < 'a' ? hex - '0' : hex - 'a' + 10;
946}
947
948
949// milliseconds to FreeRTOS ticks.
950// ms = -1 => maximum delay (portMAX_DELAY)
951uint32_t msToTicks(int ms);
952
953
957enum class ChipPackage {
958 Unknown,
963};
964
965
966ChipPackage getChipPackage();
967
968inline __attribute__((always_inline)) uint32_t getCycleCount() {
969 uint32_t ccount;
970 __asm__ __volatile__(
971 "esync \n\t"
972 "rsr %0, ccount \n\t"
973 : "=a" (ccount)
974 );
975 return ccount;
976}
977
984void replacePathSep(char * path, char newSep);
985
986
987adc1_channel_t ADC1_GPIO2Channel(gpio_num_t gpio);
988
989
990void esp_intr_alloc_pinnedToCore(int source, int flags, intr_handler_t handler, void * arg, intr_handle_t * ret_handle, int core);
991
992
993// mode: GPIO_MODE_DISABLE,
994// GPIO_MODE_INPUT,
995// GPIO_MODE_OUTPUT,
996// GPIO_MODE_OUTPUT_OD (open drain),
997// GPIO_MODE_INPUT_OUTPUT_OD (open drain),
998// GPIO_MODE_INPUT_OUTPUT
999void configureGPIO(gpio_num_t gpio, gpio_mode_t mode);
1000
1001
1002uint32_t getApbFrequency();
1003
1004uint32_t getCPUFrequencyMHz();
1005
1006
1007
1010
1011
1012// FRC1 timer has 23 bits (8388608 values)
1013constexpr int FRC1TimerMax = 8388607;
1014
1015
1016// prescaler: FRC_TIMER_PRESCALER_1, FRC_TIMER_PRESCALER_16, FRC_TIMER_PRESCALER_256
1017// 80Mhz / prescaler = timer frequency
1018inline void FRC1Timer_init(int prescaler)
1019{
1020 REG_WRITE(FRC_TIMER_LOAD_REG(0), 0);
1021 REG_WRITE(FRC_TIMER_CTRL_REG(0), prescaler | FRC_TIMER_ENABLE);
1022}
1023
1024
1025inline uint32_t FRC1Timer()
1026{
1027 return FRC1TimerMax - REG_READ(FRC_TIMER_COUNT_REG(0)); // make timer count up
1028}
1029
1030
1031
1033// AutoSemaphore
1034
1035struct AutoSemaphore {
1036 AutoSemaphore(SemaphoreHandle_t mutex) : m_mutex(mutex) { xSemaphoreTake(m_mutex, portMAX_DELAY); }
1037 ~AutoSemaphore() { xSemaphoreGive(m_mutex); }
1038private:
1039 SemaphoreHandle_t m_mutex;
1040};
1041
1042
1043
1045// CoreUsage
1046
1051
1052 static int busiestCore() { return s_busiestCore; }
1053 static int quietCore() { return s_busiestCore != -1 ? s_busiestCore ^ 1 : -1; }
1054 static void setBusiestCore(int core) { s_busiestCore = core; }
1055
1056 private:
1057 static int s_busiestCore; // 0 = core 0, 1 = core 1 (default is FABGLIB_VIDEO_CPUINTENSIVE_TASKS_CORE)
1058};
1059
1060
1061
1063// VideoMode
1064
1068enum class VideoMode {
1069 None,
1070 VGA,
1071 CVBS,
1072 I2C,
1073 SPI,
1074};
1075
1076
1081
1082 static VideoMode get() { return s_videoMode; }
1083 static void set(VideoMode value) { s_videoMode = value; }
1084
1085 private:
1086 static VideoMode s_videoMode;
1087};
1088
1089
1090
1092
1093
1356 VK_aelig,
1364 // Japanese layout support
1366 VK_MUHENKAN,
1367 VK_HENKAN,
1368 VK_KATAKANA_HIRAGANA_ROMAJI,
1369 VK_HANKAKU_ZENKAKU_KANJI,
1370 VK_SHIFT_0,
1371
1373 VK_LAST, // marks the last virtual key
1374
1375};
1376
1377
1383 uint8_t down;
1384 uint8_t scancode[8];
1385 uint8_t ASCII;
1386 uint8_t CTRL : 1;
1387 uint8_t LALT : 1;
1388 uint8_t RALT : 1;
1389 uint8_t SHIFT : 1;
1390 uint8_t GUI : 1;
1391 uint8_t CAPSLOCK : 1;
1392 uint8_t NUMLOCK : 1;
1393 uint8_t SCROLLLOCK : 1;
1394};
1395
1396
1397
1399// Virtual keys helpers
1400
1401inline bool isSHIFT(VirtualKey value)
1402{
1403 return value == VK_LSHIFT || value == VK_RSHIFT;
1404}
1405
1406
1407inline bool isALT(VirtualKey value)
1408{
1409 return value == VK_LALT || value == VK_RALT;
1410}
1411
1412
1413inline bool isCTRL(VirtualKey value)
1414{
1415 return value == VK_LCTRL || value == VK_RCTRL;
1416}
1417
1418
1419inline bool isGUI(VirtualKey value)
1420{
1421 return value == VK_LGUI || value == VK_RGUI;
1422}
1423
1424
1425
1427// ASCII control characters
1428
1429#define ASCII_NUL 0x00 // Null
1430#define ASCII_SOH 0x01 // Start of Heading
1431#define ASCII_CTRLA 0x01 // CTRL-A
1432#define ASCII_STX 0x02 // Start of Text
1433#define ASCII_CTRLB 0x02 // CTRL-B
1434#define ASCII_ETX 0x03 // End Of Text
1435#define ASCII_CTRLC 0x03 // CTRL-C
1436#define ASCII_EOT 0x04 // End Of Transmission
1437#define ASCII_CTRLD 0x04 // CTRL-D
1438#define ASCII_ENQ 0x05 // Enquiry
1439#define ASCII_CTRLE 0x05 // CTRL-E
1440#define ASCII_ACK 0x06 // Acknowledge
1441#define ASCII_CTRLF 0x06 // CTRL-F
1442#define ASCII_BEL 0x07 // Bell
1443#define ASCII_CTRLG 0x07 // CTRL-G
1444#define ASCII_BS 0x08 // Backspace
1445#define ASCII_CTRLH 0x08 // CTRL-H
1446#define ASCII_HT 0x09 // Horizontal Tab
1447#define ASCII_TAB 0x09 // Horizontal Tab
1448#define ASCII_CTRLI 0x09 // CTRL-I
1449#define ASCII_LF 0x0A // Line Feed
1450#define ASCII_CTRLJ 0x0A // CTRL-J
1451#define ASCII_VT 0x0B // Vertical Tab
1452#define ASCII_CTRLK 0x0B // CTRL-K
1453#define ASCII_FF 0x0C // Form Feed
1454#define ASCII_CTRLL 0x0C // CTRL-L
1455#define ASCII_CR 0x0D // Carriage Return
1456#define ASCII_CTRLM 0x0D // CTRL-M
1457#define ASCII_SO 0x0E // Shift Out
1458#define ASCII_CTRLN 0x0E // CTRL-N
1459#define ASCII_SI 0x0F // Shift In
1460#define ASCII_CTRLO 0x0F // CTRL-O
1461#define ASCII_DLE 0x10 // Data Link Escape
1462#define ASCII_CTRLP 0x10 // CTRL-P
1463#define ASCII_DC1 0x11 // Device Control 1
1464#define ASCII_CTRLQ 0x11 // CTRL-Q
1465#define ASCII_XON 0x11 // Transmission On
1466#define ASCII_DC2 0x12 // Device Control 2
1467#define ASCII_CTRLR 0x12 // CTRL-R
1468#define ASCII_DC3 0x13 // Device Control 3
1469#define ASCII_XOFF 0x13 // Transmission Off
1470#define ASCII_CTRLS 0x13 // CTRL-S
1471#define ASCII_DC4 0x14 // Device Control 4
1472#define ASCII_CTRLT 0x14 // CTRL-T
1473#define ASCII_NAK 0x15 // Negative Acknowledge
1474#define ASCII_CTRLU 0x15 // CTRL-U
1475#define ASCII_SYN 0x16 // Synchronous Idle
1476#define ASCII_CTRLV 0x16 // CTRL-V
1477#define ASCII_ETB 0x17 // End-of-Transmission-Block
1478#define ASCII_CTRLW 0x17 // CTRL-W
1479#define ASCII_CAN 0x18 // Cancel
1480#define ASCII_CTRLX 0x18 // CTRL-X
1481#define ASCII_EM 0x19 // End of Medium
1482#define ASCII_CTRLY 0x19 // CTRL-Y
1483#define ASCII_SUB 0x1A // Substitute
1484#define ASCII_CTRLZ 0x1A // CTRL-Z
1485#define ASCII_ESC 0x1B // Escape
1486#define ASCII_FS 0x1C // File Separator
1487#define ASCII_GS 0x1D // Group Separator
1488#define ASCII_RS 0x1E // Record Separator
1489#define ASCII_US 0x1F // Unit Separator
1490#define ASCII_SPC 0x20 // Space
1491#define ASCII_DEL 0x7F // Delete
1492
1493
1494} // end of namespace
1495
1496
1497
bool fileUpdateDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file update date and time.
Definition: fabutils.cpp:804
static bool mountSPIFFS(bool formatOnFail, char const *mountPath, size_t maxFiles=4)
Mounts filesystem on SPIFFS (Flash)
Definition: fabutils.cpp:1280
bool filePathExists(char const *filepath)
Determines if a file exists.
Definition: fabutils.cpp:762
void changeDirectory(const char *subdir)
Sets relative directory path.
Definition: fabutils.cpp:684
bool fileAccessDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file access date and time.
Definition: fabutils.cpp:822
void rename(char const *oldName, char const *newName)
Renames a file.
Definition: fabutils.cpp:1019
static bool remountSPIFFS()
Remounts SPIFFS filesystem, using the same parameters.
Definition: fabutils.cpp:1304
size_t fileSize(char const *name)
Determines file size.
Definition: fabutils.cpp:771
static bool format(DriveType driveType, int drive)
Formats SPIFFS or SD Card.
Definition: fabutils.cpp:1134
FILE * openFile(char const *filename, char const *mode)
Opens a file from current directory.
Definition: fabutils.cpp:1103
bool truncate(char const *name, size_t size)
Truncates a file to the specified size.
Definition: fabutils.cpp:1047
bool reload()
Reloads directory content.
Definition: fabutils.cpp:852
static bool mountSDCard(bool formatOnFail, char const *mountPath, size_t maxFiles=4, int allocationUnitSize=16 *1024, int MISO=16, int MOSI=17, int CLK=14, int CS=13)
Mounts filesystem on SD Card.
Definition: fabutils.cpp:1181
bool setDirectory(const char *path)
Sets absolute directory path.
Definition: fabutils.cpp:671
static void unmountSPIFFS()
Unmounts filesystem on SPIFFS (Flash)
Definition: fabutils.cpp:1295
int count()
Determines number of files in current directory.
Definition: fabutils.h:598
DirItem const * get(int index)
Gets file/directory at index.
Definition: fabutils.h:607
static bool remountSDCard()
Remounts SDCard filesystem, using the same parameters.
Definition: fabutils.cpp:1273
int getFullPath(char const *name, char *outPath=nullptr, int maxlen=0)
Composes a full file path given a relative name.
Definition: fabutils.cpp:1097
char * createTempFilename()
Creates a random temporary filename, with absolute path.
Definition: fabutils.cpp:1032
bool fileCreationDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file creation date and time.
Definition: fabutils.cpp:786
void remove(char const *name)
Removes a file or directory.
Definition: fabutils.cpp:981
static DriveType getDriveType(char const *path)
Returns the drive type of specified path.
Definition: fabutils.cpp:1122
void setSorted(bool value)
Determines if the items are sorted.
DriveType getCurrentDriveType()
Returns the drive type of current directory.
Definition: fabutils.cpp:1116
bool exists(char const *name, bool caseSensitive=true)
Determines if a file or directory exists.
Definition: fabutils.cpp:747
static void unmountSDCard()
Unmounts filesystem on SD Card.
Definition: fabutils.cpp:1260
static bool getFSInfo(DriveType driveType, int drive, int64_t *total, int64_t *used)
Gets total and free space on a filesystem.
Definition: fabutils.cpp:1311
void makeDirectory(char const *dirname)
Creates a directory.
Definition: fabutils.cpp:939
char const * directory()
Determines absolute path of current directory.
Definition: fabutils.h:591
FileBrowser allows basic file system operations (dir, mkdir, remove and rename)
Definition: fabutils.h:554
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.
Definition: tsi2c.h:85
uint8_t B
uint8_t const * data
uint8_t G
uint8_t R
int16_t height
Definition: fabutils.h:1
int16_t width
Definition: fabutils.h:0
VideoMode
Specifies a video mode.
Definition: fabutils.h:1068
ChipPackage
This enum defines ESP32 module types (packages)
Definition: fabutils.h:957
DriveType
This enum defines drive types (SPIFFS or SD Card)
Definition: fabutils.h:542
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:1097
@ VK_GRAVE_e
Definition: fabutils.h:1280
@ VK_n
Definition: fabutils.h:1136
@ VK_PRINTSCREEN
Definition: fabutils.h:1231
@ VK_APPLICATION
Definition: fabutils.h:1251
@ VK_V
Definition: fabutils.h:1170
@ VK_F12
Definition: fabutils.h:1277
@ VK_CARET_A
Definition: fabutils.h:1328
@ VK_H
Definition: fabutils.h:1156
@ VK_GRAVE_y
Definition: fabutils.h:1284
@ VK_KP_LEFT
Definition: fabutils.h:1261
@ VK_PERIOD
Definition: fabutils.h:1191
@ VK_F8
Definition: fabutils.h:1273
@ VK_ACUTEACCENT
Definition: fabutils.h:1177
@ VK_N
Definition: fabutils.h:1162
@ VK_BREAK
Definition: fabutils.h:1244
@ VK_CARET_o
Definition: fabutils.h:1324
@ VK_w
Definition: fabutils.h:1145
@ VK_NUMLOCK
Definition: fabutils.h:1246
@ VK_KP_RIGHT
Definition: fabutils.h:1263
@ VK_m
Definition: fabutils.h:1135
@ VK_y
Definition: fabutils.h:1147
@ VK_RIGHTBRACE
Definition: fabutils.h:1207
@ VK_GRAVE_I
Definition: fabutils.h:1295
@ VK_GRAVE_Y
Definition: fabutils.h:1298
@ VK_ESZETT
Definition: fabutils.h:1347
@ VK_F7
Definition: fabutils.h:1272
@ VK_KP_PERIOD
Definition: fabutils.h:1190
@ VK_GRAVE_U
Definition: fabutils.h:1297
@ VK_o
Definition: fabutils.h:1137
@ VK_GRAVE_a
Definition: fabutils.h:1279
@ VK_p
Definition: fabutils.h:1138
@ VK_RETURN
Definition: fabutils.h:1249
@ VK_KP_6
Definition: fabutils.h:1118
@ VK_D
Definition: fabutils.h:1152
@ VK_SYSREQ
Definition: fabutils.h:1232
@ VK_ESCAPE
Definition: fabutils.h:1229
@ VK_LSHIFT
Definition: fabutils.h:1220
@ VK_CARET_U
Definition: fabutils.h:1332
@ VK_KP_5
Definition: fabutils.h:1117
@ VK_UPPER_a
Definition: fabutils.h:1346
@ VK_GRAVE_E
Definition: fabutils.h:1294
@ VK_COMMA
Definition: fabutils.h:1193
@ VK_R
Definition: fabutils.h:1166
@ VK_UMLAUT_e
Definition: fabutils.h:1308
@ VK_Q
Definition: fabutils.h:1165
@ VK_MU
Definition: fabutils.h:1354
@ VK_TILDE_a
Definition: fabutils.h:1338
@ VK_PAGEUP
Definition: fabutils.h:1252
@ VK_KP_END
Definition: fabutils.h:1242
@ VK_9
Definition: fabutils.h:1111
@ VK_RALT
Definition: fabutils.h:1223
@ VK_AELIG
Definition: fabutils.h:1360
@ VK_f
Definition: fabutils.h:1128
@ VK_d
Definition: fabutils.h:1126
@ VK_x
Definition: fabutils.h:1146
@ VK_CEDILLA_c
Definition: fabutils.h:1335
@ VK_TILDE
Definition: fabutils.h:1217
@ VK_ARING
Definition: fabutils.h:1362
@ VK_UMLAUT_a
Definition: fabutils.h:1307
@ VK_SPACE
Definition: fabutils.h:1100
@ VK_YEN
Definition: fabutils.h:1365
@ VK_KP_8
Definition: fabutils.h:1120
@ VK_F11
Definition: fabutils.h:1276
@ VK_F9
Definition: fabutils.h:1274
@ VK_LEFTBRACKET
Definition: fabutils.h:1208
@ VK_UMLAUT_A
Definition: fabutils.h:1314
@ VK_7
Definition: fabutils.h:1109
@ VK_KP_9
Definition: fabutils.h:1121
@ VK_KP_4
Definition: fabutils.h:1116
@ VK_ACUTE_A
Definition: fabutils.h:1300
@ VK_ACUTE_a
Definition: fabutils.h:1286
@ VK_K
Definition: fabutils.h:1159
@ VK_F2
Definition: fabutils.h:1267
@ VK_POUND
Definition: fabutils.h:1201
@ VK_CARET_e
Definition: fabutils.h:1322
@ VK_F1
Definition: fabutils.h:1266
@ VK_HASH
Definition: fabutils.h:1197
@ VK_ACUTE_e
Definition: fabutils.h:1287
@ VK_UMLAUT_U
Definition: fabutils.h:1318
@ VK_KP_PAGEDOWN
Definition: fabutils.h:1255
@ VK_UMLAUT_O
Definition: fabutils.h:1317
@ VK_LALT
Definition: fabutils.h:1222
@ VK_GREATER
Definition: fabutils.h:1213
@ VK_KP_MULTIPLY
Definition: fabutils.h:1185
@ VK_u
Definition: fabutils.h:1143
@ VK_UP
Definition: fabutils.h:1256
@ VK_LEFTPAREN
Definition: fabutils.h:1210
@ VK_GRAVE_o
Definition: fabutils.h:1282
@ VK_CARET_I
Definition: fabutils.h:1330
@ VK_VERTICALBAR
Definition: fabutils.h:1196
@ VK_a
Definition: fabutils.h:1123
@ VK_r
Definition: fabutils.h:1140
@ VK_SLASH
Definition: fabutils.h:1189
@ VK_DEGREE
Definition: fabutils.h:1215
@ VK_RIGHTPAREN
Definition: fabutils.h:1211
@ VK_QUOTE
Definition: fabutils.h:1178
@ VK_KP_UP
Definition: fabutils.h:1257
@ VK_KP_1
Definition: fabutils.h:1113
@ VK_SEMICOLON
Definition: fabutils.h:1194
@ VK_QUESTION_INV
Definition: fabutils.h:1349
@ VK_GRAVE_A
Definition: fabutils.h:1293
@ VK_z
Definition: fabutils.h:1148
@ VK_UMLAUT_I
Definition: fabutils.h:1316
@ VK_ASTERISK
Definition: fabutils.h:1186
@ VK_ACUTE_i
Definition: fabutils.h:1288
@ VK_END
Definition: fabutils.h:1241
@ VK_PAUSE
Definition: fabutils.h:1243
@ VK_UNDERSCORE
Definition: fabutils.h:1214
@ VK_PAGEDOWN
Definition: fabutils.h:1254
@ VK_ACUTE_Y
Definition: fabutils.h:1305
@ VK_KP_DELETE
Definition: fabutils.h:1237
@ VK_RGUI
Definition: fabutils.h:1227
@ VK_B
Definition: fabutils.h:1150
@ VK_LCTRL
Definition: fabutils.h:1224
@ VK_ACUTE_O
Definition: fabutils.h:1303
@ VK_k
Definition: fabutils.h:1133
@ VK_KP_CENTER
Definition: fabutils.h:1264
@ VK_g
Definition: fabutils.h:1129
@ VK_AT
Definition: fabutils.h:1198
@ VK_O
Definition: fabutils.h:1163
@ VK_CARET_u
Definition: fabutils.h:1325
@ VK_KP_0
Definition: fabutils.h:1112
@ VK_DIAERESIS
Definition: fabutils.h:1351
@ VK_F5
Definition: fabutils.h:1270
@ VK_DOWN
Definition: fabutils.h:1258
@ VK_TILDE_O
Definition: fabutils.h:1343
@ VK_S
Definition: fabutils.h:1167
@ VK_AMPERSAND
Definition: fabutils.h:1195
@ VK_CURRENCY
Definition: fabutils.h:1353
@ VK_F6
Definition: fabutils.h:1271
@ VK_L
Definition: fabutils.h:1160
@ VK_HOME
Definition: fabutils.h:1239
@ VK_E
Definition: fabutils.h:1153
@ VK_C
Definition: fabutils.h:1151
@ VK_CARET_y
Definition: fabutils.h:1326
@ VK_SCROLLLOCK
Definition: fabutils.h:1245
@ VK_EQUALS
Definition: fabutils.h:1180
@ VK_CARET_O
Definition: fabutils.h:1331
@ VK_COLON
Definition: fabutils.h:1192
@ VK_F3
Definition: fabutils.h:1268
@ VK_P
Definition: fabutils.h:1164
@ VK_F
Definition: fabutils.h:1154
@ VK_6
Definition: fabutils.h:1108
@ VK_TAB
Definition: fabutils.h:1248
@ VK_v
Definition: fabutils.h:1144
@ VK_i
Definition: fabutils.h:1131
@ VK_U
Definition: fabutils.h:1169
@ VK_T
Definition: fabutils.h:1168
@ VK_KP_DOWN
Definition: fabutils.h:1259
@ VK_c
Definition: fabutils.h:1125
@ VK_TILDE_n
Definition: fabutils.h:1340
@ VK_KP_DIVIDE
Definition: fabutils.h:1188
@ VK_LEFTBRACE
Definition: fabutils.h:1206
@ VK_Y
Definition: fabutils.h:1173
@ VK_CEDILLA_C
Definition: fabutils.h:1336
@ VK_OSLASH
Definition: fabutils.h:1361
@ VK_LESS
Definition: fabutils.h:1212
@ VK_EXCLAIM
Definition: fabutils.h:1204
@ VK_TILDE_N
Definition: fabutils.h:1344
@ VK_5
Definition: fabutils.h:1107
@ VK_CARET_E
Definition: fabutils.h:1329
@ VK_UMLAUT_u
Definition: fabutils.h:1311
@ VK_ACUTE_E
Definition: fabutils.h:1301
@ VK_t
Definition: fabutils.h:1142
@ VK_GRAVE_O
Definition: fabutils.h:1296
@ VK_UMLAUT_y
Definition: fabutils.h:1312
@ VK_DELETE
Definition: fabutils.h:1236
@ VK_NEGATION
Definition: fabutils.h:1218
@ VK_KP_7
Definition: fabutils.h:1119
@ VK_KP_ENTER
Definition: fabutils.h:1250
@ VK_0
Definition: fabutils.h:1102
@ VK_CARET_Y
Definition: fabutils.h:1333
@ VK_GRAVEACCENT
Definition: fabutils.h:1176
@ VK_ACUTE_I
Definition: fabutils.h:1302
@ VK_LGUI
Definition: fabutils.h:1226
@ VK_A
Definition: fabutils.h:1149
@ VK_DOLLAR
Definition: fabutils.h:1200
@ VK_8
Definition: fabutils.h:1110
@ VK_1
Definition: fabutils.h:1103
@ VK_SECTION
Definition: fabutils.h:1216
@ VK_KP_3
Definition: fabutils.h:1115
@ VK_UMLAUT_o
Definition: fabutils.h:1310
@ VK_Z
Definition: fabutils.h:1174
@ VK_GRAVE_u
Definition: fabutils.h:1283
@ VK_CARET_a
Definition: fabutils.h:1321
@ VK_aring
Definition: fabutils.h:1358
@ VK_s
Definition: fabutils.h:1141
@ VK_F10
Definition: fabutils.h:1275
@ VK_X
Definition: fabutils.h:1172
@ VK_ACUTE_o
Definition: fabutils.h:1289
@ VK_BACKSPACE
Definition: fabutils.h:1238
@ VK_TILDE_o
Definition: fabutils.h:1339
@ VK_QUESTION
Definition: fabutils.h:1205
@ VK_MINUS
Definition: fabutils.h:1181
@ VK_UMLAUT_E
Definition: fabutils.h:1315
@ VK_BACKSLASH
Definition: fabutils.h:1187
@ VK_l
Definition: fabutils.h:1134
@ VK_NONE
Definition: fabutils.h:1098
@ VK_oslash
Definition: fabutils.h:1357
@ VK_RCTRL
Definition: fabutils.h:1225
@ VK_LEFT
Definition: fabutils.h:1260
@ VK_INTERPUNCT
Definition: fabutils.h:1350
@ VK_ACUTE_U
Definition: fabutils.h:1304
@ VK_b
Definition: fabutils.h:1124
@ VK_KP_INSERT
Definition: fabutils.h:1235
@ VK_J
Definition: fabutils.h:1158
@ VK_KP_MINUS
Definition: fabutils.h:1182
@ VK_CARET
Definition: fabutils.h:1199
@ VK_QUOTEDBL
Definition: fabutils.h:1179
@ VK_CARET_i
Definition: fabutils.h:1323
@ VK_ACUTE_y
Definition: fabutils.h:1291
@ VK_W
Definition: fabutils.h:1171
@ VK_UMLAUT_Y
Definition: fabutils.h:1319
@ VK_e
Definition: fabutils.h:1127
@ VK_EURO
Definition: fabutils.h:1202
@ VK_KP_2
Definition: fabutils.h:1114
@ VK_2
Definition: fabutils.h:1104
@ VK_RIGHTBRACKET
Definition: fabutils.h:1209
@ VK_KP_HOME
Definition: fabutils.h:1240
@ VK_UMLAUT_i
Definition: fabutils.h:1309
@ VK_h
Definition: fabutils.h:1130
@ VK_RIGHT
Definition: fabutils.h:1262
@ VK_ASCII
Definition: fabutils.h:1372
@ VK_F4
Definition: fabutils.h:1269
@ VK_PLUS
Definition: fabutils.h:1183
@ VK_SQUARE
Definition: fabutils.h:1352
@ VK_M
Definition: fabutils.h:1161
@ VK_4
Definition: fabutils.h:1106
@ VK_CAPSLOCK
Definition: fabutils.h:1247
@ VK_ACUTE_u
Definition: fabutils.h:1290
@ VK_I
Definition: fabutils.h:1157
@ VK_EXCLAIM_INV
Definition: fabutils.h:1348
@ VK_j
Definition: fabutils.h:1132
@ VK_KP_PAGEUP
Definition: fabutils.h:1253
@ VK_3
Definition: fabutils.h:1105
@ VK_RSHIFT
Definition: fabutils.h:1221
@ VK_q
Definition: fabutils.h:1139
@ VK_GRAVE_i
Definition: fabutils.h:1281
@ VK_G
Definition: fabutils.h:1155
@ VK_KP_PLUS
Definition: fabutils.h:1184
@ VK_PERCENT
Definition: fabutils.h:1203
@ VK_TILDE_A
Definition: fabutils.h:1342
@ VK_INSERT
Definition: fabutils.h:1234
This class helps to choice a core for intensive processing tasks.
Definition: fabutils.h:1050
This class helps to know which is the current video output (VGA or Composite)
Definition: fabutils.h:1080
char const * name
Definition: fabutils.h:535
FileBrowser item specificator.
Definition: fabutils.h:533
Describes mouse buttons status.
Definition: fabutils.h:286
MouseButtons buttons
Definition: fabutils.h:303
Describes mouse absolute position, scroll wheel delta and buttons status.
Definition: fabutils.h:299
int16_t X
Definition: fabutils.h:214
int16_t Y
Definition: fabutils.h:215
Represents the coordinate of a point.
Definition: fabutils.h:213
int16_t X1
Definition: fabutils.h:249
int16_t Y2
Definition: fabutils.h:252
int16_t X2
Definition: fabutils.h:251
int16_t Y1
Definition: fabutils.h:250
Represents a rectangle.
Definition: fabutils.h:248
int16_t height
Definition: fabutils.h:233
int16_t width
Definition: fabutils.h:232
Represents a bidimensional size.
Definition: fabutils.h:231
uint8_t scancode[8]
Definition: fabutils.h:1384
A struct which contains a virtual key, key state and associated scan code.
Definition: fabutils.h:1381