Repository navigation
Expand file tree
/
Copy pathbutton.go
More file actions
87 lines (77 loc) · 1.84 KB
/
Copy pathbutton.go
File metadata and controls
87 lines (77 loc) · 1.84 KB
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
package button
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio/v4"
)
// State represents the current state of a button
type State bool
const (
fetchDelta time.Duration = 10 * time.Millisecond
// Pressed is the state of a pressed button
Pressed State = true
// Released is the state of a released button
Released State = false
)
// Button instance
type Button struct {
pinID uint8
pin rpio.Pin
edgeDetection bool
EventChan chan State
}
// NewButton creates a new Button instance
// pinID is the GPIO pin number you're using
func NewButton(pinID uint8) *Button {
button := Button{
pinID: pinID,
edgeDetection: false,
}
button.pin = rpio.Pin(button.pinID)
button.pin.Mode(rpio.Input)
button.pin.PullDown()
return &button
}
// SetPullDown set the GPIO to PullDown (GPIO to Vcc)
func (button *Button) SetPullDown() error {
button.pin.PullDown()
return nil
}
// SetPullUp set the GPIO to PullUp (GPIO to GND)
func (button *Button) SetPullUp() error {
button.pin.PullUp()
return nil
}
// GetState return the current state of the Button
func (button *Button) GetState() (State, error) {
state := button.pin.Read()
if state == 1 {
return true, nil
} else if state == 0 {
return false, nil
}
return false, fmt.Errorf("unknown state '%d'", state)
}
// EnableEventDetection enables event detection
func (button *Button) EnableEventDetection() error {
previousState, err := button.GetState()
if err != nil {
return err
}
button.EventChan = make(chan State, 1)
go func() {
for {
currentState, err := button.GetState()
if err != nil {
log.WithField("error", err).Error("impossible to get state")
}
if currentState != previousState {
button.EventChan <- currentState
previousState = currentState
}
time.Sleep(fetchDelta)
}
}()
return nil
}