-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.go
84 lines (78 loc) · 1.71 KB
/
send.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 main
import (
"flag"
"fmt"
"log"
"os/exec"
"strconv"
)
var (
action = flag.String("action", "toggle", "One of (toggle, open, test, party, vacation)")
id = flag.Int64("id", 123456, "Transmitter ID")
button_id = flag.Int64("button_id", 2, "(2:primary, 1:secondary, 0:none)")
version = flag.Int64("version", 1, "(1: remote, 2:keypad, 9:sensor)")
dry_run = flag.Bool("dry_run", false, "skip calling sendook")
count = flag.Int64("count", 10, "Number of times to transmit the code")
)
func decodeAction(action string) (option, command int64, err error) {
switch action {
case "toggle":
command = 3
case "open":
command = 1
case "party":
option = 8
case "vacation":
option = 9
case "test":
option = 15
default:
return 0, 0, fmt.Errorf("Unknown action: %q", action)
}
return
}
func codeFromFlags() (int64, error) {
option, command, err := decodeAction(*action)
if err != nil {
return 0, err
}
// TODO : range checks
var code int64
code |= *version << 38
code |= option << 34
code |= command << 30
code |= *button_id << 22
code |= *id
return code, nil
}
func toBits(code int64) (out string) {
v := code
for i := 0; i < 42; i++ {
if v%2 == 0 {
out = "1000" + out
} else {
out = "1110" + out
}
v /= 2
}
return out
}
func main() {
flag.Parse()
code, err := codeFromFlags()
if err != nil {
log.Fatal(err)
}
bits := toBits(code)
if *dry_run {
fmt.Printf("Code: {42}%011x\n", code*4)
fmt.Printf("Binary: %b\n", code)
fmt.Printf("After pwm: %q\n", bits)
} else {
cmd := exec.Command("sudo", "sendook", "-1", "250", "-0", "250", "-r", strconv.FormatInt(*count, 10), "-p", "40000", bits)
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
}
}