/
main.go
75 lines (67 loc) · 1.8 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
package main
import (
"github.com/hybridgroup/gobot/platforms/raspi"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/http"
"strconv"
"time"
)
var (
pin int
stateCount int
delay time.Duration
bindAddr string
flircMap []int
currentState int
a *raspi.RaspiAdaptor
)
var (
mainCmd = &cobra.Command{
Run: run,
}
)
func run(cmd *cobra.Command, args []string) {
if pin == 0 {
log.Fatalln("Pin number is required")
}
a = raspi.NewRaspiAdaptor("raspi")
err := a.DigitalWrite(strconv.Itoa(pin), 0)
if err != nil {
panic(err)
}
if len(flircMap) > 0 {
go OpenFLIRC(true)
}
log.Fatalln(http.ListenAndServe(bindAddr, http.HandlerFunc(ServeHTTP)))
}
func incr() {
err := a.DigitalWrite(strconv.Itoa(pin), 1)
if err != nil {
panic(err)
}
time.Sleep(delay)
err = a.DigitalWrite(strconv.Itoa(pin), 0)
if err != nil {
panic(err)
}
time.Sleep(delay)
currentState = (currentState + 1) % stateCount
}
func setState(newState int) {
log.WithField("state", newState).Infoln("changing state")
if newState >= stateCount {
panic("invalid state")
}
for currentState != newState {
incr()
}
}
func main() {
mainCmd.Flags().IntVarP(&pin, "pin", "p", 0, "Physical pin number. Pin used to toggle the usb switcher.")
mainCmd.Flags().IntVarP(&stateCount, "state-count", "c", 4, "Number of states. This should equal the number of states on the USB switch.")
mainCmd.Flags().DurationVarP(&delay, "delay", "d", time.Millisecond*50, "Toggle delay. Time between switch toggles.")
mainCmd.Flags().StringVarP(&bindAddr, "bind-address", "b", ":8080", "Bind address. The address:port to bind to for HTTP requests.")
mainCmd.Flags().IntSliceVar(&flircMap, "flirc", []int{}, "Map key codes from FLIRC to states. The first code will trigger state 0 and so on.")
mainCmd.Execute()
}