62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
|
package main
|
|||
|
|
|||
|
import (
|
|||
|
"encoding/binary"
|
|||
|
"fmt"
|
|||
|
"math"
|
|||
|
"os"
|
|||
|
"os/signal"
|
|||
|
"time"
|
|||
|
|
|||
|
"github.com/karalabe/hid"
|
|||
|
)
|
|||
|
|
|||
|
const (
|
|||
|
vendorID = 0x1446
|
|||
|
productID = 0x6A73
|
|||
|
refreshRate = 500 * time.Millisecond
|
|||
|
calibration = 0.952 // scale correction factor: 2.1 reported = 2.0 actual
|
|||
|
)
|
|||
|
|
|||
|
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])
|
|||
|
oz := float64(raw) / 10.0 * calibration
|
|||
|
rounded := math.Round(oz*10) / 10
|
|||
|
|
|||
|
fmt.Printf("\r📦 Weight: %.1f oz ", rounded)
|
|||
|
time.Sleep(refreshRate)
|
|||
|
}
|
|||
|
}
|