-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathht16k33.common.spin2h
158 lines (123 loc) · 4.22 KB
/
ht16k33.common.spin2h
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
{
---------------------------------------------------------------------------------------------------
Filename: ht16k33.common.spin2h
Description: Low-level code common to HT16K33-based LED displays
Author: Jesse Burt
Started: Sep 3, 2022
Updated: Aug 19, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
CON
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR|1
DEF_SCL = 0
DEF_SDA = 1
DEF_HZ = 100_000
DEF_ADDR = 0
{ display visibility }
OFF = 0
ON = 1
VAR
byte _dispsetup
byte _addr_bits
OBJ
i2c: "com.i2c" ' I2C engine
core: "core.con.ht16k33" ' HW-specific constants
PUB null()
' This is not a top-level object
PUB stop()
' Stop the driver
visibility(FALSE)
osc_ena(FALSE)
waitms(100)
i2c.deinit()
PUB defaults()
' Factory default settings
osc_ena(TRUE)
brightness(15)
visibility(TRUE)
PUB blink_rate(rate)
' Set blink rate of display, in Hz
' Valid values: 0 (disable blinking), 0_5 (0.5), 1, 2
' Any other value is ignored
case rate
0:
rate := 0
0_5:
rate := %11 << core.BLINK
1:
rate := %10 << core.BLINK
2:
rate := %01 << core.BLINK
other:
return
_dispsetup := ((_dispsetup & core.BLINK_MASK) | rate)
writereg(core.DISPSETUP, _dispsetup)
PUB brightness(level)
' Set display brightness
' Valid values: 0..15
' Any other value is ignored
case level
0..15:
writereg(core.BRIGHTNESS, level)
other:
return
PUB visibility(state)
' Enable display visibility
' Valid values: TRUE/ON (-1 or 1), FALSE/OFF (0)
' Any other value is ignored
' NOTE: This doesn't affect display RAM contents; only whether it's currently displayed or not
case abs(state)
OFF, ON:
state := abs(state)
_dispsetup := ((_dispsetup & core.ONOFF_MASK) | state)
writereg(core.DISPSETUP, _dispsetup)
other:
return
PUB osc_ena(state)
' Enable the oscillator
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value is ignored
case abs(state)
0, 1: state := abs(state)
writereg(core.OSCILLATOR, state)
other:
return
PRI writereg(reg_nr, ptr_buff) | cmd_pkt[2], i
' Write reg_nr to device from ptr_buff
cmd_pkt.byte[0] := SLAVE_WR | _addr_bits
case reg_nr
core.DISP_RAM:
{ Display RAM }
cmd_pkt.byte[1] := core.DISP_RAM
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
repeat i from 0 to 7
i2c.write((byte[ptr_buff][i]) & $FF)
i2c.write($00)
i2c.stop()
core.OSCILLATOR, core.DISPSETUP, core.ROWINT, core.BRIGHTNESS:
{ Control registers }
cmd_pkt.byte[1] := reg_nr | ptr_buff
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.stop()
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.
}