forked from ImpulseAdventure/GUIslice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIslice_drv_m5stack.cpp
992 lines (832 loc) · 32.4 KB
/
GUIslice_drv_m5stack.cpp
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
// =======================================================================
// GUIslice library (driver layer for m5stack/M5Stack)
// - Calvin Hass
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
// =======================================================================
//
// The MIT License
//
// Copyright 2016-2019 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.
//
// =======================================================================
/// \file GUIslice_drv_m5stack.cpp
// Compiler guard for requested driver
#include "GUIslice_config.h" // Sets DRV_DISP_*
#if defined(DRV_DISP_M5STACK)
// =======================================================================
// Driver Layer for m5stack/M5Stack
// - https://github.com/m5stack/M5Stack
// =======================================================================
// GUIslice library
#include "GUIslice_drv_m5stack.h"
#include <stdio.h>
#include <M5Stack.h>
#include <SPI.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// Define driver naming
const char* m_acDrvDisp = "M5STACK";
const char* m_acDrvTouch = "M5STACK(NONE)";
// ------------------------------------------------------------------------
// Use default pin settings as defined in M5Stack/src/utility/Config.h
#define m_disp m5.Lcd
// =======================================================================
// Public APIs to GUIslice core library
// =======================================================================
// -----------------------------------------------------------------------
// Configuration Functions
// -----------------------------------------------------------------------
bool gslc_DrvInit(gslc_tsGui* pGui)
{
// Report any debug info if enabled
#if defined(DBG_DRIVER)
// TODO
#endif
// Initialize any library-specific members
if (pGui->pvDriver) {
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
pDriver->nColBkgnd = GSLC_COL_BLACK;
// These displays can accept partial redraw as they retain the last
// image in the controller graphics RAM
pGui->bRedrawPartialEn = true;
m_disp.init();
// TODO: Replace the following with DrvRotate()
m_disp.setRotation( pGui->nRotation );
pGui->nDispW = m_disp.width();
pGui->nDispH = m_disp.height();
// Defaults for clipping region
gslc_tsRect rClipRect = {0,0,pGui->nDispW,pGui->nDispH};
gslc_DrvSetClipRect(pGui,&rClipRect);
pinMode(TFT_LIGHT_PIN, OUTPUT);
digitalWrite(TFT_LIGHT_PIN, HIGH);
}
return true;
}
void gslc_DrvDestruct(gslc_tsGui* pGui)
{
}
const char* gslc_DrvGetNameDisp(gslc_tsGui* pGui)
{
return m_acDrvDisp;
}
const char* gslc_DrvGetNameTouch(gslc_tsGui* pGui)
{
return m_acDrvTouch;
}
// -----------------------------------------------------------------------
// Image/surface handling Functions
// -----------------------------------------------------------------------
void* gslc_DrvLoadImage(gslc_tsGui* pGui,gslc_tsImgRef sImgRef)
{
// GUIslice adapter for Adafruit-GFX doesn't preload the
// images into RAM (to keep RAM requirements low), so we
// don't need to do any further processing here. Instead,
// the loading is done during render.
if (sImgRef.eImgFlags == GSLC_IMGREF_NONE) {
return NULL;
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_FILE) {
return NULL; // No image preload done
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_SD) {
return NULL; // No image preload done
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_RAM) {
return NULL; // No image preload done
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_PROG) {
return NULL; // No image preload done
}
// Default
return NULL;
}
bool gslc_DrvSetBkgndImage(gslc_tsGui* pGui,gslc_tsImgRef sImgRef)
{
// Dispose of previous background
if (pGui->sImgRefBkgnd.eImgFlags != GSLC_IMGREF_NONE) {
gslc_DrvImageDestruct(pGui->sImgRefBkgnd.pvImgRaw);
pGui->sImgRefBkgnd = gslc_ResetImage();
}
pGui->sImgRefBkgnd = sImgRef;
pGui->sImgRefBkgnd.pvImgRaw = gslc_DrvLoadImage(pGui,sImgRef);
if (pGui->sImgRefBkgnd.pvImgRaw == NULL) {
GSLC_DEBUG_PRINT("ERROR: DrvSetBkgndImage(%s) failed\n","");
return false;
}
return true;
}
bool gslc_DrvSetBkgndColor(gslc_tsGui* pGui,gslc_tsColor nCol)
{
if (pGui->pvDriver) {
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
pDriver->nColBkgnd = nCol;
}
return true;
}
bool gslc_DrvSetElemImageNorm(gslc_tsGui* pGui,gslc_tsElem* pElem,gslc_tsImgRef sImgRef)
{
// This driver doesn't preload the image to memory,
// so we just save the reference for loading upon render
pElem->sImgRefNorm = sImgRef;
return true; // TODO
}
bool gslc_DrvSetElemImageGlow(gslc_tsGui* pGui,gslc_tsElem* pElem,gslc_tsImgRef sImgRef)
{
// This driver doesn't preload the image to memory,
// so we just save the reference for loading upon render
pElem->sImgRefGlow = sImgRef;
return true; // TODO
}
void gslc_DrvImageDestruct(void* pvImg)
{
}
bool gslc_DrvSetClipRect(gslc_tsGui* pGui,gslc_tsRect* pRect)
{
// NOTE: The clipping rect is currently saved in the
// driver struct, but the drawing code does not currently
// use it.
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
if (pRect == NULL) {
// Default to entire display
pDriver->rClipRect = {0,0,pGui->nDispW,pGui->nDispH};
} else {
pDriver->rClipRect = *pRect;
}
// TODO: For ILI9341, perhaps we can leverage m_disp.setAddrWindow(x0, y0, x1, y1)?
return true;
}
// -----------------------------------------------------------------------
// Font handling Functions
// -----------------------------------------------------------------------
const void* gslc_DrvFontAdd(gslc_teFontRefType eFontRefType,const void* pvFontRef,uint16_t nFontSz)
{
// Arduino mode currently only supports font definitions from memory
if (eFontRefType != GSLC_FONTREF_PTR) {
GSLC_DEBUG_PRINT("ERROR: DrvFontAdd(%s) failed - Arduino only supports memory-based fonts\n","");
return NULL;
}
// Return pointer to Adafruit-GFX GFXfont structure
return pvFontRef;
}
void gslc_DrvFontsDestruct(gslc_tsGui* pGui)
{
// Nothing to deallocate
}
// NOTE: Please see the comments associated with gslc_DrvDrawTxt().
// In summary, DrvGetTxtSize() is not able to gather the complete
// text sizing information from TFT_eSPI as the APIs are not available.
//
bool gslc_DrvGetTxtSize(gslc_tsGui* pGui,gslc_tsFont* pFont,const char* pStr,gslc_teTxtFlags eTxtFlags,
int16_t* pnTxtX,int16_t* pnTxtY,uint16_t* pnTxtSzW,uint16_t* pnTxtSzH)
{
uint16_t nTxtLen = 0;
uint16_t nTxtHeight = 0;
uint16_t nTxtScale = pFont->nSize;
// TFT_eSPI font API differs from Adafruit-GFX's setFont() API
if (pFont->pvFont == NULL) {
m_disp.setTextFont(1);
} else {
m_disp.setFreeFont((const GFXfont *)pFont->pvFont);
}
m_disp.setTextSize(nTxtScale);
nTxtLen = m_disp.textWidth((char*)pStr);
nTxtHeight = m_disp.fontHeight(1); // Use freefont "textfont" value
*pnTxtX = 0; // Unused
*pnTxtY = 0; // Unused
*pnTxtSzW = nTxtLen;
*pnTxtSzH = nTxtHeight;
return true;
}
bool gslc_DrvDrawTxtAlign(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,int16_t nX1,int16_t nY1,int8_t eTxtAlign,
gslc_tsFont* pFont,const char* pStr,gslc_teTxtFlags eTxtFlags,gslc_tsColor colTxt, gslc_tsColor colBg=GSLC_COL_BLACK)
{
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(colTxt);
uint16_t nTxtScale = pFont->nSize;
// TODO: Support SMOOTH_FONT?
m_disp.setTextColor(nColRaw);
// TFT_eSPI font API differs from Adafruit-GFX's setFont() API
if (pFont->pvFont == NULL) {
m_disp.setTextFont(1);
} else {
m_disp.setFreeFont((const GFXfont *)pFont->pvFont);
}
m_disp.setTextSize(nTxtScale);
// Default to mid-mid datum
int8_t nDatum = MC_DATUM;
int16_t nTxtX = nX0 + (nX1-nX0)/2;
int16_t nTxtY = nY0 + (nY1-nY0)/2;
// Override the datum depending on alignment mode
switch(eTxtAlign) {
case GSLC_ALIGN_TOP_LEFT: nDatum = TL_DATUM; nTxtX = nX0; nTxtY = nY0; break;
case GSLC_ALIGN_TOP_MID: nDatum = TC_DATUM; nTxtY = nY0; break;
case GSLC_ALIGN_TOP_RIGHT: nDatum = TR_DATUM; nTxtX = nX1; nTxtY = nY0; break;
case GSLC_ALIGN_MID_LEFT: nDatum = ML_DATUM; nTxtX = nX0; break;
case GSLC_ALIGN_MID_MID: nDatum = MC_DATUM; break;
case GSLC_ALIGN_MID_RIGHT: nDatum = MR_DATUM; nTxtX = nX1; break;
case GSLC_ALIGN_BOT_LEFT: nDatum = BL_DATUM; nTxtX = nX0; nTxtY = nY1; break;
case GSLC_ALIGN_BOT_MID: nDatum = BC_DATUM; nTxtY = nY1; break;
case GSLC_ALIGN_BOT_RIGHT: nDatum = BR_DATUM; nTxtX = nX1; nTxtY = nY1; break;
default: nDatum = MC_DATUM; break;
}
m_disp.setTextDatum(nDatum);
m_disp.drawString(pStr,nTxtX,nTxtY);
// For now, always return true
return true;
}
// NOTE: As TFT_eSPI performs some complex logic in determining the font
// baseline and associated adjustments and these are not provided
// via an API, calling DrvGetTxtSize() is insufficient to determine
// the appropriate position corrections required (eg. when centering
// text). Therefore, DrvDrawTxt() will not result in proper text
// alignment in all cases. Instead, it is recommended that
// DrvDrawTxtAlign() is used instead, which will depend on the datum
// adjustment code within TFT_eSPI. This mode of operation is
// selected by default in GUIslice_drv_tft_espi.h by setting
// DRV_OVERRIDE_TXT_ALIGN to 1.
// This method is not recommended for use with TFT_eSPI. DrvDrawTxtAlign()
// should be used instead.
bool gslc_DrvDrawTxt(gslc_tsGui* pGui,int16_t nTxtX,int16_t nTxtY,gslc_tsFont* pFont,const char* pStr,gslc_teTxtFlags eTxtFlags,gslc_tsColor colTxt, gslc_tsColor colBg=GSLC_COL_BLACK)
{
uint16_t nTxtScale = pFont->nSize;
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(colTxt);
// TODO: Support SMOOTH_FONT?
m_disp.setTextColor(nColRaw);
// m_disp.setCursor(nTxtX,nTxtY);
m_disp.setTextSize(nTxtScale);
// Default to top-left datum
m_disp.setTextDatum(TL_DATUM);
if ((eTxtFlags & GSLC_TXT_MEM) == GSLC_TXT_MEM_RAM) {
// String in SRAM; can access buffer directly
// m_disp.println(pStr);
m_disp.drawString(pStr,nTxtX,nTxtY);
} else if ((eTxtFlags & GSLC_TXT_MEM) == GSLC_TXT_MEM_PROG) {
// String in PROGMEM (flash); must access via pgm_* calls
char ch;
int nXOffset = 0;
while ((ch = pgm_read_byte(pStr++))) {
// m_disp.print(ch);
nXOffset += m_disp.drawChar(ch,nTxtX+nXOffset,nTxtY);
}
m_disp.println();
}
return true;
}
// -----------------------------------------------------------------------
// Screen Management Functions
// -----------------------------------------------------------------------
void gslc_DrvPageFlipNow(gslc_tsGui* pGui)
{
// Nothing to do as we're not double-buffered
}
// -----------------------------------------------------------------------
// Graphics Primitives Functions
// -----------------------------------------------------------------------
bool gslc_DrvDrawPoint(gslc_tsGui* pGui,int16_t nX,int16_t nY,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// Perform clipping
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
if (!gslc_ClipPt(&pDriver->rClipRect,nX,nY)) { return true; }
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.drawPixel(nX,nY,nColRaw);
return true;
}
bool gslc_DrvDrawPoints(gslc_tsGui* pGui,gslc_tsPt* asPt,uint16_t nNumPt,gslc_tsColor nCol)
{
return false;
}
bool gslc_DrvDrawFillRect(gslc_tsGui* pGui,gslc_tsRect rRect,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// Perform clipping
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
if (!gslc_ClipRect(&pDriver->rClipRect,&rRect)) { return true; }
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.fillRect(rRect.x,rRect.y,rRect.w,rRect.h,nColRaw);
return true;
}
bool gslc_DrvDrawFrameRect(gslc_tsGui* pGui,gslc_tsRect rRect,gslc_tsColor nCol)
{
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
#if (GSLC_CLIP_EN)
// Perform clipping
// - TODO: Optimize the following, perhaps with new ClipLineHV()
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
int16_t nX0, nY0, nX1, nY1;
// Top
nX0 = rRect.x;
nY0 = rRect.y;
nX1 = rRect.x + rRect.w - 1;
nY1 = nY0;
if (gslc_ClipLine(&pDriver->rClipRect, &nX0, &nY0, &nX1, &nY1)) { m_disp.drawLine(nX0, nY0, nX1, nY1, nColRaw); }
// Bottom
nX0 = rRect.x;
nY0 = rRect.y + rRect.h - 1;
nX1 = rRect.x + rRect.w - 1;
nY1 = nY0;
if (gslc_ClipLine(&pDriver->rClipRect, &nX0, &nY0, &nX1, &nY1)) { m_disp.drawLine(nX0, nY0, nX1, nY1, nColRaw); }
// Left
nX0 = rRect.x;
nY0 = rRect.y;
nX1 = nX0;
nY1 = rRect.y + rRect.h - 1;
if (gslc_ClipLine(&pDriver->rClipRect, &nX0, &nY0, &nX1, &nY1)) { m_disp.drawLine(nX0, nY0, nX1, nY1, nColRaw); }
// Right
nX0 = rRect.x + rRect.w - 1;
nY0 = rRect.y;
nX1 = nX0;
nY1 = rRect.y + rRect.h - 1;
if (gslc_ClipLine(&pDriver->rClipRect, &nX0, &nY0, &nX1, &nY1)) { m_disp.drawLine(nX0, nY0, nX1, nY1, nColRaw); }
#else
m_disp.drawRect(rRect.x,rRect.y,rRect.w,rRect.h,nColRaw);
#endif
return true;
}
bool gslc_DrvDrawLine(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,int16_t nX1,int16_t nY1,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
if (!gslc_ClipLine(&pDriver->rClipRect,&nX0,&nY0,&nX1,&nY1)) { return true; }
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.drawLine(nX0,nY0,nX1,nY1,nColRaw);
return true;
}
bool gslc_DrvDrawFrameCircle(gslc_tsGui*,int16_t nMidX,int16_t nMidY,uint16_t nRadius,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// TODO
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.drawCircle(nMidX,nMidY,nRadius,nColRaw);
return true;
}
bool gslc_DrvDrawFillCircle(gslc_tsGui*,int16_t nMidX,int16_t nMidY,uint16_t nRadius,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// TODO
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.fillCircle(nMidX,nMidY,nRadius,nColRaw);
return true;
}
bool gslc_DrvDrawFrameTriangle(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,
int16_t nX1,int16_t nY1,int16_t nX2,int16_t nY2,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// TODO
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.drawTriangle(nX0,nY0,nX1,nY1,nX2,nY2,nColRaw);
return true;
}
bool gslc_DrvDrawFillTriangle(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,
int16_t nX1,int16_t nY1,int16_t nX2,int16_t nY2,gslc_tsColor nCol)
{
#if (GSLC_CLIP_EN)
// TODO
#endif
uint16_t nColRaw = gslc_DrvAdaptColorToRaw(nCol);
m_disp.fillTriangle(nX0,nY0,nX1,nY1,nX2,nY2,nColRaw);
return true;
}
// ----- REFERENCE CODE begin
// The following code was based upon the following reference code but modified to
// adapt for use in GUIslice.
//
// URL: https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.cpp
// Original author: Adafruit
// Function: drawBitmap()
// Draw a 1-bit image (bitmap) at the specified (x,y) position from the
// provided bitmap buffer using the foreground color defined in the
// header (unset bits are transparent).
// GUIslice modified the raw memory format to add a header:
// Image array format:
// - Width[15:8], Width[7:0],
// - Height[15:8], Height[7:0],
// - ColorR[7:0], ColorG[7:0],
// - ColorB[7:0], 0x00,
// - Monochrome bitmap follows...
//
void gslc_DrvDrawMonoFromMem(gslc_tsGui* pGui,int16_t nDstX, int16_t nDstY,
const unsigned char *pBitmap,bool bProgMem)
{
const unsigned char* bmap_base = pBitmap;
int16_t w,h;
gslc_tsColor nCol;
// Read header
w = ( (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++) ) << 8;
w |= ( (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++) ) << 0;
h = ( (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++) ) << 8;
h |= ( (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++) ) << 0;
nCol.r = (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++);
nCol.g = (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++);
nCol.b = (bProgMem)? pgm_read_byte(bmap_base++) : *(bmap_base++);
bmap_base++;
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t nByte = 0;
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
if(i & 7) nByte <<= 1;
else {
if (bProgMem) {
nByte = pgm_read_byte(bmap_base + j * byteWidth + i / 8);
} else {
nByte = bmap_base[j * byteWidth + i / 8];
}
}
if(nByte & 0x80) {
gslc_DrvDrawPoint(pGui,nDstX+i,nDstY+j,nCol);
}
}
}
}
// ----- REFERENCE CODE end
void gslc_DrvDrawBmp24FromMem(gslc_tsGui* pGui,int16_t nDstX, int16_t nDstY,const unsigned char* pBitmap,bool bProgMem)
{
const int16_t* pImage = (const int16_t*)pBitmap;
int16_t h = *(pImage++);
int16_t w = *(pImage++);
int row, col;
for (row=0; row<h; row++) { // For each scanline...
for (col=0; col<w; col++) { // For each pixel...
if (bProgMem) {
//To read from Flash Memory, pgm_read_XXX is required.
//Since image is stored as uint16_t, pgm_read_word is used as it uses 16bit address
m_disp.drawPixel(nDstX+col, nDstY+row, pgm_read_word(pImage++));
} else {
m_disp.drawPixel(nDstX+col, nDstY+row, *(pImage++));
}
} // end pixel
}
}
#if (GSLC_SD_EN)
// ----- REFERENCE CODE begin
// The following code was based upon the following reference code but modified to
// adapt for use in GUIslice.
//
// URL: https://github.com/adafruit/Adafruit_ILI9341/blob/master/examples/spitftbitmap/spitftbitmap.ino
// Original author: Adafruit
// Function: bmpDraw()
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t gslc_DrvRead16SD(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t gslc_DrvRead32SD(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
void gslc_DrvDrawBmp24FromSD(gslc_tsGui* pGui,const char *filename, uint16_t x, uint16_t y)
{
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*GSLC_SD_BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= pGui->nDispW) || (y >= pGui->nDispH)) return;
//Serial.println();
//Serial.print("Loading image '");
//Serial.print(filename);
//Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == 0) {
GSLC_DEBUG_PRINT("ERROR: DrvDrawBmp24FromSD() file not found [%s]",filename);
return;
}
// Parse BMP header
if(gslc_DrvRead16SD(bmpFile) == 0x4D42) { // BMP signature
uint32_t nFileSize = gslc_DrvRead32SD(bmpFile);
//Serial.print("File size: "); Serial.println(nFileSize);
(void)gslc_DrvRead32SD(bmpFile); // Read & ignore creator bytes
bmpImageoffset = gslc_DrvRead32SD(bmpFile); // Start of image data
//Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
uint32_t nHdrSize = gslc_DrvRead32SD(bmpFile);
//Serial.print("Header size: "); Serial.println(nHdrSize);
bmpWidth = gslc_DrvRead32SD(bmpFile);
bmpHeight = gslc_DrvRead32SD(bmpFile);
if(gslc_DrvRead16SD(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = gslc_DrvRead16SD(bmpFile); // bits per pixel
//Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (gslc_DrvRead32SD(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
//Serial.print("Image size: ");
//Serial.print(bmpWidth);
//Serial.print('x');
//Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= pGui->nDispW) w = pGui->nDispW - x;
if((y+h-1) >= pGui->nDispH) h = pGui->nDispH - y;
// Set TFT address window to clipped image bounds
//xxx tft.setAddrWindow(x, y, x+w-1, y+h-1);
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
//xxx tft.pushColor(tft.Color565(r,g,b));
gslc_tsColor nCol = (gslc_tsColor){r,g,b};
gslc_tsColor nColTrans = (gslc_tsColor){GSLC_BMP_TRANS_RGB};
bool bDrawBit = true;
if (GSLC_BMP_TRANS_EN) {
if ((nCol.r == nColTrans.r) && (nCol.g == nColTrans.g) && (nCol.b == nColTrans.b)) {
bDrawBit = false;
}
}
if (bDrawBit) {
gslc_DrvDrawPoint(pGui,x+col,y+row,nCol);
}
} // end pixel
} // end scanline
//Serial.print("Loaded in ");
//Serial.print(millis() - startTime);
//Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) {
GSLC_DEBUG_PRINT("ERROR: DrvDrawBmp24FromSD() BMP format unknown [%s]",filename);
}
}
// ----- REFERENCE CODE end
#endif // GSLC_SD_EN
bool gslc_DrvDrawImage(gslc_tsGui* pGui,int16_t nDstX,int16_t nDstY,gslc_tsImgRef sImgRef)
{
#if defined(DBG_DRIVER)
char addr[6];
GSLC_DEBUG_PRINT("DBG: DrvDrawImage() with ImgBuf address=","");
sprintf(addr,"%04X",sImgRef.pImgBuf);
GSLC_DEBUG_PRINT("%s\n",addr);
#endif
// GUIslice adapter library for Adafruit-GFX does not pre-load
// image data into memory before calling DrvDrawImage(), so
// we to handle the loading now (when rendering).
if (sImgRef.eImgFlags == GSLC_IMGREF_NONE) {
return true; // Nothing to do
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_FILE) {
return false; // Not supported
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_RAM) {
if ((sImgRef.eImgFlags & GSLC_IMGREF_FMT) == GSLC_IMGREF_FMT_RAW1) {
// Draw a monochrome bitmap from SRAM
// - Dimensions and output color are defined in arrray header
gslc_DrvDrawMonoFromMem(pGui,nDstX,nDstY,sImgRef.pImgBuf,false);
return true;
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_FMT) == GSLC_IMGREF_FMT_BMP24) {
// 24-bit Bitmap in ram
gslc_DrvDrawBmp24FromMem(pGui,nDstX,nDstY,sImgRef.pImgBuf,false);
return true;
} else {
return false; // TODO: not yet supported
}
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_PROG) {
// TODO: Probably need to fix this to work with PROGMEM,
// but check (GSLC_USE_PROGMEM) first
if ((sImgRef.eImgFlags & GSLC_IMGREF_FMT) == GSLC_IMGREF_FMT_RAW1) {
// Draw a monochrome bitmap from program memory
// - Dimensions and output color are defined in array header
gslc_DrvDrawMonoFromMem(pGui,nDstX,nDstY,sImgRef.pImgBuf,true);
return true;
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_FMT) == GSLC_IMGREF_FMT_BMP24) {
// 24-bit Bitmap in flash
// FIXME: Should we be passing "true" as last param?
gslc_DrvDrawBmp24FromMem(pGui,nDstX,nDstY,sImgRef.pImgBuf,false);
return true;
} else {
return false; // TODO: not yet supported
}
} else if ((sImgRef.eImgFlags & GSLC_IMGREF_SRC) == GSLC_IMGREF_SRC_SD) {
// Load image from SD media
#if (GSLC_SD_EN)
if ((sImgRef.eImgFlags & GSLC_IMGREF_FMT) == GSLC_IMGREF_FMT_BMP24) {
// 24-bit Bitmap
gslc_DrvDrawBmp24FromSD(pGui,sImgRef.pFname,nDstX,nDstY);
return true;
} else {
// Unsupported format
return false;
}
#else
// SD card access not enabled
return false;
#endif
} else {
// Unsupported source
GSLC_DEBUG_PRINT("DBG: DrvDrawImage() unsupported source eImgFlags=%d\n", sImgRef.eImgFlags);
return false;
}
}
void gslc_DrvDrawBkgnd(gslc_tsGui* pGui)
{
if (pGui->pvDriver) {
gslc_tsDriver* pDriver = (gslc_tsDriver*)(pGui->pvDriver);
// Check to see if an image has been assigned to the background
if (pGui->sImgRefBkgnd.eImgFlags == GSLC_IMGREF_NONE) {
// No image assigned, so assume flat color background
// TODO: Create a new eImgFlags enum to signal that the
// background should be a flat color instead of
// an image.
// NOTE: We don't call m_disp.fillScreen() here as
// that API doesn't support clipping. Since
// we may be redrawing the page with a clipping
// region enabled, it is important that we don't
// redraw the entire screen.
gslc_tsRect rRect = (gslc_tsRect) { 0, 0, pGui->nDispW, pGui->nDispH };
gslc_DrvDrawFillRect(pGui, rRect, pDriver->nColBkgnd);
} else {
// An image should be loaded
// TODO: For now, re-use the DrvDrawImage(). Later, consider
// extending to support different background drawing
// capabilities such as stretching and tiling of background
// image.
gslc_DrvDrawImage(pGui,0,0,pGui->sImgRefBkgnd);
}
}
}
// -----------------------------------------------------------------------
// Touch Functions (via display driver)
// -----------------------------------------------------------------------
#if defined(DRV_TOUCH_IN_DISP)
bool gslc_DrvInitTouch(gslc_tsGui* pGui,const char* acDev) {
if (pGui == NULL) {
GSLC_DEBUG_PRINT("ERROR: DrvInitTouch(%s) called with NULL ptr\n","");
return false;
}
// For M5stack, no extra initialization is required to support the buttons
// Nothing further to do with driver
return true;
}
bool gslc_DrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPress,gslc_teInputRawEvent* peInputEvent,int16_t* pnInputVal)
{
if ((pGui == NULL) || (pGui->pvDriver == NULL)) {
GSLC_DEBUG_PRINT("ERROR: DrvGetTouch(%s) called with NULL ptr\n","");
return false;
}
// Assign defaults
*pnX = 0;
*pnY = 0;
*pnPress = 0;
*peInputEvent = GSLC_INPUT_NONE;
*pnInputVal = 0;
// Trigger the M5 update routine
M5.update();
// Btn.wasReleasefor() is only available in the latest M5stack versions.
#if defined(M5STACK_TOUCH_PRESS_LONG)
if (M5.BtnA.wasReleasefor(M5STACK_TOUCH_PRESS_LONG)) {
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_A_LONG;
} else if (M5.BtnB.wasReleasefor(M5STACK_TOUCH_PRESS_LONG)) {
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_B_LONG;
} else if (M5.BtnC.wasReleasefor(M5STACK_TOUCH_PRESS_LONG)) {
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_C_LONG;
} else if (M5.BtnA.wasReleased()) {
#else
if (M5.BtnA.wasReleased()) {
#endif
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_A;
} else if (M5.BtnB.wasReleased()) {
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_B;
} else if (M5.BtnC.wasReleased()) {
*peInputEvent = GSLC_INPUT_PIN_ASSERT;
*pnInputVal = GSLC_PIN_BTN_C;
} else {
return false; // No pin event detected
}
// If we reached here, then we had a button event
return true;
}
#endif // DRV_TOUCH_IN_DISP
// -----------------------------------------------------------------------
// Dynamic Screen rotation and Touch axes swap/flip functions
// -----------------------------------------------------------------------
/// Change display rotation and any associated touch orientation
bool gslc_DrvRotate(gslc_tsGui* pGui, uint8_t nRotation)
{
bool bChange = true;
// Determine if the new orientation has swapped axes
// versus the native orientation (0)
bool bSwap = false;
if ((nRotation == 1) || (nRotation == 3)) {
bSwap = true;
}
// Did the orientation change?
if (nRotation == pGui->nRotation) {
// Orientation did not change -- indicate this by returning
// false so that we can avoid a redraw
bChange = false;
}
// Update the GUI rotation member
pGui->nRotation = nRotation;
// Inform the display to adjust the orientation and
// update the saved display dimensions
// DRV_DISP_M5STACK
// Capture display dimensions in native orientation
m_disp.setRotation(0);
pGui->nDisp0W = m_disp.width();
pGui->nDisp0H = m_disp.height();
// Capture display dimensions in selected orientation
m_disp.setRotation(pGui->nRotation);
pGui->nDispW = m_disp.width();
pGui->nDispH = m_disp.height();
// Update the clipping region
gslc_tsRect rClipRect = {0,0,pGui->nDispW,pGui->nDispH};
gslc_DrvSetClipRect(pGui,&rClipRect);
// Now update the touch remapping
#if !defined(DRV_TOUCH_NONE)
pGui->nSwapXY = TOUCH_ROTATION_SWAPXY(pGui->nRotation);
pGui->nFlipX = TOUCH_ROTATION_FLIPX(pGui->nRotation);
pGui->nFlipY = TOUCH_ROTATION_FLIPY(pGui->nRotation);
#endif // !DRV_TOUCH_NONE
// Mark the current page ask requiring redraw
// if the rotation value changed
if (bChange) {
gslc_PageRedrawSet( pGui, true );
}
return true;
}
// =======================================================================
// Private Functions
// =======================================================================
// Convert from RGB struct to native screen format
// TODO: Use 32bit return type?
uint16_t gslc_DrvAdaptColorToRaw(gslc_tsColor nCol)
{
uint16_t nColRaw = 0;
// RGB565
nColRaw |= (((nCol.r & 0xF8) >> 3) << 11); // Mask: 1111 1000 0000 0000
nColRaw |= (((nCol.g & 0xFC) >> 2) << 5); // Mask: 0000 0111 1110 0000
nColRaw |= (((nCol.b & 0xF8) >> 3) << 0); // Mask: 0000 0000 0001 1111
return nColRaw;
}
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // Compiler guard for requested driver