-
Notifications
You must be signed in to change notification settings - Fork 1
/
Primary.go
161 lines (139 loc) · 3.17 KB
/
Primary.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
package LiquidCrystalRPI
import (
"strings"
"time"
"periph.io/x/conn/v3/driver/driverreg"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/host/v3"
)
func initialize() error {
_, err := host.Init()
if err != nil {
return err
}
_, err = driverreg.Init()
if err != nil {
return err
}
return nil
}
// This function returns an object that represents a LCD.
// The addr is the I²C device address (0x27 is the most common one).
// width and height is the LCD's size.
func NewLCD(addr uint16, width, height int) (lcd LCD, err error) {
err = initialize()
if err != nil {
return LCD{}, err
}
bus, err := i2creg.Open("1")
if err != nil {
return LCD{}, err
}
lcd = LCD{
Device: &i2c.Dev{Addr: addr, Bus: bus},
Height: height,
Width: width,
}
lcd.write(0x33, 0)
lcd.write(0x32, 0)
lcd.write(0x06, 0)
lcd.write(0x0C, 0)
lcd.write(0x28, 0)
lcd.write(0x01, 0)
time.Sleep(SLEEP)
return lcd, nil
}
// This function prints a string on the LCD.
//txt is the string that you want to print.
//line is the line of the text.
/*
Example:
func main() {
...
lcd.Print("Hello World!", 1) // print 'Hello World' at line 1
lcd.Print("Second Line", 2) // print 'Second Line' at line 2
}
*/
func (l LCD) Print(txt string, line int) {
l.write(LCD_LINES[line], 0)
for _, v := range txt {
l.write(byte(int(v)), 1)
}
}
//Clear, clears the entire screen
func (l LCD) Clear() {
l.write(0x01, 0)
}
//BackLightOff turns off the LCD's backlight
func (l LCD) BackLightOff() {
l.Device.Write([]byte{0x00})
}
//BackLightOn turns on the LCD's backlight
func (l LCD) BackLightOn() {
l.Device.Write([]byte{0x08})
}
// ScrollText iterates over the txt and prints it on the LCD
// txt : Text to be printed
// row : line number
// delay : scroll speed(lower = faster)
/*
Example:
import (
"time"
lcd "github.com/polarspetroll/LiquidCrystalRPI"
)
func main() {
l := lcd.DefaultLCD
defer l.Close()
delay := 300 * time.Millisecond
for i := 0; i < 2; i++ {
l.ScrollText("Long Text : github.com/polarspetroll/LiquidCrystalRPI", 1, delay)
}
l.Clear()
for i = 0; i < 2; i++ {
l.ScrollText("Short Text!", 1, delay)
}
}
*/
func (l LCD) ScrollText(txt string, line int, delay time.Duration) {
if len(txt) < l.Width {
for range txt {
txt += " "
}
l.short_text_scroll(txt, line, delay)
} else if len(txt) > l.Width {
l.long_text_scroll(txt, line, delay)
}
}
func (l LCD) long_text_scroll(txt string, line int, delay time.Duration) {
var tmp string
for range txt {
tmp = string(txt[0])
txt = strings.Replace(txt, string(txt[0]), "", 1)
txt += tmp
l.Print(txt[0:l.Width-1], line)
time.Sleep(delay)
}
}
func (l LCD) short_text_scroll(txt string, line int, delay time.Duration) {
var tmp string
for range txt {
tmp = string(txt[0])
txt = strings.Replace(txt, string(txt[0]), "", 1)
txt += tmp
l.Print(txt, line)
time.Sleep(delay)
}
}
func (l LCD) write_byte(b byte) {
l.Device.Write([]byte{b})
l.Device.Write([]byte{b | 0b00000100})
time.Sleep(SLEEP)
l.Device.Write([]byte{b & ^byte(0b00000100)})
time.Sleep(SLEEP)
}
func (l LCD) write(b, n byte) {
l.write_byte(n | (b & 0xF0) | 0x08)
l.write_byte(n | ((b << 4) & 0xF0) | 0x08)
}