-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
106 lines (93 loc) · 1.87 KB
/
core.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
package lcd
import (
"time"
)
const (
timeExecutionDelay = time.Microsecond * 80
timeClearDelay = time.Millisecond * 10
)
func (l *LCD) setDDRAMAddress(address byte) {
l.execInstruction(insSetDDRAMAddress, address)
}
func (l *LCD) setCGRAMAddress(address byte) {
l.execInstruction(insSetCGRAMAddress, address)
}
func (l *LCD) execInstruction(ins instruction, data byte) {
l.pins.registerSelect.Low()
data = byte(ins) | data
l.writeRaw(data)
}
func (l *LCD) writeRaw(data ...byte) {
for _, b := range data {
if len(l.pins.data) == 8 {
l.write8Bit(b)
return
}
l.write4Bit(b)
}
}
func (l *LCD) write4Bit(data byte) {
for i, pin := range l.pins.data {
if data&(1<<(i+4)) > 0 {
pin.High()
} else {
pin.Low()
}
}
l.pulseEnable(false, false)
for i, pin := range l.pins.data {
if data&(1<<i) > 0 {
pin.High()
} else {
pin.Low()
}
}
l.pulseEnable(true, true)
}
func (l *LCD) write4BitHigh(data byte) {
// send most significant 4 bits to most significant 4 pins
for i, pin := range l.pins.data[len(l.pins.data)-4:] {
if data&(1<<(i+4)) > 0 {
pin.High()
} else {
pin.Low()
}
}
l.pulseEnable(true, false)
}
func (l *LCD) write8Bit(data byte) {
for i, pin := range l.pins.data {
if data&(1<<i) > 0 {
pin.High()
} else {
pin.Low()
}
}
l.pulseEnable(true, true)
}
func (l *LCD) pulseEnable(withExecDelay bool, allowBF bool) {
time.Sleep(time.Microsecond)
l.pins.enable.High()
time.Sleep(time.Microsecond)
l.pins.enable.Low()
if withExecDelay {
l.wait(allowBF)
}
}
func (l *LCD) wait(allowBF bool) {
if l.pins.readWrite == nil || !allowBF {
time.Sleep(timeExecutionDelay)
return
}
l.pins.registerSelect.Low()
l.pins.readWrite.High()
defer l.pins.readWrite.Low()
l.pins.setDataInput()
defer l.pins.setDataOutput()
for {
l.pulseEnable(false, false)
if !l.pins.data[len(l.pins.data)-1].Read() {
break
}
}
}