-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.led-seg.ht16k33.spin2
319 lines (269 loc) · 10.8 KB
/
display.led-seg.ht16k33.spin2
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
{
---------------------------------------------------------------------------------------------------
Filename: display.led-seg.ht16k33.spin2
Description: Driver for HT16K33-based LED displays (segment type)
Author: Jesse Burt
Started: Jun 22, 2021
Updated: Aug 19, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
#include "ht16k33.common.spin2h"
#include "terminal.common.spin2h"
CON
{ these can be overridden in the parent object }
{ default I/O settings; these can be overridden in the parent object }
SCL = 0
SDA = 1
I2C_FREQ= 100_000
I2C_ADDR= 0
WIDTH = 2
HEIGHT = 1
VAR
long _col, _row, _disp_width, _disp_height, _disp_xmax, _disp_ymax
word _disp_buff[7] ' 112 bits/segments
byte _lastchar
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ, I2C_ADDR, WIDTH, HEIGHT)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS, DISP_W, DISP_H): status
' SCL_PIN, SDA_PIN, I2C_HZ: I2C bus I/O pins and speed
' ADDR_BITS: specify LSBs of slave address (%000..%111)
' DISP_W, DISP_H: dimensions of display, in digits/characters
if ( lookdown(SCL_PIN: 0..63) and lookdown(SDA_PIN: 0..63) and (lookdown(ADDR_BITS: %000..%111)) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
waitus(core.T_POR) ' wait for device startup
_addr_bits := ADDR_BITS << 1
_disp_width := DISP_W
_disp_height := DISP_H
_disp_xmax := WIDTH-1
_disp_ymax := HEIGHT-1
if ( i2c.present(SLAVE_WR | _addr_bits) ) ' test device presence
clear()
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 char = putchar
PUB putchar(c) | cmd_pkt, i
' Write character to display
' NOTE: Interprets control characters
case c
BS, DEL: ' backspace/delete
prev_digit() ' move back to previous digit
update_buff(" ") ' and display a SPACE over it
LF:
move_down()
FF: ' clear/form feed
clear()
CR:
pos_x(0)
" ".."-", "/".."~": ' printable characters
_lastchar := c
update_buff(c)
next_digit()
".": ' period/decimal point
if (lookdown(_lastchar: "0".."9")) ' if previous char was a num,
_lastchar := "." ' draw the decimal point in
prev_digit() ' the same digit as the num
update_buff(c)
next_digit()
else
_lastchar := "."
update_buff(c)
next_digit()
other:
return
cmd_pkt.byte[0] := SLAVE_WR | _addr_bits
cmd_pkt.byte[1] := core.DISP_RAM
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
repeat i from 0 to 7
i2c.wrword_lsbf(_disp_buff[i])
i2c.stop()
PUB clear()
' Clear display
repeat 7
putchar(" ")
pos_xy(0, 0)
PUB move_down()
' Move cursor down one row
' NOTE: Wraps around to the first row
_row++
if ( _row > _disp_ymax )
_row := 0
PUB move_left()
' Move cursor left one column
' NOTE: Wraps around to the last column
_col--
if ( _col < 0 )
_col := _disp_xmax
PUB move_right()
' Move cursor right one column
' NOTE: Wraps around to the first column
_col++
if ( _col > _disp_xmax )
_col := 0
PUB move_up()
' Move cursor up one row
' NOTE: Wraps around to the last row
_row--
if ( _row < 0 )
_row := _disp_ymax
PUB position = pos_xy
PUB pos_xy(x, y)
' Set cursor position
if ( (x >= 0) and (x <= _disp_xmax) and (y >= 0) and (y <= _disp_ymax) )
_col := x
_row := y
PUB positionx = pos_x
PUB pos_x(x)
' Set cursor X position
if ( (x >= 0) and (x <= _disp_xmax) )
_col := x
PUB positiony = pos_y
PUB pos_y(y)
' Set cursor Y position
if ( (y >= 0) and (y <= _disp_ymax) )
_row := y
PRI next_digit()
' Advance to next display digit
' NOTE:
' * Wraps around to the first column
' * Wraps around to first row
_col++
if ( _col > _disp_xmax )
_col := 0
move_down()
PRI prev_digit()
' Move back to previous display digit
' NOTE:
' * Wraps around to the last column
' * Wraps around to last row
_col--
if ( _col < 0 )
_col := _disp_xmax
move_up()
PRI update_buff(c)
' Update display buffer with character 'c'
if ( c == "." )
' if drawing a period/decimal point, OR it in with the current digit's
' data, so it doesn't clear the digit and just draw the period
_disp_buff[(_row * _disp_width) + _col] |= _fnt_tbl[c-32]
else
' otherwise, just write the digit data
_disp_buff[(_row * _disp_width) + _col] := _fnt_tbl[c-32]
DAT
_fnt_tbl word %0000_0000_0000_0000 ' (SP) - 32/$20
word %0001_0010_0000_0000
word %0000_0000_0010_0010 ' "
word %0001_0010_1100_1110 ' #
word %0001_0010_1110_1101 ' $
word %0010_1101_1110_0100 ' %
word %0010_0100_1101_1010 ' &
word %0000_0100_0000_0000 ' '
word %0000_0000_0011_1001 ' (
word %0000_0000_0000_1111 ' )
word %0011_1111_0000_0000 ' *
word %0001_0010_1100_0000 ' +
word %0000_1000_0000_0000 ' ,
word %0000_0000_1100_0000 ' -
word %0100_0000_0000_0000 ' .
word %0000_1100_0000_0000 ' /
word %0000_1100_0011_1111 ' 0 - 48/$30
word %0000_0000_0000_0110 ' 1
word %0000_0000_1101_1011 ' 2
word %0000_0000_1100_1111 ' 3
word %0000_0000_1110_0110 ' 4
word %0000_0000_1110_1101 ' 5
word %0000_0000_1111_1101 ' 6
word %0000_0000_0000_0111 ' 7
word %0000_0000_1111_1111 ' 8
word %0000_0000_1110_1111 ' 9
word %0001_0010_0000_0000 ' :
word %0000_1010_0000_0000 ' ;
word %0010_0100_0000_0000 ' <
word %0000_0000_1100_1000 ' =
word %0000_1001_0000_0000 ' >
word %0001_0000_1000_0011 ' ?
word %0010_0000_1011_0111 ' @
word %0000_0000_1111_0111 ' A - 65/$41
word %0001_0010_1000_1111 ' B
word %0000_0000_0011_1001 ' C
word %0001_0010_0000_1111 ' D
word %0000_0000_1111_1001 ' E
word %0000_0000_1111_0001 ' F
word %0000_0000_1011_1101 ' G
word %0000_0000_1111_0110 ' H
word %0001_0010_0000_1001 ' I
word %0000_0000_0001_1110 ' J
word %0010_0100_0111_0000 ' K
word %0000_0000_0011_1000 ' L
word %0000_0101_0011_0110 ' M
word %0010_0001_0011_0110 ' N
word %0000_0000_0011_1111 ' O
word %0000_0000_1111_0011 ' P
word %0010_0000_0011_1111 ' Q
word %0010_0000_1111_0011 ' R
word %0010_0001_0000_1001 ' S
word %0001_0010_0000_0001 ' T
word %0000_0000_0011_1110 ' U
word %0000_1100_0011_0000 ' V
word %0010_1000_0011_0110 ' W
word %0010_1101_0000_0000 ' X
word %0001_0000_1110_0010 ' Y
word %0000_1100_0000_1001 ' Z
word %0000_0000_0011_1001 ' (
word %0010_0001_0000_0000 ' \
word %0000_0000_0000_1111 ' )
word %0000_0100_0000_0010 ' ^
word %0000_0000_0000_1000 ' _
word %0000_0001_0000_0000 ' `
word %0000_0000_1101_1111 ' a
word %0000_0000_1111_1100 ' b
word %0000_0000_1101_1000 ' c
word %0000_0000_1101_1110 ' d
word %0000_0000_0111_1001 ' e
word %0000_0000_0111_0001 ' f
word %0000_0001_1000_1111 ' g
word %0000_0000_1111_0100 ' h
word %0001_0000_0000_0000 ' i
word %0000_0000_0000_1110 ' j
word %0011_0110_0000_0000 ' k
word %0001_0010_0000_0000 ' l
word %0001_0000_1101_0100 ' m
word %0000_0000_1101_0100 ' n
word %0000_0000_1101_1100 ' o
word %0000_0100_0111_0001 ' p
word %0010_0000_1110_0011 ' q
word %0000_0000_0101_0000 ' r
word %0000_0001_1000_1101 ' s
word %0000_0000_0111_1000 ' t
word %0000_0000_0001_1100 ' u
word %0000_1000_0001_0000 ' v
word %0010_1000_0001_0100 ' w
word %0010_1101_0000_0000 ' x
word %0001_0000_1110_0010 ' y
word %0000_1100_0000_1001 ' z
word %0000_1001_0100_1001 ' {
word %0001_0010_0000_0000 ' |
word %0010_0100_1000_1001 ' }
word %0000_0000_1100_0000 ' ~
word %0000_0000_0000_0000 ' (DEL)
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.
}