Skip to content

Instantly share code, notes, and snippets.

@non117
Last active July 9, 2024 13:45
Show Gist options
  • Save non117/7b55503edc53218451bdc9d470c90d0b to your computer and use it in GitHub Desktop.
Save non117/7b55503edc53218451bdc9d470c90d0b to your computer and use it in GitHub Desktop.
co2mini
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/zserge/hid"
)
const (
vendorID = "04d9:a052"
co2op = 0x50
)
var (
key = []byte{0x86, 0x41, 0xc9, 0xa8, 0x7f, 0x41, 0x3c, 0xac}
)
func monitor(device hid.Device) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
if err := device.Open(); err != nil {
log.Fatal(err)
}
defer device.Close()
if err := device.SetReport(0, key); err != nil {
log.Fatal(err)
}
for {
select {
case <-sigs:
return
default:
buf, err := device.Read(-1, 3*time.Second)
if err != nil || len(buf) == 0 {
continue
}
val := int(buf[1])<<8 | int(buf[2])
switch buf[0] {
case co2op:
fmt.Printf("co2\t%d\t%d\n", val, time.Now().Unix())
return
}
}
}
}
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
hid.UsbWalk(func(device hid.Device) {
info := device.Info()
id := fmt.Sprintf("%04x:%04x", info.Vendor, info.Product)
if id != vendorID {
return
}
monitor(device)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment