forked from talkkonnect/go-hd44780
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_conn.go
executable file
·294 lines (246 loc) · 5.27 KB
/
gpio_conn.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
package hd44780
// Hitachi HD44780U support library
import (
"github.com/stianeikeland/go-rpio"
"strings"
"sync"
"time"
)
type GPIO4bit struct {
sync.Mutex
RSPin int // GOIO 7 --> Raspberry Physical Pin 26
EPin int // GPIO 8 --> Raspberry Physical Pin 24
D4Pin int // GPIO 25 --> Raspberry Physical Pin 22
D5Pin int // GPIO 24 --> Raspberry Physical Pin 18
D6Pin int // GPIO 23 --> Raspberry Physical Pin 16
D7Pin int // GPIO 18 --> Raspberry Physical Pin 12
// max lines
Lines int
// Memory address for each line
LinesAddr []byte
// LCD width (number of character in line)
Width int
lcdRS rpio.Pin
lcdE rpio.Pin
lcdD4 rpio.Pin
lcdD5 rpio.Pin
lcdD6 rpio.Pin
lcdD7 rpio.Pin
lastLines []string
active bool
}
// NewGPIO4bit create new GPIO4bit structure with some defaults
func NewGPIO4bit() (h *GPIO4bit) {
h = &GPIO4bit{
RSPin: RSPin,
EPin: EPin,
D4Pin: D4Pin,
D5Pin: D5Pin,
D6Pin: D6Pin,
D7Pin: D7Pin,
Lines: Lines,
LinesAddr: []byte{lcdLine1, lcdLine2, lcdLine3, lcdLine4},
Width: Width,
}
return
}
// Open / initialize LCD interface
func (h *GPIO4bit) Open() (err error) {
h.Lock()
defer h.Unlock()
if h.active {
return
}
if err := rpio.Open(); err != nil {
return err
}
h.lcdRS = initPin(h.RSPin)
h.lcdE = initPin(h.EPin)
h.lcdD4 = initPin(h.D4Pin)
h.lcdD5 = initPin(h.D5Pin)
h.lcdD6 = initPin(h.D6Pin)
h.lcdD7 = initPin(h.D7Pin)
h.lastLines = make([]string, h.Lines, h.Lines)
h.reset()
h.active = true
return
}
// Active return true when interface is working ok
func (h *GPIO4bit) Active() bool {
return h.active
}
// Reset interface
func (h *GPIO4bit) Reset() {
h.Lock()
defer h.Unlock()
h.reset()
}
func (h *GPIO4bit) reset() {
// initialize
h.write4Bits(0x3, lcdCmd)
time.Sleep(5 * time.Millisecond)
h.write4Bits(0x3, lcdCmd)
time.Sleep(240 * time.Microsecond)
h.write4Bits(0x3, lcdCmd)
time.Sleep(240 * time.Microsecond)
h.write4Bits(0x2, lcdCmd)
time.Sleep(240 * time.Microsecond)
h.writeByte(0x28, lcdCmd) // Data length, number of lines, font size
h.writeByte(0x0C, lcdCmd) // Display On,Cursor Off, Blink Off
h.writeByte(0x06, lcdCmd) // Cursor move direction
h.writeByte(0x01, lcdCmd) // Clear display
time.Sleep(5 * time.Millisecond)
}
// Clear display
func (h *GPIO4bit) Clear() {
h.Lock()
defer h.Unlock()
if !h.active {
return
}
h.writeByte(lcdLine1, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine2, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine3, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine4, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
}
// Close interface, clear display.
func (h *GPIO4bit) Close() {
h.Lock()
defer h.Unlock()
if !h.active {
return
}
h.writeByte(lcdLine1, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine2, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine3, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(lcdLine4, lcdCmd)
for i := 0; i < Width; i++ {
h.writeByte(' ', lcdChr)
}
h.writeByte(0x01, lcdCmd) // 000001 Clear display
time.Sleep(5 * time.Millisecond)
h.writeByte(0x0C, lcdCmd) // 001000 Display Off
h.lcdRS.Low()
h.lcdE.Low()
h.lcdD4.Low()
h.lcdD5.Low()
h.lcdD6.Low()
h.lcdD7.Low()
rpio.Close()
h.active = false
}
// writeByte send byte to lcd
func (h *GPIO4bit) writeByte(bits byte, characterMode bool) {
if characterMode {
h.lcdRS.High()
} else {
h.lcdRS.Low()
}
// High bits
h.push4bits(bits >> 4)
// Low bits
h.push4bits(bits)
time.Sleep(eDelay)
}
// write4Bits send (lower) 4bits to lcd
func (h *GPIO4bit) write4Bits(bits byte, characterMode bool) {
if characterMode {
h.lcdRS.High()
} else {
h.lcdRS.Low()
}
h.push4bits(bits)
time.Sleep(eDelay)
}
// push4bits push 4 bites on data lines
func (h *GPIO4bit) push4bits(bits byte) {
if bits&0x01 == 0x01 {
h.lcdD4.High()
} else {
h.lcdD4.Low()
}
if bits&0x02 == 0x02 {
h.lcdD5.High()
} else {
h.lcdD5.Low()
}
if bits&0x04 == 0x04 {
h.lcdD6.High()
} else {
h.lcdD6.Low()
}
if bits&0x08 == 0x08 {
h.lcdD7.High()
} else {
h.lcdD7.Low()
}
// Toggle 'Enable' pin
time.Sleep(ePulse)
h.lcdE.High()
time.Sleep(ePulse)
h.lcdE.Low()
time.Sleep(ePulse)
}
// DisplayLines sends one or more lines separated by \n to lcd
func (h *GPIO4bit) DisplayLines(msg string) {
for line, text := range strings.Split(msg, "\n") {
h.Display(line, text)
}
}
// Display only one line
func (h *GPIO4bit) Display(line int, text string) {
h.Lock()
defer h.Unlock()
if !h.active {
return
}
if line >= h.Lines {
return
}
if len(text) < Width {
text = text + strings.Repeat(" ", h.Width-len(text))
} else {
text = text[:h.Width]
}
// skip not changed lines
if h.lastLines[line] == text {
return
}
h.lastLines[line] = text
h.writeByte(h.LinesAddr[line], lcdCmd)
for c := 0; c < h.Width; c++ {
h.writeByte(byte(text[c]), lcdChr)
}
}
func (h *GPIO4bit) SetChar(pos byte, def []byte) {
if len(def) != 8 {
panic("invalid def - req 8 bytes")
}
h.writeByte(0x40+pos*8, lcdCmd)
for _, d := range def {
h.writeByte(d, lcdChr)
}
}
func (h *GPIO4bit) ToggleBacklight() {
}