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)
}})
+6 -6
View File
@@ -11,18 +11,18 @@ import (
// 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.
type bbSPI struct {
SCK drivers.PinOutput
SDO drivers.PinOutput
Delay uint32
config func()
SCK drivers.PinOutput
SDO drivers.PinOutput
Delay uint32
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 {
+5 -5
View File
@@ -16,8 +16,8 @@ type DeviceSPI struct {
buf [7]byte
// SPI bus (requires chip select to be usable).
bus drivers.SPI
config func()
bus drivers.SPI
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
+7 -7
View File
@@ -12,10 +12,10 @@ import (
// Device wraps FT6336 I2C Self-Capacitive touch
type Device struct {
bus drivers.I2C
buf []byte
Address uint8
config func()
bus drivers.I2C
buf []byte
Address uint8
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
}
+6 -6
View File
@@ -15,9 +15,9 @@ const TIMEOUT = 23324 // max sensing distance (4m)
// Device holds the pins
type Device struct {
trigger drivers.PinOutput
echo drivers.PinInput
config func()
trigger drivers.PinOutput
echo drivers.PinInput
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
+6 -6
View File
@@ -8,9 +8,9 @@ import (
)
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
config func()
bus drivers.SPI
cs drivers.PinOutput
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.
+8 -8
View File
@@ -16,11 +16,11 @@ import (
// Device wraps MCP2515 SPI CAN Module.
type Device struct {
spi SPI
cs drivers.PinOutput
msg *CANMsg
mcpMode byte
config func()
spi SPI
cs drivers.PinOutput
msg *CANMsg
mcpMode byte
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)
+15 -15
View File
@@ -19,18 +19,18 @@ var (
// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
enPin drivers.PinOutput
rwPin drivers.PinOutput
config func()
width int16
height int16
rowOffset int16
columnOffset int16
bufferLength int16
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
enPin drivers.PinOutput
rwPin drivers.PinOutput
configurePins func()
width int16
height int16
rowOffset int16
columnOffset int16
bufferLength int16
}
// 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,
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)
+13 -13
View File
@@ -25,14 +25,14 @@ type Config struct {
}
type Device struct {
bus *machine.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
busy drivers.PinInput
config func()
buffer []uint8
rotation Rotation
bus *machine.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
busy drivers.PinInput
configurePins func()
buffer []uint8
rotation Rotation
}
type Rotation uint8
@@ -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,