From 4ffbed4013796975ea926f24d6b13a7235e6ab91 Mon Sep 17 00:00:00 2001 From: Damien Date: Mon, 4 Sep 2023 07:36:35 +1100 Subject: [PATCH] feat(lib): stringer required for driver, deviceid and device --- acr122usb/device.go | 4 ++++ acr122usb/driver.go | 6 ++++++ device.go | 1 + driver.go | 2 ++ periphio/i2cdevice.go | 4 ++++ periphio/i2cdeviceid.go | 4 ++++ periphio/i2cdriver.go | 4 ++++ pigpio/pn532i2c.device.go | 5 +++++ pigpio/pn532i2c.deviceid.go | 4 ++++ pigpio/pn532i2c.driver.go | 6 +++++- 10 files changed, 39 insertions(+), 1 deletion(-) diff --git a/acr122usb/device.go b/acr122usb/device.go index 7615069..403ec69 100644 --- a/acr122usb/device.go +++ b/acr122usb/device.go @@ -85,6 +85,10 @@ func (d *Acr122UsbDevice) Logger() *zap.SugaredLogger { return d.logger } +func (d *Acr122UsbDevice) String() string { + return d.id.String() +} + func (d *Acr122UsbDevice) SetLastError(err error) { d.LastError = err } diff --git a/acr122usb/driver.go b/acr122usb/driver.go index 3e5d8f0..70af827 100644 --- a/acr122usb/driver.go +++ b/acr122usb/driver.go @@ -1,6 +1,8 @@ package acr122usb import ( + "fmt" + "github.com/google/gousb" "github.com/maitredede/gonfc" "go.uber.org/zap" @@ -29,6 +31,10 @@ func (Acr122USBDriver) Product() string { return "acr122" } +func (d Acr122USBDriver) String() string { + return fmt.Sprintf("%s %s", d.Manufacturer(), d.Product()) +} + func (Acr122USBDriver) Conflicts(otherDriver gonfc.Driver) bool { if _, ok := otherDriver.(*Acr122USBDriver); ok { return true diff --git a/device.go b/device.go index 859d30d..53e29d2 100644 --- a/device.go +++ b/device.go @@ -9,6 +9,7 @@ import ( type Device interface { ID() DeviceID Close() error + String() string SetPropertyBool(property Property, value bool) error SetPropertyInt(property Property, value int) error diff --git a/driver.go b/driver.go index fdca8a5..e694f68 100644 --- a/driver.go +++ b/driver.go @@ -10,10 +10,12 @@ type Driver interface { LookupDevices(logger *zap.SugaredLogger) ([]DeviceID, error) Conflicts(otherDriver Driver) bool + String() string } type DeviceID interface { Driver() Driver Path() string Open(logger *zap.SugaredLogger) (Device, error) + String() string } diff --git a/periphio/i2cdevice.go b/periphio/i2cdevice.go index f89401b..657a91d 100644 --- a/periphio/i2cdevice.go +++ b/periphio/i2cdevice.go @@ -36,6 +36,10 @@ func (d *PeriphioI2CDevice) Close() error { return d.hwBus.Close() } +func (d *PeriphioI2CDevice) String() string { + return d.id.String() +} + func (d *PeriphioI2CDevice) SetPropertyBool(property gonfc.Property, value bool) error { panic("TODO") } diff --git a/periphio/i2cdeviceid.go b/periphio/i2cdeviceid.go index e5fe22d..85055d3 100644 --- a/periphio/i2cdeviceid.go +++ b/periphio/i2cdeviceid.go @@ -24,3 +24,7 @@ func (d *PeriphioI2CDeviceID) Path() string { func (d *PeriphioI2CDeviceID) Open(logger *zap.SugaredLogger) (gonfc.Device, error) { return d.drv.openDevice(logger) } + +func (d *PeriphioI2CDeviceID) String() string { + return fmt.Sprintf("%s %s", d.drv.String(), d.Path()) +} diff --git a/periphio/i2cdriver.go b/periphio/i2cdriver.go index 7fb32f2..b318157 100644 --- a/periphio/i2cdriver.go +++ b/periphio/i2cdriver.go @@ -42,6 +42,10 @@ func (d *PeriphioI2CDriver) Product() string { return "pn532-i2c" } +func (d *PeriphioI2CDriver) String() string { + return fmt.Sprintf("%s %s", d.Manufacturer(), d.Product()) +} + func (d *PeriphioI2CDriver) LookupDevices(logger *zap.SugaredLogger) ([]gonfc.DeviceID, error) { dev, err := d.openDevice(logger) if err != nil { diff --git a/pigpio/pn532i2c.device.go b/pigpio/pn532i2c.device.go index 00d8a64..dfbe6dc 100644 --- a/pigpio/pn532i2c.device.go +++ b/pigpio/pn532i2c.device.go @@ -33,9 +33,14 @@ func (d *PN532PiGPIOI2CDevice) Logger() *zap.SugaredLogger { return d.logger } +func (d *PN532PiGPIOI2CDevice) String() string { + return d.id.String() +} + func (d *PN532PiGPIOI2CDevice) SetLastError(err error) { d.LastError = err } + func (d *PN532PiGPIOI2CDevice) GetInfiniteSelect() bool { return d.InfiniteSelect } diff --git a/pigpio/pn532i2c.deviceid.go b/pigpio/pn532i2c.deviceid.go index 1b7d0b0..91f5b2f 100644 --- a/pigpio/pn532i2c.deviceid.go +++ b/pigpio/pn532i2c.deviceid.go @@ -21,6 +21,10 @@ func (d *PN532PiGPIOI2CDeviceID) Path() string { return fmt.Sprintf("tcp://%v?bus=%v&addr=%v&flags=%v", d.drv.host, d.drv.i2cBus, d.drv.i2cAddress, d.drv.i2cFlags) } +func (d *PN532PiGPIOI2CDeviceID) String() string { + return fmt.Sprintf("%s %s", d.drv.String(), d.Path()) +} + func (d *PN532PiGPIOI2CDeviceID) Open(logger *zap.SugaredLogger) (gonfc.Device, error) { dev, err := d.drv.openDevice(logger) if err != nil { diff --git a/pigpio/pn532i2c.driver.go b/pigpio/pn532i2c.driver.go index 5a9eded..67cb5e2 100644 --- a/pigpio/pn532i2c.driver.go +++ b/pigpio/pn532i2c.driver.go @@ -36,13 +36,17 @@ func NewI2CDriver(host string, i2cBus byte, i2cAddress byte, i2cFlags uint32) (* } func (d *PN532PiGPIOI2CDriver) Manufacturer() string { - return "PN532 I2C via PiGPIO" + return "PiGPIO - PN532 I2C" } func (d *PN532PiGPIOI2CDriver) Product() string { return d.host } +func (d *PN532PiGPIOI2CDriver) String() string { + return fmt.Sprintf("%s %s", d.Manufacturer(), d.Product()) +} + func (d *PN532PiGPIOI2CDriver) Conflicts(otherDriver gonfc.Driver) bool { if o, ok := otherDriver.(*PN532PiGPIOI2CDriver); ok { return strings.EqualFold(o.host, d.host)