-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
109 lines (88 loc) · 2.34 KB
/
main.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
package main
import (
"log"
"os"
"path/filepath"
"regexp"
"time"
"github.com/morphar/remotepi/pkg/rc5"
"github.com/stianeikeland/go-rpio/v4"
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
}
// $ cat /proc/asound/card0/pcm*/sub*/status
// closed
// $ cat /proc/asound/card0/pcm*/sub*/status
// state: RUNNING
// The regexp for matching ON state of the audio stream
var reRunning = regexp.MustCompile("state: RUNNING")
func main() {
// Open pin with the remote control rca connected
err := rpio.Open()
exitOnErr(err)
defer rpio.Close()
// Currently only supports pin 17
pin := rpio.Pin(17)
defer pin.Low()
// Create a couple of amplifier rc commands
// onOff := rc5Command(16, 12, 0)
// volumeUp := rc5Command(16, 16, 0)
// volumeDown := rc5Command(16, 17, 0)
turnOn := rc5.CommandX(16, 12, 1, 0)
turnOff := rc5.CommandX(16, 12, 2, 0)
// directVolume := rc5xCommand(16, 111, 10, 0)
// onOff := 0b11010000001100
// turnOff := 0b1101000000001100000010
// sendSignal(pin, uint(onOff), true)
// Delays before turning on or off the amplifier
offDeleay := 2 * time.Minute
// Find all audio card status files (hopefully only 1)
statusFiles, err := filepath.Glob("/proc/asound/card0/pcm*/sub*/status")
exitOnErr(err)
// if len(matches) != 1 {
// log.Fatal("For now, this only works with 1 audio card")
// }
// statusFile := matches[0]
// Setup the check vars
var lastOn time.Time
var stateOn bool
for {
// Find the current state - more specifically: is any cards running?
curStateOn := false
for _, statusFile := range statusFiles {
src, err := os.ReadFile(statusFile)
exitOnErr(err)
if reRunning.Match(src) {
curStateOn = true
break
}
}
// cur state is on, update the lastOn timestamp
if curStateOn {
lastOn = time.Now()
}
// If any card is running and the state is not on: send the ON signal
if curStateOn && !stateOn {
stateOn = true
rc5.Send(pin, turnOn, true)
time.Sleep(time.Second)
pin.Input()
pin.PullOff()
continue
}
// If the current state is off and the off delay has passed since last on: send the OFF signal
if !curStateOn && stateOn && time.Since(lastOn) > offDeleay {
stateOn = false
rc5.Send(pin, turnOff, true)
pin.Input()
pin.PullOff()
}
time.Sleep(time.Second)
}
}
func exitOnErr(err error) {
if err != nil {
log.Fatal(err)
}
}