From d02d21ecea979c8526ae70187eacd47c493cf8ef Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Mon, 30 Jun 2025 08:16:15 +0100 Subject: [PATCH] feat: allow gps init with address Adafruit's Mini GPS PA1010D Module works with this device driver, but requires 0x10 as the address, rather than 0x42. This change allows the device to be initialised with whatever i2c address is needed, while maintaining backward compatibility. Adds new constants to allow easy configuration of both the ublox device and the PA1010D. --- examples/gps/i2c/main.go | 2 +- gps/gps.go | 8 +++++++- gps/registers.go | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/examples/gps/i2c/main.go b/examples/gps/i2c/main.go index 8f7873c..123cc26 100644 --- a/examples/gps/i2c/main.go +++ b/examples/gps/i2c/main.go @@ -10,7 +10,7 @@ import ( func main() { println("GPS I2C Example") machine.I2C0.Configure(machine.I2CConfig{}) - ublox := gps.NewI2C(machine.I2C0) + ublox := gps.NewI2CWithAddress(machine.I2C0, gps.UBLOX_I2C_ADDRESS) parser := gps.NewParser() var fix gps.Fix for { diff --git a/gps/gps.go b/gps/gps.go index 86bfd6a..16ba870 100644 --- a/gps/gps.go +++ b/gps/gps.go @@ -69,10 +69,16 @@ func NewUART(uart drivers.UART) Device { } // NewI2C creates a new I2C GPS connection. +// Uses the default i2c address (0x42) for backward compatibility reasons. func NewI2C(bus drivers.I2C) Device { + return NewI2CWithAddress(bus, I2C_ADDRESS) +} + +// NewI2CWithAddress creates a new I2C GPS connection on the provided address +func NewI2CWithAddress(bus drivers.I2C, i2cAddress uint16) Device { return Device{ bus: bus, - address: I2C_ADDRESS, + address: i2cAddress, buffer: make([]byte, bufferSize), bufIdx: bufferSize, sentence: strings.Builder{}, diff --git a/gps/registers.go b/gps/registers.go index 5ef9918..6d74d87 100644 --- a/gps/registers.go +++ b/gps/registers.go @@ -4,7 +4,11 @@ package gps // The I2C address which this device listens to. const ( - I2C_ADDRESS = 0x42 + // To ensure backward compatibility + I2C_ADDRESS = UBLOX_I2C_ADDRESS + + UBLOX_I2C_ADDRESS = 0x42 + PA1010D_I2C_ADDRESS = 0x10 ) const (