35 #include "freertos/FreeRTOS.h" 36 #include "freertos/semphr.h" 38 #include <driver/adc.h> 45 #define GPIO_UNUSED GPIO_NUM_MAX 56 #ifdef BOARD_HAS_PSRAM 57 #define FABGL_NEED_PSRAM_DISABLE_HACK 60 #ifdef CONFIG_SPIRAM_SUPPORT 61 #define FABGL_NEED_PSRAM_DISABLE_HACK 65 #ifdef FABGL_NEED_PSRAM_DISABLE_HACK 66 #define PSRAM_HACK asm(" nop") 81 const T & tmax(
const T & a,
const T & b)
83 return (a < b) ? b : a;
87 constexpr
auto imax = tmax<int>;
91 const T & tmin(
const T & a,
const T & b)
93 return !(b < a) ? a : b;
97 constexpr
auto imin = tmin<int>;
101 template <
typename T>
102 const T & tclamp(
const T & v,
const T & lo,
const T & hi)
104 return (v < lo ? lo : (v > hi ? hi : v));
108 constexpr
auto iclamp = tclamp<int>;
111 template <
typename T>
112 const T & twrap(
const T & v,
const T & lo,
const T & hi)
114 return (v < lo ? hi : (v > hi ? lo : v));
118 template <
typename T>
119 void tswap(T & v1, T & v2)
127 constexpr
auto iswap = tswap<int>;
130 template <
typename T>
131 T moveItems(T dest, T src,
size_t n)
137 for (pd += n, ps += n; n--;)
147 void rgb222_to_hsv(
int R,
int G,
int B,
double * h,
double * s,
double * v);
163 Point(
int X_,
int Y_) :
X(X_),
Y(Y_) { }
165 Point add(Point
const & p)
const {
return Point(
X + p.X,
Y + p.Y); }
166 Point sub(Point
const & p)
const {
return Point(
X - p.X,
Y - p.Y); }
167 Point neg()
const {
return Point(-
X, -
Y); }
168 bool operator==(Point
const & r) {
return X == r.X &&
Y == r.Y; }
169 bool operator!=(Point
const & r) {
return X != r.X ||
Y != r.Y; }
170 } __attribute__ ((packed));
182 } __attribute__ ((packed));
198 Rect(
int X1_,
int Y1_,
int X2_,
int Y2_) :
X1(X1_),
Y1(Y1_),
X2(X2_),
Y2(Y2_) { }
199 Rect(Rect
const & r) {
X1 = r.X1;
Y1 = r.Y1;
X2 = r.X2;
Y2 = r.Y2; }
201 bool operator==(Rect
const & r) {
return X1 == r.X1 &&
Y1 == r.Y1 &&
X2 == r.X2 &&
Y2 == r.Y2; }
202 bool operator!=(Rect
const & r) {
return X1 != r.X1 ||
Y1 != r.Y1 ||
X2 != r.X2 ||
Y2 != r.Y2; }
203 Point pos()
const {
return Point(
X1,
Y1); }
204 Size size()
const {
return Size(
X2 -
X1 + 1,
Y2 -
Y1 + 1); }
205 int width()
const {
return X2 -
X1 + 1; }
206 int height()
const {
return Y2 -
Y1 + 1; }
207 Rect translate(
int offsetX,
int offsetY)
const {
return Rect(
X1 + offsetX,
Y1 + offsetY,
X2 + offsetX,
Y2 + offsetY); }
208 Rect translate(Point
const & offset)
const {
return Rect(
X1 + offset.X,
Y1 + offset.Y,
X2 + offset.X,
Y2 + offset.Y); }
209 Rect move(Point
const & position)
const {
return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
210 Rect move(
int x,
int y)
const {
return Rect(x, y, x + width() - 1, y + height() - 1); }
211 Rect shrink(
int value)
const {
return Rect(
X1 + value,
Y1 + value,
X2 - value,
Y2 - value); }
212 Rect hShrink(
int value)
const {
return Rect(
X1 + value,
Y1,
X2 - value,
Y2); }
213 Rect vShrink(
int value)
const {
return Rect(
X1,
Y1 + value,
X2,
Y2 - value); }
214 Rect resize(
int width,
int height)
const {
return Rect(
X1,
Y1,
X1 + width - 1,
Y1 + height - 1); }
215 Rect resize(Size size)
const {
return Rect(
X1,
Y1,
X1 + size.width - 1,
Y1 + size.height - 1); }
216 Rect intersection(Rect
const & rect)
const;
217 bool intersects(Rect
const & rect)
const {
return X1 <= rect.X2 &&
X2 >= rect.X1 &&
Y1 <= rect.Y2 &&
Y2 >= rect.Y1; }
218 bool contains(Rect
const & rect)
const {
return (rect.X1 >=
X1) && (rect.Y1 >=
Y1) && (rect.X2 <=
X2) && (rect.Y2 <=
Y2); }
219 bool contains(Point
const & point)
const {
return point.X >=
X1 && point.Y >=
Y1 && point.X <=
X2 && point.Y <=
Y2; }
220 bool contains(
int x,
int y)
const {
return x >=
X1 && y >=
Y1 && x <=
X2 && y <=
Y2; }
221 Rect merge(Rect
const & rect)
const;
222 } __attribute__ ((packed));
253 #define FONTINFOFLAGS_ITALIC 1 254 #define FONTINFOFLAGS_UNDERLINE 2 255 #define FONTINFODLAFS_STRIKEOUT 4 256 #define FONTINFOFLAGS_VARWIDTH 8 274 uint8_t
const *
data;
275 uint32_t
const * chptr;
289 bool expired(
int valueMS);
301 template <
typename T>
305 StackItem(StackItem * next_, T
const & item_) : next(next_), item(item_) { }
308 template <
typename T>
311 Stack() : m_items(nullptr) { }
312 bool isEmpty() {
return m_items ==
nullptr; }
313 void push(T
const & value) {
314 m_items =
new StackItem<T>(m_items, value);
318 StackItem<T> * iptr = m_items;
319 m_items = iptr->next;
328 for (
auto i = m_items; i; i = i->next)
333 StackItem<T> * m_items;
341 template <
typename ...Params>
344 template <
typename Func>
345 void operator=(Func f) {
346 m_closure = [] (
void * func,
const Params & ...params) ->
void { (*(Func *)func)(params...); };
347 m_func = heap_caps_malloc(
sizeof(Func), MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL);
348 moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f,
sizeof(Func) /
sizeof(uint32_t));
352 heap_caps_free(m_func);
355 void operator()(
const Params & ...params) {
357 m_closure(m_func, params...);
361 void (*m_closure)(
void * func,
const Params & ...params);
362 void * m_func =
nullptr;
375 int append(
char const * str);
376 int appendFmt(
const char *format, ...);
377 void append(
char const * strlist[],
int count);
378 void insert(
int index,
char const * str);
379 void set(
int index,
char const * str);
380 void remove(
int index);
381 int count() {
return m_count; }
382 char const *
get(
int index) {
return m_items[index]; }
385 void select(
int index,
bool value);
387 bool selected(
int index);
388 void copyFrom(StringList
const & src);
391 void checkAllocatedSpace(
int requiredItems);
393 char const * * m_items;
404 uint16_t m_allocated;
416 class LightMemoryPool {
418 LightMemoryPool(
int poolSize);
420 void * alloc(
int size);
421 void free(
void * mem) {
if (mem) markFree((uint8_t*)mem - m_mem - 2); }
430 void mark(
int pos, int16_t size,
bool allocated);
431 void markFree(
int pos) { m_mem[pos + 1] &= 0x7f; }
432 int16_t getSize(
int pos);
433 bool isFree(
int pos);
519 DirItem const *
get(
int index) {
return m_items + index; }
529 bool exists(
char const * name,
bool caseSensitive =
true);
553 bool fileCreationDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
568 bool fileUpdateDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
583 bool fileAccessDate(
char const * name,
int * year,
int * month,
int * day,
int * hour,
int * minutes,
int * seconds);
592 void setIncludeHiddenFiles(
bool value) { m_includeHiddenFiles = value; }
608 void remove(
char const * name);
616 void rename(
char const * oldName,
char const * newName);
626 bool truncate(
char const * name,
size_t size);
644 int getFullPath(
char const * name,
char * outPath =
nullptr,
int maxlen = 0);
654 FILE *
openFile(
char const * filename,
char const * mode);
710 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);
738 static bool mountSPIFFS(
bool formatOnFail,
char const * mountPath,
size_t maxFiles = 4);
774 static bool getFSInfo(
DriveType driveType,
int drive, int64_t * total, int64_t * used);
779 int countDirEntries(
int * namesLength);
782 static bool s_SPIFFSMounted;
783 static char const * s_SPIFFSMountPath;
784 static size_t s_SPIFFSMaxFiles;
787 static bool s_SDCardMounted;
788 static char const * s_SDCardMountPath;
789 static size_t s_SDCardMaxFiles;
790 static int s_SDCardAllocationUnitSize;
791 static int8_t s_SDCardMISO;
792 static int8_t s_SDCardMOSI;
793 static int8_t s_SDCardCLK;
794 static int8_t s_SDCardCS;
800 bool m_includeHiddenFiles;
801 char * m_namesStorage;
811 bool clipLine(
int & x1,
int & y1,
int & x2,
int & y2, Rect
const & clipRect,
bool checkOnly);
814 void removeRectangle(Stack<Rect> & rects, Rect
const & mainRect, Rect
const & rectToRemove);
817 bool calcParity(uint8_t v);
821 void * realloc32(
void * ptr,
size_t size);
822 void free32(
void * ptr);
825 inline gpio_num_t int2gpio(
int gpio)
827 return gpio == -1 ? GPIO_UNUSED : (gpio_num_t)gpio;
832 inline char digit2hex(
int digit)
834 return digit < 10 ?
'0' + digit :
'a' + digit - 10;
839 inline int hex2digit(
char hex)
841 return hex <
'a' ? hex -
'0' : hex -
'a' + 10;
847 uint32_t msToTicks(
int ms);
850 enum class ChipPackage {
859 ChipPackage getChipPackage();
861 inline __attribute__((always_inline)) uint32_t getCycleCount() {
863 __asm__ __volatile__(
865 "rsr %0, ccount \n\t" 877 void replacePathSep(
char * path,
char newSep);
887 inline uint32_t
UARTConf(
int parity,
int dataLength,
int stopBits)
889 uint32_t w = 0x8000000 | (dataLength << 2) | (stopBits << 4);
891 w |= (parity == 1 ? 0b10 : 0b11);
896 adc1_channel_t ADC1_GPIO2Channel(gpio_num_t gpio);
899 void esp_intr_alloc_pinnedToCore(
int source,
int flags, intr_handler_t handler,
void * arg, intr_handle_t * ret_handle,
int core);
908 void configureGPIO(gpio_num_t gpio, gpio_mode_t mode);
911 uint32_t getApbFrequency();
913 uint32_t getCPUFrequencyMHz();
919 struct AutoSemaphore {
920 AutoSemaphore(SemaphoreHandle_t mutex) : m_mutex(mutex) { xSemaphoreTake(m_mutex, portMAX_DELAY); }
921 ~AutoSemaphore() { xSemaphoreGive(m_mutex); }
923 SemaphoreHandle_t m_mutex;
936 static int busiestCore() {
return s_busiestCore; }
937 static int quietCore() {
return s_busiestCore != -1 ? s_busiestCore ^ 1 : -1; }
938 static void setBusiestCore(
int core) { s_busiestCore = core; }
941 static int s_busiestCore;
1207 #define ASCII_NUL 0x00 // Null 1208 #define ASCII_SOH 0x01 // Start of Heading 1209 #define ASCII_CTRLA 0x01 // CTRL-A 1210 #define ASCII_STX 0x02 // Start of Text 1211 #define ASCII_CTRLB 0x02 // CTRL-B 1212 #define ASCII_ETX 0x03 // End Of Text 1213 #define ASCII_CTRLC 0x03 // CTRL-C 1214 #define ASCII_EOT 0x04 // End Of Transmission 1215 #define ASCII_CTRLD 0x04 // CTRL-D 1216 #define ASCII_ENQ 0x05 // Enquiry 1217 #define ASCII_CTRLE 0x05 // CTRL-E 1218 #define ASCII_ACK 0x06 // Acknowledge 1219 #define ASCII_CTRLF 0x06 // CTRL-F 1220 #define ASCII_BEL 0x07 // Bell 1221 #define ASCII_CTRLG 0x07 // CTRL-G 1222 #define ASCII_BS 0x08 // Backspace 1223 #define ASCII_CTRLH 0x08 // CTRL-H 1224 #define ASCII_HT 0x09 // Horizontal Tab 1225 #define ASCII_TAB 0x09 // Horizontal Tab 1226 #define ASCII_CTRLI 0x09 // CTRL-I 1227 #define ASCII_LF 0x0A // Line Feed 1228 #define ASCII_CTRLJ 0x0A // CTRL-J 1229 #define ASCII_VT 0x0B // Vertical Tab 1230 #define ASCII_CTRLK 0x0B // CTRL-K 1231 #define ASCII_FF 0x0C // Form Feed 1232 #define ASCII_CTRLL 0x0C // CTRL-L 1233 #define ASCII_CR 0x0D // Carriage Return 1234 #define ASCII_CTRLM 0x0D // CTRL-M 1235 #define ASCII_SO 0x0E // Shift Out 1236 #define ASCII_CTRLN 0x0E // CTRL-N 1237 #define ASCII_SI 0x0F // Shift In 1238 #define ASCII_CTRLO 0x0F // CTRL-O 1239 #define ASCII_DLE 0x10 // Data Link Escape 1240 #define ASCII_CTRLP 0x10 // CTRL-P 1241 #define ASCII_DC1 0x11 // Device Control 1 1242 #define ASCII_CTRLQ 0x11 // CTRL-Q 1243 #define ASCII_XON 0x11 // Transmission On 1244 #define ASCII_DC2 0x12 // Device Control 2 1245 #define ASCII_CTRLR 0x12 // CTRL-R 1246 #define ASCII_DC3 0x13 // Device Control 3 1247 #define ASCII_XOFF 0x13 // Transmission Off 1248 #define ASCII_CTRLS 0x13 // CTRL-S 1249 #define ASCII_DC4 0x14 // Device Control 4 1250 #define ASCII_CTRLT 0x14 // CTRL-T 1251 #define ASCII_NAK 0x15 // Negative Acknowledge 1252 #define ASCII_CTRLU 0x15 // CTRL-U 1253 #define ASCII_SYN 0x16 // Synchronous Idle 1254 #define ASCII_CTRLV 0x16 // CTRL-V 1255 #define ASCII_ETB 0x17 // End-of-Transmission-Block 1256 #define ASCII_CTRLW 0x17 // CTRL-W 1257 #define ASCII_CAN 0x18 // Cancel 1258 #define ASCII_CTRLX 0x18 // CTRL-X 1259 #define ASCII_EM 0x19 // End of Medium 1260 #define ASCII_CTRLY 0x19 // CTRL-Y 1261 #define ASCII_SUB 0x1A // Substitute 1262 #define ASCII_CTRLZ 0x1A // CTRL-Z 1263 #define ASCII_ESC 0x1B // Escape 1264 #define ASCII_FS 0x1C // File Separator 1265 #define ASCII_GS 0x1D // Group Separator 1266 #define ASCII_RS 0x1E // Record Separator 1267 #define ASCII_US 0x1F // Unit Separator 1268 #define ASCII_SPC 0x20 // Space 1269 #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.
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.
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.
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)