forked from KarolBedkowski/go-hd44780
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_conn.go
195 lines (161 loc) · 3.1 KB
/
i2c_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
package hd44780
import (
"strings"
// "sync"
//"github.com/kidoman/embd"
//"github.com/kidoman/embd/controller/hd44780"
"github.com/zlowred/embd"
"github.com/zlowred/embd/controller/hd44780"
// load only rpi
//_ "github.com/kidoman/embd/host/rpi"
_ "github.com/zlowred/embd/host/rpi"
)
// I2C4bit allow communicate wit HD44780 via I2C in 4bit mode
type I2C4bit struct {
// sync.Mutex
// max lines
Lines int
// LCD width (number of character in line)
Width int
// i2c address
addr byte
lastLines []string
active bool
hd *hd44780.HD44780
backlight bool
}
// NewI2C4bit create new I2C4bit structure with some defaults
func NewI2C4bit(addr byte) (h *I2C4bit) {
h = &I2C4bit{
Lines: 2,
addr: addr,
Width: lcdWidth,
}
return
}
// Open / initialize LCD interface
func (h *I2C4bit) Open() (err error) {
// h.Lock()
// defer h.Unlock()
if h.active {
return
}
if err := embd.InitI2C(); err != nil {
panic(err)
}
bus := embd.NewI2CBus(1)
h.hd, err = hd44780.NewI2C(
bus,
h.addr,
hd44780.PCF8574PinMap,
hd44780.RowAddress16Col,
hd44780.TwoLine,
// hd44780.BlinkOff,
hd44780.CursorOff,
hd44780.EntryIncrement,
)
if err != nil {
return err
}
h.lastLines = make([]string, h.Lines, h.Lines)
h.reset()
h.hd.BacklightOn()
h.backlight = true
h.active = true
return
}
// Active return true when interface is working ok
func (h *I2C4bit) Active() bool {
return h.active
}
// Reset interface
func (h *I2C4bit) Reset() {
// h.Lock()
// defer h.Unlock()
h.hd.Clear()
}
// Clear screen
func (h *I2C4bit) Clear() {
// h.Lock()
// defer h.Unlock()
h.hd.Clear()
}
func (h *I2C4bit) reset() {
// clear the display
h.hd.Clear()
}
// Close interface, clear display.
func (h *I2C4bit) Close() {
// h.Lock()
// defer h.Unlock()
if !h.active {
return
}
h.hd.BacklightOff()
h.backlight = false
h.hd.Clear()
embd.CloseI2C()
h.active = false
}
// DisplayLines sends one or more lines separated by \n to lcd
func (h *I2C4bit) DisplayLines(msg string) {
for line, text := range strings.Split(msg, "\n") {
h.Display(line, text)
}
}
// Display only one line
func (h *I2C4bit) Display(line int, text string) {
// h.Lock()
// defer h.Unlock()
if !h.active {
return
}
if line >= h.Lines {
return
}
if !h.backlight {
return
}
// skip not changed lines
if h.lastLines[line] == text {
return
}
h.lastLines[line] = text
textLen := len(text)
if textLen < lcdWidth {
text = text + strings.Repeat(" ", h.Width-textLen)
} else if textLen > h.Width {
text = text[:h.Width]
}
h.hd.SetCursor(0, line)
for _, c := range text {
h.hd.WriteChar(byte(c))
}
}
func (h *I2C4bit) ToggleBacklight() {
if !h.active {
return
}
if h.backlight {
h.hd.BacklightOff()
h.hd.Clear()
h.hd.Home()
h.backlight = false
} else {
h.hd.BacklightOn()
h.backlight = true
for l, line := range h.lastLines {
h.lastLines[l] = ""
h.Display(l, line)
}
}
}
func (h *I2C4bit) SetChar(pos byte, def []byte) {
if len(def) != 8 {
panic("invalid def - req 8 bytes")
}
h.hd.WriteInstruction(0x40 + pos*8)
for _, d := range def {
h.hd.WriteChar(d)
}
}