-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.go
262 lines (226 loc) · 4.77 KB
/
lcd.go
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
package gogrove
import (
"context"
"sync"
"time"
"periph.io/x/periph/conn/i2c"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/host"
)
const (
displayRGBAddr uint16 = 0x62
displayTextAddr uint16 = 0x3e
textCmd uint8 = 0x80
)
// LCD holds session info for interacting with GrovePi LCD.
type LCD struct {
rgbmu sync.Mutex
txtmu sync.Mutex
b i2c.BusCloser
}
// NewLCD initializes a new session with the LCD.
func NewLCD() (*LCD, error) {
if _, err := host.Init(); err != nil {
return nil, err
}
b, err := i2creg.Open("")
if err != nil {
return nil, err
}
return &LCD{b: b}, nil
}
// Close closes session with LCD.
func (l *LCD) Close() error { return l.b.Close() }
// SetRGB sets the background RGB LCD color.
func (l *LCD) SetRGB(r, g, b uint8) error {
l.rgbmu.Lock()
defer l.rgbmu.Unlock()
err := l.b.Tx(displayRGBAddr, []byte{0, 0}, nil)
if err != nil {
return err
}
err = l.b.Tx(displayRGBAddr, []byte{0x1, 0}, nil)
if err != nil {
return err
}
err = l.b.Tx(displayRGBAddr, []byte{0x08, 0xaa}, nil)
if err != nil {
return err
}
err = l.b.Tx(displayRGBAddr, []byte{0x4, r}, nil)
if err != nil {
return err
}
err = l.b.Tx(displayRGBAddr, []byte{0x3, g}, nil)
if err != nil {
return err
}
err = l.b.Tx(displayRGBAddr, []byte{0x2, b}, nil)
if err != nil {
return err
}
return nil
}
// ClearText clears the display text.
func (l *LCD) ClearText() error {
l.txtmu.Lock()
defer l.txtmu.Unlock()
err := l.b.Tx(displayTextAddr, []byte{textCmd, 0x01}, nil)
if err != nil {
return err
}
return nil
}
// SetText clears display text, and sets text, accepts newlines and
// auto wraps long line.
func (l *LCD) SetText(str string) error {
l.txtmu.Lock()
defer l.txtmu.Unlock()
// clear
err := l.b.Tx(displayTextAddr, []byte{textCmd, 0x01}, nil)
if err != nil {
return err
}
// display on, no cursor
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x08 | 0x04}, nil)
if err != nil {
return err
}
// 2 lines
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x28}, nil)
if err != nil {
return err
}
count := 0
row := 0
for _, c := range str {
if c == '\n' || count == 16 {
count = 0
row++
if row == 2 {
break
}
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0xc0}, nil)
if err != nil {
return err
}
if c == '\n' {
continue
}
}
count++
err = l.b.Tx(displayTextAddr, []byte{0x40, uint8(c)}, nil)
if err != nil {
return err
}
}
return nil
}
// SetText2 clears display text, and sets text, accepts 2 lines
// truncating after 16 chars
func (l *LCD) SetText2(line1, line2 string) error {
l.txtmu.Lock()
defer l.txtmu.Unlock()
// clear
err := l.b.Tx(displayTextAddr, []byte{textCmd, 0x01}, nil)
if err != nil {
return err
}
// display on, no cursor
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x08 | 0x04}, nil)
if err != nil {
return err
}
// 2 lines
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x28}, nil)
if err != nil {
return err
}
err = l.displayLine(line1)
if err != nil {
return err
}
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0xc0}, nil)
if err != nil {
return err
}
err = l.displayLine(line2)
if err != nil {
return err
}
return nil
}
func (l *LCD) displayLine(str string) error {
count := 0
for _, c := range str {
if c == '\n' || count == 16 {
break
}
count++
err := l.b.Tx(displayTextAddr, []byte{0x40, uint8(c)}, nil)
if err != nil {
return err
}
}
return nil
}
// ScrollText clears display text, and sets text, accepts 2 lines
// scrolling both lines if longer than 16. Pass a context to cancel to
// terminate function, otherwise scolls forever.
func (l *LCD) ScrollText(ctx context.Context, line1, line2 string) error {
l.txtmu.Lock()
defer l.txtmu.Unlock()
i := 0
for {
select {
case <-ctx.Done():
return nil
default:
// clear
err := l.b.Tx(displayTextAddr, []byte{textCmd, 0x01}, nil)
if err != nil {
return err
}
// display on, no cursor
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x08 | 0x04}, nil)
if err != nil {
return err
}
// 2 lines
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0x28}, nil)
if err != nil {
return err
}
stbegin := 0
frame := 16
if len(line1) > 16 {
stbegin = i % len(line1)
}
if len(line1)-stbegin < 16 {
frame = len(line1) - stbegin
}
err = l.displayLine(line1[stbegin:][:frame])
if err != nil {
return err
}
err = l.b.Tx(displayTextAddr, []byte{textCmd, 0xc0}, nil)
if err != nil {
return err
}
stbegin = 0
frame = 16
if len(line2) > 16 {
stbegin = i % len(line2)
}
if len(line2)-stbegin < 16 {
frame = len(line2) - stbegin
}
err = l.displayLine(line2[stbegin:][:frame])
if err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
i++
}
}
}