-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_listeners.go
64 lines (58 loc) · 920 Bytes
/
event_listeners.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
package gopio
import (
"fmt"
"io/ioutil"
"os"
)
func (p *Pin) OnValueChange(cpin Pin, callback func(g Pin)) error {
file := fmt.Sprintf("%s/gpio%d/value", basedir, p.Num)
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
var listenvalue = LOW
if string(b) == "0\n" {
listenvalue = HIGH
}
var v VALUE
for {
v, err = p.Value()
if err != nil {
panic(err)
}
if v == listenvalue {
callback(cpin)
}
}
}
func (p *Pin) OnValueHigh(cpin Pin, callback func(g Pin)) error {
var v VALUE
var err error
for {
v, err = p.Value()
if err != nil {
panic(err)
}
if v == HIGH {
callback(cpin)
}
}
}
func (p *Pin) OnValueLow(cpin Pin, callback func(g Pin)) error {
var v VALUE
var err error
for {
v, err = p.Value()
if err != nil {
panic(err)
}
if v == LOW {
callback(cpin)
}
}
}