From c815f2560e1f83c7a268155e557b1a583a7cefaa Mon Sep 17 00:00:00 2001 From: Patricio Whittingslow Date: Tue, 16 Sep 2025 09:26:00 -0300 Subject: [PATCH] apply @gen2thomas suggestions --- apa102/apa102.go | 2 +- apa102/softspi.go | 12 ++++---- bmi160/bmi160.go | 10 +++---- ...per_baremetal.go => easystepper_tinygo.go} | 0 ft6336/ft6336.go | 14 ++++----- hcsr04/hcsr04.go | 12 ++++---- .../legacy/{pinhal_os.go => pinhal_go.go} | 0 .../{pinhal_baremetal.go => pinhal_tinygo.go} | 0 max72xx/max72xx.go | 12 ++++---- mcp2515/mcp2515.go | 16 +++++----- pin.go | 10 +++++++ ssd1351/ssd1351.go | 30 +++++++++---------- waveshare-epd/epd1in54/epd1in54.go | 26 ++++++++-------- 13 files changed, 77 insertions(+), 67 deletions(-) rename easystepper/{easystepper_baremetal.go => easystepper_tinygo.go} (100%) rename internal/legacy/{pinhal_os.go => pinhal_go.go} (100%) rename internal/legacy/{pinhal_baremetal.go => pinhal_tinygo.go} (100%) diff --git a/apa102/apa102.go b/apa102/apa102.go index f383f73..9de5e63 100644 --- a/apa102/apa102.go +++ b/apa102/apa102.go @@ -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) }}) diff --git a/apa102/softspi.go b/apa102/softspi.go index abb4be5..48dcf99 100644 --- a/apa102/softspi.go +++ b/apa102/softspi.go @@ -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 { diff --git a/bmi160/bmi160.go b/bmi160/bmi160.go index 7898e30..9649377 100644 --- a/bmi160/bmi160.go +++ b/bmi160/bmi160.go @@ -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 diff --git a/easystepper/easystepper_baremetal.go b/easystepper/easystepper_tinygo.go similarity index 100% rename from easystepper/easystepper_baremetal.go rename to easystepper/easystepper_tinygo.go diff --git a/ft6336/ft6336.go b/ft6336/ft6336.go index 09e2ad2..4a8ad8f 100644 --- a/ft6336/ft6336.go +++ b/ft6336/ft6336.go @@ -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 } diff --git a/hcsr04/hcsr04.go b/hcsr04/hcsr04.go index 77c56ee..e9129a2 100644 --- a/hcsr04/hcsr04.go +++ b/hcsr04/hcsr04.go @@ -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 diff --git a/internal/legacy/pinhal_os.go b/internal/legacy/pinhal_go.go similarity index 100% rename from internal/legacy/pinhal_os.go rename to internal/legacy/pinhal_go.go diff --git a/internal/legacy/pinhal_baremetal.go b/internal/legacy/pinhal_tinygo.go similarity index 100% rename from internal/legacy/pinhal_baremetal.go rename to internal/legacy/pinhal_tinygo.go diff --git a/max72xx/max72xx.go b/max72xx/max72xx.go index 19232b5..d1fc0a6 100644 --- a/max72xx/max72xx.go +++ b/max72xx/max72xx.go @@ -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. diff --git a/mcp2515/mcp2515.go b/mcp2515/mcp2515.go index db520ea..4f5bcc8 100644 --- a/mcp2515/mcp2515.go +++ b/mcp2515/mcp2515.go @@ -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 diff --git a/pin.go b/pin.go index ae81ac1..5d569a1 100644 --- a/pin.go +++ b/pin.go @@ -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) diff --git a/ssd1351/ssd1351.go b/ssd1351/ssd1351.go index 072b048..d17b4da 100644 --- a/ssd1351/ssd1351.go +++ b/ssd1351/ssd1351.go @@ -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) diff --git a/waveshare-epd/epd1in54/epd1in54.go b/waveshare-epd/epd1in54/epd1in54.go index 86c7d9b..38e4702 100644 --- a/waveshare-epd/epd1in54/epd1in54.go +++ b/waveshare-epd/epd1in54/epd1in54.go @@ -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,