-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
107 lines (84 loc) · 1.94 KB
/
init.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
package lcd
import (
"fmt"
"sync"
"time"
)
func (l *LCD) init4Bit() {
l.write4BitHigh(0b00100000) // 4-bit mode - N/F flags ignored
// from here we can send high then low nibble
if l.lines > 1 {
l.write4Bit(0b00101000 | (byte(l.fontSize) << 2)) // 4 bit, 2 lines, 5x8
} else {
l.write4Bit(0b00100000 | (byte(l.fontSize) << 2)) // 4 bit, 1 line, 5x8
}
}
func (l *LCD) init8Bit() {
if l.lines > 1 {
l.write8Bit(0b00111000 | (byte(l.fontSize) << 2)) // 8-bit mode, 2 lines, 5x8
} else {
l.write8Bit(0b00110000 | (byte(l.fontSize) << 2)) // 8-bit mode, 2 lines, 5x8
}
}
func (l *LCD) init() {
l.pins.init()
l.pins.registerSelect.Low() // instruction
l.write4BitHigh(0b00110000)
time.Sleep(time.Millisecond * 10)
l.write4BitHigh(0b00110000)
time.Sleep(time.Microsecond * 200)
l.write4BitHigh(0b00110000)
time.Sleep(time.Millisecond * 10)
if len(l.pins.data) == 4 {
l.init4Bit()
} else {
l.init8Bit()
}
// Display OFF
l.execInstruction(insSetDisplayMode, l.displayMode.byte())
// Clear
l.Clear()
// Entry mode
l.SetCursorIncrement(true)
// Turn on!
l.On()
}
func (l *LCD) Close() {
if l.pins.readWrite == nil {
l.pins.readWrite.Low()
}
l.pins.registerSelect.Low()
for _, pin := range l.pins.data {
pin.Low()
}
newMu.Lock()
defer newMu.Unlock()
newCount--
if newCount == 0 {
closeGPIO()
}
}
var newMu sync.Mutex
var newCount int
// New opens communication with the LCD for further instruction
func New(columns, lines uint8, fontSize FontSize, registerSelect, enable, readWrite Pin, data ...Pin) (*LCD, error) {
if len(data) != 4 && len(data) != 8 {
return nil, fmt.Errorf("you must specify either 4 or 8 data pins")
}
newMu.Lock()
defer newMu.Unlock()
newCount++
lcd := &LCD{
columns: columns,
lines: lines,
fontSize: fontSize,
pins: pins{
registerSelect: registerSelect,
enable: enable,
readWrite: readWrite,
data: data,
},
}
lcd.init()
return lcd, nil
}