39 #include "freertos/FreeRTOS.h" 40 #include "freertos/semphr.h" 42 #include <driver/adc.h> 43 #include <esp_system.h> 44 #include "sdmmc_cmd.h" 51 #ifdef ESP_IDF_VERSION 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"); 101 template <
typename T>
102 const T & tmax(
const T & a,
const T & b)
104 return (a < b) ? b : a;
108 constexpr
auto imax = tmax<int>;
111 template <
typename T>
112 const T & tmin(
const T & a,
const T & b)
114 return !(b < a) ? a : b;
118 constexpr
auto imin = tmin<int>;
122 template <
typename T>
123 const T & tclamp(
const T & v,
const T & lo,
const T & hi)
125 return (v < lo ? lo : (v > hi ? hi : v));
129 constexpr
auto iclamp = tclamp<int>;
132 template <
typename T>
133 const T & twrap(
const T & v,
const T & lo,
const T & hi)
135 return (v < lo ? hi : (v > hi ? lo : v));
139 template <
typename T>
140 void tswap(T & v1, T & v2)
148 constexpr
auto iswap = tswap<int>;
151 template <
typename T>
152 T moveItems(T dest, T src,
size_t n)
158 for (pd += n, ps += n; n--;)
168 void rgb222_to_hsv(
int R,
int G,
int B,
double * h,
double * s,
double * v);
171 inline uint16_t changeEndiannesWord(uint16_t value)
173 return ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
177 inline uint32_t changeEndiannesDWord(uint32_t value)
179 return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | ((value & 0xff000000) >> 24);
196 Point(
int X_,
int Y_) :
X(X_),
Y(Y_) { }
198 Point add(Point
const & p)
const {
return Point(
X + p.X,
Y + p.Y); }
199 Point sub(Point
const & p)
const {
return Point(
X - p.X,
Y - p.Y); }
200 Point neg()
const {
return Point(-
X, -
Y); }
201 bool operator==(Point
const & r) {
return X == r.X &&
Y == r.Y; }
202 bool operator!=(Point
const & r) {
return X != r.X ||
Y != r.Y; }
203 } __attribute__ ((packed));
215 bool operator==(Size
const & r) {
return width == r.width &&
height == r.height; }
216 bool operator!=(Size
const & r) {
return width != r.width ||
height != r.height; }
217 } __attribute__ ((packed));
233 Rect(
int X1_,
int Y1_,
int X2_,
int Y2_) :
X1(X1_),
Y1(Y1_),
X2(X2_),
Y2(Y2_) { }
234 Rect(Rect
const & r) {
X1 = r.X1;
Y1 = r.Y1;
X2 = r.X2;
Y2 = r.Y2; }
236 bool operator==(Rect
const & r) {
return X1 == r.X1 &&
Y1 == r.Y1 &&
X2 == r.X2 &&
Y2 == r.Y2; }
237 bool operator!=(Rect
const & r) {
return X1 != r.X1 ||
Y1 != r.Y1 ||
X2 != r.X2 ||
Y2 != r.Y2; }
238 Point pos()
const {
return Point(
X1,
Y1); }
239 Size size()
const {
return Size(
X2 -
X1 + 1,
Y2 -
Y1 + 1); }
240 int width()
const {
return X2 -
X1 + 1; }
241 int height()
const {
return Y2 -
Y1 + 1; }
242 Rect translate(
int offsetX,
int offsetY)
const {
return Rect(
X1 + offsetX,
Y1 + offsetY,
X2 + offsetX,
Y2 + offsetY); }
243 Rect translate(Point
const & offset)
const {
return Rect(
X1 + offset.X,
Y1 + offset.Y,
X2 + offset.X,
Y2 + offset.Y); }
244 Rect move(Point
const & position)
const {
return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
245 Rect move(
int x,
int y)
const {
return Rect(x, y, x + width() - 1, y + height() - 1); }
246 Rect shrink(
int value)
const {
return Rect(
X1 + value,
Y1 + value,
X2 - value,
Y2 - value); }
247 Rect hShrink(
int value)
const {
return Rect(
X1 + value,
Y1,
X2 - value,
Y2); }
248 Rect vShrink(
int value)
const {
return Rect(
X1,
Y1 + value,
X2,
Y2 - value); }
249 Rect resize(
int width,
int height)
const {
return Rect(
X1,
Y1,
X1 + width - 1,
Y1 + height - 1); }
250 Rect resize(Size size)
const {
return Rect(
X1,
Y1,
X1 + size.width - 1,
Y1 + size.height - 1); }
251 Rect intersection(Rect
const & rect)
const;
252 bool intersects(Rect
const & rect)
const {
return X1 <= rect.X2 &&
X2 >= rect.X1 &&
Y1 <= rect.Y2 &&
Y2 >= rect.Y1; }
253 bool contains(Rect
const & rect)
const {
return (rect.X1 >=
X1) && (rect.Y1 >=
Y1) && (rect.X2 <=
X2) && (rect.Y2 <=
Y2); }
254 bool contains(Point
const & point)
const {
return point.X >=
X1 && point.Y >=
Y1 && point.X <=
X2 && point.Y <=
Y2; }
255 bool contains(
int x,
int y)
const {
return x >=
X1 && y >=
Y1 && x <=
X2 && y <=
Y2; }
256 Rect merge(Rect
const & rect)
const;
257 } __attribute__ ((packed));
288 #define FONTINFOFLAGS_ITALIC 1 289 #define FONTINFOFLAGS_UNDERLINE 2 290 #define FONTINFODLAFS_STRIKEOUT 4 291 #define FONTINFOFLAGS_VARWIDTH 8 309 uint8_t
const *
data;
310 uint32_t
const * chptr;
325 bool expired(
int valueMS);
337 template <
typename T>
341 StackItem(StackItem * next_, T
const & item_) : next(next_), item(item_) { }
344 template <
typename T>
347 Stack() : m_items(nullptr) { }
348 bool isEmpty() {
return m_items ==
nullptr; }
349 void push(T
const & value) {
350 m_items =
new StackItem<T>(m_items, value);
354 StackItem<T> * iptr = m_items;
355 m_items = iptr->next;
364 for (
auto i = m_items; i; i = i->next)
369 StackItem<T> * m_items;
377 template <
typename ...Params>
381 Delegate() : m_func(nullptr) {
385 Delegate(
const Delegate & c) =
delete;
388 template <
typename Func>
389 Delegate(Func f) : Delegate() {
398 template <
typename Func>
399 void operator=(Func f) {
401 m_closure = [] (
void * func,
const Params & ...params) ->
void { (*(Func *)func)(params...); };
402 m_func = heap_caps_malloc(
sizeof(Func), MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
403 moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f,
sizeof(Func) /
sizeof(uint32_t));
407 void operator=(
const Delegate&) =
delete;
409 void operator()(
const Params & ...params) {
411 m_closure(m_func, params...);
416 void (*m_closure)(
void * func,
const Params & ...params);
421 heap_caps_free(m_func);
435 int append(
char const * str);
436 int appendFmt(
const char *format, ...);
437 void append(
char const * strlist[],
int count);
438 void appendSepList(
char const * strlist,
char separator);
439 void insert(
int index,
char const * str);
440 void set(
int index,
char const * str);
441 void remove(
int index);
442 int count() {
return m_count; }
443 char const *
get(
int index) {
return m_items[index]; }
446 void select(
int index,
bool value);
448 bool selected(
int index);
449 int getFirstSelected();
450 void copyFrom(StringList
const & src);
451 void copySelectionMapFrom(StringList
const & src);
454 void checkAllocatedSpace(
int requiredItems);
456 char const * * m_items;
467 uint16_t m_allocated;
479 class LightMemoryPool {
481 LightMemoryPool(
int poolSize);
483 void * alloc(
int size);
484 void free(
void * mem) {
if (mem) markFree((uint8_t*)mem - m_mem - 2); }
493 void mark(
int pos, int16_t size,
bool allocated);
494 void markFree(
int pos) { m_mem[pos + 1] &= 0x7f; }
495 int16_t getSize(
int pos);
496 bool isFree(
int pos);
585 DirItem const *
get(
int index) {
return m_items + index; }
597 bool exists(
char const * name,
bool caseSensitive =
true);
632 bool fileCreationDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
647 bool fileUpdateDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
662 bool fileAccessDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
671 void setIncludeHiddenFiles(
bool value) { m_includeHiddenFiles = value; }
687 void remove(
char const * name);
695 void rename(
char const * oldName,
char const * newName);
705 bool truncate(
char const * name,
size_t size);
723 int getFullPath(
char const * name,
char * outPath =
nullptr,
int maxlen = 0);
733 FILE *
openFile(
char const * filename,
char const * mode);
789 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);
798 static bool mountedSDCard() {
return s_SDCardMounted; }
819 static bool mountSPIFFS(
bool formatOnFail,
char const * mountPath,
size_t maxFiles = 4);
855 static bool getFSInfo(
DriveType driveType,
int drive, int64_t * total, int64_t * used);
860 int countDirEntries(
int * namesLength);
863 static bool s_SPIFFSMounted;
864 static char const * s_SPIFFSMountPath;
865 static size_t s_SPIFFSMaxFiles;
868 static bool s_SDCardMounted;
869 static char const * s_SDCardMountPath;
870 static size_t s_SDCardMaxFiles;
871 static int s_SDCardAllocationUnitSize;
872 static int8_t s_SDCardMISO;
873 static int8_t s_SDCardMOSI;
874 static int8_t s_SDCardCLK;
875 static int8_t s_SDCardCS;
876 static sdmmc_card_t * s_SDCard;
882 bool m_includeHiddenFiles;
883 char * m_namesStorage;
893 bool clipLine(
int & x1,
int & y1,
int & x2,
int & y2, Rect
const & clipRect,
bool checkOnly);
896 void removeRectangle(Stack<Rect> & rects, Rect
const & mainRect, Rect
const & rectToRemove);
899 bool calcParity(uint8_t v);
903 void * realloc32(
void * ptr,
size_t size);
904 void free32(
void * ptr);
907 inline gpio_num_t int2gpio(
int gpio)
909 return gpio == -1 ? GPIO_UNUSED : (gpio_num_t)gpio;
914 inline char digit2hex(
int digit)
916 return digit < 10 ?
'0' + digit :
'a' + digit - 10;
921 inline int hex2digit(
char hex)
923 return hex <
'a' ? hex -
'0' : hex -
'a' + 10;
929 uint32_t msToTicks(
int ms);
946 inline __attribute__((always_inline)) uint32_t getCycleCount() {
948 __asm__ __volatile__(
950 "rsr %0, ccount \n\t" 962 void replacePathSep(
char * path,
char newSep);
972 inline uint32_t
UARTConf(
int parity,
int dataLength,
int stopBits)
974 uint32_t w = 0x8000000 | (dataLength << 2) | (stopBits << 4);
976 w |= (parity == 1 ? 0b10 : 0b11);
981 adc1_channel_t ADC1_GPIO2Channel(gpio_num_t gpio);
984 void esp_intr_alloc_pinnedToCore(
int source,
int flags, intr_handler_t handler,
void * arg, intr_handle_t * ret_handle,
int core);
993 void configureGPIO(gpio_num_t gpio, gpio_mode_t mode);
996 uint32_t getApbFrequency();
998 uint32_t getCPUFrequencyMHz();
1004 struct AutoSemaphore {
1005 AutoSemaphore(SemaphoreHandle_t mutex) : m_mutex(mutex) { xSemaphoreTake(m_mutex, portMAX_DELAY); }
1006 ~AutoSemaphore() { xSemaphoreGive(m_mutex); }
1008 SemaphoreHandle_t m_mutex;
1021 static int busiestCore() {
return s_busiestCore; }
1022 static int quietCore() {
return s_busiestCore != -1 ? s_busiestCore ^ 1 : -1; }
1023 static void setBusiestCore(
int core) { s_busiestCore = core; }
1026 static int s_busiestCore;
1352 #define ASCII_NUL 0x00 // Null 1353 #define ASCII_SOH 0x01 // Start of Heading 1354 #define ASCII_CTRLA 0x01 // CTRL-A 1355 #define ASCII_STX 0x02 // Start of Text 1356 #define ASCII_CTRLB 0x02 // CTRL-B 1357 #define ASCII_ETX 0x03 // End Of Text 1358 #define ASCII_CTRLC 0x03 // CTRL-C 1359 #define ASCII_EOT 0x04 // End Of Transmission 1360 #define ASCII_CTRLD 0x04 // CTRL-D 1361 #define ASCII_ENQ 0x05 // Enquiry 1362 #define ASCII_CTRLE 0x05 // CTRL-E 1363 #define ASCII_ACK 0x06 // Acknowledge 1364 #define ASCII_CTRLF 0x06 // CTRL-F 1365 #define ASCII_BEL 0x07 // Bell 1366 #define ASCII_CTRLG 0x07 // CTRL-G 1367 #define ASCII_BS 0x08 // Backspace 1368 #define ASCII_CTRLH 0x08 // CTRL-H 1369 #define ASCII_HT 0x09 // Horizontal Tab 1370 #define ASCII_TAB 0x09 // Horizontal Tab 1371 #define ASCII_CTRLI 0x09 // CTRL-I 1372 #define ASCII_LF 0x0A // Line Feed 1373 #define ASCII_CTRLJ 0x0A // CTRL-J 1374 #define ASCII_VT 0x0B // Vertical Tab 1375 #define ASCII_CTRLK 0x0B // CTRL-K 1376 #define ASCII_FF 0x0C // Form Feed 1377 #define ASCII_CTRLL 0x0C // CTRL-L 1378 #define ASCII_CR 0x0D // Carriage Return 1379 #define ASCII_CTRLM 0x0D // CTRL-M 1380 #define ASCII_SO 0x0E // Shift Out 1381 #define ASCII_CTRLN 0x0E // CTRL-N 1382 #define ASCII_SI 0x0F // Shift In 1383 #define ASCII_CTRLO 0x0F // CTRL-O 1384 #define ASCII_DLE 0x10 // Data Link Escape 1385 #define ASCII_CTRLP 0x10 // CTRL-P 1386 #define ASCII_DC1 0x11 // Device Control 1 1387 #define ASCII_CTRLQ 0x11 // CTRL-Q 1388 #define ASCII_XON 0x11 // Transmission On 1389 #define ASCII_DC2 0x12 // Device Control 2 1390 #define ASCII_CTRLR 0x12 // CTRL-R 1391 #define ASCII_DC3 0x13 // Device Control 3 1392 #define ASCII_XOFF 0x13 // Transmission Off 1393 #define ASCII_CTRLS 0x13 // CTRL-S 1394 #define ASCII_DC4 0x14 // Device Control 4 1395 #define ASCII_CTRLT 0x14 // CTRL-T 1396 #define ASCII_NAK 0x15 // Negative Acknowledge 1397 #define ASCII_CTRLU 0x15 // CTRL-U 1398 #define ASCII_SYN 0x16 // Synchronous Idle 1399 #define ASCII_CTRLV 0x16 // CTRL-V 1400 #define ASCII_ETB 0x17 // End-of-Transmission-Block 1401 #define ASCII_CTRLW 0x17 // CTRL-W 1402 #define ASCII_CAN 0x18 // Cancel 1403 #define ASCII_CTRLX 0x18 // CTRL-X 1404 #define ASCII_EM 0x19 // End of Medium 1405 #define ASCII_CTRLY 0x19 // CTRL-Y 1406 #define ASCII_SUB 0x1A // Substitute 1407 #define ASCII_CTRLZ 0x1A // CTRL-Z 1408 #define ASCII_ESC 0x1B // Escape 1409 #define ASCII_FS 0x1C // File Separator 1410 #define ASCII_GS 0x1D // Group Separator 1411 #define ASCII_RS 0x1E // Record Separator 1412 #define ASCII_US 0x1F // Unit Separator 1413 #define ASCII_SPC 0x20 // Space 1414 #define ASCII_DEL 0x7F // Delete
uint32_t UARTConf(int parity, int dataLength, int stopBits)
Composes UART configuration word.
bool truncate(char const *name, size_t size)
Truncates a file to the specified size.
void rename(char const *oldName, char const *newName)
Renames a file.
bool filePathExists(char const *filepath)
Determines if a file exists.
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.
A struct which contains a virtual key, key state and associated scan code.
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
size_t fileSize(char const *name)
Determines file size.
This class helps to choice a core for intensive processing tasks.
bool fileUpdateDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file update date and time.
Describes mouse absolute position, scroll wheel delta and buttons status.
bool fileCreationDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file creation date and time.
static void unmountSDCard()
Unmounts filesystem on SD Card.
Represents the coordinate of a point.
FileBrowser item specificator.
char * createTempFilename()
Creates a random temporary filename, with absolute path.
int getFullPath(char const *name, char *outPath=nullptr, int maxlen=0)
Composes a full file path given a relative name.
DriveType getCurrentDriveType()
Returns the drive type of current directory.
static bool format(DriveType driveType, int drive)
Formats SPIFFS or SD Card.
DriveType
This enum defines drive types (SPIFFS or SD Card)
static bool remountSDCard()
Remounts SDCard filesystem, using the same parameters.
char const * directory()
Determines absolute path of current directory.
void setSorted(bool value)
Determines if the items are sorted.
ChipPackage
This enum defines ESP32 module types (packages)
Represents a bidimensional size.
void makeDirectory(char const *dirname)
Creates a directory.
bool fileAccessDate(char const *name, int *year, int *month, int *day, int *hour, int *minutes, int *seconds)
Gets file access date and time.
static DriveType getDriveType(char const *path)
Returns the drive type of specified path.
void changeDirectory(const char *subdir)
Sets relative directory path.
FILE * openFile(char const *filename, char const *mode)
Opens a file from current directory.
static bool mountSPIFFS(bool formatOnFail, char const *mountPath, size_t maxFiles=4)
Mounts filesystem on SPIFFS (Flash)
bool exists(char const *name, bool caseSensitive=true)
Determines if a file or directory exists.
int count()
Determines number of files in current directory.
bool reload()
Reloads directory content.
static bool getFSInfo(DriveType driveType, int drive, int64_t *total, int64_t *used)
Gets total and free space on a filesystem.
static bool remountSPIFFS()
Remounts SPIFFS filesystem, using the same parameters.
static void unmountSPIFFS()
Unmounts filesystem on SPIFFS (Flash)
FileBrowser allows basic file system operations (dir, mkdir, remove and rename)