feature: some additional changes to drivers.Pin HAL for clarity and readability.

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2025-09-20 08:29:32 +02:00
parent 97ed81556b
commit d224b5d648
33 changed files with 139 additions and 171 deletions
+1 -2
View File
@@ -8,7 +8,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
const ( const (
@@ -38,7 +37,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 pin.Output, delay uint32) *Device { func NewSoftwareSPI(sckPin, sdoPin drivers.PinOutput, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: 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)
+2 -2
View File
@@ -11,8 +11,8 @@ 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.PinOutputFunc
SDO drivers.PinOutput SDO drivers.PinOutputFunc
Delay uint32 Delay uint32
configurePins func() configurePins func()
} }
+2 -3
View File
@@ -5,14 +5,13 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is // DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported. // also an I2C interface, but it is not yet supported.
type DeviceSPI struct { type DeviceSPI struct {
// Chip select pin // Chip select pin
csb drivers.PinOutput csb drivers.PinOutputFunc
buf [7]byte buf [7]byte
@@ -24,7 +23,7 @@ type DeviceSPI struct {
// 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
// touched, provide a fully configured SPI object and call Configure to start // touched, provide a fully configured SPI object and call Configure to start
// using this device. // using this device.
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI { func NewSPI(csb drivers.PinOutput, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{ return &DeviceSPI{
csb: csb.Set, // chip select csb: csb.Set, // chip select
bus: spi, bus: spi,
+2 -3
View File
@@ -5,18 +5,17 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
// Device wraps a GPIO connection to a buzzer. // Device wraps a GPIO connection to a buzzer.
type Device struct { type Device struct {
pin drivers.PinOutput pin drivers.PinOutputFunc
High bool High bool
BPM float64 BPM float64
} }
// New returns a new buzzer driver given which pin to use // New returns a new buzzer driver given which pin to use
func New(pin pin.Output) Device { func New(pin drivers.PinOutput) Device {
return Device{ return Device{
pin: pin.Set, pin: pin.Set,
High: false, High: false,
+1 -1
View File
@@ -32,7 +32,7 @@ func (sm StepMode) stepCount() uint {
// Device holds the pins and the delay between steps // Device holds the pins and the delay between steps
type Device struct { type Device struct {
pins [4]drivers.PinOutput pins [4]drivers.PinOutputFunc
config func() config func()
stepDelay time.Duration stepDelay time.Duration
stepNumber uint8 stepNumber uint8
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
) )
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]drivers.PinOutput) (*Device, error) { func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]drivers.PinOutputFunc) (*Device, error) {
if stepcount == 0 || rpm == 0 { if stepcount == 0 || rpm == 0 {
return nil, errors.New("zero rpm and/or stepcount") return nil, errors.New("zero rpm and/or stepcount")
} }
+1 -1
View File
@@ -17,7 +17,7 @@ func New(config DeviceConfig) (*Device, error) {
return nil, errors.New("config.StepCount and config.RPM must be > 0") return nil, errors.New("config.StepCount and config.RPM must be > 0")
} }
return &Device{ return &Device{
pins: [4]drivers.PinOutput{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set}, pins: [4]drivers.PinOutputFunc{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set},
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)), stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
stepMode: config.Mode, stepMode: config.Mode,
config: func() { config: func() {
+1 -2
View File
@@ -7,7 +7,6 @@ package ft6336
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/touch" "tinygo.org/x/drivers/touch"
) )
@@ -20,7 +19,7 @@ type Device struct {
} }
// New returns FT6336 device for the provided I2C bus using default address. // New returns FT6336 device for the provided I2C bus using default address.
func New(i2c drivers.I2C, intPin pin.Input) *Device { func New(i2c drivers.I2C, intPin drivers.PinInput) *Device {
return &Device{ return &Device{
bus: i2c, bus: i2c,
buf: make([]byte, 11), buf: make([]byte, 11),
+5 -6
View File
@@ -11,7 +11,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
// Rotation controls the rotation used by the display. // Rotation controls the rotation used by the display.
@@ -23,10 +22,10 @@ type FrameRate uint8
// 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.PinOutputFunc
resetPin drivers.PinOutput resetPin drivers.PinOutputFunc
csPin drivers.PinOutput csPin drivers.PinOutputFunc
blPin drivers.PinOutput blPin drivers.PinOutputFunc
width int16 width int16
height int16 height int16
columnOffsetCfg int16 columnOffsetCfg int16
@@ -53,7 +52,7 @@ type Config struct {
} }
// New creates a new ST7789 connection. The SPI wire must already be configured. // New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) Device {
legacy.ConfigurePinOut(resetPin) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
+3 -4
View File
@@ -9,20 +9,19 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
const TIMEOUT = 23324 // max sensing distance (4m) 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.PinOutputFunc
echo drivers.PinInput echo drivers.PinInputFunc
configurePins func() configurePins func()
} }
// New returns a new ultrasonic driver given 2 pins // New returns a new ultrasonic driver given 2 pins
func New(trigger pin.Output, echo pin.Input) Device { func New(trigger drivers.PinOutput, echo drivers.PinInput) Device {
return Device{ return Device{
trigger: trigger.Set, trigger: trigger.Set,
echo: echo.Get, echo: echo.Get,
+5 -5
View File
@@ -3,7 +3,7 @@ package legacy
import ( import (
"errors" "errors"
"tinygo.org/x/drivers/internal/pin" "tinygo.org/x/drivers"
) )
// ConfigurePinOut is a legacy function used to configure pins as outputs. // ConfigurePinOut is a legacy function used to configure pins as outputs.
@@ -11,7 +11,7 @@ import (
// Deprecated: Do not configure pins in drivers. // Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that // This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users. // previously configured pins in initialization to avoid breaking users.
func ConfigurePinOut(po pin.Output) { func ConfigurePinOut(po drivers.PinOutput) {
configurePinOut(po) configurePinOut(po)
} }
@@ -20,7 +20,7 @@ func ConfigurePinOut(po pin.Output) {
// Deprecated: Do not configure pins in drivers. // Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that // This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users. // previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPulldown(pi pin.Input) { func ConfigurePinInputPulldown(pi drivers.PinInput) {
configurePinInputPulldown(pi) configurePinInputPulldown(pi)
} }
@@ -29,7 +29,7 @@ func ConfigurePinInputPulldown(pi pin.Input) {
// Deprecated: Do not configure pins in drivers. // Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that // This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users. // previously configured pins in initialization to avoid breaking users.
func ConfigurePinInput(pi pin.Input) { func ConfigurePinInput(pi drivers.PinInput) {
configurePinInput(pi) configurePinInput(pi)
} }
@@ -38,7 +38,7 @@ func ConfigurePinInput(pi pin.Input) {
// Deprecated: Do not configure pins in drivers. // Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that // This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users. // previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPullup(pi pin.Input) { func ConfigurePinInputPullup(pi drivers.PinInput) {
configurePinInputPullup(pi) configurePinInputPullup(pi)
} }
+8 -6
View File
@@ -2,10 +2,12 @@
package legacy package legacy
import "tinygo.org/x/drivers/internal/pin" import (
"tinygo.org/x/drivers"
)
func configurePinOut(p pin.Output) {} func configurePinOut(p drivers.PinOutput) {}
func configurePinInput(p pin.Input) {} func configurePinInput(p drivers.PinInput) {}
func configurePinInputPulldown(p pin.Input) {} func configurePinInputPulldown(p drivers.PinInput) {}
func configurePinInputPullup(p pin.Input) {} func configurePinInputPullup(p drivers.PinInput) {}
func pinIsNoPin(a any) bool { return false } func pinIsNoPin(a any) bool { return false }
+5 -5
View File
@@ -5,22 +5,22 @@ package legacy
import ( import (
"machine" "machine"
"tinygo.org/x/drivers/internal/pin" "tinygo.org/x/drivers"
) )
func configurePinOut(po pin.Output) { func configurePinOut(po drivers.PinOutput) {
configurePin(po, machine.PinOutput) configurePin(po, machine.PinOutput)
} }
func configurePinInputPulldown(pi pin.Input) { func configurePinInputPulldown(pi drivers.PinInput) {
configurePin(pi, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput. configurePin(pi, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
} }
func configurePinInput(pi pin.Input) { func configurePinInput(pi drivers.PinInput) {
configurePin(pi, machine.PinInput) configurePin(pi, machine.PinInput)
} }
func configurePinInputPullup(pi pin.Input) { func configurePinInputPullup(pi drivers.PinInput) {
configurePin(pi, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput. configurePin(pi, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
} }
-23
View File
@@ -1,23 +0,0 @@
package pin
import "tinygo.org/x/drivers"
// Here to aid relevant documentation links of [drivers.PinOutput] and [drivers.PinInput].
var _ drivers.PinOutput
// Output represents a pin hardware abstraction layer for a pin that can output a digital signal.
//
// This is an alternative to [drivers.PinOutput] abstraction which is a function type and has
// not been standardized as of yet as a standard HAL in the drivers package,
// [discussion ongoing here].
//
// [discussion ongoing here]: https://github.com/orgs/tinygo-org/discussions/5043
type Output interface {
Set(level bool)
}
// Input represents a pin hardware abstraction layer.
// See [Output] for more information on why this type exists separate to drivers.
type Input interface {
Get() (level bool)
}
+2 -3
View File
@@ -5,7 +5,6 @@ import (
"errors" "errors"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
// ErrThermocoupleOpen is returned when the thermocouple input is open. // ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -14,13 +13,13 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
} }
// Create a new Device to read from a MAX6675 thermocouple. // Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI // Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum. // should be 4.3MHz maximum.
func NewDevice(bus drivers.SPI, cs pin.Output) *Device { func NewDevice(bus drivers.SPI, cs drivers.PinOutput) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs.Set, cs: cs.Set,
+2 -3
View File
@@ -5,19 +5,18 @@ package max72xx
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
configurePins 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
// The SPI frequency must not be higher than 10MHz. // The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin. // parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus drivers.SPI, cs pin.Output) *Device { func NewDevice(bus drivers.SPI, cs drivers.PinOutput) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs.Set, cs: cs.Set,
+2 -3
View File
@@ -12,13 +12,12 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
// 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.PinOutputFunc
msg *CANMsg msg *CANMsg
mcpMode byte mcpMode byte
configurePins func() configurePins func()
@@ -38,7 +37,7 @@ const (
) )
// New returns a new MCP2515 driver. Pass in a fully configured SPI bus. // New returns a new MCP2515 driver. Pass in a fully configured SPI bus.
func New(b drivers.SPI, csPin pin.Output) *Device { func New(b drivers.SPI, csPin drivers.PinOutput) *Device {
d := &Device{ d := &Device{
spi: SPI{ spi: SPI{
bus: b, bus: b,
+4 -5
View File
@@ -9,15 +9,14 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
// 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.PinOutputFunc
rstPin drivers.PinOutput rstPin drivers.PinOutputFunc
scePin drivers.PinOutput scePin drivers.PinOutputFunc
buffer []byte buffer []byte
width int16 width int16
height int16 height int16
@@ -30,7 +29,7 @@ type Config struct {
} }
// New creates a new PCD8544 connection. The SPI bus must already be configured. // New creates a new PCD8544 connection. The SPI bus must already be configured.
func New(bus drivers.SPI, dcPin, rstPin, scePin pin.Output) *Device { func New(bus drivers.SPI, dcPin, rstPin, scePin drivers.PinOutput) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
+20 -10
View File
@@ -1,29 +1,39 @@
package drivers package drivers
// PinOutput is hardware abstraction for a pin which outputs a // PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
type PinOutput interface {
Set(level bool)
}
// PinInput represents a pin hardware abstraction layer.
type PinInput interface {
Get() (level bool)
}
// PinOutputFunc is hardware abstraction for a function that causes pin to output a
// digital signal (high or low level). // digital signal (high or low level).
// //
// // Code conversion demo: from machine.Pin to drivers.PinOutput // // Code conversion demo: from machine.Pin to drivers.PinOutputFunc
// led := machine.LED // led := machine.LED
// led.Configure(machine.PinConfig{Mode: machine.PinOutput}) // led.Configure(machine.PinConfig{Mode: machine.PinOutput})
// var pin drivers.PinOutput = led.Set // Going from a machine.Pin to a drivers.PinOutput // var pin drivers.PinOutputFunc = led.Set // Going from a machine.Pin to a drivers.PinOutputFunc
type PinOutput func(level bool) type PinOutputFunc func(level bool)
// High sets the underlying pin's level to high. This is equivalent to calling PinOutput(true). // High sets the underlying pin's level to high. This is equivalent to calling PinOutput(true).
func (po PinOutput) High() { func (po PinOutputFunc) High() {
po(true) po(true)
} }
// Low sets the underlying pin's level to low. This is equivalent to calling PinOutput(false). // Low sets the underlying pin's level to low. This is equivalent to calling PinOutput(false).
func (po PinOutput) Low() { func (po PinOutputFunc) Low() {
po(false) po(false)
} }
// PinInput is hardware abstraction for a pin which receives a // PinInputFunc is hardware abstraction for a pin which receives a
// digital signal and reads it (high or low level). // digital signal and reads it (high or low level).
// //
// // Code conversion demo: from machine.Pin to drivers.PinInput // // Code conversion demo: from machine.Pin to drivers.PinInputFunc
// input := machine.LED // input := machine.LED
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput // 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 // var pin drivers.PinInputFunc = input.Get // Going from a machine.Pin to a drivers.PinInputFunc
type PinInput func() (level bool) type PinInputFunc func() (level bool)
+2 -3
View File
@@ -4,7 +4,6 @@ package shiftregister
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type NumberBit int8 type NumberBit int8
@@ -18,7 +17,7 @@ const (
// Device holds pin number // Device holds pin number
type Device struct { type Device struct {
latch, clock, out drivers.PinOutput // IC wiring latch, clock, out drivers.PinOutputFunc // IC wiring
config func() config func()
bits NumberBit // Pin number bits NumberBit // Pin number
mask uint32 // keep all pins state mask uint32 // keep all pins state
@@ -32,7 +31,7 @@ type ShiftPin struct {
} }
// New returns a new shift output register device // New returns a new shift output register device
func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device { func New(Bits NumberBit, Latch, Clock, Out drivers.PinOutput) *Device {
return &Device{ return &Device{
latch: Latch.Set, latch: Latch.Set,
clock: Clock.Set, clock: Clock.Set,
+4 -4
View File
@@ -17,10 +17,10 @@ type Bus interface {
} }
type Device struct { type Device struct {
rs drivers.PinOutput rs drivers.PinOutputFunc
wr drivers.PinOutput wr drivers.PinOutputFunc
cs drivers.PinOutput cs drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
bus Bus bus Bus
} }
+3 -3
View File
@@ -20,9 +20,9 @@ type Rotation uint8
// 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.PinOutputFunc
resetPin drivers.PinOutput resetPin drivers.PinOutputFunc
csPin drivers.PinOutput csPin drivers.PinOutputFunc
width int16 width int16
height int16 height int16
batchLength int16 batchLength int16
+6 -7
View File
@@ -10,7 +10,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
var ( var (
@@ -21,11 +20,11 @@ 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.PinOutputFunc
resetPin drivers.PinOutput resetPin drivers.PinOutputFunc
csPin drivers.PinOutput csPin drivers.PinOutputFunc
enPin drivers.PinOutput enPin drivers.PinOutputFunc
rwPin drivers.PinOutput rwPin drivers.PinOutputFunc
configurePins func() configurePins func()
width int16 width int16
height int16 height int16
@@ -43,7 +42,7 @@ type Config struct {
} }
// New creates a new SSD1351 connection. The SPI wire must already be configured. // New creates a new SSD1351 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin drivers.PinOutput) Device {
return Device{ return Device{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
+6 -7
View File
@@ -11,7 +11,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -40,10 +39,10 @@ type Device = DeviceOf[pixel.RGB565BE]
// formats. // formats.
type DeviceOf[T Color] struct { type DeviceOf[T Color] struct {
bus drivers.SPI bus drivers.SPI
dcPin drivers.PinOutput dcPin drivers.PinOutputFunc
resetPin drivers.PinOutput resetPin drivers.PinOutputFunc
csPin drivers.PinOutput csPin drivers.PinOutputFunc
blPin drivers.PinOutput blPin drivers.PinOutputFunc
width int16 width int16
height int16 height int16
columnOffset int16 columnOffset int16
@@ -66,13 +65,13 @@ type Config struct {
} }
// New creates a new ST7735 connection. The SPI wire must already be configured. // New creates a new ST7735 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin) return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
} }
// NewOf creates a new ST7735 connection with a particular pixel format. The SPI // NewOf creates a new ST7735 connection with a particular pixel format. The SPI
// wire must already be configured. // wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] { func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
+7 -8
View File
@@ -14,7 +14,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -47,10 +46,10 @@ type Device = DeviceOf[pixel.RGB565BE]
// formats. // formats.
type DeviceOf[T Color] struct { type DeviceOf[T Color] struct {
bus drivers.SPI bus drivers.SPI
dcPin drivers.PinOutput dcPin drivers.PinOutputFunc
resetPin drivers.PinOutput resetPin drivers.PinOutputFunc
csPin drivers.PinOutput csPin drivers.PinOutputFunc
blPin drivers.PinOutput blPin drivers.PinOutputFunc
width int16 width int16
height int16 height int16
columnOffsetCfg int16 columnOffsetCfg int16
@@ -84,18 +83,18 @@ type Config struct {
} }
// New creates a new ST7789 connection. The SPI wire must already be configured. // New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin) return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
} }
// NewOf creates a new ST7789 connection with a particular pixel format. The SPI // NewOf creates a new ST7789 connection with a particular pixel format. The SPI
// wire must already be configured. // wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] { func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(blPin) legacy.ConfigurePinOut(blPin)
var cs drivers.PinOutput var cs drivers.PinOutputFunc
if !legacy.PinIsNoPin(csPin) { if !legacy.PinIsNoPin(csPin) {
cs = csPin.Set cs = csPin.Set
} }
+10 -11
View File
@@ -9,7 +9,6 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/lora" "tinygo.org/x/drivers/lora"
) )
@@ -21,15 +20,15 @@ const (
// Device wraps an SPI connection to a SX127x device. // Device wraps an SPI connection to a SX127x device.
type Device struct { type Device struct {
spi drivers.SPI // SPI bus for module communication spi drivers.SPI // SPI bus for module communication
rstPin drivers.PinOutput // GPIO for reset rstPin drivers.PinOutputFunc // GPIO for reset
radioEventChan chan lora.RadioEvent // Channel for Receiving events radioEventChan chan lora.RadioEvent // Channel for Receiving events
loraConf lora.Config // Current Lora configuration loraConf lora.Config // Current Lora configuration
controller RadioController // to manage interactions with the radio controller RadioController // to manage interactions with the radio
deepSleep bool // Internal Sleep state deepSleep bool // Internal Sleep state
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261) deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
spiTxBuf []byte // global Tx buffer to avoid heap allocations in interrupt spiTxBuf []byte // global Tx buffer to avoid heap allocations in interrupt
spiRxBuf []byte // global Rx buffer to avoid heap allocations in interrupt spiRxBuf []byte // global Rx buffer to avoid heap allocations in interrupt
} }
// -------------------------------------------------- // --------------------------------------------------
@@ -43,7 +42,7 @@ func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
} }
// New creates a new SX127x connection. The SPI bus must already be configured. // New creates a new SX127x connection. The SPI bus must already be configured.
func New(spi drivers.SPI, rstPin pin.Output) *Device { func New(spi drivers.SPI, rstPin drivers.PinOutput) *Device {
k := Device{ k := Device{
spi: spi, spi: spi,
rstPin: rstPin.Set, rstPin: rstPin.Set,
+5 -6
View File
@@ -12,7 +12,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -32,10 +31,10 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
width int16 width int16
height int16 height int16
buffer []uint8 buffer []uint8
@@ -50,7 +49,7 @@ type Device struct {
type Speed uint8 type Speed uint8
// New returns a new uc8151 driver. Pass in a fully configured SPI bus. // New returns a new uc8151 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) legacy.ConfigurePinOut(rstPin)
+5 -6
View File
@@ -15,7 +15,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -27,10 +26,10 @@ type Config struct {
type Device struct { type Device struct {
bus *machine.SPI bus *machine.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
configurePins func() configurePins func()
buffer []uint8 buffer []uint8
rotation Rotation rotation Rotation
@@ -83,7 +82,7 @@ var partialRefresh = [159]uint8{
} }
// New returns a new epd1in54 driver. Pass in a fully configured SPI bus. // New returns a new epd1in54 driver. Pass in a fully configured SPI bus.
func New(bus *machine.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus *machine.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
return Device{ return Device{
buffer: make([]uint8, (uint32(Width)*uint32(Height))/8), buffer: make([]uint8, (uint32(Width)*uint32(Height))/8),
bus: bus, bus: bus,
+5 -6
View File
@@ -10,7 +10,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -22,10 +21,10 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
logicalWidth int16 logicalWidth int16
width int16 width int16
height int16 height int16
@@ -54,7 +53,7 @@ var lutPartialUpdate = [30]uint8{
} }
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus. // New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) legacy.ConfigurePinOut(rstPin)
+5 -6
View File
@@ -10,7 +10,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -21,10 +20,10 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
width int16 width int16
height int16 height int16
buffer [][]uint8 buffer [][]uint8
@@ -34,7 +33,7 @@ type Device struct {
type Color uint8 type Color uint8
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus. // New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) legacy.ConfigurePinOut(rstPin)
+4 -4
View File
@@ -19,10 +19,10 @@ const Baudrate = 4_000_000 // 4 MHz
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
blackBuffer []byte blackBuffer []byte
redBuffer []byte redBuffer []byte
+5 -6
View File
@@ -17,7 +17,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -29,10 +28,10 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
logicalWidth int16 logicalWidth int16
width int16 width int16
height int16 height int16
@@ -62,7 +61,7 @@ var lutPartialUpdate = [30]uint8{
} }
// New returns a new epd2in9 driver. Pass in a fully configured SPI bus. // New returns a new epd2in9 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) legacy.ConfigurePinOut(rstPin)
+5 -6
View File
@@ -14,7 +14,6 @@ import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -26,10 +25,10 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs drivers.PinOutputFunc
dc drivers.PinOutput dc drivers.PinOutputFunc
rst drivers.PinOutput rst drivers.PinOutputFunc
isBusy drivers.PinInput isBusy drivers.PinInputFunc
logicalWidth int16 logicalWidth int16
width int16 width int16
height int16 height int16
@@ -41,7 +40,7 @@ type Device struct {
type Rotation uint8 type Rotation uint8
// New returns a new epd4in2 driver. Pass in a fully configured SPI bus. // New returns a new epd4in2 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin drivers.PinOutput, busyPin drivers.PinInput) Device {
legacy.ConfigurePinOut(csPin) legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin) legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin) legacy.ConfigurePinOut(rstPin)