39#include "freertos/FreeRTOS.h"
40#include "freertos/semphr.h"
42#include <driver/adc.h>
43#include <esp_system.h>
52 #define FABGL_ESP_IDF_VERSION_VAL ESP_IDF_VERSION_VAL
53 #define FABGL_ESP_IDF_VERSION ESP_IDF_VERSION
55 #define FABGL_ESP_IDF_VERSION_VAL(major, minor, patch) ((major << 16) | (minor << 8) | (patch))
56 #define FABGL_ESP_IDF_VERSION FABGL_ESP_IDF_VERSION_VAL(0, 0, 0)
61#define GPIO_UNUSED GPIO_NUM_MAX
72 #ifdef BOARD_HAS_PSRAM
73 #define FABGL_NEED_PSRAM_DISABLE_HACK
76 #ifdef CONFIG_SPIRAM_SUPPORT
77 #define FABGL_NEED_PSRAM_DISABLE_HACK
81#ifdef FABGL_NEED_PSRAM_DISABLE_HACK
82 #define PSRAM_HACK asm(" nop")
89#define ASM_MEMW asm(" MEMW");
91#define ASM_NOP asm(" NOP");
93#define PSRAM_WORKAROUND1 asm(" nop;nop;nop;nop");
94#define PSRAM_WORKAROUND2 asm(" memw");
102#define TORAD(a) ((a) * M_PI / 180.)
110const T & tmax(
const T & a,
const T & b)
112 return (a < b) ? b : a;
116constexpr auto imax = tmax<int>;
120const T & tmin(
const T & a,
const T & b)
122 return !(b < a) ? a : b;
126constexpr auto imin = tmin<int>;
131const T & tclamp(
const T & v,
const T & lo,
const T & hi)
133 return (v < lo ? lo : (v > hi ? hi : v));
137constexpr auto iclamp = tclamp<int>;
141const T & twrap(
const T & v,
const T & lo,
const T & hi)
143 return (v < lo ? hi : (v > hi ? lo : v));
148void tswap(T & v1, T & v2)
156constexpr auto iswap = tswap<int>;
160T moveItems(T dest, T src,
size_t n)
166 for (pd += n, ps += n; n--;)
176void rgb222_to_hsv(
int R,
int G,
int B,
double * h,
double * s,
double * v);
179inline uint16_t changeEndiannesWord(uint16_t value)
181 return ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
185inline uint32_t changeEndiannesDWord(uint32_t value)
187 return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | ((value & 0xff000000) >> 24);
198void APLLCalcParams(
double freq, APLLParams * params, uint8_t * a, uint8_t * b,
double * out_freq,
double * error);
214 Point(
int X_,
int Y_) :
X(X_),
Y(Y_) { }
216 Point add(Point
const & p)
const {
return Point(
X + p.X,
Y + p.Y); }
217 Point sub(Point
const & p)
const {
return Point(
X - p.X,
Y - p.Y); }
218 Point neg()
const {
return Point(-
X, -
Y); }
219 bool operator==(Point
const & r) {
return X == r.X &&
Y == r.Y; }
220 bool operator!=(Point
const & r) {
return X != r.X ||
Y != r.Y; }
221} __attribute__ ((packed));
233 bool operator==(Size
const & r) {
return width == r.width &&
height == r.height; }
234 bool operator!=(Size
const & r) {
return width != r.width ||
height != r.height; }
235} __attribute__ ((packed));
251 Rect(
int X1_,
int Y1_,
int X2_,
int Y2_) :
X1(X1_),
Y1(Y1_),
X2(X2_),
Y2(Y2_) { }
252 Rect(Rect
const & r) {
X1 = r.X1;
Y1 = r.Y1;
X2 = r.X2;
Y2 = r.Y2; }
254 bool operator==(Rect
const & r) {
return X1 == r.X1 &&
Y1 == r.Y1 &&
X2 == r.X2 &&
Y2 == r.Y2; }
255 bool operator!=(Rect
const & r) {
return X1 != r.X1 ||
Y1 != r.Y1 ||
X2 != r.X2 ||
Y2 != r.Y2; }
256 Point pos()
const {
return Point(
X1,
Y1); }
257 Size size()
const {
return Size(
X2 -
X1 + 1,
Y2 -
Y1 + 1); }
258 int width()
const {
return X2 -
X1 + 1; }
259 int height()
const {
return Y2 -
Y1 + 1; }
260 Rect translate(
int offsetX,
int offsetY)
const {
return Rect(
X1 + offsetX,
Y1 + offsetY,
X2 + offsetX,
Y2 + offsetY); }
261 Rect translate(Point
const & offset)
const {
return Rect(
X1 + offset.X,
Y1 + offset.Y,
X2 + offset.X,
Y2 + offset.Y); }
262 Rect move(Point
const & position)
const {
return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
263 Rect move(
int x,
int y)
const {
return Rect(x, y, x + width() - 1, y + height() - 1); }
264 Rect shrink(
int value)
const {
return Rect(
X1 + value,
Y1 + value,
X2 - value,
Y2 - value); }
265 Rect hShrink(
int value)
const {
return Rect(
X1 + value,
Y1,
X2 - value,
Y2); }
266 Rect vShrink(
int value)
const {
return Rect(
X1,
Y1 + value,
X2,
Y2 - value); }
267 Rect resize(
int width,
int height)
const {
return Rect(
X1,
Y1,
X1 + width - 1,
Y1 + height - 1); }
268 Rect resize(Size size)
const {
return Rect(
X1,
Y1,
X1 + size.width - 1,
Y1 + size.height - 1); }
269 Rect intersection(Rect
const & rect)
const;
270 bool intersects(Rect
const & rect)
const {
return X1 <= rect.X2 &&
X2 >= rect.X1 &&
Y1 <= rect.Y2 &&
Y2 >= rect.Y1; }
271 bool contains(Rect
const & rect)
const {
return (rect.X1 >=
X1) && (rect.Y1 >=
Y1) && (rect.X2 <=
X2) && (rect.Y2 <=
Y2); }
272 bool contains(Point
const & point)
const {
return point.X >=
X1 && point.Y >=
Y1 && point.X <=
X2 && point.Y <=
Y2; }
273 bool contains(
int x,
int y)
const {
return x >=
X1 && y >=
Y1 && x <=
X2 && y <=
Y2; }
274 Rect merge(Rect
const & rect)
const;
275} __attribute__ ((packed));
306#define FONTINFOFLAGS_ITALIC 1
307#define FONTINFOFLAGS_UNDERLINE 2
308#define FONTINFODLAFS_STRIKEOUT 4
309#define FONTINFOFLAGS_VARWIDTH 8
327 uint8_t
const *
data;
328 uint32_t
const * chptr;
343 bool expired(
int valueMS);
359 StackItem(StackItem * next_, T
const & item_) : next(next_), item(item_) { }
365 Stack() : m_items(nullptr) { }
366 bool isEmpty() {
return m_items ==
nullptr; }
367 void push(T
const & value) {
368 m_items =
new StackItem<T>(m_items, value);
372 StackItem<T> * iptr = m_items;
373 m_items = iptr->next;
382 for (
auto i = m_items; i; i = i->next)
387 StackItem<T> * m_items;
395template <
typename ...Params>
399 Delegate() : m_func(nullptr) {
403 Delegate(
const Delegate & c) =
delete;
406 template <
typename Func>
407 Delegate(Func f) : Delegate() {
416 template <
typename Func>
417 void operator=(Func f) {
419 m_closure = [] (
void * func,
const Params & ...params) ->
void { (*(Func *)func)(params...); };
420 m_func = heap_caps_malloc(
sizeof(Func), MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
421 moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f,
sizeof(Func) /
sizeof(uint32_t));
425 void operator=(
const Delegate&) =
delete;
427 void operator()(
const Params & ...params) {
429 m_closure(m_func, params...);
434 void (*m_closure)(
void * func,
const Params & ...params);
439 heap_caps_free(m_func);
453 int append(
char const * str);
454 int appendFmt(
const char *format, ...);
455 void append(
char const * strlist[],
int count);
456 void appendSepList(
char const * strlist,
char separator);
457 void insert(
int index,
char const * str);
458 void set(
int index,
char const * str);
459 void remove(
int index);
460 int count() {
return m_count; }
461 char const * get(
int index) {
return m_items[index]; }
464 void select(
int index,
bool value);
466 bool selected(
int index);
467 int getFirstSelected();
468 void copyFrom(StringList
const & src);
469 void copySelectionMapFrom(StringList
const & src);
472 void checkAllocatedSpace(
int requiredItems);
474 char const * * m_items;
485 uint16_t m_allocated;
497class LightMemoryPool {
499 LightMemoryPool(
int poolSize);
501 void * alloc(
int size);
502 void free(
void * mem) {
if (mem) markFree((uint8_t*)mem - m_mem - 2); }
511 void mark(
int pos, int16_t size,
bool allocated);
512 void markFree(
int pos) { m_mem[pos + 1] &= 0x7f; }
513 int16_t getSize(
int pos);
514 bool isFree(
int pos);
615 bool exists(
char const * name,
bool caseSensitive =
true);
650 bool fileCreationDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
665 bool fileUpdateDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
680 bool fileAccessDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
689 void setIncludeHiddenFiles(
bool value) { m_includeHiddenFiles = value; }
705 void remove(
char const * name);
713 void rename(
char const * oldName,
char const * newName);
723 bool truncate(
char const * name,
size_t size);
741 int getFullPath(
char const * name,
char * outPath =
nullptr,
int maxlen = 0);
751 FILE *
openFile(
char const * filename,
char const * mode);
807 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);
816 static bool mountedSDCard() {
return s_SDCardMounted; }
837 static bool mountSPIFFS(
bool formatOnFail,
char const * mountPath,
size_t maxFiles = 4);
873 static bool getFSInfo(
DriveType driveType,
int drive, int64_t * total, int64_t * used);
878 int countDirEntries(
int * namesLength);
881 static bool s_SPIFFSMounted;
882 static char const * s_SPIFFSMountPath;
883 static size_t s_SPIFFSMaxFiles;
886 static bool s_SDCardMounted;
887 static char const * s_SDCardMountPath;
888 static size_t s_SDCardMaxFiles;
889 static int s_SDCardAllocationUnitSize;
890 static int8_t s_SDCardMISO;
891 static int8_t s_SDCardMOSI;
892 static int8_t s_SDCardCLK;
893 static int8_t s_SDCardCS;
894 static sdmmc_card_t * s_SDCard;
900 bool m_includeHiddenFiles;
901 char * m_namesStorage;
911bool clipLine(
int & x1,
int & y1,
int & x2,
int & y2, Rect
const & clipRect,
bool checkOnly);
914void removeRectangle(Stack<Rect> & rects, Rect
const & mainRect, Rect
const & rectToRemove);
917bool calcParity(uint8_t v);
921void * realloc32(
void * ptr,
size_t size);
922void free32(
void * ptr);
925inline gpio_num_t int2gpio(
int gpio)
927 return gpio == -1 ? GPIO_UNUSED : (gpio_num_t)gpio;
932inline char digit2hex(
int digit)
934 return digit < 10 ?
'0' + digit :
'a' + digit - 10;
939inline int hex2digit(
char hex)
941 return hex <
'a' ? hex -
'0' : hex -
'a' + 10;
947uint32_t msToTicks(
int ms);
964inline __attribute__((always_inline)) uint32_t getCycleCount() {
966 __asm__ __volatile__(
968 "rsr %0, ccount \n\t"
980void replacePathSep(
char * path,
char newSep);
990inline uint32_t
UARTConf(
int parity,
int dataLength,
int stopBits)
992 uint32_t w = 0x8000000 | (dataLength << 2) | (stopBits << 4);
994 w |= (parity == 1 ? 0b10 : 0b11);
999adc1_channel_t ADC1_GPIO2Channel(gpio_num_t gpio);
1002void esp_intr_alloc_pinnedToCore(
int source,
int flags, intr_handler_t handler,
void * arg, intr_handle_t * ret_handle,
int core);
1011void configureGPIO(gpio_num_t gpio, gpio_mode_t mode);
1014uint32_t getApbFrequency();
1016uint32_t getCPUFrequencyMHz();
1022struct AutoSemaphore {
1023 AutoSemaphore(SemaphoreHandle_t mutex) : m_mutex(mutex) { xSemaphoreTake(m_mutex, portMAX_DELAY); }
1024 ~AutoSemaphore() { xSemaphoreGive(m_mutex); }
1026 SemaphoreHandle_t m_mutex;
1039 static int busiestCore() {
return s_busiestCore; }
1040 static int quietCore() {
return s_busiestCore != -1 ? s_busiestCore ^ 1 : -1; }
1041 static void setBusiestCore(
int core) { s_busiestCore = core; }
1044 static int s_busiestCore;
1370#define ASCII_NUL 0x00
1371#define ASCII_SOH 0x01
1372#define ASCII_CTRLA 0x01
1373#define ASCII_STX 0x02
1374#define ASCII_CTRLB 0x02
1375#define ASCII_ETX 0x03
1376#define ASCII_CTRLC 0x03
1377#define ASCII_EOT 0x04
1378#define ASCII_CTRLD 0x04
1379#define ASCII_ENQ 0x05
1380#define ASCII_CTRLE 0x05
1381#define ASCII_ACK 0x06
1382#define ASCII_CTRLF 0x06
1383#define ASCII_BEL 0x07
1384#define ASCII_CTRLG 0x07
1385#define ASCII_BS 0x08
1386#define ASCII_CTRLH 0x08
1387#define ASCII_HT 0x09
1388#define ASCII_TAB 0x09
1389#define ASCII_CTRLI 0x09
1390#define ASCII_LF 0x0A
1391#define ASCII_CTRLJ 0x0A
1392#define ASCII_VT 0x0B
1393#define ASCII_CTRLK 0x0B
1394#define ASCII_FF 0x0C
1395#define ASCII_CTRLL 0x0C
1396#define ASCII_CR 0x0D
1397#define ASCII_CTRLM 0x0D
1398#define ASCII_SO 0x0E
1399#define ASCII_CTRLN 0x0E
1400#define ASCII_SI 0x0F
1401#define ASCII_CTRLO 0x0F
1402#define ASCII_DLE 0x10
1403#define ASCII_CTRLP 0x10
1404#define ASCII_DC1 0x11
1405#define ASCII_CTRLQ 0x11
1406#define ASCII_XON 0x11
1407#define ASCII_DC2 0x12
1408#define ASCII_CTRLR 0x12
1409#define ASCII_DC3 0x13
1410#define ASCII_XOFF 0x13
1411#define ASCII_CTRLS 0x13
1412#define ASCII_DC4 0x14
1413#define ASCII_CTRLT 0x14
1414#define ASCII_NAK 0x15
1415#define ASCII_CTRLU 0x15
1416#define ASCII_SYN 0x16
1417#define ASCII_CTRLV 0x16
1418#define ASCII_ETB 0x17
1419#define ASCII_CTRLW 0x17
1420#define ASCII_CAN 0x18
1421#define ASCII_CTRLX 0x18
1422#define ASCII_EM 0x19
1423#define ASCII_CTRLY 0x19
1424#define ASCII_SUB 0x1A
1425#define ASCII_CTRLZ 0x1A
1426#define ASCII_ESC 0x1B
1427#define ASCII_FS 0x1C
1428#define ASCII_GS 0x1D
1429#define ASCII_RS 0x1E
1430#define ASCII_US 0x1F
1431#define ASCII_SPC 0x20
1432#define ASCII_DEL 0x7F
bool fileUpdateDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file update date and time.
static bool mountSPIFFS(bool formatOnFail, char const *mountPath, size_t maxFiles=4)
Mounts filesystem on SPIFFS (Flash)
bool filePathExists(char const *filepath)
Determines if a file exists.
void changeDirectory(const char *subdir)
Sets relative directory path.
bool fileAccessDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file access date and time.
void rename(char const *oldName, char const *newName)
Renames a file.
static bool remountSPIFFS()
Remounts SPIFFS filesystem, using the same parameters.
size_t fileSize(char const *name)
Determines file size.
static bool format(DriveType driveType, int drive)
Formats SPIFFS or SD Card.
FILE * openFile(char const *filename, char const *mode)
Opens a file from current directory.
bool truncate(char const *name, size_t size)
Truncates a file to the specified size.
bool reload()
Reloads directory content.
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.
bool setDirectory(const char *path)
Sets absolute directory path.
static void unmountSPIFFS()
Unmounts filesystem on SPIFFS (Flash)
int count()
Determines number of files in current directory.
DirItem const * get(int index)
Gets file/directory at index.
static bool remountSDCard()
Remounts SDCard filesystem, using the same parameters.
int getFullPath(char const *name, char *outPath=nullptr, int maxlen=0)
Composes a full file path given a relative name.
char * createTempFilename()
Creates a random temporary filename, with absolute path.
bool fileCreationDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file creation date and time.
void remove(char const *name)
Removes a file or directory.
static DriveType getDriveType(char const *path)
Returns the drive type of specified path.
void setSorted(bool value)
Determines if the items are sorted.
DriveType getCurrentDriveType()
Returns the drive type of current directory.
bool exists(char const *name, bool caseSensitive=true)
Determines if a file or directory exists.
static void unmountSDCard()
Unmounts filesystem on SD Card.
static bool getFSInfo(DriveType driveType, int drive, int64_t *total, int64_t *used)
Gets total and free space on a filesystem.
void makeDirectory(char const *dirname)
Creates a directory.
char const * directory()
Determines absolute path of current directory.
FileBrowser allows basic file system operations (dir, mkdir, remove and rename)
uint32_t UARTConf(int parity, int dataLength, int stopBits)
Composes UART configuration word.
ChipPackage
This enum defines ESP32 module types (packages)
DriveType
This enum defines drive types (SPIFFS or SD Card)
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
This class helps to choice a core for intensive processing tasks.
FileBrowser item specificator.
Describes mouse absolute position, scroll wheel delta and buttons status.
Represents the coordinate of a point.
Represents a bidimensional size.
A struct which contains a virtual key, key state and associated scan code.