Skip to content

Commit

Permalink
feat(lib): stringer required for driver, deviceid and device
Browse files Browse the repository at this point in the history
  • Loading branch information
maitredede committed Sep 3, 2023
1 parent 0ce2eb6 commit 4ffbed4
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions acr122usb/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions acr122usb/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package acr122usb

import (
"fmt"

"github.com/google/gousb"
"github.com/maitredede/gonfc"
"go.uber.org/zap"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions periphio/i2cdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 4 additions & 0 deletions periphio/i2cdeviceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions periphio/i2cdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pigpio/pn532i2c.device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pigpio/pn532i2c.deviceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion pigpio/pn532i2c.driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4ffbed4

Please sign in to comment.