FabGL
ESP32 VGA 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 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 
24 #pragma once
25 
26 
35 #include "freertos/FreeRTOS.h"
36 
37 
38 namespace fabgl {
39 
40 
41 
43 
44 
45 // Integer square root by Halleck's method, with Legalize's speedup
46 int isqrt (int x);
47 
48 
49 template <typename T>
50 const T & tmax(const T & a, const T & b)
51 {
52  return (a < b) ? b : a;
53 }
54 
55 
56 constexpr auto imax = tmax<int>;
57 
58 
59 template <typename T>
60 const T & tmin(const T & a, const T & b)
61 {
62  return !(b < a) ? a : b;
63 }
64 
65 
66 constexpr auto imin = tmin<int>;
67 
68 
69 
70 template <typename T>
71 const T & tclamp(const T & v, const T & lo, const T & hi)
72 {
73  return (v < lo ? lo : (v > hi ? hi : v));
74 }
75 
76 
77 constexpr auto iclamp = tclamp<int>;
78 
79 
80 template <typename T>
81 const T & twrap(const T & v, const T & lo, const T & hi)
82 {
83  return (v < lo ? hi : (v > hi ? lo : v));
84 }
85 
86 
87 template <typename T>
88 void tswap(T & v1, T & v2)
89 {
90  T t = v1;
91  v1 = v2;
92  v2 = t;
93 }
94 
95 
96 constexpr auto iswap = tswap<int>;
97 
98 
99 template <typename T>
100 T moveItems(T dest, T src, size_t n)
101 {
102  T pd = dest;
103  T ps = src;
104  if (pd != ps) {
105  if (ps < pd)
106  for (pd += n, ps += n; n--;)
107  *--pd = *--ps;
108  else
109  while (n--)
110  *pd++ = *ps++;
111  }
112  return dest;
113 }
114 
115 
116 
118 
119 
125 struct Point {
126  int16_t X;
127  int16_t Y;
129  Point() : X(0), Y(0) { }
130  Point(int X_, int Y_) : X(X_), Y(Y_) { }
131 
132  Point add(Point const & p) const { return Point(X + p.X, Y + p.Y); }
133  Point sub(Point const & p) const { return Point(X - p.X, Y - p.Y); }
134  Point neg() const { return Point(-X, -Y); }
135  bool operator==(Point const & r) { return X == r.X && Y == r.Y; }
136  bool operator!=(Point const & r) { return X != r.X || Y != r.Y; }
137 };
138 
139 
143 struct Size {
144  int16_t width;
145  int16_t height;
147  Size() : width(0), height(0) { }
148  Size(int width_, int height_) : width(width_), height(height_) { }
149 };
150 
151 
152 
158 struct Rect {
159  int16_t X1;
160  int16_t Y1;
161  int16_t X2;
162  int16_t Y2;
164  Rect() : X1(0), Y1(0), X2(0), Y2(0) { }
165  Rect(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) { }
166  Rect(Rect const & r) { X1 = r.X1; Y1 = r.Y1; X2 = r.X2; Y2 = r.Y2; }
167 
168  bool operator==(Rect const & r) { return X1 == r.X1 && Y1 == r.Y1 && X2 == r.X2 && Y2 == r.Y2; }
169  bool operator!=(Rect const & r) { return X1 != r.X1 || Y1 != r.Y1 || X2 != r.X2 || Y2 != r.Y2; }
170  Point pos() const { return Point(X1, Y1); }
171  Size size() const { return Size(X2 - X1 + 1, Y2 - Y1 + 1); }
172  int width() const { return X2 - X1 + 1; }
173  int height() const { return Y2 - Y1 + 1; }
174  Rect translate(int offsetX, int offsetY) const { return Rect(X1 + offsetX, Y1 + offsetY, X2 + offsetX, Y2 + offsetY); }
175  Rect translate(Point const & offset) const { return Rect(X1 + offset.X, Y1 + offset.Y, X2 + offset.X, Y2 + offset.Y); }
176  Rect move(Point const & position) const { return Rect(position.X, position.Y, position.X + width() - 1, position.Y + height() - 1); }
177  Rect move(int x, int y) const { return Rect(x, y, x + width() - 1, y + height() - 1); }
178  Rect shrink(int value) const { return Rect(X1 + value, Y1 + value, X2 - value, Y2 - value); }
179  Rect hShrink(int value) const { return Rect(X1 + value, Y1, X2 - value, Y2); }
180  Rect vShrink(int value) const { return Rect(X1, Y1 + value, X2, Y2 - value); }
181  Rect resize(int width, int height) const { return Rect(X1, Y1, X1 + width - 1, Y1 + height - 1); }
182  Rect resize(Size size) const { return Rect(X1, Y1, X1 + size.width - 1, Y1 + size.height - 1); }
183  Rect intersection(Rect const & rect) const { return Rect(tmax(X1, rect.X1), tmax(Y1, rect.Y1), tmin(X2, rect.X2), tmin(Y2, rect.Y2)); }
184  bool intersects(Rect const & rect) const { return X1 <= rect.X2 && X2 >= rect.X1 && Y1 <= rect.Y2 && Y2 >= rect.Y1; }
185  bool contains(Rect const & rect) const { return (rect.X1 >= X1) && (rect.Y1 >= Y1) && (rect.X2 <= X2) && (rect.Y2 <= Y2); }
186  bool contains(Point const & point) const { return point.X >= X1 && point.Y >= Y1 && point.X <= X2 && point.Y <= Y2; }
187  bool contains(int x, int y) const { return x >= X1 && y >= Y1 && x <= X2 && y <= Y2; }
188 };
189 
190 
191 
195 struct MouseButtons {
196  uint8_t left : 1;
197  uint8_t middle : 1;
198  uint8_t right : 1;
199 };
200 
201 
202 
206 struct MouseStatus {
207  int16_t X;
208  int16_t Y;
209  int8_t wheelDelta;
211 };
212 
213 
214 
216 // TimeOut
217 
218 
219 struct TimeOut {
220  TimeOut();
221 
222  // -1 means "infinite", never times out
223  bool expired(int valueMS);
224 
225 private:
226  int64_t m_start;
227 };
228 
229 
230 
232 // Stack
233 
234 
235 template <typename T>
236 struct StackItem {
237  StackItem * next;
238  T item;
239  StackItem(StackItem * next_, T const & item_) : next(next_), item(item_) { }
240 };
241 
242 template <typename T>
243 class Stack {
244 public:
245  Stack() : m_items(nullptr) { }
246  bool isEmpty() { return m_items == nullptr; }
247  void push(T const & value) {
248  m_items = new StackItem<T>(m_items, value);
249  }
250  T pop() {
251  if (m_items) {
252  StackItem<T> * iptr = m_items;
253  m_items = iptr->next;
254  T r = iptr->item;
255  delete iptr;
256  return r;
257  } else
258  return T();
259  }
260  int count() {
261  int r = 0;
262  for (auto i = m_items; i; i = i->next)
263  ++r;
264  return r;
265  }
266 private:
267  StackItem<T> * m_items;
268 };
269 
270 
271 
273 // Delegate
274 
275 template <typename ...Params>
276 struct Delegate {
277 
278  template <typename Func>
279  void operator=(Func f) {
280  m_closure = [] (void * func, const Params & ...params) -> void { (*(Func *)func)(params...); };
281  m_func = heap_caps_malloc(sizeof(Func), MALLOC_CAP_32BIT);
282  moveItems<uint32_t*>((uint32_t*)m_func, (uint32_t*)&f, sizeof(Func) / sizeof(uint32_t));
283  }
284 
285  ~Delegate() {
286  heap_caps_free(m_func);
287  }
288 
289  void operator()(const Params & ...params) {
290  if (m_func)
291  m_closure(m_func, params...);
292  }
293 
294 private:
295  void (*m_closure)(void * func, const Params & ...params);
296  void * m_func = nullptr;
297 };
298 
299 
300 
302 // StringList
303 
304 class StringList {
305 
306 public:
307  StringList();
308  ~StringList();
309  int append(char const * str);
310  void insert(int index, char const * str);
311  void set(int index, char const * str);
312  void remove(int index);
313  int count() { return m_count; }
314  char const * get(int index) { return m_items[index]; }
315  void clear();
316  void takeStrings();
317  void select(int index, bool value);
318  void deselectAll();
319  bool selected(int index);
320  void copyFrom(StringList const & src);
321 
322 private:
323  void checkAllocatedSpace(int requiredItems);
324 
325  char const * * m_items;
326 
327  // each 32 bit word can select up to 32 items, one bit per item
328  uint32_t * m_selMap;
329 
330  // If true (default is false) all strings added (append/insert/set) are copied.
331  // Strings will be freed when no more used (destructor, clear(), etc...).
332  // This flag is permanently switched to True by takeStrings() call.
333  bool m_ownStrings;
334 
335  uint16_t m_count; // actual items
336  uint16_t m_allocated; // allocated items
337 
338 };
339 
340 
342 // FileBrowser
343 
344 
348 struct DirItem {
349  bool isDir;
350  char const * name;
351 };
352 
353 
357 class FileBrowser {
358 public:
359 
360  FileBrowser();
361  ~FileBrowser();
362 
368  void setDirectory(const char * path); // set absolute path
369 
375  void changeDirectory(const char * subdir); // set relative path
376 
380  void reload();
381 
387  char const * directory() { return m_dir; }
388 
394  int count() { return m_count; }
395 
403  DirItem const * get(int index) { return m_items + index; }
404 
412  bool exists(char const * name);
413 
419  void setSorted(bool value);
420 
421  void setIncludeHiddenFiles(bool value) { m_includeHiddenFiles = value; }
422 
428  void makeDirectory(char const * dirname);
429 
437  void remove(char const * name);
438 
445  void rename(char const * oldName, char const * newName);
446 
456  int getFullPath(char const * name, char * outPath = nullptr, int maxlen = 0);
457 
458 private:
459 
460  void clear();
461  int countDirEntries(int * namesLength);
462 
463  char * m_dir;
464  int m_count;
465  DirItem * m_items;
466  bool m_sorted;
467  bool m_includeHiddenFiles;
468  char * m_namesStorage;
469 };
470 
471 
472 
473 
475 
476 
477 
478 bool clipLine(int & x1, int & y1, int & x2, int & y2, Rect const & clipRect, bool checkOnly);
479 
480 
481 void removeRectangle(Stack<Rect> & rects, Rect const & mainRect, Rect const & rectToRemove);
482 
483 
484 bool calcParity(uint8_t v);
485 
486 // why these? this is like heap_caps_malloc with MALLOC_CAP_32BIT. Unfortunately
487 // heap_caps_malloc crashes, so we need this workaround.
488 void * realloc32(void * ptr, size_t size);
489 void free32(void * ptr);
490 
491 
492 void suspendInterrupts();
493 void resumeInterrupts();
494 
495 
497 
498 
688  VK_LAST, // marks the last virtual key
689 };
690 
691 
692 } // end of namespace
693 
694 
695 
Definition: fabutils.h:592
Definition: fabutils.h:636
Definition: fabutils.h:572
Definition: fabutils.h:672
Definition: fabutils.h:649
int16_t width
Definition: fabutils.h:144
Definition: fabutils.h:648
Definition: fabutils.h:521
uint8_t middle
Definition: fabutils.h:197
Definition: fabutils.h:655
Definition: fabutils.h:601
Definition: fabutils.h:617
uint8_t left
Definition: fabutils.h:196
Definition: fabutils.h:663
Definition: fabutils.h:597
Definition: fabutils.h:576
Definition: fabutils.h:541
Definition: fabutils.h:570
Definition: fabutils.h:613
int16_t X
Definition: fabutils.h:126
Definition: fabutils.h:579
void makeDirectory(char const *dirname)
Creates a directory.
Definition: fabutils.cpp:539
Definition: fabutils.h:523
Definition: fabutils.h:586
Definition: fabutils.h:646
Definition: fabutils.h:652
Definition: fabutils.h:668
Definition: fabutils.h:651
Definition: fabutils.h:551
Definition: fabutils.h:568
Definition: fabutils.h:678
Definition: fabutils.h:578
Definition: fabutils.h:506
Definition: fabutils.h:546
Definition: fabutils.h:580
Definition: fabutils.h:676
Definition: fabutils.h:685
Definition: fabutils.h:503
Definition: fabutils.h:610
char const * directory()
Determines absolute path of current directory.
Definition: fabutils.h:387
Definition: fabutils.h:640
Definition: fabutils.h:628
void reload()
Reloads directory content.
Definition: fabutils.cpp:488
Definition: fabutils.h:534
Definition: fabutils.h:612
Definition: fabutils.h:525
Definition: fabutils.h:520
Definition: fabutils.h:507
Definition: fabutils.h:600
Definition: fabutils.h:550
Definition: fabutils.h:683
Definition: fabutils.h:650
int16_t height
Definition: fabutils.h:145
MouseButtons buttons
Definition: fabutils.h:210
Definition: fabutils.h:677
Definition: fabutils.h:559
Definition: fabutils.h:548
Definition: fabutils.h:619
Definition: fabutils.h:590
Definition: fabutils.h:517
Definition: fabutils.h:618
Definition: fabutils.h:607
Definition: fabutils.h:656
void changeDirectory(const char *subdir)
Sets relative directory path.
Definition: fabutils.cpp:420
Definition: fabutils.h:516
Definition: fabutils.h:661
Definition: fabutils.h:666
int16_t Y2
Definition: fabutils.h:162
Definition: fabutils.h:630
Definition: fabutils.h:532
Definition: fabutils.h:682
Definition: fabutils.h:653
Definition: fabutils.h:587
Definition: fabutils.h:637
Definition: fabutils.h:631
Definition: fabutils.h:561
Definition: fabutils.h:522
Definition: fabutils.h:582
Definition: fabutils.h:609
Definition: fabutils.h:611
Definition: fabutils.h:585
Definition: fabutils.h:593
Definition: fabutils.h:662
Definition: fabutils.h:622
Definition: fabutils.h:514
Definition: fabutils.h:633
Describes mouse absolute position, scroll wheel delta and buttons status.
Definition: fabutils.h:206
Definition: fabutils.h:560
Definition: fabutils.h:589
Definition: fabutils.h:552
Definition: fabutils.h:635
int16_t Y
Definition: fabutils.h:127
Definition: fabutils.h:530
Definition: fabutils.h:565
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition: fabutils.h:502
Definition: fabutils.h:556
Definition: fabutils.h:545
Definition: fabutils.h:671
Definition: fabutils.h:641
Represents the coordinate of a point.
Definition: fabutils.h:125
Definition: fabutils.h:608
Definition: fabutils.h:686
Definition: canvas.cpp:47
Definition: fabutils.h:537
Definition: fabutils.h:670
Definition: fabutils.h:562
Definition: fabutils.h:606
int count()
Determines number of files in current directory.
Definition: fabutils.h:394
Definition: fabutils.h:647
Definition: fabutils.h:542
FileBrowser item specificator.
Definition: fabutils.h:348
Definition: fabutils.h:526
Definition: fabutils.h:527
Definition: fabutils.h:528
Definition: fabutils.h:540
Definition: fabutils.h:665
Definition: fabutils.h:513
Definition: fabutils.h:626
Represents a rectangle.
Definition: fabutils.h:158
Definition: fabutils.h:535
Describes mouse buttons status.
Definition: fabutils.h:195
char const * name
Definition: fabutils.h:350
Definition: fabutils.h:554
Definition: fabutils.h:553
Definition: fabutils.h:632
Definition: fabutils.h:642
Definition: fabutils.h:531
Definition: fabutils.h:524
Definition: fabutils.h:569
Definition: fabutils.h:509
Definition: fabutils.h:536
Definition: fabutils.h:510
Definition: fabutils.h:681
Definition: fabutils.h:567
Definition: fabutils.h:518
Definition: fabutils.h:604
Definition: fabutils.h:512
Definition: fabutils.h:684
Definition: fabutils.h:614
bool isDir
Definition: fabutils.h:349
Definition: fabutils.h:539
Definition: fabutils.h:571
int16_t X
Definition: fabutils.h:207
Definition: fabutils.h:615
Definition: fabutils.h:584
Represents a bidimensional size.
Definition: fabutils.h:143
Definition: fabutils.h:543
Definition: fabutils.h:627
Definition: fabutils.h:511
int getFullPath(char const *name, char *outPath=nullptr, int maxlen=0)
Compose a full file path given a relative name.
Definition: fabutils.cpp:618
Definition: fabutils.h:557
Definition: fabutils.h:639
Definition: fabutils.h:508
Definition: fabutils.h:602
Definition: fabutils.h:674
Definition: fabutils.h:660
void setDirectory(const char *path)
Sets absolute directory path.
Definition: fabutils.cpp:409
Definition: fabutils.h:673
Definition: fabutils.h:558
Definition: fabutils.h:658
Definition: fabutils.h:644
Definition: fabutils.h:675
Definition: fabutils.h:623
Definition: fabutils.h:529
Definition: fabutils.h:621
Definition: fabutils.h:664
Definition: fabutils.h:594
uint8_t right
Definition: fabutils.h:198
Definition: fabutils.h:577
Definition: fabutils.h:573
void setSorted(bool value)
Determines if the items are sorted.
int16_t Y1
Definition: fabutils.h:160
Definition: fabutils.h:645
Definition: fabutils.h:605
Definition: fabutils.h:667
Definition: fabutils.h:625
Definition: fabutils.h:549
Definition: fabutils.h:680
Definition: fabutils.h:519
Definition: fabutils.h:583
Definition: fabutils.h:547
Definition: fabutils.h:581
Definition: fabutils.h:620
Definition: fabutils.h:575
Definition: fabutils.h:624
Definition: fabutils.h:634
bool exists(char const *name)
Determines if a file exists.
Definition: fabutils.cpp:468
Definition: fabutils.h:515
Definition: fabutils.h:555
int16_t X1
Definition: fabutils.h:159
Definition: fabutils.h:657
Definition: fabutils.h:654
Definition: fabutils.h:595
void rename(char const *oldName, char const *newName)
Renames a file.
Definition: fabutils.cpp:600
Definition: fabutils.h:599
Definition: fabutils.h:566
int16_t Y
Definition: fabutils.h:208
Definition: fabutils.h:533
Definition: fabutils.h:538
Definition: fabutils.h:616
Definition: fabutils.h:669
Definition: fabutils.h:505
Definition: fabutils.h:603
Definition: fabutils.h:591
Definition: fabutils.h:588
Definition: fabutils.h:544
Definition: fabutils.h:643
Definition: fabutils.h:629
Definition: fabutils.h:598
Definition: fabutils.h:564
Definition: fabutils.h:659
int8_t wheelDelta
Definition: fabutils.h:209
Definition: fabutils.h:638
Definition: fabutils.h:574
int16_t X2
Definition: fabutils.h:161
Definition: fabutils.h:596
Definition: fabutils.h:679
Definition: fabutils.h:563
FileBrowser allows basic file system operations (dir, mkdir, remove and rename)
Definition: fabutils.h:357