forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.lcd.st7735.spin
940 lines (806 loc) · 27.3 KB
/
display.lcd.st7735.spin
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
{
--------------------------------------------
Filename: display.lcd.st7735.spin
Author: Jesse Burt
Description: Driver for Sitronix ST77xx-based displays
Copyright (c) 2024
Started Mar 7, 2020
Updated Jan 3, 2024
See end of file for terms of use.
--------------------------------------------
}
{ these displays use way more memory than the P1 has, so drawing directly to the display is the
only supported method }
#define GFX_DIRECT
#define MEMMV_NATIVE wordmove
#include "graphics.common.spinh"
CON
MAX_COLOR = 65535
BYTESPERPX = 2
' Display visibility modes
NORMAL = 0
ALL_OFF = 1
INVERTED = 2
' Operating modes
' NORMAL = 0
IDLE = 1
PARTIAL = 2
' Subpixel order
RGB = 0
BGR = 1
' Power control 5
OFF = 0
SMALL = 1
MEDLOW = 2
MED = 3
MEDHI = 4
LARGE = 5
BCLK1_1 = 0
BCLK1_2 = 1
BCLK1_4 = 2
BCLK2_2 = 3
BCLK2_4 = 4
BCLK4_4 = 5
BCLK4_8 = 6
BCLK4_16 = 7
AUTO = 0
AVDD_X2_VGH25 = 0
AVDD_X3 = 1
AVDD_X3_VGH25 = 2
{ default I/O settings; these can be overridden in the parent object }
{ display dimensions }
WIDTH = 128
HEIGHT = 128
XMAX = WIDTH-1
YMAX = HEIGHT-1
CENTERX = WIDTH/2
CENTERY = HEIGHT/2
{ SPI }
CS = 0
SCK = 1
MOSI = 2
DC = 3
RST = 0
VAR
word _framerate
byte _CS, _RESET, _DC
byte _offs_x, _offs_y
{ Shadow registers }
byte _colmod, _madctl, _opmode
OBJ
spi: "com.spi.20mhz" ' SPI engine
core: "core.con.st7735" ' HW-specific constants
time: "time" ' basic timekeeping methods
PUB null{}
' This is not a top-level object
PUB start{}: status
' Start the driver using default I/O settings
return startx(CS, SCK, MOSI, DC, RST, WIDTH, HEIGHT, 0)
PUB startx(CS_PIN, SCK_PIN, SDA_PIN, DC_PIN, RESET_PIN, DISP_W, DISP_H, ptr_drawbuff): status
' Start using custom I/O settings
' NOTE: RES_PIN is optional, but recommended (pin # only validated in reset())
' ptr_drawbuff is ignored (exists only for API compatibility with other drivers)
if ( lookdown(CS_PIN: 0..31) and lookdown(SCK_PIN: 0..31) and lookdown(SDA_PIN: 0..31) and ...
lookdown(DC_PIN: 0..31) )
if ( status := spi.init(SCK_PIN, SDA_PIN, -1, core#SPI_MODE) )
_RESET := RESET_PIN
_DC := DC_PIN
_CS := CS_PIN
outa[_CS] := 1
dira[_CS] := 1
outa[_DC] := 1
dira[_DC] := 1
reset{}
set_dims(DISP_W, DISP_H)
return
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
PUB stop{}
' Stop the driver
visibility(ALL_OFF)
powered(FALSE)
spi.deinit{}
dira[_CS] := 0
dira[_DC] := 0
PUB defaults{}
' Apply power-on-reset default settings (ST7735R)
reset{}
powered(TRUE)
frame_rate_ctrl(1, 44, 45, 0, 0, 0)
frame_rate_ctrl(1, 44, 45, 0, 0, 0)
frame_rate_ctrl(1, 44, 45, 1, 44, 45)
inversion_ctrl(%011)
pwr_ctrl1(4_900, 4_600, -4_600, AUTO)
pwr_ctrl2(2_400, AVDD_X3, -10_000)
pwr_ctrl(NORMAL, MEDLOW, SMALL, 1, 1, 1, 1, 1)
pwr_ctrl(PARTIAL, MEDLOW, SMALL, 2, 4, 2, 1, 2)
pwr_ctrl(IDLE, MEDLOW, SMALL, 2, 2, 2, 2, 2)
com_voltage(-0_525)
invert_colors(FALSE)
mirror_h(FALSE)
mirror_v(FALSE)
subpix_order(RGB)
color_depth(16)
disp_offset(2, 3)
draw_area(0, 0, 127, 127)
gamma_tbl_pos(@gammatable_pos)
gamma_tbl_neg(@gammatable_neg)
disp_part_area(0, 161) ' Can be 0, 159 also, depending on GM pins config
opmode(NORMAL)
visibility(NORMAL)
PUB preset_bluetab240x240{} | byte tmp[4]
' ST7789VW: Adafruit 1.3" 240x240 LCD, blue-tabbed overlay
reset{}
time.msleep(150)
command(core.SLPOUT)
time.msleep(10)
tmp := $55
writereg(core.COLMOD, 1, @tmp)
time.msleep(10)
tmp := $08
writereg(core.MADCTL, 1, @tmp)
tmp[0] := 0
tmp[1] := 0
tmp[2] := 0
tmp[3] := 240
writereg(core.CASET, 4, @tmp)
tmp[0] := 0
tmp[1] := 0
tmp[2] := 320>>8
tmp[3] := 320&$ff
writereg(core.RASET, 4, @tmp)
command(core.INVON)
time.msleep(10)
command(core.NORON)
time.msleep(10)
command(core.DISPON)
time.msleep(10)
PUB preset_greentab128x128{}
' Like defaults, but with settings applicable to green-tabbed 128x128 displays (ST7735R)
reset{}
powered(TRUE)
frame_rate_ctrl(1, 44, 45, 0, 0, 0)
frame_rate_ctrl(1, 44, 45, 0, 0, 0)
frame_rate_ctrl(1, 44, 45, 1, 44, 45)
inversion_ctrl(%011)
pwr_ctrl1(5_000, 4_600, -4_600, AUTO)
pwr_ctrl2(2_400, AVDD_X3, -10_000)
pwr_ctrl(NORMAL, MEDLOW, SMALL, 1, 1, 1, 1, 1)
pwr_ctrl(PARTIAL, MEDLOW, SMALL, 2, 2, 2, 1, 2)
pwr_ctrl(IDLE, MEDLOW, SMALL, 2, 4, 2, 4, 2)
com_voltage(-0_525)
invert_colors(FALSE)
mirror_h(TRUE)
mirror_v(TRUE)
subpix_order(BGR)
color_depth(16)
disp_offset(2, 3)
draw_area(0, 0, _disp_xmax, _disp_ymax)
gamma_tbl_pos(@gammatable_pos)
gamma_tbl_neg(@gammatable_neg)
disp_part_area(0, _disp_ymax)
opmode(NORMAL)
visibility(NORMAL)
PUB preset_adafruit_1p3_240x240_land_up{}
' ST7789VW: Adafruit 1.3" 240x240 (#4313, blue tab), landscape (up)
set_dims(240, 240)
preset_bluetab240x240{}
rotation(0)
mirror_h(false)
mirror_v(false)
disp_offset(0, 0)
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p3_240x240_land_down{}
' ST7789VW: Adafruit 1.3" 240x240 (#4313, blue tab), landscape (down)
set_dims(240, 240)
preset_bluetab240x240{}
rotation(0)
mirror_h(true)
mirror_v(true)
disp_offset(0, 80) ' 80: 320-240
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p3_240x240_port_up{}
' ST7789VW: Adafruit 1.3" 240x240 (#4313, blue tab), portrait (up)
set_dims(240, 240)
preset_bluetab240x240{}
rotation(1)
mirror_h(false)
mirror_v(true)
disp_offset(80, 0) ' 80: 320-240
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p3_240x240_port_down{}
' ST7789VW: Adafruit 1.3" 240x240 (#4313, blue tab), portrait (down)
set_dims(240, 240)
preset_bluetab240x240{}
rotation(1)
mirror_h(true)
mirror_v(false)
disp_offset(0, 0) ' 80: 320-240
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p44_128x128_land_up{}
' ST7735R: Adafruit 1.44" 128x128 (#2088, green tab), landscape (up)
set_dims(128, 128)
preset_greentab128x128{}
rotation(0)
mirror_h(false)
mirror_v(false)
disp_offset(2, 1)
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p44_128x128_land_down{}
' ST7735R: Adafruit 1.44" 128x128 (#2088, green tab), landscape (down)
set_dims(128, 128)
preset_greentab128x128{}
rotation(0)
mirror_h(true)
mirror_v(true)
disp_offset(2, 3)
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p44_128x128_port_up{}
' ST7735R: Adafruit 1.44" 128x128 (#2088, green tab), portrait (up)
set_dims(128, 128)
preset_greentab128x128{}
rotation(1)
mirror_h(false)
mirror_v(true)
disp_offset(3, 2)
draw_area(0, 0, _disp_xmax, _disp_ymax)
PUB preset_adafruit_1p44_128x128_port_down{}
' ST7735R: Adafruit 1.44" 128x128 (#2088, green tab), portrait (up)
set_dims(128, 128)
preset_greentab128x128{}
rotation(1)
mirror_h(true)
mirror_v(false)
disp_offset(1, 2)
draw_area(0, 0, _disp_xmax, _disp_ymax)
#ifdef GFX_DIRECT
PUB bitmap(ptr_bmap, xs, ys, bm_wid, bm_lns) | offs, nr_pix
' Display bitmap
' ptr_bmap: pointer to bitmap data
' (xs, ys): upper-left corner of bitmap
' bm_wid: width of bitmap, in pixels
' bm_lns: number of lines in bitmap
draw_area(xs, ys, xs + (bm_wid - 1), ys + (bm_lns - 1))
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
' calc total number of pixels to write, based on dims and color depth
' clamp to a minimum of 1 to avoid odd behavior
nr_pix := 1 #> ((xs + bm_wid - 1) * (ys + bm_lns - 1) * BYTESPERPX)
outa[_DC] := core#DATA
spi.wrblock_lsbf(ptr_bmap, nr_pix)
outa[_CS] := 1
#endif
#ifdef GFX_DIRECT
PUB box(x1, y1, x2, y2, color, fill) | byte cmd_pkt[10]
' Draw a box
' (x1, y1): upper-left corner of box
' (x2, y2): lower-right corner of box
' color: border and (optional) fill color
' fill: filled flag (0: no fill, nonzero: fill)
if (x2 < x1) or (y2 < y1)
return
if (fill)
{ add currently set display offsets }
x1 += _offs_x
x2 += _offs_x
y1 += _offs_y
y2 += _offs_y
{ filled box: set the display's draw boundaries to the size of
the box, and send enough data to draw H * W pixels }
cmd_pkt[0] := core#CASET ' D/C L
cmd_pkt[1] := x1.byte[1] ' D/C H
cmd_pkt[2] := x1.byte[0]
cmd_pkt[3] := x2.byte[1]
cmd_pkt[4] := x2.byte[0]
cmd_pkt[5] := core#RASET ' D/C L
cmd_pkt[6] := y1.byte[1] ' D/C H
cmd_pkt[7] := y1.byte[0]
cmd_pkt[8] := y2.byte[1]
cmd_pkt[9] := y2.byte[0]
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(cmd_pkt[0]) ' column cmd
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[1], 4) ' x1, x2
outa[_DC] := core#CMD
spi.wr_byte(cmd_pkt[5]) ' row cmd
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[6], 4) ' y1, y2
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, ((y2-y1)+1) * ((x2-x1)+1))
outa[_CS] := 1
else
' no-fill box: set the display's draw boundaries to just the
' 1-pixel wide/tall segment, and send enough data to draw it
draw_area(x1, y1, x2, y1) ' top
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (x2-x1)+1)
draw_area(x1, y2, x2, y2) ' bottom
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (x2-x1)+1)
draw_area(x1, y1, x1, y2) ' left
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (y2-y1)+1)
draw_area(x2, y1, x2, y2) ' right
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (y2-y1)+1)
outa[_CS] := 1
#endif
#ifdef GFX_DIRECT
PUB clear{}
' Clear the display directly, bypassing the display buffer
draw_area(0, 0, _disp_xmax, _disp_ymax)
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(_bgcolor, _buff_sz/2)
outa[_CS] := 1
#else
PUB clear{}
' Clear the display buffer
wordfill(_ptr_drawbuffer, _bgcolor, _buff_sz/2)
#endif
PUB color_depth(format)
' Set expected color format of pixel data, in bits per pixel
' Valid values: 12, 16, 18
case format
12, 16, 18:
#ifdef ST7789
format := lookdown(format: 0, 0, 12, 0, 16, 18) | core#RGB_65K
#else
format := lookdown(format: 0, 0, 12, 0, 16, 18)
#endif
writereg(core#COLMOD, 1, @format)
PUB com_voltage(level)
' Set VCOM voltage level, in millivolts
' Valid values:
' -0_425..-2_000 (rounded to nearest 25mV; clamped to range; POR: -0_525)
level := (-(-2_000 #> level <# -0_425) / 25) - 17
writereg(core#VMCTR1, 1, @level)
PUB draw_area(sx, sy, ex, ey) | tmp, byte tmpx[4], byte tmpy[4]
' Set display start (sx, sy) and end (ex, ey) drawing boundaries
if ((sx => 0) and (ex =< _disp_xmax) and (sy => 0) and (ey =< _disp_ymax))
{ The ST77xx requires (ex, ey) be greater than (sx, sy).
If they're not, swap them. }
sx += _offs_x
sy += _offs_y
ex += _offs_x
ey += _offs_y
if (ex < sx) ' ex is less than sx?
tmp := sx ' swap them
sx := ex
ex := tmp
if (ey < sy) ' ey is less than sy?
tmp := sy ' swap them
sy := ey
ey := tmp
tmpx[0] := sx.byte[1]
tmpx[1] := sx.byte[0]
tmpx[2] := ex.byte[1]
tmpx[3] := ex.byte[0]
tmpy[0] := sy.byte[1]
tmpy[1] := sy.byte[0]
tmpy[2] := ey.byte[1]
tmpy[3] := ey.byte[0]
writereg(core#CASET, 4, @tmpx)
writereg(core#RASET, 4, @tmpy)
PUB invert_colors(state)
' Invert display colors
' Valid values:
' TRUE (non-zero), FALSE (0)
if (state)
visibility(INVERTED)
else
visibility(NORMAL)
PUB disp_offset(x, y)
' Set display offset
_offs_x := (0 #> x <# 127)
_offs_y := (0 #> y <# 159)
PUB rotation(state)
' Rotate display
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value returns the current setting
_madctl := ((_madctl & core#MV_MASK) | (((state <> 0) & 1) << core#MV))
writereg(core#MADCTL, 1, @_madctl)
PUB visibility(mode) | inv_state
' Set display visiblity
' NOTE: Doesn't affect display RAM contents.
' NOTE: There is a mandatory 120ms delay imposed by calling this method
case mode
ALL_OFF:
mode := core#DISPOFF
NORMAL:
mode := core#DISPON
inv_state := core#INVOFF
INVERTED:
mode := core#DISPON
inv_state := core#INVON
other:
return
command(mode)
command(inv_state)
time.msleep(120)
PUB frame_rate_ctrl(ln_per, f_porch, b_porch, lim_ln_per, lim_f_porch, lim_b_porch) | byte tmp[6], nr_bytes
' Set frame frequency
' Valid values:
' ln_per: 0..15
' f_porch: 0..63
' b_porch: 0..63
' lim_* variants (effective when in Line Inversion Mode - used only in opmode(PARTIAL))
' (same as above)
' - ignored when opmode is NORMAL or IDLE
case _opmode
NORMAL, IDLE:
nr_bytes := 3
PARTIAL:
nr_bytes := 6
other:
return
tmp[0] := (0 #> ln_per <# 15)
tmp[1] := (0 #> f_porch <# 63)
tmp[2] := (0 #> b_porch <# 63)
if (_opmode == PARTIAL)
tmp[3] := (0 #> lim_ln_per <# 15)
tmp[4] := (0 #> lim_f_porch <# 63)
tmp[5] := (0 #> lim_b_porch <# 63)
_framerate := (core#FOSC / ((ln_per * 2 + 40) * (_disp_height + f_porch + b_porch)))
writereg(core#FRMCTR1 + _opmode, nr_bytes, @tmp)
PUB gamma_tbl_neg(ptr_buff)
' Modify gamma table (negative polarity)
writereg(core#GMCTRN1, 16, ptr_buff)
PUB gamma_tbl_pos(ptr_buff)
' Modify gamma table (positive polarity)
writereg(core#GMCTRP1, 16, ptr_buff)
PUB inversion_ctrl(mask)
' Set display inversion mode control bitmask
' Valid values: %000..%111
' 0: Dot inversion
' 1: Line inversion
' Bits 321:
' 3 - Inversion setting in OpMode(NORMAL) (PORT: 0)
' 2 - Inversion setting in OpMode(IDLE) (PORT: 1)
' 1 - Inversion setting in OpMode(PARTIAL) (POR: 1)
mask &= %111
writereg(core#INVCTR, 1, @mask)
#ifdef GFX_DIRECT
PUB line(x1, y1, x2, y2, color) | sx, sy, ddx, ddy, err, e2
' Draw line from x1, y1 to x2, y2
if (x1 == x2)
draw_area(x1, y1, x1, y2) ' vertical
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (||(y2-y1))+1)
outa[_CS] := 1
return
if (y1 == y2)
draw_area(x1, y1, x2, y1) ' horizontal
outa[_CS] := 0
outa[_DC] := core#CMD
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA
spi.wrwordx_msbf(color, (||(x2-x1))+1)
outa[_CS] := 1
return
ddx := ||(x2-x1)
ddy := ||(y2-y1)
err := ddx-ddy
sx := -1
if (x1 < x2)
sx := 1
sy := -1
if (y1 < y2)
sy := 1
repeat until ((x1 == x2) and (y1 == y2))
plot(x1, y1, color)
e2 := err << 1
if (e2 > -ddy)
err -= ddy
x1 += sx
if (e2 < ddx)
err += ddx
y1 += sy
#endif
PUB mirror_h(state)
' Mirror the display, horizontally
' Valid values: TRUE (non-zero), FALSE (0)
_madctl := ((_madctl & core#MX_MASK) | (((state <> 0) & 1) << core#MX))
writereg(core#MADCTL, 1, @_madctl)
PUB mirror_v(state)
' Mirror the display, vertically
' Valid values: TRUE (non-zero), FALSE (0)
_madctl := ((_madctl & core#MY_MASK) | (((state <> 0) & 1) << core#MY))
writereg(core#MADCTL, 1, @_madctl)
PUB opmode(mode)
' Set operating mode
' Valid values:
' NORMAL (0): Normal display mode
' PARTIAL (1): Partial display mode
' IDLE (2): Idle/reduced color (8 color) mode
' Any other value is ignored
case mode
NORMAL:
command(core#IDMOFF)
command(core#NORON)
PARTIAL:
command(core#PTLON)
IDLE:
command(core#IDMON)
other:
return
_opmode := mode
PUB disp_part_area(sy, ey) | byte tmp[4]
' Define visible area (rows) of display when operating in partial-display mode
tmp[0] := 0
tmp[1] := (sy & $FF)
tmp[2] := 0
tmp[3] := (ey & $FF)
writereg(core#PTLAR, 4, @tmp)
#ifdef ST7789
PUB plot(x, y, color) | tmp, xs, ys, xe, ye, byte cmd_pkt[13]
' Plot pixel at (x, y) in color
if ((x < 0) or (x > _disp_xmax) or (y < 0) or (y > _disp_ymax))
return ' coords out of bounds, ignore
#ifdef GFX_DIRECT
' direct to display
{ build command packet }
cmd_pkt[0] := core#CASET ' D/C L
cmd_pkt[1] := (x + _offs_x) >> 8 ' D/C H
cmd_pkt[2] := (x + _offs_x) & $ff
cmd_pkt[3] := (x + _offs_x) >> 8 ' D/C H
cmd_pkt[4] := (x + _offs_x) & $ff
cmd_pkt[5] := core#RASET ' D/C L
cmd_pkt[6] := (y + _offs_y) >> 8 ' D/C H
cmd_pkt[7] := (y + _offs_y) & $ff
cmd_pkt[8] := (y + _offs_y) >> 8 ' D/C H
cmd_pkt[9] := (y + _offs_y) & $ff
cmd_pkt[10] := core#RAMWR ' D/C L
cmd_pkt[11] := color.byte[1] ' D/C H
cmd_pkt[12] := color.byte[0]
{ write X coordinate }
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(cmd_pkt[0])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[1], 4)
{ write Y coordinate }
outa[_DC] := core#CMD
spi.wr_byte(cmd_pkt[5])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[6], 4)
{ write pixel color }
outa[_DC] := core#CMD
spi.wr_byte(cmd_pkt[10])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[11], 2)
outa[_CS] := 1
#else
' buffered display
word[_ptr_drawbuffer][x + (y * _disp_width)] := color
#endif
#else
PUB plot(x, y, color) | tmp, xs, ys, xe, ye, byte cmd_pkt[9]
' Plot pixel at (x, y) in color
if ((x < 0) or (x > _disp_xmax) or (y < 0) or (y > _disp_ymax))
return ' coords out of bounds, ignore
#ifdef GFX_DIRECT
' direct to display
{ build command packet }
cmd_pkt[0] := core#CASET ' D/C L
cmd_pkt[1] := (x + _offs_x) ' D/C H
cmd_pkt[2] := (x + _offs_x)
cmd_pkt[3] := core#RASET ' D/C L
cmd_pkt[4] := (y + _offs_y) ' D/C H
cmd_pkt[5] := (y + _offs_y)
cmd_pkt[6] := core#RAMWR ' D/C L
cmd_pkt[7] := color.byte[1] ' D/C H
cmd_pkt[8] := color.byte[0]
{ write X coordinate }
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(cmd_pkt[0])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[1], 2)
{ write Y coordinate }
outa[_DC] := core#CMD
spi.wr_byte(cmd_pkt[3])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[4], 2)
{ write pixel color }
outa[_DC] := core#CMD
spi.wr_byte(cmd_pkt[6])
outa[_DC] := core#DATA
spi.wrblock_lsbf(@cmd_pkt[7], 2)
outa[_CS] := 1
#else
' buffered display
word[_ptr_drawbuffer][x + (y * _disp_width)] := color
#endif
#endif
#ifndef GFX_DIRECT
PUB point(x, y): pix_clr
' Get color of pixel at x, y
x := (0 #> x <# _disp_xmax)
y := (0 #> y <# _disp_ymax)
return word[_ptr_drawbuffer][x + (y * _disp_width)]
#endif
PUB powered(state)
' Enable display power
' Valid values:
' TRUE (non-zero), FALSE (0)
' NOTE: This incurs a 120ms delay after calling
command( (((state <> 0) & 1) + core#SLPIN) )
time.msleep(120)
PUB pwr_ctrl(mode, ap, sap, bclkdiv1, bclkdiv2, bclkdiv3, bclkdiv4, bclkdiv5) | byte tmp[2]
' Set partial mode/full-colors power control
' Valid values:
' mode: Settings applied to operating mode
' 0: Normal mode/full color
' 1: Idle mode/8-color
' 2: Partial mode/full color
' ap, sap: Set opamp current
' OFF (0): Disabled
' SMALL (1), MEDLOW (2), MED (3), MEDHI (4), LARGE (5)
' boost_clkdiv: Set booster circuit clock frequency divisor
' Setting Booster circuit 1
' 1: BCLK / 1
' 1_5: BCLK / 1.5
' 2: BCLK / 2
' 4: BCLK / 4
mode := (NORMAL #> mode <# PARTIAL)
ap := (OFF #> ap <# LARGE)
sap := ((OFF #> sap <# LARGE) << core#SAP)
case bclkdiv1
1, 1_5, 2, 4:
bclkdiv1 := lookdownz(bclkdiv1: 1, 1_5, 2, 4)
other:
return
case bclkdiv2
1, 1_5, 2, 4:
bclkdiv2 := lookdownz(bclkdiv2: 1, 1_5, 2, 4)
other:
return
case bclkdiv3
1, 1_5, 2, 4:
bclkdiv3 := lookdownz(bclkdiv3: 1, 1_5, 2, 4)
other:
return
case bclkdiv4
1, 1_5, 2, 4:
bclkdiv4 := lookdownz(bclkdiv4: 1, 1_5, 2, 4)
other:
return
case bclkdiv5
1, 1_5, 2, 4:
bclkdiv5 := lookdownz(bclkdiv5: 1, 1_5, 2, 4)
other:
return
tmp[0] := (ap | sap | (bclkdiv5 << core#DCMSB))
tmp[1] := ((bclkdiv4 << 6) | (bclkdiv3 << 4) | (bclkdiv2 << 2) | bclkdiv1)
writereg(core#PWCTR3 + mode, 2, @tmp)
PUB pwr_ctrl1(avdd, gvdd, gvcl, mode) | byte tmp[3]
' Set LCD supply voltages, in millivolts
' Valid values:
' avdd: 4_500..5_100, (rounded to nearest 100mV; clamped to range; POR: 4_900)
' gvdd: 3_150..4_700, (rounded to nearest 50mV; clamped to range; POR: 4_600)
' gvcl: -4_700..-3_150, (rounded to nearest 50mV; clamped to range; POR: -4_600)
' mode: 2, 3, AUTO (0) (POR: AUTO)
avdd := ((((4_500 #> avdd <# 5_100) / 100) - 45) << core#AVDD)
gvdd := ((4_700 - (3_150 #> gvdd <# 4_700)) / 50) & core#VRHP_BITS
tmp[0] := (avdd | gvdd)
tmp[1] := ((4_700 - (-(-4_700 #> gvcl <# -3_150))) / 50) & core#VRHN_BITS
tmp[2] := (((0 #> lookdownz(mode: 2, 3, AUTO) <# 2) << core#MODE) | core#MODE_RSVD)
writereg(core#PWCTR1, 3, @tmp)
PUB pwr_ctrl2(v25, vgh, vgl) | tmp
' Set LCD supply voltages, in millivolts
' Valid values:
' V25: 2_100, 2_200, 2_300, 2_400 (clamped to range; POR: 2_400)
' VGH: AVDD_X2_VGH25 (0), AVDD_X3 (1), AVDD_X3_VGH25 (2) (clamped to range; POR: AVDD_X3)
' VGL: -13_000, -12_500, -10_000, -7_500 (clamped to range; POR: -10_000)
v25 := ((((2_100 #> v25 <# 2_400) / 100) - 21) << core#VGH25)
vgh := (AVDD_X2_VGH25 #> vgh <# AVDD_X3_VGH25)
vgl := ((0 #> lookdownz(vgl: -7_500, -10_000, -12_500, -13_000) <# 3) << core#VGLSEL)
tmp := (v25 | vgh | vgl)
writereg(core#PWCTR2, 1, @tmp)
PUB reset{}
' Reset the display controller
if (lookdown(_RESET: 0..31)) ' I/O pin defined - hard reset
outa[_RESET] := 1
time.usleep(10)
outa[_RESET] := 0
time.usleep(10)
outa[_RESET] := 1
time.msleep(5)
else ' no I/O pin defined - do
command(core#SOFT_RESET) ' soft reset instead
pub scroll_up_fs(px)
' dummy method
PUB show{}
' Write the draw buffer to the display
#ifndef GFX_DIRECT
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(core#RAMWR)
outa[_DC] := core#DATA ' D/C high = data
spi.wrblock_lsbf(_ptr_drawbuffer, _buff_sz)
outa[_CS] := 1
#endif
PUB subpix_order(order)
' Set subpixel color order
' Valid values:
' RGB (0): Red-Green-Blue order
' BGR (1): Blue-Green-Red order
_madctl := ((_madctl & core#RGB_MASK) | ((0 #> order <# 1) << core#RGB))
writereg(core#MADCTL, 1, @_madctl)
#ifndef GFX_DIRECT
PRI memfill(xs, ys, val, count)
' Fill region of display buffer memory
' xs, ys: Start of region
' val: Color
' count: Number of consecutive memory locations to write
wordfill(_ptr_drawbuffer + ((xs << 1) + (ys * _bytesperln)), val, count)
#endif
PRI command(c)
' Single-byte command without parameters
case c
$00, $01, $11..$13, $20, $21, $28, $29, $38, $39:
outa[_DC] := core#CMD ' D/C low = command
outa[_CS] := 0
spi.wr_byte(c)
outa[_CS] := 1
return
PRI writereg(reg_nr, nr_bytes, ptr_buff)
' Write nr_bytes to device from ptr_buff
case reg_nr
$2A..$2C, $30, $36, $3A, $B1..$B4, $B6, $C0..$C5, $E0, $E1, $FC:
outa[_DC] := core#CMD
outa[_CS] := 0
spi.wr_byte(reg_nr)
outa[_DC] := core#DATA
spi.wrblock_lsbf(ptr_buff, nr_bytes)
outa[_CS] := 1
return
DAT
gammatable_neg byte $02, $1C, $07, $12
byte $37, $32, $29, $2D
byte $29, $25, $2B, $39
byte $00, $01, $03, $10
gammatable_pos byte $03, $1D, $07, $06
byte $2E, $2C, $29, $2D
byte $2E, $2E, $37, $3F
byte $00, $00, $02, $10
DAT
{
Copyright 2024 Jesse Burt
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.
}