-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
84 lines (71 loc) · 1.55 KB
/
button.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
package hkdoorbell
import (
"bufio"
"time"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/log"
"github.com/ra1nb0w/hkdoorbell/rpi"
)
type Button struct {
buttonExit bool
gpio int
switchButton *characteristic.ProgrammableSwitchEvent
stdinScanner *bufio.Scanner
runButtonPressed func()
}
func InitButton(gpio int, switchButton *characteristic.ProgrammableSwitchEvent, scanner *bufio.Scanner, runBut func()) *Button {
return &Button{
buttonExit: false,
gpio: gpio,
switchButton: switchButton,
stdinScanner: scanner,
runButtonPressed: runBut,
}
}
func (b *Button) StartLinux() {
p, err := rpi.OpenPin(b.gpio, rpi.IN)
if err != nil {
panic(err)
}
defer p.Close()
c := 0
for {
if b.buttonExit {
return
}
// we have an external pull-up
// we avoid bouncing
v, _ := p.Read()
if v == 0 {
c = 2
}
if c > 0 {
c--
if c == 0 && v == 1 {
log.Debug.Println(">>> Someone pressed the doorbell button <<<")
go b.runButtonPressed()
b.switchButton.SetValue(1)
}
}
time.Sleep(100 * time.Millisecond)
}
}
// to activate the button on macOS just write something
// on terminal and press enter
func (b *Button) StartMacOS() {
for {
if b.buttonExit {
return
}
b.stdinScanner.Scan()
// Holds the string that scanned
if len(b.stdinScanner.Text()) != 0 {
log.Debug.Println(">>> Someone pressed the doorbell button <<<")
go b.runButtonPressed()
b.switchButton.SetValue(1)
}
}
}
func (b *Button) Stop() {
b.buttonExit = false
}