apply @gen2thomas suggestions

This commit is contained in:
Patricio Whittingslow
2025-09-16 09:26:00 -03:00
parent 3cd6afb84c
commit 8d3cac8322
13 changed files with 77 additions and 67 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ func New(b drivers.SPI) *Device {
// NewSoftwareSPI returns a new APA102 driver that will use a software based
// implementation of the SPI protocol.
func NewSoftwareSPI(sckPin, sdoPin legacy.PinOutput, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, config: func() {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin)
legacy.ConfigurePinOut(sdoPin)
}})
+3 -3
View File
@@ -14,15 +14,15 @@ type bbSPI struct {
SCK drivers.PinOutput
SDO drivers.PinOutput
Delay uint32
config func()
configurePins func()
}
// Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() {
if s.config == nil {
if s.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
s.config()
s.configurePins()
s.SCK(false)
s.SDO(false)
if s.Delay == 0 {
+4 -4
View File
@@ -17,7 +17,7 @@ type DeviceSPI struct {
// SPI bus (requires chip select to be usable).
bus drivers.SPI
config func()
configurePins func()
}
// NewSPI returns a new device driver. The pin and SPI interface are not
@@ -27,7 +27,7 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
csb: csb.Set, // chip select
bus: spi,
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(csb)
},
}
@@ -37,10 +37,10 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
// configures the BMI160, but it does not configure the SPI interface (it is
// assumed to be up and running).
func (d *DeviceSPI) Configure() error {
if d.config == nil {
if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
}
d.config()
d.configurePins()
d.csb(true)
// The datasheet recommends doing a register read from address 0x7F to get
+4 -4
View File
@@ -15,7 +15,7 @@ type Device struct {
bus drivers.I2C
buf []byte
Address uint8
config func()
configurePins func()
}
// New returns FT6336 device for the provided I2C bus using default address.
@@ -24,7 +24,7 @@ func New(i2c drivers.I2C, intPin legacy.PinInput) *Device {
bus: i2c,
buf: make([]byte, 11),
Address: Address,
config: func() {
configurePins: func() {
legacy.ConfigurePinInputPulldown(intPin)
},
}
@@ -36,11 +36,11 @@ type Config struct {
// Configure the FT6336 device.
func (d *Device) Configure(config Config) error {
if d.config == nil {
if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
}
d.write1Byte(0xA4, 0x00)
d.config()
d.configurePins()
return nil
}
+4 -4
View File
@@ -17,7 +17,7 @@ const TIMEOUT = 23324 // max sensing distance (4m)
type Device struct {
trigger drivers.PinOutput
echo drivers.PinInput
config func()
configurePins func()
}
// New returns a new ultrasonic driver given 2 pins
@@ -25,7 +25,7 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
return Device{
trigger: trigger.Set,
echo: echo.Get,
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(trigger)
legacy.ConfigurePinInput(echo)
},
@@ -34,10 +34,10 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
// Configure configures the pins of the Device
func (d *Device) Configure() {
if d.config == nil {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.config()
d.configurePins()
}
// ReadDistance returns the distance of the object in mm
+4 -4
View File
@@ -10,7 +10,7 @@ import (
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
config func()
configurePins func()
}
// NewDriver creates a new max7219 connection. The SPI wire must already be configured
@@ -20,7 +20,7 @@ func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
return &Device{
bus: bus,
cs: cs.Set,
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(cs)
},
}
@@ -28,10 +28,10 @@ func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
// Configure setups the pins.
func (driver *Device) Configure() {
if driver.config == nil {
if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
driver.config()
driver.configurePins()
}
// SetScanLimit sets the scan limit. Maximum is 8.
+4 -4
View File
@@ -20,7 +20,7 @@ type Device struct {
cs drivers.PinOutput
msg *CANMsg
mcpMode byte
config func()
configurePins func()
}
// CANMsg stores CAN message fields.
@@ -46,7 +46,7 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
},
cs: csPin.Set,
msg: &CANMsg{},
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(csPin)
},
}
@@ -56,10 +56,10 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
// Configure sets up the device for communication.
func (d *Device) Configure() {
if d.config == nil {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.config()
d.configurePins()
}
const beginTimeoutValue int = 10
+10
View File
@@ -2,8 +2,18 @@ package drivers
// PinOutput is hardware abstraction for a pin which outputs a
// digital signal (high or low voltage).
//
// // Code conversion demo: from machine.Pin to drivers.PinOutput
// led := machine.LED
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
// var pin drivers.PinOutput = led.Set // Going from a machine.Pin to a drivers.PinOutput
type PinOutput func(level bool)
// PinInput is hardware abstraction for a pin which receives a
// digital signal and reads it (high or low voltage).
//
// // Code conversion demo: from machine.Pin to drivers.PinInput
// input := machine.LED
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput
// var pin drivers.PinInput = input.Get // Going from a machine.Pin to a drivers.PinInput
type PinInput func() (level bool)
+4 -4
View File
@@ -25,7 +25,7 @@ type Device struct {
csPin drivers.PinOutput
enPin drivers.PinOutput
rwPin drivers.PinOutput
config func()
configurePins func()
width int16
height int16
rowOffset int16
@@ -50,7 +50,7 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput)
csPin: csPin.Set,
enPin: enPin.Set,
rwPin: rwPin.Set,
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
@@ -62,7 +62,7 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput)
// Configure initializes the display with default configuration
func (d *Device) Configure(cfg Config) {
if d.config == nil {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
if cfg.Width == 0 {
@@ -84,7 +84,7 @@ func (d *Device) Configure(cfg Config) {
}
// configure GPIO pins
d.config()
d.configurePins()
// reset the device
d.resetPin(true)
+6 -6
View File
@@ -30,7 +30,7 @@ type Device struct {
dc drivers.PinOutput
rst drivers.PinOutput
busy drivers.PinInput
config func()
configurePins func()
buffer []uint8
rotation Rotation
}
@@ -90,7 +90,7 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy
dc: dcPin.Set,
rst: rstPin.Set,
busy: busyPin.Get,
config: func() {
configurePins: func() {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -100,10 +100,10 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy
}
func (d *Device) LDirInit(cfg Config) {
if d.config == nil {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.config()
d.configurePins()
d.bus.Configure(machine.SPIConfig{
Frequency: 2000000,
@@ -159,10 +159,10 @@ func (d *Device) LDirInit(cfg Config) {
}
func (d *Device) HDirInit(cfg Config) {
if d.config == nil {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.config()
d.configurePins()
d.bus.Configure(machine.SPIConfig{
Frequency: 2000000,