-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcd.go
341 lines (296 loc) · 6.07 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
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
package main
import (
"github.com/stianeikeland/go-rpio"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"log"
"strings"
"sync"
"time"
)
//Display (output) interface
type Display interface {
Display(string)
Close()
}
const (
//Timing constants
ePulse = 1 * time.Microsecond
eDelay = 70 * time.Microsecond
//BCM http://pinout.xyz/
lcdRS = 6
lcdE = 5
lcdD4 = 25
lcdD5 = 24
lcdD6 = 23
lcdD7 = 17
//Define some device constants
lcdWidth = 16 // Maximum characters per line
lcdChr = true
lcdCmd = false
lcdLine1 = 0x80 // LCD RAM address for the 1st line
lcdLine2 = 0xC0 // LCD RAM address for the 2nd line
)
func removeNlChars(str string) string {
isOk := func(r rune) bool {
return r < 32 || r >= 127
}
t := transform.Chain(norm.NFKD, transform.RemoveFunc(isOk))
str, _, _ = transform.String(t, str)
return str
}
//Lcd output
type Lcd struct {
sync.Mutex
lcdRS rpio.Pin
lcdE rpio.Pin
lcdD4 rpio.Pin
lcdD5 rpio.Pin
lcdD6 rpio.Pin
lcdD7 rpio.Pin
line1 string
line2 string
active bool
msg chan string
end chan bool
}
//NewLcd create and init new lcd output
func NewLcd() (l *Lcd) {
if err := rpio.Open(); err != nil {
panic(err)
}
l = &Lcd{
lcdRS: initPin(lcdRS),
lcdE: initPin(lcdE),
lcdD4: initPin(lcdD4),
lcdD5: initPin(lcdD5),
lcdD6: initPin(lcdD6),
lcdD7: initPin(lcdD7),
active: true,
msg: make(chan string),
end: make(chan bool),
}
l.reset()
go func() {
for {
select {
case msg := <-l.msg:
l.display(msg)
case _ = <-l.end:
l.close()
return
}
}
}()
return l
}//NewLcd()
//Display show some message
func (l *Lcd) Display(msg string) {
l.msg <- msg
}//Display(str)
//Close LCD
func (l *Lcd) Close() {
log.Printf("Lcd.Close")
if l.active {
l.end <- true
}
}//Close()
func initPin(pin int) (p rpio.Pin) {
p = rpio.Pin(pin)
rpio.PinMode(p, rpio.Output)
return
}//initPin(pin int) (p rpio.Pin)
func (l *Lcd) reset() {
log.Printf("Lcd.reset()")
//l.writeByte(0x33, lcdCmd) // 110011 Initialise
l.write4Bits(0x3, lcdCmd) // 110011 Initialise
time.Sleep(5 * time.Millisecond)
//l.writeByte(0x32, lcdCmd) // 110010 Initialise
l.write4Bits(0x3, lcdCmd) // 110010 Initialise
time.Sleep(120 * time.Microsecond)
//l.writeByte(0x30, lcdCmd) // 110000 Initialise
l.write4Bits(0x3, lcdCmd) // 110010 Initialise
time.Sleep(120 * time.Microsecond)
l.write4Bits(0x2, lcdCmd) // 110010 Initialise
time.Sleep(120 * time.Microsecond)
l.writeByte(0x28, lcdCmd) // 101000 Data length, number of lines, font size
l.writeByte(0x0C, lcdCmd) // 001100 Display On,Cursor Off, Blink Off
l.writeByte(0x06, lcdCmd) // 000110 Cursor move direction
l.writeByte(0x01, lcdCmd) // 000001 Clear display
time.Sleep(5 * time.Millisecond)
log.Printf("Lcd.reset() finished")
}//reset()
func (l *Lcd) close() {
l.Lock()
defer l.Unlock()
log.Printf("Lcd.close() active: %v", l.active)
if !l.active {
return
}
l.writeByte(lcdLine1, lcdCmd)
for i := 0; i < lcdWidth; i++ {
l.writeByte(' ', lcdChr)
}
l.writeByte(lcdLine2, lcdCmd)
for i := 0; i < lcdWidth; i++ {
l.writeByte(' ', lcdChr)
}
time.Sleep(1 * time.Second)
l.writeByte(0x01, lcdCmd) // 000001 Clear display
l.writeByte(0x0C, lcdCmd) // 001000 Display Off
l.lcdRS.Low()
l.lcdE.Low()
l.lcdD4.Low()
l.lcdD5.Low()
l.lcdD6.Low()
l.lcdD7.Low()
rpio.Close()
l.active = false
close(l.msg)
close(l.end)
}//close()
// writeByte send byte to lcd
func (l *Lcd) writeByte(bits uint8, characterMode bool) {
if characterMode {
l.lcdRS.High()
} else {
l.lcdRS.Low()
}
//High bits
if bits&0x10 == 0x10 {
l.lcdD4.High()
} else {
l.lcdD4.Low()
}
if bits&0x20 == 0x20 {
l.lcdD5.High()
} else {
l.lcdD5.Low()
}
if bits&0x40 == 0x40 {
l.lcdD6.High()
} else {
l.lcdD6.Low()
}
if bits&0x80 == 0x80 {
l.lcdD7.High()
} else {
l.lcdD7.Low()
}
//Toggle 'Enable' pin
time.Sleep(ePulse)
l.lcdE.High()
time.Sleep(ePulse)
l.lcdE.Low()
time.Sleep(ePulse)
//time.Sleep(eDelay)
//Low bits
if bits&0x01 == 0x01 {
l.lcdD4.High()
} else {
l.lcdD4.Low()
}
if bits&0x02 == 0x02 {
l.lcdD5.High()
} else {
l.lcdD5.Low()
}
if bits&0x04 == 0x04 {
l.lcdD6.High()
} else {
l.lcdD6.Low()
}
if bits&0x08 == 0x08 {
l.lcdD7.High()
} else {
l.lcdD7.Low()
}
//Toggle 'Enable' pin
time.Sleep(ePulse)
l.lcdE.High()
time.Sleep(ePulse)
l.lcdE.Low()
time.Sleep(eDelay)
}//writeByte(bits uint8, characterMode bool)
//write4Bits send 4bits to lcd
func (l *Lcd) write4Bits(bits uint8, characterMode bool) {
if characterMode {
l.lcdRS.High()
} else {
l.lcdRS.Low()
}
//Low bits
if bits&0x01 == 0x01 {
l.lcdD4.High()
} else {
l.lcdD4.Low()
}
if bits&0x02 == 0x02 {
l.lcdD5.High()
} else {
l.lcdD5.Low()
}
if bits&0x04 == 0x04 {
l.lcdD6.High()
} else {
l.lcdD6.Low()
}
if bits&0x08 == 0x08 {
l.lcdD7.High()
} else {
l.lcdD7.Low()
}
//Toggle 'Enable' pin
time.Sleep(ePulse)
l.lcdE.High()
time.Sleep(ePulse)
l.lcdE.Low()
time.Sleep(eDelay)
}//write4Bits(bits uint8, characterMode bool)
func (l *Lcd) display(msg string) {
l.Lock()
defer l.Unlock()
if !l.active {
return
}
log.Printf("Lcd.display('%#v')", msg)
for line, m := range strings.Split(msg, "\n") {
if len(m) < lcdWidth {
m = m + strings.Repeat(" ", lcdWidth-len(m))
}
switch line {
case 0:
if l.line1 == m {
continue
}
l.line1 = m
l.writeByte(lcdLine1, lcdCmd)
case 1:
if l.line2 == m {
continue
}
l.line2 = m
l.writeByte(lcdLine2, lcdCmd)
default:
log.Printf("Lcd.display: to many lines %d: '%v'", line, m)
return
}
for i := 0; i < lcdWidth; i++ {
l.writeByte(byte(m[i]), lcdChr)
}
}
}//display(msg string)
func main() {
var disp Display
log.Printf("main: starting lcd")
disp = NewLcd()
disp.Display(" \n yeah Go!")
defer func() {
if e := recover(); e != nil {
log.Printf("Recover: %v", e)
}
disp.Close()
log.Printf("main.defer: all closed")
}()
time.Sleep(1 * time.Second)
} //main()