forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.oled.ssd1331.spin
768 lines (662 loc) · 23.4 KB
/
display.oled.ssd1331.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
{
---------------------------------------------------------------------------------------------------
Filename: display.oled.ssd1331.spin
Description: Driver for Solomon Systech SSD1331 RGB OLED displays
Author: Jesse Burt
Started: Apr 28, 2019
Updated: Feb 2, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
#define MEMMV_NATIVE wordmove
#include "graphics.common.spinh"
CON
{ /// default I/O settings; these can be overridden in the parent object }
{ display dimensions }
WIDTH = 96
HEIGHT = 64
{ SPI }
CS = 0
SCK = 1
MOSI = 2
DC = 3
RST = 4
SPI_FREQ = 6_666_666
BPP = 16 ' bits per pixel/color depth of the display
{ /// }
BYTESPERPX = 1 #> (BPP/8) ' limit to minimum of 1
BPPDIV = 1 #> (8 / BPP) ' limit to range 1 .. (8/BPP)
XMAX = WIDTH-1
YMAX = HEIGHT-1
CENTERX = WIDTH/2
CENTERY = HEIGHT/2
BUFF_SZ = (WIDTH * HEIGHT) / BPPDIV
MAX_COLOR = (1 << BPP)-1
' Transaction type selection
CMD = 0
DATA = 1
' Display power modes
OFF = 0
ON = 1
DIM = 2
' Display visibility modes
NORMAL = 0
ALL_ON = 1
ALL_OFF = 2
INVERTED = 3
' Color depth formats
COLOR_256 = %00
COLOR_65K = %01
COLOR_65K2 = %10
' Address increment mode
HORIZ = 0
VERT = 1
' Subpixel order
RGB = 0
BGR = 1
OBJ
core: "core.con.ssd1331" ' HW-specific constants
time: "time" ' timekeeping methods
spi: "com.spi.20mhz" ' SPI engine
VAR
{ I/O pins }
long _CS, _DC, _RES
#ifndef GFX_DIRECT
word _framebuffer[BUFF_SZ]
#endif
{ shadow registers }
byte _sh_SETCOLUMN, _sh_SETROW, _sh_SETCONTRAST_A, _sh_SETCONTRAST_B, _sh_SETCONTRAST_C
byte _sh_MASTERCCTRL, _sh_SECPRECHG[3], _sh_REMAPCOLOR, _sh_DISPSTARTLINE, _sh_DISPOFFSET
byte _sh_DISPMODE, _sh_MULTIPLEX, _sh_DIM, _sh_MASTERCFG, _sh_DISPONOFF, _sh_PWRSAVE
byte _sh_PHASE12PER, _sh_CLK, _sh_GRAYTABLE, _sh_PRECHGLEV, _sh_VCOMH, _sh_CMDLOCK
byte _sh_HVSCROLL, _sh_FILL
PUB null()
' This is not a top-level object
PUB start(): status
' Start the driver using default I/O settings
#ifdef GFX_DIRECT
return startx(CS, SCK, MOSI, DC, RST, WIDTH, HEIGHT, 0)
#else
return startx(CS, SCK, MOSI, DC, RST, WIDTH, HEIGHT, @_framebuffer)
#endif
PUB startx(CS_PIN, CLK_PIN, DIN_PIN, DC_PIN, RES_PIN, DISP_W, DISP_H, ptr_drawbuff): status
' Start using custom I/O settings
' RES_PIN optional, but recommended (pin # only validated in reset())
if ( lookdown(CS_PIN: 0..31) and lookdown(DC_PIN: 0..31) and lookdown(DIN_PIN: 0..31) and ...
lookdown(CLK_PIN: 0..31) )
if ( status := spi.init(CLK_PIN, DIN_PIN, -1, core.SPI_MODE) )
_CS := CS_PIN
_DC := DC_PIN
_RES := RES_PIN
outa[_CS] := 1
dira[_CS] := 1
outa[_DC] := 1
dira[_DC] := 1
reset()
set_dims(DISP_W, DISP_H)
set_address(ptr_drawbuff)
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()
' Turn off display, stop SPI engine, clear out variable space
visibility(ALL_OFF)
powered(FALSE)
spi.deinit()
dira[_CS] := 0
dira[_DC] := 0
if ( lookdown(_RES: 0..31) )
dira[_RES] := 0
longfill(@_CS, 0, 3)
longfill(@_ptr_drawbuffer, 0, 14) ' graphics.common.spinh
wordfill(@_charpx_xmax, 0, 4) ' graphics.common.spinh
bytefill(@_charcell_w, 0, 6) ' graphics.common.spinh
bytefill(@_sh_SETCOLUMN, 0, 26)
PUB defaults()
' Factory default settings
#ifdef HAS_RESET
reset()
#else
visibility(ALL_OFF)
disp_start_line(0)
disp_lines(64)
ext_supply_ena()
clk_freq(956)
clk_div(1)
contrast(127)
powered(TRUE)
draw_area(0, 0, 95, 63)
clear()
visibility(NORMAL)
#endif
PUB preset_96x64()
' Preset: 96px wide, setup for 64px height
visibility(ALL_OFF)
color_depth(COLOR_65K)
powered(FALSE)
disp_lines(64)
ext_supply_ena()
clk_freq(956)
clk_div(1)
contrast(127)
interlace_ena(false)
powered(TRUE)
draw_area(0, 0, 95, 63)
clear()
visibility(NORMAL)
PUB preset_96x64_hi_perf()
' Preset: 96px wide, setup for 64px height, display osc. set to max clock
visibility(ALL_OFF)
color_depth(COLOR_65K)
powered(FALSE)
disp_lines(64)
ext_supply_ena()
clk_freq(980)
clk_div(1)
contrast(127)
interlace_ena(false)
powered(TRUE)
draw_area(0, 0, 95, 63)
clear()
visibility(NORMAL)
PUB preset_96x()
' Preset: 96px wide, determine settings for height at runtime
visibility(ALL_OFF)
color_depth(COLOR_65K)
powered(FALSE)
disp_lines(_disp_height)
ext_supply_ena()
clk_freq(956)
clk_div(1)
contrast(127)
interlace_ena(false)
powered(TRUE)
draw_area(0, 0, _disp_width, _disp_height)
clear()
visibility(NORMAL)
PUB addr_mode(mode): curr_mode
' Set display internal addressing mode
' Valid values:
' HORIZ (0): Horizontal addressing mode
' VERT (1): Vertical addressing mode
curr_mode := _sh_REMAPCOLOR
case mode
HORIZ, VERT:
other:
return ((curr_mode >> core.ADDRINC) & 1)
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.SEGREMAP_MASK) | mode)
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
#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
' 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(sx, sy, ex, ey, color, filled) | tmp[3]
' Draw a box
' sx, sy: Start coordinates
' ex, ey: End coordinates
' color: Box color
' filled: Flag to set whether to fill the box or not
sx := 0 #> sx <# _disp_xmax
sy := 0 #> sy <# _disp_ymax
ex := sx #> ex <# _disp_xmax
ey := sy #> ey <# _disp_ymax
if ( ||(filled) <> (_sh_FILL & 1) ) ' only call this if the filled
if (filled) ' param is different than the
fill_accel_ena(TRUE) ' current filled setting
else ' to avoid having to sending
fill_accel_ena(FALSE) ' it to the display every time
{ start, end coords }
tmp.byte[0] := sx
tmp.byte[1] := sy
tmp.byte[2] := ex
tmp.byte[3] := ey
{ fg color - left-justified }
tmp.byte[4] := ((color & $f800) >> 11) << 1 ' R LSB is don't care
tmp.byte[5] := (color & $07e0) >> 5
tmp.byte[6] := (color & $1f) << 1 ' B LSB is don't care
{ bg color - left-justified }
tmp.byte[7] := ((color & $f800) >> 11) << 1 ' R LSB is don't care
tmp.byte[8] := (color & $07e0) >> 5
tmp.byte[9] := (color & $1f) << 1 ' B LSB is don't care
writereg(core.DRAWRECT, 10, @tmp)
#endif
#ifdef GFX_DIRECT
PUB clear() | tmp
' Clear the display
tmp.byte[0] := 0
tmp.byte[1] := 0
tmp.byte[2] := _disp_xmax
tmp.byte[3] := _disp_ymax
writereg(core.CLEAR, 4, @tmp)
#else
PUB clear()
' Clear the display buffer
wordfill(_ptr_drawbuffer, _bgcolor, _buff_sz/2)
#endif
PUB clk_div(divider): curr_div
' Set clock frequency divider used by the display controller
' Valid values: 1..16
' Any other value returns the current setting
curr_div := _sh_CLK
case divider
1..16:
divider -= 1
other:
return ((curr_div & core.CLKDIV_BITS) + 1)
_sh_CLK := ((_sh_CLK & core.CLKDIV_MASK) | divider)
writereg(core.CLKDIV_FRQ, 1, @_sh_CLK)
PUB clk_freq(freq): curr_freq
' Set display internal oscillator frequency, in kHz
' Valid values: 800..980, in steps of 12
' Any other value returns the current setting
curr_freq := _sh_CLK
case freq
800..980:
freq := ((freq-800) / 12) << core.FOSCFREQ
other:
curr_freq := (curr_freq >> core.FOSCFREQ) & core.FOSCFREQ_BITS
return (curr_freq * 12) + 800
_sh_CLK := ((_sh_CLK & core.FOSCFREQ_MASK) | freq)
writereg(core.CLKDIV_FRQ, 1, @_sh_CLK)
PUB color_depth(format): curr_fmt
' Set expected color format of pixel data
' Valid values:
' COLOR_256 (0): 8-bit/256 color
' COLOR_65K (1): 16-bit/65536 color format 1
' COLOR_65K2 (2): 16-bit/65536 color format 2
' Any other value returns the current setting
curr_fmt := _sh_REMAPCOLOR
case format
COLOR_256, COLOR_65K, COLOR_65K2:
format <<= core.COLORFMT
other:
return curr_fmt >> core.COLORFMT
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.COLORFMT_MASK) | format)
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB contrast(level)
' Set display contrast/brightness
' Valid values: 0..255
' Any other value returns the current setting
contrast_a(level)
contrast_b(level)
contrast_c(level)
PUB contrast_a(level): curr_lvl
' Set contrast/brightness level of subpixel A
' Valid values: 0..255
' Any other value returns the current setting
case level
0..255:
_sh_SETCONTRAST_A := level
writereg(core.CONTRASTA, 1, @_sh_SETCONTRAST_A)
other:
return _sh_SETCONTRAST_A
PUB contrast_b(level): curr_lvl
' Set contrast/brightness level of subpixel B
' Valid values: 0..255
' Any other value returns the current setting
case level
0..255:
_sh_SETCONTRAST_B := level
writereg(core.CONTRASTB, 1, @_sh_SETCONTRAST_B)
other:
return _sh_SETCONTRAST_B
PUB contrast_c(level): curr_lvl
' Set contrast/brightness level of subpixel C
' Valid values: 0..255
' Any other value returns the current setting
case level
0..255:
_sh_SETCONTRAST_C := level
writereg(core.CONTRASTC, 1, @_sh_SETCONTRAST_C)
other:
return _sh_SETCONTRAST_C
#ifdef GFX_DIRECT
PUB copy(sx, sy, ex, ey, dx, dy) | tmp[2]
' Use the display's accelerated Copy Region function
' Valid values:
' sx, ex, dx: 0..95 (clamped to range)
' sy, ey, dy: 0..63 (clamped to range)
tmp.byte[0] := 0 #> sx <# 95
tmp.byte[1] := 0 #> sy <# 63
tmp.byte[2] := 0 #> ex <# 95
tmp.byte[3] := 0 #> ey <# 63
tmp.byte[4] := 0 #> dx <# 95
tmp.byte[5] := 0 #> dy <# 63
writereg(core.COPY, 6, @tmp)
#endif
PUB copy_invert_ena(state): curr_state
' Enable inverted colors, when using copy()
' NOTE: This only affects the accelerated/direct-draw variant of copy()
curr_state := _sh_FILL
case ||(state)
0, 1:
state := ||(state) << core.REVCOPY
other:
return (((curr_state >> core.REVCOPY) & 1) == 1)
_sh_FILL := ((_sh_FILL & core.REVCOPY_MASK) | state)
writereg(core.FILLCPY, 1, @_sh_FILL)
PUB current_limit(divisor)
' Set master current limit divisor
_sh_MASTERCCTRL := ((1 #> divisor <# 16) - 1)
writereg(core.MASTERCURRENT, 1, @_sh_MASTERCCTRL)
PUB disp_lines(lines)
' Set maximum number of display lines
' Valid values: 16..64 (clamped to range)
_sh_MULTIPLEX := ((16 #> lines <# 64) - 1)
writereg(core.SETMULTIPLEX, 1, @_sh_MULTIPLEX)
PUB disp_offset(lines)
' Set display offset/vertical shift
_sh_DISPOFFSET := (0 #> lines <# 63)
writereg(core.DISPLAYOFFSET, 1, @_sh_DISPOFFSET)
PUB disp_start_line(line)
' Set display start line
_sh_DISPSTARTLINE := (0 #> line <# 63)
writereg(core.STARTLINE, 1, @_sh_DISPSTARTLINE)
PUB draw_area(sx, sy, ex, ey) | tmp
' Set drawable display region for subsequent drawing operations
' Valid values:
' sx, ex: 0..95
' sy, ey: 0..63
' Any other value will be ignored
ifnot (lookup(sx: 0..95) or lookup(sy: 0..63) or lookup(ex: 0..95) or lookup(ey: 0..63))
return
tmp.byte[0] := sx
tmp.byte[1] := ex
writereg(core.SETCOLUMN, 2, @tmp)
tmp.byte[0] := sy
tmp.byte[1] := ey
writereg(core.SETROW, 2, @tmp)
PUB ext_supply_ena() | tmp
tmp := core.MASTERCFG_EXT_VCC
writereg(core.SETMASTER, 1, @tmp)
PUB fill_accel_ena(state)
' Enable the display's native/accelerated fill function, when using box()
_sh_FILL := ((_sh_FILL & core.FILL_MASK) | ((state <> 0) & 1))
writereg(core.FILLCPY, 1, @_sh_FILL)
PUB interlace_ena(state)
' Alternate every other display line:
' Lines 0..31 will appear on even rows (starting on row 0)
' Lines 32..63 will appear on odd rows (starting on row 1)
' Valid values: TRUE (non-zero), FALSE (0)
{ invert logic for COMSPLIT bit }
state := ((((state <> 0) & 1) ^ 1) << core.COMSPLIT)
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.COMSPLIT_MASK) | state)
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB invert_colors(state) | tmp
' Invert display colors
if (state)
visibility(INVERTED)
else
visibility(NORMAL)
#ifdef GFX_DIRECT
PUB line(sx, sy, ex, ey, color) | tmp[2]
' Draw line from sx, sy to ex, ey, in color
sx := 0 #> sx <# _disp_xmax
sy := 0 #> sy <# _disp_ymax
ex := 0 #> ex <# _disp_xmax
ey := 0 #> ey <# _disp_ymax
tmp.byte[0] := sx
tmp.byte[1] := sy
tmp.byte[2] := ex
tmp.byte[3] := ey
tmp.byte[4] := ((color & $f800) >> 11) << 1 ' R LSB is don't care
tmp.byte[5] := (color & $07e0) >> 5
tmp.byte[6] := (color & $1f) << 1 ' B LSB is don't care
writereg(core.DRAWLINE, 7, @tmp)
#endif
PUB mirror_h(state): curr_state
' Mirror the display, horizontally
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value returns the current setting
curr_state := _sh_REMAPCOLOR
case ||(state)
0, 1:
state := ||(state) << core.SEGREMAP
other:
return ((curr_state >> core.SEGREMAP) & 1) == 1
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.SEGREMAP_MASK | state))
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB mirror_v(state): curr_state
' Mirror the display, vertically
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value returns the current setting
curr_state := _sh_REMAPCOLOR
case ||(state)
0, 1:
state := ||(state) << core.COMREMAP
other:
return ((curr_state >> core.COMREMAP) & 1) == 1
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.COMREMAP_MASK) | state)
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB phase1_period(clks)
' Set discharge/phase 1 period, in display clocks
' Valid values: 1..15 (clamped to range)
_sh_PHASE12PER := ((_sh_PHASE12PER & core.PHASE1_MASK) | (1 #> clks <# 15))
writereg(core.PRECHG, 1, @_sh_PHASE12PER)
PUB phase2_period(clks)
' Set charge/phase 2 period, in display clocks
' Valid values: 1..15 (clamped to range)
_sh_PHASE12PER := ((_sh_PHASE12PER & core.PHASE2_MASK) | ((1 #> clks <# 15) << core.PHASE2))
writereg(core.PRECHG, 1, @_sh_PHASE12PER)
PUB plot(x, y, color) | tmpx, tmpy
' 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
tmpx.byte[0] := x
tmpx.byte[1] := x
tmpy.byte[0] := y
tmpy.byte[1] := y
writereg(core.SETCOLUMN, 2, @tmpx)
writereg(core.SETROW, 2, @tmpy)
outa[_DC] := DATA
outa[_CS] := 0
{ color_depth(COLOR_65K) }
spi.wr_byte(color.byte[1])
spi.wr_byte(color.byte[0])
{ color_depth(COLOR_65K2) }
' spi.wr_byte(rgb565_r5(color) << 1)
' spi.wr_byte(rgb565_g5(color))
' spi.wr_byte(rgb565_b5(color) << 1)
outa[_CS] := 1
#else
' buffered display
word[_ptr_drawbuffer][x + (y * _disp_width)] := color
#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 power_saving_ena(state)
' Enable display power saving mode
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value returns the current setting
state := lookupz(((state <> 0) & 1): core.PWRSAVE_DIS, core.PWRSAVE_ENA)
_sh_PWRSAVE := state
writereg(core.PWRMODE, 1, @_sh_PWRSAVE)
PUB powered(state)
' Enable display power
case ||(state)
OFF, ON, DIM:
state := lookupz(||(state): core.DISPLAYOFF, ...
core.DISPLAYON, ...
core.DISPLAYONDIM )
_sh_DISPONOFF := state
command(_sh_DISPONOFF)
other:
return
PUB precharge_lvl(level)
' Set first pre-charge voltage level (phase 2) of segment pins, in millivolts
' Valid values: 100..500 (clamped to range)
level := (((100 #> level <# 500) * 10) - 100_0) / 12_9
_sh_PRECHGLEV := level << core.PRECHG_LVL
writereg(core.PRECHGLVL, 1, @_sh_PRECHGLEV)
PUB precharge_speed(seg_a, seg_b, seg_c) | tmp[2]
_sh_SECPRECHG[0] := 0 #> seg_a <# 255
_sh_SECPRECHG[1] := 0 #> seg_b <# 255
_sh_SECPRECHG[2] := 0 #> seg_c <# 255
tmp.byte[0] := seg_a
tmp.byte[1] := core.PRECHGB
tmp.byte[2] := seg_b
tmp.byte[3] := core.PRECHGC
tmp.byte[4] := seg_c
writereg(core.PRECHGA, 5, @tmp)
PUB reset()
' Reset the display controller
if lookdown(_RES: 0..31)
dira[_RES] := 1
outa[_RES] := 1
outa[_RES] := 0
time.usleep(core.T_RES)
outa[_RES] := 1
time.usleep(core.T_RES_COMPLT)
PUB scroll_fwid_right_cont(sy, ey, xstep, dly) | byte cmd_pkt[5]
' Scroll a full-width vertical region of the display right, continuously
' (sy, ey): vertical region to scroll (sy: 0..95, ey: 0..63)
' xstep: number of columns/pixels to scroll horizontally in each step
' ystep: number of rows/pixels to scroll vertically in each step
' dly: inter-scroll step delay, in frames (6, 10, 100, 200)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop()
cmd_pkt[0] := xstep
cmd_pkt[1] := sy
cmd_pkt[2] := ( (sy #> ey) - sy) + 1
cmd_pkt[3] := 0
cmd_pkt[4] := lookdownz(dly: 6, 10, 100, 200)
writereg(core.SCROLLSETUP, 5, @cmd_pkt)
command(core.SCROLLSTART)
PUB scroll_fwid_right_up_cont(sy, ey, xstep, ystep, dly) | byte cmd_pkt[5]
' Scroll a full-width vertical region of the display up and right, continuously
' (sy, ey): vertical region to scroll (sy: 0..95, ey: 0..63)
' xstep: number of rows/pixels to horizontally scroll vertical region in each step
' ystep: number of rows/pixels to scroll vertically in each step
' dly: inter-scroll step delay, in frames (6, 10, 100, 200)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop()
cmd_pkt[0] := xstep
cmd_pkt[1] := sy
cmd_pkt[2] := ( (sy #> ey) - sy) + 1
cmd_pkt[3] := ystep
cmd_pkt[4] := lookdownz(dly: 6, 10, 100, 200)
writereg(core.SCROLLSETUP, 5, @cmd_pkt)
command(core.SCROLLSTART)
PUB scroll_stop()
' Stop a running scroll command
command(core.SCROLLSTOP)
PUB show()
' Write the current display buffer to the display
#ifndef GFX_DIRECT
{ buffered displays only }
outa[_DC] := DATA
outa[_CS] := 0
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
order := ((RGB #> order <# BGR) << core.SUBPIX_ORDER)
_sh_REMAPCOLOR := ((_sh_REMAPCOLOR & core.SUBPIX_ORDER_MASK) | order)
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB vcomh_voltage(level): curr_lvl
' Set COM output voltage, in millivolts
' Valid values: 440, 520, 610, 710, 830
' Any other value returns the current setting
case level
440, 520, 610, 710, 830:
level := lookdown(level: 440, 520, 610, 710, 830)
_sh_VCOMH := lookup(level: $00, $10, $20, $30, $3E)
writereg(core.VCOMH, 1, @_sh_VCOMH)
other:
curr_lvl := lookdown(_sh_VCOMH: $00, $10, $20, $30, $3E)
return lookup(curr_lvl: 440, 520, 610, 710, 830)
PUB vert_alt_scan(state)
' Alternate Left-Right, Right-Left scanning, every other display line
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value returns the current setting
state := ||(state) << core.COMLR_SWAP
_sh_REMAPCOLOR := ( (_sh_REMAPCOLOR & core.COMLR_SWAP_MASK) | ...
(((state <> 0) & 1) << core.COMLR_SWAP) )
writereg(core.SETREMAP, 1, @_sh_REMAPCOLOR)
PUB visibility(mode): curr_mode
' Set display visibility
case mode
NORMAL, ALL_ON, ALL_OFF, INVERTED:
_sh_DISPMODE := mode + core.NORMALDISPLAY
command(_sh_DISPMODE)
other:
curr_mode := _sh_DISPMODE
return (_sh_DISPMODE - core.NORMALDISPLAY)
PUB wr_buffer(ptr_buff, len)
' Write alternate buffer to display
outa[_DC] := DATA
outa[_CS] := 0
spi.wrblock_lsbf(ptr_buff, len)
outa[_CS] := 1
PRI command(c)
' Issue a command with no parameters to the display
outa[_DC] := CMD
outa[_CS] := 0
spi.wr_byte(c)
outa[_CS] := 1
#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 writereg(reg_nr, nr_bytes, ptr_buff)
' Write nr_bytes from ptr_buff to device
case reg_nr
{ commands with parameters }
$15, $21..$27, $75, $81..$83, $87, $8A..$8C, $A0..$A2, $A8, {
} $AD, $B0, $B1, $B3, $BB, $E3:
outa[_DC] := CMD
outa[_CS] := 0
spi.wr_byte(reg_nr)
spi.wrblock_lsbf(ptr_buff, nr_bytes)
outa[_CS] := 1
return
other:
return
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.
}