forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.oled.ssd130x.spin
730 lines (647 loc) · 22 KB
/
display.oled.ssd130x.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
{
---------------------------------------------------------------------------------------------------
Filename: display.oled.ssd130x.spin
Description: Driver for Solomon Systech SSD130x OLED displays
Author: Jesse Burt
Started: Apr 26, 2018
Updated: Jan 27, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
#define 1BPP
#define MEMMV_NATIVE bytemove
#include "graphics.common.spinh"
{ if a specific display controller isn't defined, default to SSD1306 }
#ifndef SSD1306
# ifndef SSD1309
# define SSD1306
# endif
#endif
CON
{ /// default I/O settings; these can be overridden in the parent object }
{ display dimensions }
WIDTH = 128
HEIGHT = 64
XMAX = WIDTH-1
YMAX = HEIGHT-1
CENTERX = WIDTH/2
CENTERY = HEIGHT/2
{ I2C }
SCL = DEF_SCL
SDA = DEF_SDA
RST = 0
I2C_FREQ = DEF_HZ
I2C_ADDR = DEF_ADDR
{ SPI }
CS = 0
SCK = 1
MOSI = 2
DC = 3
RST = 0
{ /// }
BPP = 1 ' bits per pixel/color depth of the display
BYTESPERPX = 1 #> (BPP/8) ' limit to minimum of 1
BPPDIV = BYTESPERPX #> (8 / BPP) ' limit to range BYTESPERPX .. (8/BPP)
BUFF_SZ = (WIDTH * HEIGHT) / BPPDIV
MAX_COLOR = (1 << BPP)-1
SLAVE_WR = core#SLAVE_ADDR
SLAVE_RD = core#SLAVE_ADDR|1
DEF_SCL = 28
DEF_SDA = 29
DEF_HZ = 100_000
DEF_ADDR = 0
' States for D/C pin
DATA = 1
CMD = 0
' Display visibility modes
NORMAL = 0
ALL_ON = 1
INVERTED = 2
' Addressing modes
HORIZ = 0
VERT = 1
PAGE = 2
OBJ
core: "core.con.ssd130x"
time: "time"
#ifdef SSD130X_SPI
spi: "com.spi.4mhz" ' SPI engine
#else
{ default to I2C }
#define SSD130X_I2C
i2c: "com.i2c" ' I2C engine
#endif
VAR
long _CS, _DC, _RES
byte _addr_bits
byte _framebuffer[BUFF_SZ]
PUB null{}
' This is not a top-level object
#ifdef SSD130X_I2C
PUB start{}: status
' Start using default I/O settings
return startx(SCL, SDA, RST, I2C_FREQ, I2C_ADDR, WIDTH, HEIGHT, @_framebuffer)
PUB startx(SCL_PIN, SDA_PIN, RES_PIN, I2C_HZ, ADDR_BITS, DISP_WID, DISP_HT, ptr_dispbuff): status
' Start the driver with custom I/O settings
' SCL_PIN: 0..31
' SDA_PIN: 0..31
' RES_PIN: 0..31 (optional; use -1 to disable)
' I2C_HZ: max official is 400_000 (unenforced, YMMV!)
' SLAVE_LSB: 0, 1
' DISP_WID: 96, 128
' DISP_HT: 32, 64
if ( lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
time.usleep(core#TPOR) ' wait for device startup
_addr_bits := ||(ADDR_BITS == 1) << 1 ' slave address bit option
_RES := RES_PIN ' -1 to disable
reset{}
if ( i2c.present(SLAVE_WR | _addr_bits) ) ' test device bus presence
_disp_width := DISP_WID
_disp_height := DISP_HT
_disp_xmax := _disp_width-1
_disp_ymax := _disp_height-1
' calc display memory usage from dimensions and 1bpp depth
_buff_sz := (_disp_width * _disp_height) / 8
_bytesperln := _disp_width * BYTESPERPX
set_address(ptr_dispbuff) ' set display buffer address
#ifdef GFX_DIRECT
set_putchar(@putchar_90deg_1bpp)
#endif
return
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#elseifdef SSD130X_SPI
PUB start{}: status
' Start the driver using default I/O settings
return startx(CS, SCK, MOSI, DC, RST, WIDTH, HEIGHT, @_framebuffer)
PUB startx(CS_PIN, SCK_PIN, SDIN_PIN, DC_PIN, RES_PIN, DISP_WID, DISP_HT, ptr_dispbuff): status
' Start the driver with custom I/O settings
' CS_PIN: 0..31
' SCK_PIN: 0..31
' SDIN_PIN: 0..31
' DC_PIN: 0..31
' RES_PIN: 0..31 (optional; use -1 to disable)
' WIDTH: 96, 128
' HEIGHT: 32, 64
if ( lookdown(CS_PIN: 0..31) and lookdown(SCK_PIN: 0..31) and lookdown(SDIN_PIN: 0..31) and ...
lookdown(DC_PIN: 0..31) )
if ( status := spi.init(SCK_PIN, SDIN_PIN, -1, core#SPI_MODE) )
time.usleep(core#TPOR) ' wait for device startup
_CS := CS_PIN
_DC := DC_PIN
_RES := RES_PIN ' -1 to disable
reset{}
outa[_CS] := 1
dira[_CS] := 1
outa[_DC] := 0
dira[_DC] := 1
_disp_width := DISP_WID
_disp_height := DISP_HT
_disp_xmax := _disp_width-1
_disp_ymax := _disp_height-1
' calc display memory usage from dimensions and 1bpp depth
_buff_sz := (_disp_width * _disp_height) / 8
_bytesperln := _disp_width * BYTESPERPX
set_address(ptr_dispbuff) ' set display buffer address
#ifdef GFX_DIRECT
set_putchar(@putchar_90deg_1bpp)
#endif
return
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#endif
PUB stop{}
' Stop the driver
powered(FALSE)
#ifdef SSD130X_I2C
i2c.deinit{}
#elseifdef SSD130X_SPI
spi.deinit{}
#endif
PUB defaults{}
' Apply power-on-reset default settings
#ifndef HAS_RESET
' this code will be called only if HAS_RESET isn't defined at build-time
' define it if the display's reset pin is to be controlled by GPIO
' or if it's tied to the MCU's reset pin or similar
powered(FALSE)
disp_lines(64)
disp_start_line(0)
chg_pump_voltage(7_500)
addr_mode(PAGE)
contrast(127)
visibility(NORMAL)
draw_area(0, 0, 127, 63)
powered(TRUE)
#else
reset{}
#endif
PUB preset_128x{}
' Preset: 128px wide, determine settings for height at runtime
disp_lines(_disp_height)
disp_start_line(0)
chg_pump_voltage(7_500)
addr_mode(HORIZ)
visibility(NORMAL)
case _disp_height
32:
com_pin_cfg(0, 0)
64:
com_pin_cfg(1, 0)
other:
com_pin_cfg(0, 0)
powered(TRUE)
PUB preset_128x32{}
' Preset: 128px wide, setup for 32px height
disp_lines(32)
disp_start_line(0)
chg_pump_voltage(7_500)
addr_mode(HORIZ)
visibility(NORMAL)
com_pin_cfg(0, 0)
powered(TRUE)
PUB preset_128x64{}
' Preset: 128px wide, setup for 64px height
disp_lines(64)
disp_start_line(0)
chg_pump_voltage(7_500)
addr_mode(HORIZ)
visibility(NORMAL)
com_pin_cfg(1, 0)
powered(TRUE)
PUB addr_mode(mode)
' Set Memory Addressing Mode
' Valid values:
' 0: Horizontal addressing mode
' 1: Vertical
' *2: Page
' Any other value is ignored
case mode
HORIZ, VERT, PAGE:
writereg(core#MEM_ADDRMODE, 1, @mode)
other:
return
#ifdef GFX_DIRECT
PUB bitmap(ptr_bmap, sx, sy, ex, ey) | bm_sz
' Display bitmap
' ptr_bmap: pointer to bitmap data
' (sx, sy): upper-left corner of bitmap
' (ex, ey): lower-right corner of bitmap
draw_area(sx, sy, ex, ey)
bm_sz := ((ex-sx) * (ey-sy)) / 8
wr_buffer(ptr_bmap, bm_sz)
#endif
#ifdef GFX_DIRECT
PUB box(sx, sy, ex, ey, c, f)
#endif
#ifdef GFX_DIRECT
PUB tx = putchar ' these two are aliases to the function pointer
PUB char = putchar ' `putchar`, which points to a low-level routine
PUB putchar_90deg_1bpp(ch) | ch_offs
' Low-level character rendering routine
' For font file definitions with these characteristics:
' * 90 degrees rotation (landscape)
' * each glyph word is a column of the glyph, e.g. for 5x8 'A':
' %01111100
' %00010010
' %00010001
' %00010010
' %01111100
if ( (ch < _fnt_cmin) or (ch > _fnt_cmax) ) ' don't waste any time if the char is invalid
return
ch_offs := _font_addr + (ch * _fnt_width)
draw_area(_charpx_x, _charpx_y, _charpx_x+_fnt_width, _charpx_y+_charcell_h)
wr_buffer(ch_offs, _charcell_w-1) ' write the glyph directly to the display
_charpx_x += _charcell_w ' go to next column
if (_charpx_x > _charpx_xmax) ' last col?
_charpx_x := 0 ' go to first col of
_charpx_y += _charcell_h ' next line
if (_charpx_y > _charpx_ymax) ' last col of last row?
_charpx_x := _charpx_y := 0 ' wrap to beginning of disp
#endif
PUB chg_pump_voltage(v)
' Set charge pump regulator voltage, in millivolts
' Valid values:
' 0 (off), 6_000, *7_500, 8_500, 9_000
' Any other value is ignored
' NOTE: This must be called before display power is enabled with Powered()
#ifdef SSD1306
' the SSD1309 doesn't have an internal charge pump,
' so only build this code for the SSD1306
case v
0_000:
v := core#CHGP_OFF
6_000:
v := core#CHGP_6000
7_500:
v := core#CHGP_7500
8_500:
v := core#CHGP_8500
9_000:
v := core#CHGP_9000
other:
return
writereg(core#CHGPUMP, 1, @v)
#endif
PUB clear{}
' Clear the display
#ifdef GFX_DIRECT
#ifdef SSD130X_I2C
i2c.start
i2c.write(SLAVE_WR | _addr_bits)
i2c.wr_byte(core#CTRLBYTE_DATA)
repeat _buff_sz
i2c.wr_byte(_bgcolor)
i2c.stop
#elseifdef SSD130X_SPI
outa[_DC] := DATA
outa[_CS] := 0
repeat _buff_sz
spi.wr_byte(_bgcolor)
outa[_CS] := 1
#endif
#else
bytefill(_ptr_drawbuffer, _bgcolor, _buff_sz)
#endif
PUB clk_freq(freq)
' Set display internal oscillator frequency, in kHz
' Valid values: (disply-specific)
' SSD1306:
' 333, 337, 342, 347, 352, 357, 362, 367, 372, 377, 382, 387, 392,
' 397, 402, 407
' SSD1309:
' 360, 372, 384, 396, 408, 420, 432, 444, 456, 468, 480, 492, 504,
' 516, 528, 540
' Any other value is ignored
' NOTE: Range is interpolated, based solely on the range specified in the
' datasheet, divided into 16 steps
case freq
core#FOSC_MIN..core#FOSC_MAX:
#ifdef SSD1306
freq := ((freq / 5) - 66) << core#OSCFREQ
#elseifdef SSD1309
freq := ((freq / 12) - 30) << core#OSCFREQ
#endif
writereg(core#SETOSCFREQ, 1, @freq)
other:
return
PUB com_pin_cfg(pin_config, remap) | config
' Set COM Pins Hardware Configuration and Left/Right Remap
' Valid values:
' pin_config: 0: Sequential 1: Alternative (POR)
' remap: 0: Disable Left/Right remap (POR) 1: Enable remap
' Any other value sets the default value
config := %0000_0010 ' XXX use named constant/clarify
case pin_config
0:
other:
config := config | (1 << 4)
case remap
1:
config := config | (1 << 5)
other:
writereg(core#SETCOM_CFG, 1, @config)
PUB contrast(level)
' Set Contrast Level
' Valid values: 0..255 (clamped to range)
level := (0 #> level <# 255)
writereg(core#CONTRAST, 1, @level)
PUB draw_area(sx, sy, ex, ey) | tmp
' Set displayable area
ifnot (lookup(sx: 0..127) or lookup(sy: 0..63) or lookup(ex: 0..127) or lookup(ey: 0..63))
return
sy >>= 3 ' convert y-coordinates to
ey >>= 3 ' page numbers
tmp := (ex << 8) | sx
writereg(core#SET_COLADDR, 2, @tmp)
tmp := (ey << 8) | sy
writereg(core#SET_PAGEADDR, 2, @tmp)
PUB invert_colors(state) | tmp
' Invert display colors
if (state)
visibility(INVERTED)
else
visibility(NORMAL)
PUB disp_lines(lines)
' Set total number of display lines
' Valid values: 16..64
' Typical values: 32, 64
' Any other value is ignored
lines := ((16 #> lines <# 64) - 1)
writereg(core#SETMUXRATIO, 1, @lines)
PUB disp_offset(offset)
' Set display offset/vertical shift
' Valid values: 0..63 (default: 0)
' Any other value sets the default value
offset := (0 #> offset <# 63)
writereg(core#SETDISPOFFS, 1, @offset)
PUB disp_start_line(line)
' Set Display Start Line
' Valid values: 0..63 (default: 0)
' Any other value sets the default value
command(core#DISP_STLINE + (0 #> line <# 63))
PUB visibility(mode)
' Set display visibility
case mode
NORMAL:
command(core#RAMDISP_ON)
command(core#DISP_NORM)
ALL_ON:
command(core#RAMDISP_ON | 1)
INVERTED:
command(core#DISP_NORM | 1)
other:
return
PUB mirror_h(state)
' Mirror display, horizontally
' Valid values: TRUE (non-zero), *FALSE (0)
' Any other value is ignored
' NOTE: Takes effect only after next display update
command(core#SEG_MAP0 | ((state <> 0) & 1))
PUB mirror_v(state)
' Mirror display, vertically
' Valid values: TRUE (non-zero), *FALSE (0)
' Any other value is ignored
' NOTE: Takes effect only after next display update
if (state)
state := 8
command(core#COMDIR_NORM | state)
PUB plot(x, y, color)
' 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
' (not implemented)
#else
' buffered display
case color
1:
byte[_ptr_drawbuffer][x + (y>>3) * _disp_width] |= (|< (y&7))
0:
byte[_ptr_drawbuffer][x + (y>>3) * _disp_width] &= !(|< (y&7))
-1:
byte[_ptr_drawbuffer][x + (y>>3) * _disp_width] ^= (|< (y&7))
other:
return
#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 (byte[_ptr_drawbuffer][(x + (y >> 3) * _disp_width)] & (1 << (y & 7)) <> 0) * -1
#endif
PUB powered(state) | tmp
' Enable display power
state := (((state <> 0) & 1) + core#DISP_OFF)
command(state)
PUB precharge_period(phs1_clks, phs2_clks) | tmp
' Set display refresh pre-charge period, in display clocks
' Valid values: 1..15 (clamped to range)
phs1_clks := (1 #> phs1_clks <# 15)
phs2_clks := (1 #> phs2_clks <# 15)
tmp := (phs2_clks << 4) | phs1_clks
writereg(core#SETPRECHARGE, 1, @tmp)
PUB reset{}
' Reset the display controller
if (lookdown(_RES: 0..31))
outa[_RES] := 1
dira[_RES] := 1
time.usleep(3)
outa[_RES] := 0
time.usleep(3)
outa[_RES] := 1
PUB scroll_left_cont(sx, sy, ex, ey, dly) | cmd_pkt[2]
' Scroll a region of the display left, continuously
' (sx, sy): upper-left coordinates (sx: 0..127, sy: 0..63)
' (ex, ey): lower-right coordinates (ex: sx..127, ey: sy..63)
' dly: inter-scroll step delay, in frames (2, 3, 4, 5, 6, 32, 64, 128)
' NOTE: Y-coordinates are scaled to multiples of 8 (hardware limitation)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop{}
cmd_pkt.byte[0] := 0 ' dummy byte
cmd_pkt.byte[1] := ((0 #> sy <# 63) >> 3) ' div coord by 8
cmd_pkt.byte[2] := lookdownz(dly: 6, 32, 64, 128, 3, 4, 5, 2)
cmd_pkt.byte[3] := ((sy #> ey <# 63) >> 3)
cmd_pkt.byte[4] := (0 #> sx <# 127)
cmd_pkt.byte[5] := (sx #> ex <# 127) ' ex _must_ be >= sx
writereg(core#HSCROLL_L, 6, @cmd_pkt)
command(core#STARTSCROLL)
PUB scroll_left_up_cont(sy, ey, vlines, dly) | cmd_pkt[2]
' Scroll a region of the display left and up, continously
' (sy, ey): top and bottom of scroll region (0..63)
' vlines: vertical lines to scroll in each step (1..63)
' dly: inter-scroll step delay, in frames
' NOTE: Y-coordinates are scaled to multiples of 8 (hardware limitation)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop{}
cmd_pkt.byte[0] := 0
cmd_pkt.byte[1] := (0 #> sy <# 63) / 8
cmd_pkt.byte[2] := lookdownz(dly: 6, 32, 64, 128, 3, 4, 5, 2)
cmd_pkt.byte[3] := (sy #> ey <# 63) / 8
cmd_pkt.byte[4] := (1 #> vlines <# 63)
writereg(core#SCROLL_VHL, 5, @cmd_pkt)
command(core#STARTSCROLL)
PUB scroll_right_cont(sx, sy, ex, ey, dly) | cmd_pkt[2]
' Scroll a region of the display right, continously
' (sx, sy): upper-left coordinates (sx: 0..127, sy: 0..63)
' (ex, ey): lower-right coordinates (ex: sx..127, ey: sy..63)
' dly: inter-scroll step delay, in frames (2, 3, 4, 5, 6, 32, 64, 128)
' NOTE: Y-coordinates are scaled to multiples of 8 (hardware limitation)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop{}
cmd_pkt.byte[0] := 0
cmd_pkt.byte[1] := (0 #> sy <# 63) / 8
cmd_pkt.byte[2] := lookdownz(dly: 6, 32, 64, 128, 3, 4, 5, 2)
cmd_pkt.byte[3] := (sy #> ey <# 63) / 8
cmd_pkt.byte[4] := (0 #> sx <# 127)
cmd_pkt.byte[5] := (sx #> ex <# 127)
writereg(core#HSCROLL_R, 6, @cmd_pkt)
command(core#STARTSCROLL)
PUB scroll_right_up_cont(sy, ey, vlines, dly) | cmd_pkt[2]
' Scroll a region of the display right and up, continously
' (sy, ey): top and bottom of scroll region (0..63)
' vlines: vertical lines to scroll in each step (1..63)
' dly: inter-scroll step delay, in frames
' NOTE: Y-coordinates are scaled to multiples of 8 (hardware limitation)
' NOTE: ey must be greater than or equal to sy
' NOTE: scrolling is continuous, until stopped by calling scroll_stop()
scroll_stop{}
cmd_pkt.byte[0] := 0
cmd_pkt.byte[1] := (0 #> sy <# 63) / 8
cmd_pkt.byte[2] := lookdownz(dly: 6, 32, 64, 128, 3, 4, 5, 2)
cmd_pkt.byte[3] := (sy #> ey <# 63) / 8
cmd_pkt.byte[4] := (1 #> vlines <# 63)
writereg(core#SCROLL_VHR, 5, @cmd_pkt)
command(core#STARTSCROLL)
PUB scroll_stop{}
' Stop a running scroll command
command(core#STOPSCROLL)
PUB show{} | tmp
' Write display buffer to display
draw_area(0, 0, _disp_xmax, _disp_ymax)
#ifdef SSD130X_I2C
i2c.start{}
i2c.wr_byte(SLAVE_WR | _addr_bits)
i2c.wr_byte(core#CTRLBYTE_DATA)
i2c.wrblock_lsbf(_ptr_drawbuffer, _buff_sz)
i2c.stop{}
#elseifdef SSD130X_SPI
outa[_DC] := DATA
outa[_CS] := 0
spi.wrblock_lsbf(_ptr_drawbuffer, _buff_sz)
outa[_CS] := 1
#endif
PUB vcomh_voltage(level)
' Set COM output voltage, in millivolts
' Valid values:
' SSD1306:
' 0_650: 0.65 * Vcc
' *0_770: 0.77 * Vcc
' 0_830: 0.83 * Vcc
' SSD1309:
' 0_640: 0.64 * Vcc
' *0_780: 0.78 * Vcc
' 0_840: 0.84 * Vcc
' Any other value is ignored
case level
#ifdef SSD1306
0_650:
level := %000 << core#VCOMH
0_770:
level := %010 << core#VCOMH
0_830:
level := %011 << core#VCOMH
#elseifdef SSD1309
0_640:
level := %0000 << core#VCOMH
0_780:
level := %1101 << core#VCOMH
0_840:
level := %1111 << core#VCOMH
#endif
other:
return
writereg(core#SETVCOMDESEL, 1, @level)
PUB wr_buffer(ptr_buff, len)
' Write alternate buffer to display
' ptr_buff: address of buffer to write to display
' len: bytes to write
' NOTE: Does not set position on display
#ifdef SSD130X_I2C
i2c.start{}
i2c.wr_byte(SLAVE_WR | _addr_bits)
i2c.wr_byte(core#CTRLBYTE_DATA)
i2c.wrblock_lsbf(ptr_buff, len)
i2c.stop{}
#elseifdef SSD130X_SPI
outa[_DC] := DATA
outa[_CS] := 0
spi.wrblock_lsbf(ptr_buff, len)
outa[_CS] := 1
#endif
PRI command(c) | cmd_pkt
' Issue a command with no parameters to the display
#ifdef SSD130X_I2C
cmd_pkt.byte[0] := SLAVE_WR | _addr_bits
cmd_pkt.byte[1] := core#CTRLBYTE_CMD
cmd_pkt.byte[2] := c
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.stop{}
#elseifdef SSD130X_SPI
outa[_DC] := CMD
outa[_CS] := 0
spi.wr_byte(c)
outa[_CS] := 1
#endif
#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
bytefill(_ptr_drawbuffer + (xs + (ys * _bytesperln)), val, count)
#endif
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt[2]
' Write nr_bytes from val to device
#ifdef SSD130X_I2C
cmd_pkt.byte[0] := SLAVE_WR | _addr_bits
cmd_pkt.byte[1] := core#CTRLBYTE_CMD
cmd_pkt.byte[2] := reg_nr
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wrblock_lsbf(ptr_buff, nr_bytes)
i2c.stop{}
#elseifdef SSD130X_SPI
outa[_DC] := CMD
outa[_CS] := 0
spi.wr_byte(reg_nr)
spi.wrblock_lsbf(ptr_buff, nr_bytes)
outa[_CS] := 1
#endif
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.
}