39#include "freertos/FreeRTOS.h"
40#include "freertos/semphr.h"
42#include <driver/adc.h>
43#include <esp_system.h>
45#include "soc/frc_timer_reg.h"
53 #define FABGL_ESP_IDF_VERSION_VAL ESP_IDF_VERSION_VAL
54 #define FABGL_ESP_IDF_VERSION ESP_IDF_VERSION
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)
62#define GPIO_UNUSED (GPIO_NUM_MAX)
63#define GPIO_AUTO ((gpio_num_t)(GPIO_NUM_MAX + 1))
74 #ifdef BOARD_HAS_PSRAM
75 #define FABGL_NEED_PSRAM_DISABLE_HACK
78 #ifdef CONFIG_SPIRAM_SUPPORT
79 #define FABGL_NEED_PSRAM_DISABLE_HACK
83#ifdef FABGL_NEED_PSRAM_DISABLE_HACK
84 #define PSRAM_HACK asm(" nop")
91#define ASM_MEMW asm(" MEMW");
93#define ASM_NOP asm(" NOP");
95#define PSRAM_WORKAROUND1 asm(" nop;nop;nop;nop");
96#define PSRAM_WORKAROUND2 asm(" memw");
104#define TORAD(a) ((a) * M_PI / 180.)
112const T & tmax(
const T & a,
const T & b)
114 return (a < b) ? b : a;
118constexpr auto imax = tmax<int>;
122const T & tmin(
const T & a,
const T & b)
124 return !(b < a) ? a : b;
128constexpr auto imin = tmin<int>;
133const T & tclamp(
const T & v,
const T & lo,
const T & hi)
135 return (v < lo ? lo : (v > hi ? hi : v));
139constexpr auto iclamp = tclamp<int>;
143const T & twrap(
const T & v,
const T & lo,
const T & hi)
145 return (v < lo ? hi : (v > hi ? lo : v));
150void tswap(T & v1, T & v2)
158constexpr auto iswap = tswap<int>;
162T moveItems(T dest, T src,
size_t n)
168 for (pd += n, ps += n; n--;)
178void rgb222_to_hsv(
int R,
int G,
int B,
double * h,
double * s,
double * v);
181inline uint16_t changeEndiannesWord(uint16_t value)
183 return ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
187inline uint32_t changeEndiannesDWord(uint32_t value)
189 return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | ((value & 0xff000000) >> 24);
200void APLLCalcParams(
double freq, APLLParams * params, uint8_t * a, uint8_t * b,
double * out_freq,
double * error);
202int calcI2STimingParams(
int sampleRate,
int * outA,
int * outB,
int * outN,
int * outM);
218 Point(
int X_,
int Y_) :
X(X_),
Y(Y_) { }
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));
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));
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; }
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));
310#define FONTINFOFLAGS_ITALIC 1
311#define FONTINFOFLAGS_UNDERLINE 2
312#define FONTINFODLAFS_STRIKEOUT 4
313#define FONTINFOFLAGS_VARWIDTH 8
331 uint8_t
const *
data;
332 uint32_t
const * chptr;
347 bool expired(
int valueMS);
363 StackItem(StackItem * next_, T
const & item_) : next(next_), item(item_) { }
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);
376 StackItem<T> * iptr = m_items;
377 m_items = iptr->next;
386 for (
auto i = m_items; i; i = i->next)
391 StackItem<T> * m_items;
399template <
typename ...Params>
403 Delegate() : m_func(nullptr) {
407 Delegate(
const Delegate & c) =
delete;
410 template <
typename Func>
411 Delegate(Func f) : Delegate() {
420 template <
typename Func>
421 void operator=(Func f) {
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));
429 void operator=(
const Delegate&) =
delete;
431 void operator()(
const Params & ...params) {
433 m_closure(m_func, params...);
438 void (*m_closure)(
void * func,
const Params & ...params);
443 heap_caps_free(m_func);
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]; }
468 void select(
int index,
bool value);
470 bool selected(
int index);
471 int getFirstSelected();
472 void copyFrom(StringList
const & src);
473 void copySelectionMapFrom(StringList
const & src);
476 void checkAllocatedSpace(
int requiredItems);
478 char const * * m_items;
489 uint16_t m_allocated;
501class LightMemoryPool {
503 LightMemoryPool(
int poolSize);
505 void * alloc(
int size);
506 void free(
void * mem) {
if (mem) markFree((uint8_t*)mem - m_mem - 2); }
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);
619 bool exists(
char const * name,
bool caseSensitive =
true);
654 bool fileCreationDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
669 bool fileUpdateDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
684 bool fileAccessDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
693 void setIncludeHiddenFiles(
bool value) { m_includeHiddenFiles = value; }
709 void remove(
char const * name);
717 void rename(
char const * oldName,
char const * newName);
727 bool truncate(
char const * name,
size_t size);
745 int getFullPath(
char const * name,
char * outPath =
nullptr,
int maxlen = 0);
755 FILE *
openFile(
char const * filename,
char const * mode);
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);
820 static bool mountedSDCard() {
return s_SDCardMounted; }
841 static bool mountSPIFFS(
bool formatOnFail,
char const * mountPath,
size_t maxFiles = 4);
877 static bool getFSInfo(
DriveType driveType,
int drive, int64_t * total, int64_t * used);
882 int countDirEntries(
int * namesLength);
885 static bool s_SPIFFSMounted;
886 static char const * s_SPIFFSMountPath;
887 static size_t s_SPIFFSMaxFiles;
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;
904 bool m_includeHiddenFiles;
905 char * m_namesStorage;
915bool clipLine(
int & x1,
int & y1,
int & x2,
int & y2, Rect
const & clipRect,
bool checkOnly);
918void removeRectangle(Stack<Rect> & rects, Rect
const & mainRect, Rect
const & rectToRemove);
921bool calcParity(uint8_t v);
925void * realloc32(
void * ptr,
size_t size);
926void free32(
void * ptr);
929inline gpio_num_t int2gpio(
int gpio)
931 return gpio == -1 ? GPIO_UNUSED : (gpio_num_t)gpio;
936inline char digit2hex(
int digit)
938 return digit < 10 ?
'0' + digit :
'a' + digit - 10;
943inline int hex2digit(
char hex)
945 return hex <
'a' ? hex -
'0' : hex -
'a' + 10;
951uint32_t msToTicks(
int ms);
968inline __attribute__((always_inline)) uint32_t getCycleCount() {
970 __asm__ __volatile__(
972 "rsr %0, ccount \n\t"
984void replacePathSep(
char * path,
char newSep);
987adc1_channel_t ADC1_GPIO2Channel(gpio_num_t gpio);
990void esp_intr_alloc_pinnedToCore(
int source,
int flags, intr_handler_t handler,
void * arg, intr_handle_t * ret_handle,
int core);
999void configureGPIO(gpio_num_t gpio, gpio_mode_t mode);
1002uint32_t getApbFrequency();
1004uint32_t getCPUFrequencyMHz();
1013constexpr int FRC1TimerMax = 8388607;
1018inline void FRC1Timer_init(
int prescaler)
1020 REG_WRITE(FRC_TIMER_LOAD_REG(0), 0);
1021 REG_WRITE(FRC_TIMER_CTRL_REG(0), prescaler | FRC_TIMER_ENABLE);
1025inline uint32_t FRC1Timer()
1027 return FRC1TimerMax - REG_READ(FRC_TIMER_COUNT_REG(0));
1035struct AutoSemaphore {
1036 AutoSemaphore(SemaphoreHandle_t mutex) : m_mutex(mutex) { xSemaphoreTake(m_mutex, portMAX_DELAY); }
1037 ~AutoSemaphore() { xSemaphoreGive(m_mutex); }
1039 SemaphoreHandle_t m_mutex;
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; }
1057 static int s_busiestCore;
1082 static VideoMode get() {
return s_videoMode; }
1083 static void set(
VideoMode value) { s_videoMode = value; }
1368 VK_KATAKANA_HIRAGANA_ROMAJI,
1369 VK_HANKAKU_ZENKAKU_KANJI,
1429#define ASCII_NUL 0x00
1430#define ASCII_SOH 0x01
1431#define ASCII_CTRLA 0x01
1432#define ASCII_STX 0x02
1433#define ASCII_CTRLB 0x02
1434#define ASCII_ETX 0x03
1435#define ASCII_CTRLC 0x03
1436#define ASCII_EOT 0x04
1437#define ASCII_CTRLD 0x04
1438#define ASCII_ENQ 0x05
1439#define ASCII_CTRLE 0x05
1440#define ASCII_ACK 0x06
1441#define ASCII_CTRLF 0x06
1442#define ASCII_BEL 0x07
1443#define ASCII_CTRLG 0x07
1444#define ASCII_BS 0x08
1445#define ASCII_CTRLH 0x08
1446#define ASCII_HT 0x09
1447#define ASCII_TAB 0x09
1448#define ASCII_CTRLI 0x09
1449#define ASCII_LF 0x0A
1450#define ASCII_CTRLJ 0x0A
1451#define ASCII_VT 0x0B
1452#define ASCII_CTRLK 0x0B
1453#define ASCII_FF 0x0C
1454#define ASCII_CTRLL 0x0C
1455#define ASCII_CR 0x0D
1456#define ASCII_CTRLM 0x0D
1457#define ASCII_SO 0x0E
1458#define ASCII_CTRLN 0x0E
1459#define ASCII_SI 0x0F
1460#define ASCII_CTRLO 0x0F
1461#define ASCII_DLE 0x10
1462#define ASCII_CTRLP 0x10
1463#define ASCII_DC1 0x11
1464#define ASCII_CTRLQ 0x11
1465#define ASCII_XON 0x11
1466#define ASCII_DC2 0x12
1467#define ASCII_CTRLR 0x12
1468#define ASCII_DC3 0x13
1469#define ASCII_XOFF 0x13
1470#define ASCII_CTRLS 0x13
1471#define ASCII_DC4 0x14
1472#define ASCII_CTRLT 0x14
1473#define ASCII_NAK 0x15
1474#define ASCII_CTRLU 0x15
1475#define ASCII_SYN 0x16
1476#define ASCII_CTRLV 0x16
1477#define ASCII_ETB 0x17
1478#define ASCII_CTRLW 0x17
1479#define ASCII_CAN 0x18
1480#define ASCII_CTRLX 0x18
1481#define ASCII_EM 0x19
1482#define ASCII_CTRLY 0x19
1483#define ASCII_SUB 0x1A
1484#define ASCII_CTRLZ 0x1A
1485#define ASCII_ESC 0x1B
1486#define ASCII_FS 0x1C
1487#define ASCII_GS 0x1D
1488#define ASCII_RS 0x1E
1489#define ASCII_US 0x1F
1490#define ASCII_SPC 0x20
1491#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)
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.
VideoMode
Specifies a video mode.
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.
This class helps to know which is the current video output (VGA or Composite)
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.