antplusbridge/cmd/bridge/ant.go
Erik Brakkee b24513fcc3 feat: add CLI with Cobra and timestamped logging
Introduce structured CLI argument parsing using Cobra with three
configurable flags:
  --vendor: USB vendor ID (default: 0x0fcf)
  --product: USB product ID (default: 0x1008)
  --device: ANT+ device number (default: 3001)

Add timestamped logging for all log statements with format
YYYY-MM-DD HH:MM:SS. Log vendor, product, and device values at
startup in both hex and decimal.

Remove hardcoded USB identifiers from main.go and consolidate all
startup logic into cli.go with a dedicated Config struct.

Update ant.go ListenForTrainer signature to accept uint16 for
vendor and product IDs.
2026-05-27 00:36:06 +02:00

76 lines
1.7 KiB
Go

package main
import (
"context"
"io"
"github.com/google/gousb"
"github.com/half2me/antgo/ant"
"github.com/half2me/antgo/device"
"github.com/half2me/antgo/driver/usb"
)
func resetAndWait(drv io.ReadWriter) error {
if _, err := drv.Write(ant.SystemResetMessage()); err != nil {
return err
}
// drain until we see the startup message (0x6F)
for {
msg, err := ant.ReadMsg(drv)
if err != nil {
return err
}
if msg.Class() == ant.MESSAGE_STARTUP {
return nil
}
}
}
func ListenForTrainer(vendorId uint16, productId uint16, deviceNumber uint32,
events chan<- Event) {
drv, err := usb.GetDevice(gousb.ID(vendorId), gousb.ID(productId))
if err != nil {
panic(err)
}
defer drv.Close()
logMsg("device opened")
if err := resetAndWait(drv); err != nil {
panic(err)
}
if err := device.StartRxScanModeWait(drv); err != nil {
panic(err)
}
logMsg("scan mode started")
msgs := make(chan ant.BroadcastMessage, 100)
go device.DumpBroadcastMessages(context.Background(), drv, msgs)
logMsg("listening...")
for msg := range msgs {
if msg.DeviceNumber() != deviceNumber {
logPrintf("Received msg from ignored ant+ device %v\n", msg.DeviceNumber())
continue
}
//fmt.Printf("raw msg: device type=0x%02x device number=%d content=%x\n",
// msg.DeviceType(), msg.DeviceNumber(), msg.Content())
switch msg.DeviceType() {
case ant.DEVICE_TYPE_POWER:
// Check data page first
page := msg[4] & 0x7F // mask toggle bit
if page != 0x10 {
continue
}
cur := ant.PowerMessage(msg)
logPrintf("ant: %v: power: %dW cadence: %d rpm\n",
msg.DeviceNumber(), cur.InstantaneousPower(), cur.InstantaneousCadence())
events <- Event{
Power: cur.InstantaneousPower(),
Cadence: cur.InstantaneousCadence(),
}
}
}
}