-
Notifications
You must be signed in to change notification settings - Fork 202
/
GUIslice.c
executable file
·3663 lines (3088 loc) · 114 KB
/
GUIslice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =======================================================================
// GUIslice library
// - Calvin Hass
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
//
// - Version 0.10.4 (2018/10/13)
// =======================================================================
//
// The MIT License
//
// Copyright 2018 Calvin Hass
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// =======================================================================
// GUIslice library
// Import configuration ( which will import a sub-config depending on device type)
#include "GUIslice_config.h"
#include "GUIslice.h"
#include "GUIslice_ex.h"
#include "GUIslice_drv.h"
#include <stdio.h>
#ifdef DBG_FRAME_RATE
#include <time.h> // for FrameRate reporting
#endif
#if (GSLC_USE_FLOAT)
#include <math.h>
#endif
#if (GSLC_USE_PROGMEM)
#include <avr/pgmspace.h> // For memcpy_P()
#endif
#include <stdarg.h> // For va_*
// Version definition
#define GUISLICE_VER "0.10.2"
// ========================================================================
/// Global debug output function
/// - The user assigns this function via gslc_InitDebug()
GSLC_CB_DEBUG_OUT g_pfDebugOut = NULL;
// Forward declaration for trigonometric lookup table
extern uint16_t m_nLUTSinF0X16[257];
// ------------------------------------------------------------------------
// Error Strings
// - These strings are generally stored in FLASH memory if possible
// ------------------------------------------------------------------------
const char GSLC_PMEM ERRSTR_NULL[] = "ERROR: %z() called with NULL ptr\n";
const char GSLC_PMEM ERRSTR_PXD_NULL[] = "ERROR: %z() pXData NULL\n";
// ------------------------------------------------------------------------
// General Functions
// ------------------------------------------------------------------------
char* gslc_GetVer(gslc_tsGui* pGui)
{
return (char*)GUISLICE_VER;
}
bool gslc_Init(gslc_tsGui* pGui,void* pvDriver,gslc_tsPage* asPage,uint8_t nMaxPage,gslc_tsFont* asFont,uint8_t nMaxFont)
{
unsigned nInd;
bool bOk = true;
// Initialize state
pGui->nDispW = 0;
pGui->nDispH = 0;
pGui->nDispDepth = 0;
#if defined(DRV_DISP_ADAGFX) || defined(DRV_DISP_ADAGFX_AS) || defined(DRV_DISP_TFT_ESPI) || defined(DRV_DISP_M5STACK)
pGui->nRotation = GSLC_ROTATE;
pGui->nSwapXY = ADATOUCH_SWAP_XY;
pGui->nFlipX = ADATOUCH_FLIP_X;
pGui->nFlipY = ADATOUCH_FLIP_Y;
#endif
pGui->nPageMax = nMaxPage;
pGui->nPageCnt = 0;
pGui->asPage = asPage;
pGui->pCurPage = NULL;
pGui->pCurPageCollect = NULL;
// Initialize collection of fonts with user-supplied pointer
pGui->asFont = asFont;
pGui->nFontMax = nMaxFont;
pGui->nFontCnt = 0;
for (nInd=0;nInd<(pGui->nFontMax);nInd++) {
gslc_ResetFont(&(pGui->asFont[nInd]));
}
// Initialize temporary element
#if (GSLC_FEATURE_COMPOUND)
gslc_ResetElem(&(pGui->sElemTmp));
#endif
// Last touch event
pGui->nTouchLastX = 0;
pGui->nTouchLastY = 0;
pGui->nTouchLastPress = 0;
pGui->pfuncXEvent = NULL;
pGui->sImgRefBkgnd = gslc_ResetImage();
// Save a link to the driver
pGui->pvDriver = pvDriver;
// Default to no support for partial redraw
// - This may be overridden by the driver-specific init
pGui->bRedrawPartialEn = false;
#ifdef DBG_FRAME_RATE
pGui->nFrameRateCnt = 0;
pGui->nFrameRateStart = time(NULL);
#endif
// Initialize the display and touch drivers
if (bOk) { bOk &= gslc_DrvInit(pGui); }
#if !defined(DRV_TOUCH_NONE)
if (bOk) { bOk &= gslc_InitTouch(pGui,GSLC_DEV_TOUCH); }
#endif
if (!bOk) { GSLC_DEBUG_PRINT("ERROR: Init(%s) failed\n",""); }
return bOk;
}
void gslc_InitDebug(GSLC_CB_DEBUG_OUT pfunc)
{
g_pfDebugOut = pfunc;
}
// Internal enumerations for printf() parser state machine
typedef enum {
GSLC_DEBUG_PRINT_NORM,
GSLC_DEBUG_PRINT_TOKEN,
GSLC_DEBUG_PRINT_UINT16,
GSLC_DEBUG_PRINT_STR,
GSLC_DEBUG_PRINT_STR_P
} gslc_teDebugPrintState;
// A lightweight printf() routine that calls user function for
// character output (enabling redirection to Serial). Only
// supports the following tokens:
// - %u (16-bit unsigned int in RAM) [see NOTE]
// - %d (16-bit signed int in RAM)
// - %s (null-terminated string in RAM)
// - %z (null-terminated string in FLASH)
// Format strings are expected to be in FLASH if GSLC_USE_PROGMEM enabled
// NOTE:
// - Due to the way variadic arguments are passed, we can't pass uint16_t on Arduino
// as the parameters are promoted to "int" (ie. int16_t). Passing a value over 32767
// appears to be promoted to int32_t which involves pushing two more bytes onto
// the stack, causing the remainder of the va_args() to be offset.
// PRE:
// - g_pfDebugOut defined
void gslc_DebugPrintf(const char* pFmt, ...)
{
if (g_pfDebugOut) {
char* pStr=NULL;
unsigned nMaxDivisor;
unsigned nNumRemain=0;
bool bNumStart=false,bNumNeg=false;
unsigned nNumDivisor=1;
uint16_t nFmtInd=0;
char cFmt,cOut;
va_list vlist;
va_start(vlist,pFmt);
gslc_teDebugPrintState nState = GSLC_DEBUG_PRINT_NORM;
// Determine maximum number digit size
#if defined(__AVR__)
nMaxDivisor = 10000; // ~2^16
#else
nMaxDivisor = 1000000000; // ~2^32
#endif
#if (GSLC_USE_PROGMEM)
cFmt = pgm_read_byte(&pFmt[nFmtInd]);
#else
cFmt = pFmt[nFmtInd];
#endif
while (cFmt != 0) {
if (nState == GSLC_DEBUG_PRINT_NORM) {
if (cFmt == '%') {
nState = GSLC_DEBUG_PRINT_TOKEN;
} else {
// Normal char
(g_pfDebugOut)(cFmt);
}
nFmtInd++; // Advance format index
} else if (nState == GSLC_DEBUG_PRINT_TOKEN) {
// Get token
if (cFmt == 'd') {
nState = GSLC_DEBUG_PRINT_UINT16;
// Detect negative value and convert to unsigned value
// with negation flag. This enables us to reuse the same
// decoding logic.
int nNumInt = va_arg(vlist,int);
if (nNumInt < 0) {
bNumNeg = true;
nNumRemain = -nNumInt;
} else {
bNumNeg = false;
nNumRemain = nNumInt;
}
bNumStart = false;
nNumDivisor = nMaxDivisor;
} else if (cFmt == 'u') {
nState = GSLC_DEBUG_PRINT_UINT16;
nNumRemain = va_arg(vlist,unsigned);
bNumNeg = false;
bNumStart = false;
nNumDivisor = nMaxDivisor;
} else if (cFmt == 's') {
nState = GSLC_DEBUG_PRINT_STR;
pStr = va_arg(vlist,char*);
} else if (cFmt == 'z') {
nState = GSLC_DEBUG_PRINT_STR_P;
pStr = va_arg(vlist,char*);
} else {
// ERROR
}
nFmtInd++; // Advance format index
} else if (nState == GSLC_DEBUG_PRINT_STR) {
while (*pStr != 0) {
cOut = *pStr;
(g_pfDebugOut)(cOut);
pStr++;
}
nState = GSLC_DEBUG_PRINT_NORM;
// Don't advance format string index
} else if (nState == GSLC_DEBUG_PRINT_STR_P) {
do {
#if (GSLC_USE_PROGMEM)
cOut = pgm_read_byte(pStr);
#else
cOut = *pStr;
#endif
if (cOut != 0) {
(g_pfDebugOut)(cOut);
pStr++;
}
} while (cOut != 0);
nState = GSLC_DEBUG_PRINT_NORM;
// Don't advance format string index
} else if (nState == GSLC_DEBUG_PRINT_UINT16) {
// Handle the negation flag if required
if (bNumNeg) {
cOut = '-';
(g_pfDebugOut)(cOut);
bNumNeg = false; // Clear the negation flag
}
// We remain in this state until we have consumed all of the digits
// in the original number (starting with the most significant).
// Each time we process a digit, the parser doesn't advance its input.
if (nNumRemain < nNumDivisor) {
if (bNumStart) {
cOut = '0';
(g_pfDebugOut)(cOut);
} else {
// We haven't started outputting a number yet
// Check for special case of zero
if (nNumRemain == 0) {
cOut = '0';
(g_pfDebugOut)(cOut);
// Now fall through to done state
nNumDivisor = 1;
}
}
} else {
bNumStart = true;
unsigned nValDigit = nNumRemain / nNumDivisor;
cOut = nValDigit+'0';
nNumRemain -= nNumDivisor*nValDigit;
(g_pfDebugOut)(cOut);
}
// Detect end of digit decode (ie. 1's)
if (nNumDivisor == 1) {
// Done
nState = GSLC_DEBUG_PRINT_NORM;
} else {
// Shift the divisor by an order of magnitude
nNumDivisor /= 10;
}
// Don't advance format string index
}
// Read the format string (usually the next character)
#if (GSLC_USE_PROGMEM)
cFmt = pgm_read_byte(&pFmt[nFmtInd]);
#else
cFmt = pFmt[nFmtInd];
#endif
}
va_end(vlist);
} // g_pfDebugOut
}
// ------------------------------------------------------------------------
// Error strings
// ------------------------------------------------------------------------
extern const char ERRSTR_NULL[];
// ------------------------------------------------------------------------
void gslc_Quit(gslc_tsGui* pGui)
{
// Close all elements and fonts
gslc_GuiDestruct(pGui);
}
// Main polling loop for GUIslice
void gslc_Update(gslc_tsGui* pGui)
{
if (pGui->pCurPage == NULL) {
return; // No page added yet
}
#if !defined(DRV_TOUCH_NONE)
// ---------------------------------------------
// Touch handling
// ---------------------------------------------
int16_t nTouchX = 0;
int16_t nTouchY = 0;
uint16_t nTouchPress = 0;
bool bTouchEvent = true;
// Handle touchscreen presses
// - We clear the event queue here so that we don't fall behind
// - In the time it takes to update the display, several mouse /
// finger events may have occurred. If we only handle a single
// motion event per display update, then we may experience very
// lagging responsiveness from the controls.
// - Instead, we drain the even queue before proceeding on to the
// display update, giving rise to a much more responsive GUI.
// The maximum number of touch events that can be handled per
// main loop is defined by the GSLC_TOUCH_MAX_EVT config param.
// - Note that SDL2 may synchronize the RenderPresent call to
// the VSYNC, which will effectively insert a delay into the
// gslc_PageRedrawGo() call below. It might be possible to
// adjust this blocking behavior via SDL_RENDERER_PRESENTVSYNC.
// In case we are flooded with events, limit the maximum number
// that we handle in one gslc_Update() call.
bool bDoneEvts = false;
uint16_t nNumEvts = 0;
do {
bTouchEvent = gslc_GetTouch(pGui,&nTouchX,&nTouchY,&nTouchPress);
if (bTouchEvent) {
// Track and handle the touch events
// - Handle the events on the current page
gslc_TrackTouch(pGui,pGui->pCurPage,nTouchX,nTouchY,nTouchPress);
#ifdef DBG_TOUCH
// Highlight current touch for coordinate debug
gslc_tsRect rMark = gslc_ExpandRect((gslc_tsRect){(int16_t)nTouchX,(int16_t)nTouchY,1,1},1,1);
gslc_DrawFrameRect(pGui,rMark,GSLC_COL_YELLOW);
#endif
nNumEvts++;
}
// Should we stop handling events?
if ((!bTouchEvent) || (nNumEvts >= GSLC_TOUCH_MAX_EVT)) {
bDoneEvts = true;
}
} while (!bDoneEvts);
#endif // !DRV_TOUCH_NONE
// ---------------------------------------------
// Issue a timer tick to all pages
uint8_t nPageInd;
gslc_tsPage* pPage = NULL;
for (nPageInd=0;nPageInd<pGui->nPageCnt;nPageInd++) {
pPage = &pGui->asPage[nPageInd];
gslc_tsEvent sEvent = gslc_EventCreate(pGui,GSLC_EVT_TICK,0,(void*)pPage,NULL);
gslc_PageEvent(pGui,sEvent);
}
// Perform any redraw required for current page
gslc_PageRedrawGo(pGui);
// Simple "frame" rate reporting
// - Note that the rate is based on the number of calls to gslc_Update()
// per second, which may or may not redraw the frame
#ifdef DBG_FRAME_RATE
pGui->nFrameRateCnt++;
uint32_t nElapsed = (time(NULL) - pGui->nFrameRateStart);
if (nElapsed > 0) {
GSLC_DEBUG_PRINT("Update rate: %6u / sec\n",pGui->nFrameRateCnt);
pGui->nFrameRateStart = time(NULL);
pGui->nFrameRateCnt = 0;
}
#endif
}
gslc_tsEvent gslc_EventCreate(gslc_tsGui* pGui,gslc_teEventType eType,uint8_t nSubType,void* pvScope,void* pvData)
{
gslc_tsEvent sEvent;
sEvent.eType = eType;
sEvent.nSubType = nSubType;
sEvent.pvScope = pvScope;
sEvent.pvData = pvData;
return sEvent;
}
// ------------------------------------------------------------------------
// Graphics General Functions
// ------------------------------------------------------------------------
bool gslc_IsInRect(int16_t nSelX,int16_t nSelY,gslc_tsRect rRect)
{
if ( (nSelX >= rRect.x) && (nSelX <= rRect.x+rRect.w) &&
(nSelY >= rRect.y) && (nSelY <= rRect.y+rRect.h) ) {
return true;
} else {
return false;
}
}
bool gslc_IsInWH(int16_t nSelX,int16_t nSelY,uint16_t nWidth,uint16_t nHeight)
{
if ( (nSelX >= 0) && (nSelX <= nWidth-1) &&
(nSelY >= 0) && (nSelY <= nHeight-1) ) {
return true;
} else {
return false;
}
}
// Ensure the coordinates are increasing from nX0->nX1 and nY0->nY1
// NOTE: UNUSED
void gslc_OrderCoord(int16_t* pnX0,int16_t* pnY0,int16_t* pnX1,int16_t* pnY1)
{
int16_t nTmp;
if ((*pnX1) < (*pnX0)) {
nTmp = (*pnX0);
(*pnX0) = (*pnX1);
(*pnX1) = nTmp;
}
if ((*pnY1) < (*pnY0)) {
nTmp = (*pnY0);
(*pnY0) = (*pnY1);
(*pnY1) = nTmp;
}
}
bool gslc_ClipPt(gslc_tsRect* pClipRect,int16_t nX,int16_t nY)
{
int16_t nCX0 = pClipRect->x;
int16_t nCY0 = pClipRect->y;
int16_t nCX1 = pClipRect->x + pClipRect->w - 1;
int16_t nCY1 = pClipRect->y + pClipRect->h - 1;
if ( (nX < nCX0) || (nX > nCX1) ) { return false; }
if ( (nY < nCY0) || (nY > nCY1) ) { return false; }
return true;
}
// This routine implements a basic Cohen-Sutherland line-clipping algorithm
// TODO: Optimize the code further
bool gslc_ClipLine(gslc_tsRect* pClipRect,int16_t* pnX0,int16_t* pnY0,int16_t* pnX1,int16_t* pnY1)
{
int16_t nTmpX,nTmpY;
int16_t nCXMin,nCXMax,nCYMin,nCYMax;
uint8_t nRegion0,nRegion1,nRegionSel;
int16_t nLX0,nLY0,nLX1,nLY1;
int16_t nCX0 = pClipRect->x;
int16_t nCY0 = pClipRect->y;
int16_t nCX1 = pClipRect->x + pClipRect->w - 1;
int16_t nCY1 = pClipRect->y + pClipRect->h - 1;
if (nCX0 > nCX1) {
nCXMin = nCX1;
nCXMax = nCX0;
} else {
nCXMin = nCX0;
nCXMax = nCX1;
}
if (nCY0 > nCY1) {
nCYMin = nCY1;
nCYMax = nCY0;
} else {
nCYMin = nCY0;
nCYMax = nCY1;
}
nTmpX = 0;
nTmpY = 0;
while (1) {
nLX0 = *pnX0;
nLY0 = *pnY0;
nLX1 = *pnX1;
nLY1 = *pnY1;
// Step 1: Assign a region code to each endpoint
nRegion0 = 0;
nRegion1 = 0;
if (nLX0 < nCX0) { nRegion0 |= 1; }
else if (nLX0 > nCX1) { nRegion0 |= 2; }
if (nLY0 < nCY0) { nRegion0 |= 4; }
else if (nLY0 > nCY1) { nRegion0 |= 8; }
if (nLX1 < nCX0) { nRegion1 |= 1; }
else if (nLX1 > nCX1) { nRegion1 |= 2; }
if (nLY1 < nCY0) { nRegion1 |= 4; }
else if (nLY1 > nCY1) { nRegion1 |= 8; }
// Step 2: Check for complete inclusion
if ((nRegion0 == 0) && (nRegion1 == 0)) {
return true;
}
// Step 3: Check for complete exclusion
if ((nRegion0 & nRegion1) != 0) {
return false;
}
// Step 4: Clipping
nRegionSel = nRegion0 ? nRegion0 : nRegion1;
if (nRegionSel & 8) {
nTmpX = nLX0 + (nLX1 - nLX0) * (nCYMax - nLY0) / (nLY1 - nLY0);
nTmpY = nCYMax;
} else if (nRegionSel & 4) {
nTmpX = nLX0 + (nLX1 - nLX0) * (nCYMin - nLY0) / (nLY1 - nLY0);
nTmpY = nCYMin;
} else if (nRegionSel & 2) {
nTmpY = nLY0 + (nLY1 - nLY0) * (nCXMax - nLX0) / (nLX1 - nLX0);
nTmpX = nCXMax;
} else if (nRegionSel & 1) {
nTmpY = nLY0 + (nLY1 - nLY0) * (nCXMin - nLX0) / (nLX1 - nLX0);
nTmpX = nCXMin;
}
// Update endpoint
if (nRegionSel == nRegion0) {
*pnX0 = nTmpX;
*pnY0 = nTmpY;
} else {
*pnX1 = nTmpX;
*pnY1 = nTmpY;
}
} // while(1)
return true;
}
bool gslc_ClipRect(gslc_tsRect* pClipRect,gslc_tsRect* pRect)
{
int16_t nCX0 = pClipRect->x;
int16_t nCY0 = pClipRect->y;
int16_t nCX1 = pClipRect->x + pClipRect->w - 1;
int16_t nCY1 = pClipRect->y + pClipRect->h - 1;
int16_t nRX0 = pRect->x;
int16_t nRY0 = pRect->y;
int16_t nRX1 = pRect->x + pRect->w - 1;
int16_t nRY1 = pRect->y + pRect->h - 1;
// Check for completely out of clip view
if ( (nRX1 < nCX0) || (nRX0 > nCX1) ) { return false; }
if ( (nRY1 < nCY0) || (nRY0 > nCY1) ) { return false; }
// Reduce rect as required to fit view
nRX0 = (nRX0<nCX0)? nCX0 : nRX0;
nRY0 = (nRY0<nCY0)? nCY0 : nRY0;
nRX1 = (nRX1>nCX1)? nCX1 : nRX1;
nRY1 = (nRY1>nCY1)? nCY1 : nRY1;
pRect->x = nRX0;
pRect->y = nRY0;
pRect->w = nRX1-nRX0+1;
pRect->h = nRY1-nRY0+1;
return true;
}
gslc_tsImgRef gslc_ResetImage()
{
gslc_tsImgRef sImgRef;
sImgRef.eImgFlags = GSLC_IMGREF_NONE;
sImgRef.pFname = NULL;
sImgRef.pImgBuf = NULL;
sImgRef.pvImgRaw = NULL;
return sImgRef;
}
gslc_tsImgRef gslc_GetImageFromFile(const char* pFname,gslc_teImgRefFlags eFmt)
{
gslc_tsImgRef sImgRef;
sImgRef.eImgFlags = GSLC_IMGREF_SRC_FILE | (GSLC_IMGREF_FMT & eFmt);
sImgRef.pFname = pFname;
sImgRef.pImgBuf = NULL;
sImgRef.pvImgRaw = NULL;
return sImgRef;
}
gslc_tsImgRef gslc_GetImageFromSD(const char* pFname,gslc_teImgRefFlags eFmt)
{
gslc_tsImgRef sImgRef;
#if (GSLC_SD_EN)
sImgRef.eImgFlags = GSLC_IMGREF_SRC_SD | (GSLC_IMGREF_FMT & eFmt);
sImgRef.pFname = pFname;
sImgRef.pImgBuf = NULL;
sImgRef.pvImgRaw = NULL;
#else
// TODO: Change message to also handle non-Arduino output
GSLC_DEBUG_PRINT("ERROR: GetImageFromSD(%s) not supported as Config:GSLC_SD_EN=0\n","");
sImgRef.eImgFlags = GSLC_IMGREF_NONE;
#endif
return sImgRef;
}
gslc_tsImgRef gslc_GetImageFromRam(unsigned char* pImgBuf,gslc_teImgRefFlags eFmt)
{
gslc_tsImgRef sImgRef;
sImgRef.eImgFlags = GSLC_IMGREF_SRC_RAM | (GSLC_IMGREF_FMT & eFmt);
sImgRef.pFname = NULL;
sImgRef.pImgBuf = pImgBuf;
sImgRef.pvImgRaw = NULL;
return sImgRef;
}
gslc_tsImgRef gslc_GetImageFromProg(const unsigned char* pImgBuf,gslc_teImgRefFlags eFmt)
{
gslc_tsImgRef sImgRef;
sImgRef.eImgFlags = GSLC_IMGREF_SRC_PROG | (GSLC_IMGREF_FMT & eFmt);
sImgRef.pFname = NULL;
sImgRef.pImgBuf = pImgBuf;
sImgRef.pvImgRaw = NULL;
return sImgRef;
}
// Sine function with optional lookup table
int16_t gslc_sinFX(int16_t n64Ang)
{
int16_t nRetValS;
#if (GSLC_USE_FLOAT)
// Use floating-point math library function
// Calculate angle in radians
float fAngRad = n64Ang*GSLC_2PI/(360.0*64.0);
// Perform floating point calc
float fSin = sin(fAngRad);
// Return as fixed point result
nRetValS = fSin * 32767.0;
return nRetValS;
#else
// Use lookup tables
bool bNegate = false;
// Support multiple waveform periods
if (n64Ang >= 360*64) {
// For some reason this modulus is broken!
n64Ang = n64Ang % (int16_t)(360*64);
//n64Ang = n64Ang - (360*64);
} else if (n64Ang <= -360*64) {
n64Ang = -(-n64Ang % 360*64);
}
// Handle negative range
if (n64Ang < 0) {
n64Ang = -n64Ang;
bNegate = !bNegate;
}
// Handle 3rd and 4th phase
if (n64Ang >= 180*64) {
n64Ang -= 180*64;
bNegate = !bNegate;
}
// Handle 2nd phase
if (n64Ang >= 90*64) {
n64Ang = 180*64 - n64Ang;
}
// n64Ang is quarter-phase range [0 .. 90*64]
// suitable for lookup table indexing
uint16_t nLutInd = (n64Ang * 256)/(90*64);
uint16_t nLutVal = m_nLUTSinF0X16[nLutInd];
// Leave MSB for the signed bit
nLutVal /= 2;
if (bNegate) {
nRetValS = -nLutVal;
} else {
nRetValS = nLutVal;
}
return nRetValS;
#endif
}
// Cosine function with optional lookup table
int16_t gslc_cosFX(int16_t n64Ang)
{
#if (GSLC_USE_FLOAT)
int16_t nRetValS;
// Use floating-point math library function
// Calculate angle in radians
float fAngRad = n64Ang*GSLC_2PI/(360.0*64.0);
// Perform floating point calc
float fCos = cos(fAngRad);
// Return as fixed point result
nRetValS = fCos * 32767.0;
return nRetValS;
#else
// Use lookup tables
// Cosine function is equivalent to Sine shifted by 90 degrees
return gslc_sinFX(n64Ang+90*64);
#endif
}
// Convert from polar to cartesian
void gslc_PolarToXY(uint16_t nRad,int16_t n64Ang,int16_t* nDX,int16_t* nDY)
{
*nDX = (int16_t)nRad * gslc_sinFX(n64Ang)/(int16_t)32767;
*nDY = (int16_t)nRad * -gslc_cosFX(n64Ang)/(int16_t)32767;
}
// Call with nMidAmt=500 to create simple linear blend between two colors
gslc_tsColor gslc_ColorBlend2(gslc_tsColor colStart,gslc_tsColor colEnd,uint16_t nMidAmt,uint16_t nBlendAmt)
{
gslc_tsColor colMid;
colMid.r = (colEnd.r+colStart.r)/2;
colMid.g = (colEnd.g+colStart.g)/2;
colMid.b = (colEnd.b+colStart.b)/2;
return gslc_ColorBlend3(colStart,colMid,colEnd,nMidAmt,nBlendAmt);
}
gslc_tsColor gslc_ColorBlend3(gslc_tsColor colStart,gslc_tsColor colMid,gslc_tsColor colEnd,uint16_t nMidAmt,uint16_t nBlendAmt)
{
gslc_tsColor colNew;
nMidAmt = (nMidAmt >1000)?1000:nMidAmt;
nBlendAmt = (nBlendAmt>1000)?1000:nBlendAmt;
uint16_t nRngLow = nMidAmt;
uint16_t nRngHigh = 1000-nMidAmt;
uint16_t nSubBlendAmt;
if (nBlendAmt >= nMidAmt) {
nSubBlendAmt = (nBlendAmt - nMidAmt)*1000/nRngHigh;
colNew.r = nSubBlendAmt*(colEnd.r - colMid.r)/1000 + colMid.r;
colNew.g = nSubBlendAmt*(colEnd.g - colMid.g)/1000 + colMid.g;
colNew.b = nSubBlendAmt*(colEnd.b - colMid.b)/1000 + colMid.b;
} else {
nSubBlendAmt = (nBlendAmt - 0)*1000/nRngLow;
colNew.r = nSubBlendAmt*(colMid.r - colStart.r)/1000 + colStart.r;
colNew.g = nSubBlendAmt*(colMid.g - colStart.g)/1000 + colStart.g;
colNew.b = nSubBlendAmt*(colMid.b - colStart.b)/1000 + colStart.b;
}
return colNew;
}
bool gslc_ColorEqual(gslc_tsColor a,gslc_tsColor b)
{
return a.r == b.r && a.g == b.g && a.b == b.b;
}
// ------------------------------------------------------------------------
// Graphics Primitive Functions
// ------------------------------------------------------------------------
void gslc_DrawSetPixel(gslc_tsGui* pGui,int16_t nX,int16_t nY,gslc_tsColor nCol)
{
#if (DRV_HAS_DRAW_POINT)
// Call optimized driver point drawing
gslc_DrvDrawPoint(pGui,nX,nY,nCol);
#else
GSLC_DEBUG_PRINT("ERROR: Mandatory DrvDrawPoint() is not defined in driver\n");
#endif
gslc_PageFlipSet(pGui,true);
}
// Draw an arbitrary line using Bresenham's algorithm
// - Algorithm reference: https://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm#C
void gslc_DrawLine(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,int16_t nX1,int16_t nY1,gslc_tsColor nCol)
{
#if (DRV_HAS_DRAW_LINE)
// Call optimized driver line drawing
gslc_DrvDrawLine(pGui,nX0,nY0,nX1,nY1,nCol);
#else
// Perform Bresenham's line algorithm
int16_t nDX = abs(nX1-nX0);
int16_t nDY = abs(nY1-nY0);
int16_t nSX = (nX0 < nX1)? 1 : -1;
int16_t nSY = (nY0 < nY1)? 1 : -1;
int16_t nErr = ( (nDX>nDY)? nDX : -nDY )/2;
int16_t nE2;
// Check for degenerate cases
// TODO: Need to test these optimizations
bool bDone = false;
if (nDX == 0) {
if (nDY == 0) {
return;
} else if (nY1-nY0 >= 0) {
gslc_DrawLineV(pGui,nX0,nY0,nDY+1,nCol);
bDone = true;
} else {
gslc_DrawLineV(pGui,nX1,nY1,nDY+1,nCol);
bDone = true;
}
} else if (nDY == 0) {
if (nX1-nX0 >= 0) {
gslc_DrawLineH(pGui,nX0,nY0,nDX+1,nCol);
bDone = true;
} else {
gslc_DrawLineH(pGui,nX1,nY1,nDX+1,nCol);
bDone = true;
}
}
if (!bDone) {
for (;;) {
// Set the pixel
gslc_DrvDrawPoint(pGui,nX0,nY0,nCol);
// Calculate next coordinates
if ( (nX0 == nX1) && (nY0 == nY1) ) break;
nE2 = nErr;
if (nE2 > -nDX) { nErr -= nDY; nX0 += nSX; }
if (nE2 < nDY) { nErr += nDX; nY0 += nSY; }
}
}
gslc_PageFlipSet(pGui,true);
#endif
}
void gslc_DrawLineH(gslc_tsGui* pGui,int16_t nX, int16_t nY, uint16_t nW,gslc_tsColor nCol)
{
uint16_t nOffset;
for (nOffset=0;nOffset<nW;nOffset++) {
gslc_DrvDrawPoint(pGui,nX+nOffset,nY,nCol);
}
gslc_PageFlipSet(pGui,true);
}
void gslc_DrawLineV(gslc_tsGui* pGui,int16_t nX, int16_t nY, uint16_t nH,gslc_tsColor nCol)
{
uint16_t nOffset;
for (nOffset=0;nOffset<nH;nOffset++) {
gslc_DrvDrawPoint(pGui,nX,nY+nOffset,nCol);
}
gslc_PageFlipSet(pGui,true);
}
// Note that angle is in degrees * 64
void gslc_DrawLinePolar(gslc_tsGui* pGui,int16_t nX,int16_t nY,uint16_t nRadStart,uint16_t nRadEnd,int16_t n64Ang,gslc_tsColor nCol)
{
// Draw the ray representing the current value
int16_t nDxS = nRadStart * gslc_sinFX(n64Ang)/32768;
int16_t nDyS = nRadStart * gslc_cosFX(n64Ang)/32768;
int16_t nDxE = nRadEnd * gslc_sinFX(n64Ang)/32768;
int16_t nDyE = nRadEnd * gslc_cosFX(n64Ang)/32768;
gslc_DrawLine(pGui,nX+nDxS,nY-nDyS,nX+nDxE,nY-nDyE,nCol);
}
void gslc_DrawFrameRect(gslc_tsGui* pGui,gslc_tsRect rRect,gslc_tsColor nCol)
{
// Ensure dimensions are valid
if ((rRect.w == 0) || (rRect.h == 0)) {
return;
}
#if (DRV_HAS_DRAW_RECT_FRAME)
// Call optimized driver implementation
gslc_DrvDrawFrameRect(pGui,rRect,nCol);
#else
// Emulate rect frame with four lines
int16_t nX,nY;
uint16_t nH,nW;
nX = rRect.x;
nY = rRect.y;
nW = rRect.w;
nH = rRect.h;
gslc_DrawLineH(pGui,nX,nY,nW-1,nCol); // Top
gslc_DrawLineH(pGui,nX,(int16_t)(nY+nH-1),nW-1,nCol); // Bottom
gslc_DrawLineV(pGui,nX,nY,nH-1,nCol); // Left
gslc_DrawLineV(pGui,(int16_t)(nX+nW-1),nY,nH-1,nCol); // Right
#endif
gslc_PageFlipSet(pGui,true);
}
void gslc_DrawFillRect(gslc_tsGui* pGui,gslc_tsRect rRect,gslc_tsColor nCol)
{
// Ensure dimensions are valid
if ((rRect.w == 0) || (rRect.h == 0)) {
return;
}
#if (DRV_HAS_DRAW_RECT_FILL)
// Call optimized driver implementation
gslc_DrvDrawFillRect(pGui,rRect,nCol);
#else
// Emulate it with individual line draws
// TODO: This should be avoided as it will generally be very inefficient
int nRow;
for (nRow=0;nRow<rRect.h;nRow++) {
gslc_DrawLineH(pGui, rRect.x, rRect.y+nRow, rRect.w, nCol);
}
#endif
gslc_PageFlipSet(pGui,true);
}
// Expand or contract a rectangle in width and/or height (equal
// amounts on both side), based on the centerpoint of the rectangle.
gslc_tsRect gslc_ExpandRect(gslc_tsRect rRect,int16_t nExpandW,int16_t nExpandH)
{
gslc_tsRect rNew = {0,0,0,0};