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 // NewSoftwareSPI returns a new APA102 driver that will use a software based
// implementation of the SPI protocol. // implementation of the SPI protocol.
func NewSoftwareSPI(sckPin, sdoPin legacy.PinOutput, delay uint32) *Device { 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(sckPin)
legacy.ConfigurePinOut(sdoPin) legacy.ConfigurePinOut(sdoPin)
}}) }})
+6 -6
View File
@@ -11,18 +11,18 @@ import (
// most purposes other than the APA102 package. It might be desirable to make // most purposes other than the APA102 package. It might be desirable to make
// this more generic and include it in the TinyGo "machine" package instead. // this more generic and include it in the TinyGo "machine" package instead.
type bbSPI struct { type bbSPI struct {
SCK drivers.PinOutput SCK drivers.PinOutput
SDO drivers.PinOutput SDO drivers.PinOutput
Delay uint32 Delay uint32
config func() configurePins func()
} }
// Configure sets up the SCK and SDO pins as outputs and sets them low // Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() { func (s *bbSPI) Configure() {
if s.config == nil { if s.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
s.config() s.configurePins()
s.SCK(false) s.SCK(false)
s.SDO(false) s.SDO(false)
if s.Delay == 0 { if s.Delay == 0 {
+5 -5
View File
@@ -16,8 +16,8 @@ type DeviceSPI struct {
buf [7]byte buf [7]byte
// SPI bus (requires chip select to be usable). // SPI bus (requires chip select to be usable).
bus drivers.SPI bus drivers.SPI
config func() configurePins func()
} }
// NewSPI returns a new device driver. The pin and SPI interface are not // 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{ return &DeviceSPI{
csb: csb.Set, // chip select csb: csb.Set, // chip select
bus: spi, bus: spi,
config: func() { configurePins: func() {
legacy.ConfigurePinOut(csb) 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 // configures the BMI160, but it does not configure the SPI interface (it is
// assumed to be up and running). // assumed to be up and running).
func (d *DeviceSPI) Configure() error { func (d *DeviceSPI) Configure() error {
if d.config == nil { if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated return legacy.ErrConfigBeforeInstantiated
} }
d.config() d.configurePins()
d.csb(true) d.csb(true)
// The datasheet recommends doing a register read from address 0x7F to get // The datasheet recommends doing a register read from address 0x7F to get
+7 -7
View File
@@ -12,10 +12,10 @@ import (
// Device wraps FT6336 I2C Self-Capacitive touch // Device wraps FT6336 I2C Self-Capacitive touch
type Device struct { type Device struct {
bus drivers.I2C bus drivers.I2C
buf []byte buf []byte
Address uint8 Address uint8
config func() configurePins func()
} }
// New returns FT6336 device for the provided I2C bus using default address. // 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, bus: i2c,
buf: make([]byte, 11), buf: make([]byte, 11),
Address: Address, Address: Address,
config: func() { configurePins: func() {
legacy.ConfigurePinInputPulldown(intPin) legacy.ConfigurePinInputPulldown(intPin)
}, },
} }
@@ -36,11 +36,11 @@ type Config struct {
// Configure the FT6336 device. // Configure the FT6336 device.
func (d *Device) Configure(config Config) error { func (d *Device) Configure(config Config) error {
if d.config == nil { if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated return legacy.ErrConfigBeforeInstantiated
} }
d.write1Byte(0xA4, 0x00) d.write1Byte(0xA4, 0x00)
d.config() d.configurePins()
return nil return nil
} }
+6 -6
View File
@@ -15,9 +15,9 @@ const TIMEOUT = 23324 // max sensing distance (4m)
// Device holds the pins // Device holds the pins
type Device struct { type Device struct {
trigger drivers.PinOutput trigger drivers.PinOutput
echo drivers.PinInput echo drivers.PinInput
config func() configurePins func()
} }
// New returns a new ultrasonic driver given 2 pins // New returns a new ultrasonic driver given 2 pins
@@ -25,7 +25,7 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
return Device{ return Device{
trigger: trigger.Set, trigger: trigger.Set,
echo: echo.Get, echo: echo.Get,
config: func() { configurePins: func() {
legacy.ConfigurePinOut(trigger) legacy.ConfigurePinOut(trigger)
legacy.ConfigurePinInput(echo) legacy.ConfigurePinInput(echo)
}, },
@@ -34,10 +34,10 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
// Configure configures the pins of the Device // Configure configures the pins of the Device
func (d *Device) Configure() { func (d *Device) Configure() {
if d.config == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
d.config() d.configurePins()
} }
// ReadDistance returns the distance of the object in mm // ReadDistance returns the distance of the object in mm
+6 -6
View File
@@ -8,9 +8,9 @@ import (
) )
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutput
config func() configurePins func()
} }
// NewDriver creates a new max7219 connection. The SPI wire must already be configured // 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{ return &Device{
bus: bus, bus: bus,
cs: cs.Set, cs: cs.Set,
config: func() { configurePins: func() {
legacy.ConfigurePinOut(cs) legacy.ConfigurePinOut(cs)
}, },
} }
@@ -28,10 +28,10 @@ func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
// Configure setups the pins. // Configure setups the pins.
func (driver *Device) Configure() { func (driver *Device) Configure() {
if driver.config == nil { if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
driver.config() driver.configurePins()
} }
// SetScanLimit sets the scan limit. Maximum is 8. // SetScanLimit sets the scan limit. Maximum is 8.
+8 -8
View File
@@ -16,11 +16,11 @@ import (
// Device wraps MCP2515 SPI CAN Module. // Device wraps MCP2515 SPI CAN Module.
type Device struct { type Device struct {
spi SPI spi SPI
cs drivers.PinOutput cs drivers.PinOutput
msg *CANMsg msg *CANMsg
mcpMode byte mcpMode byte
config func() configurePins func()
} }
// CANMsg stores CAN message fields. // CANMsg stores CAN message fields.
@@ -46,7 +46,7 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
}, },
cs: csPin.Set, cs: csPin.Set,
msg: &CANMsg{}, msg: &CANMsg{},
config: func() { configurePins: func() {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
}, },
} }
@@ -56,10 +56,10 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
// Configure sets up the device for communication. // Configure sets up the device for communication.
func (d *Device) Configure() { func (d *Device) Configure() {
if d.config == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
d.config() d.configurePins()
} }
const beginTimeoutValue int = 10 const beginTimeoutValue int = 10
+10
View File
@@ -2,8 +2,18 @@ package drivers
// PinOutput is hardware abstraction for a pin which outputs a // PinOutput is hardware abstraction for a pin which outputs a
// digital signal (high or low voltage). // 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) type PinOutput func(level bool)
// PinInput is hardware abstraction for a pin which receives a // PinInput is hardware abstraction for a pin which receives a
// digital signal and reads it (high or low voltage). // 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) type PinInput func() (level bool)
+15 -15
View File
@@ -19,18 +19,18 @@ var (
// Device wraps an SPI connection. // Device wraps an SPI connection.
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
dcPin drivers.PinOutput dcPin drivers.PinOutput
resetPin drivers.PinOutput resetPin drivers.PinOutput
csPin drivers.PinOutput csPin drivers.PinOutput
enPin drivers.PinOutput enPin drivers.PinOutput
rwPin drivers.PinOutput rwPin drivers.PinOutput
config func() configurePins func()
width int16 width int16
height int16 height int16
rowOffset int16 rowOffset int16
columnOffset int16 columnOffset int16
bufferLength int16 bufferLength int16
} }
// Config is the configuration for the display // Config is the configuration for the display
@@ -50,7 +50,7 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput)
csPin: csPin.Set, csPin: csPin.Set,
enPin: enPin.Set, enPin: enPin.Set,
rwPin: rwPin.Set, rwPin: rwPin.Set,
config: func() { configurePins: func() {
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin) 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 // Configure initializes the display with default configuration
func (d *Device) Configure(cfg Config) { func (d *Device) Configure(cfg Config) {
if d.config == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
if cfg.Width == 0 { if cfg.Width == 0 {
@@ -84,7 +84,7 @@ func (d *Device) Configure(cfg Config) {
} }
// configure GPIO pins // configure GPIO pins
d.config() d.configurePins()
// reset the device // reset the device
d.resetPin(true) d.resetPin(true)
+13 -13
View File
@@ -25,14 +25,14 @@ type Config struct {
} }
type Device struct { type Device struct {
bus *machine.SPI bus *machine.SPI
cs drivers.PinOutput cs drivers.PinOutput
dc drivers.PinOutput dc drivers.PinOutput
rst drivers.PinOutput rst drivers.PinOutput
busy drivers.PinInput busy drivers.PinInput
config func() configurePins func()
buffer []uint8 buffer []uint8
rotation Rotation rotation Rotation
} }
type Rotation uint8 type Rotation uint8
@@ -90,7 +90,7 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy
dc: dcPin.Set, dc: dcPin.Set,
rst: rstPin.Set, rst: rstPin.Set,
busy: busyPin.Get, busy: busyPin.Get,
config: func() { configurePins: func() {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) 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) { func (d *Device) LDirInit(cfg Config) {
if d.config == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
d.config() d.configurePins()
d.bus.Configure(machine.SPIConfig{ d.bus.Configure(machine.SPIConfig{
Frequency: 2000000, Frequency: 2000000,
@@ -159,10 +159,10 @@ func (d *Device) LDirInit(cfg Config) {
} }
func (d *Device) HDirInit(cfg Config) { func (d *Device) HDirInit(cfg Config) {
if d.config == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(legacy.ErrConfigBeforeInstantiated)
} }
d.config() d.configurePins()
d.bus.Configure(machine.SPIConfig{ d.bus.Configure(machine.SPIConfig{
Frequency: 2000000, Frequency: 2000000,