summaryrefslogtreecommitdiff
path: root/cmd/beep/main.go
blob: 56263d521a7d92586d877889a63bb18bed3d20e0 (plain) (blame)
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
package main

import (
	"encoding/binary"
	"time"

	"pm3.dev/cvend"
	"pm3.dev/pic32"
)

func must[V any](v V, err error) V {
	if err != nil {
		panic(err)
	}
	return v
}

func main() {
	cv := must(cvend.OpenIPP(nil))
	defer cv.Close()
	var lastIgnition bool
	pic := must(pic32.OpenIPP(func(msgType byte, msgData []byte) {
		if msgType == 0x03 && binary.BigEndian.Uint16(msgData[:2]) == 0x03fb {
			ignition := msgData[2] != 0
			if lastIgnition != ignition {
				lastIgnition = ignition
				if ignition {
					if err := cv.SendIPP(0x22, []byte{0x06, 0x00, 0x01, 0x00}); err != nil {
						panic(err)
					}
				}
			}
		}
	}))
	defer pic.Close()
	for {
		if err := pic.SendIPP(0x02, []byte{0x03, 0xfb}); err != nil {
			panic(err)
		}
		time.Sleep(100 * time.Millisecond)
	}
}