scale-code/main.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

2025-06-06 18:29:25 -04:00
package main
import (
"encoding/binary"
"fmt"
"math"
"os"
"os/signal"
"time"
"github.com/karalabe/hid"
)
const (
vendorID = 0x1446
productID = 0x6A73
refreshRate = 500 * time.Millisecond
)
func main() {
devices := hid.Enumerate(vendorID, productID)
if len(devices) == 0 {
fmt.Println("❌ Scale not found.")
os.Exit(1)
}
device, err := devices[0].Open()
if err != nil {
fmt.Println("❌ Failed to open scale:", err)
os.Exit(1)
}
defer device.Close()
fmt.Println("✅ Scale connected. Reading... (Ctrl+C to quit)")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Print("\n👋 Exiting.\n")
device.Close()
os.Exit(0)
}()
for {
buf := make([]byte, 8)
n, err := device.Read(buf)
if err != nil || n < 6 {
fmt.Print("\r⚠ No data ")
time.Sleep(refreshRate)
continue
}
raw := binary.LittleEndian.Uint16(buf[4:6])
2025-06-07 12:05:01 -04:00
oz := float64(raw) / 10.0
2025-06-06 18:29:25 -04:00
rounded := math.Round(oz*10) / 10
fmt.Printf("\r📦 Weight: %.1f oz ", rounded)
time.Sleep(refreshRate)
}
}