revision: modify proposed pin HAL.

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2025-09-16 20:33:50 +02:00
parent f55c0b1853
commit dcf53ac04a
38 changed files with 554 additions and 554 deletions
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"image/color"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
const (
@@ -37,10 +37,10 @@ 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 {
func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin)
legacy.ConfigurePinOut(sdoPin)
pin.ConfigureOutput(sckPin)
pin.ConfigureOutput(sdoPin)
}})
}
+10 -11
View File
@@ -1,8 +1,7 @@
package apa102
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
@@ -11,8 +10,8 @@ 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
SCK pin.OutputFunc
SDO pin.OutputFunc
Delay uint32
configurePins func()
}
@@ -20,11 +19,11 @@ type bbSPI struct {
// Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() {
if s.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
s.configurePins()
s.SCK(false)
s.SDO(false)
s.SCK.Low()
s.SDO.Low()
if s.Delay == 0 {
s.Delay = 1
}
@@ -53,19 +52,19 @@ func (s *bbSPI) Transfer(b byte) (byte, error) {
for i := uint8(0); i < 8; i++ {
// half clock cycle high to start
s.SCK(true)
s.SCK.High()
s.delay()
// write the value to SDO (MSB first)
if b&(1<<(7-i)) == 0 {
s.SDO(false)
s.SDO.Low()
} else {
s.SDO(true)
s.SDO.High()
}
s.delay()
// half clock cycle low
s.SCK(false)
s.SCK.Low()
s.delay()
// for actual SPI would try to read the SDI value here
+16 -16
View File
@@ -4,14 +4,14 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported.
type DeviceSPI struct {
// Chip select pin
csb drivers.PinOutput
csb pin.OutputFunc
buf [7]byte
@@ -23,12 +23,12 @@ type DeviceSPI struct {
// 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
// using this device.
func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
csb: csb.Set, // chip select
bus: spi,
configurePins: func() {
legacy.ConfigurePinOut(csb)
pin.ConfigureOutput(csb)
},
}
}
@@ -38,10 +38,10 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
// assumed to be up and running).
func (d *DeviceSPI) Configure() error {
if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
return pin.ErrConfigBeforeInstantiated
}
d.configurePins()
d.csb(true)
d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
@@ -93,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -130,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -160,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -208,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.csb(false)
d.csb.Low()
d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
return data[1]
}
@@ -224,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address
buf[1] = data
d.csb(false)
d.csb.Low()
d.bus.Tx(buf, buf)
d.csb(true)
d.csb.High()
}
+5 -6
View File
@@ -4,19 +4,18 @@ package buzzer // import "tinygo.org/x/drivers/buzzer"
import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps a GPIO connection to a buzzer.
type Device struct {
pin drivers.PinOutput
pin pin.OutputFunc
High bool
BPM float64
}
// New returns a new buzzer driver given which pin to use
func New(pin legacy.PinOutput) Device {
func New(pin pin.Output) Device {
return Device{
pin: pin.Set,
High: false,
@@ -26,14 +25,14 @@ func New(pin legacy.PinOutput) Device {
// On sets the buzzer to a high state.
func (l *Device) On() (err error) {
l.pin(true)
l.pin.High()
l.High = true
return
}
// Off sets the buzzer to a low state.
func (l *Device) Off() (err error) {
l.pin(false)
l.pin.Low()
l.High = false
return
}
+52 -52
View File
@@ -4,7 +4,7 @@ package easystepper // import "tinygo.org/x/drivers/easystepper"
import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)
// StepMode determines the coil sequence used to perform a single step
@@ -32,7 +32,7 @@ func (sm StepMode) stepCount() uint {
// Device holds the pins and the delay between steps
type Device struct {
pins [4]drivers.PinOutput
pins [4]pin.OutputFunc
config func()
stepDelay time.Duration
stepNumber uint8
@@ -62,8 +62,8 @@ func (d *Device) Move(steps int32) {
// Off turns off all motor pins
func (d *Device) Off() {
for _, pin := range d.pins {
pin(false)
for _, p := range d.pins {
p.Low()
}
}
@@ -124,28 +124,28 @@ func (d *Device) stepMotor(step uint8) {
func (d *Device) stepMotor4(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[1](false)
d.pins[2](true)
d.pins[3](false)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].High()
d.pins[3].Low()
break
case 1:
d.pins[0](false)
d.pins[1](true)
d.pins[2](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].High()
d.pins[3].Low()
break
case 2:
d.pins[0](false)
d.pins[1](true)
d.pins[2](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].Low()
d.pins[3].High()
break
case 3:
d.pins[0](true)
d.pins[1](false)
d.pins[2](false)
d.pins[3](true)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].Low()
d.pins[3].High()
break
}
d.stepNumber = step
@@ -155,45 +155,45 @@ func (d *Device) stepMotor4(step uint8) {
func (d *Device) stepMotor8(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].Low()
case 1:
d.pins[0](true)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 2:
d.pins[0](false)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 3:
d.pins[0](false)
d.pins[2](true)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].High()
d.pins[3].Low()
case 4:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].Low()
case 5:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].High()
case 6:
d.pins[0](false)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
case 7:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
}
d.stepNumber = step
}
+6 -3
View File
@@ -4,20 +4,23 @@ import (
"errors"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]drivers.PinOutput) (*Device, error) {
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]pin.Output) (*Device, error) {
if stepcount == 0 || rpm == 0 {
return nil, errors.New("zero rpm and/or stepcount")
}
var ps [4]pin.OutputFunc
for i := range pins {
if pins[i] == nil {
return nil, errors.New("nil pin")
}
ps[i] = pins[i].Set
}
d := &Device{
pins: pins,
pins: ps,
stepDelay: time.Second * 60 / time.Duration((stepcount * rpm)),
stepMode: mode,
config: func() {},
+7 -8
View File
@@ -7,8 +7,7 @@ import (
"machine"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// New returns a new single easystepper driver given a DeviceConfig
@@ -17,14 +16,14 @@ func New(config DeviceConfig) (*Device, error) {
return nil, errors.New("config.StepCount and config.RPM must be > 0")
}
return &Device{
pins: [4]drivers.PinOutput{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set},
pins: [4]pin.OutputFunc{config.Pin1.Set, config.Pin2.Set, config.Pin3.Set, config.Pin4.Set},
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
stepMode: config.Mode,
config: func() {
legacy.ConfigurePinOut(config.Pin1)
legacy.ConfigurePinOut(config.Pin2)
legacy.ConfigurePinOut(config.Pin3)
legacy.ConfigurePinOut(config.Pin4)
pin.ConfigureOutput(config.Pin1)
pin.ConfigureOutput(config.Pin2)
pin.ConfigureOutput(config.Pin3)
pin.ConfigureOutput(config.Pin4)
},
}, nil
}
@@ -32,7 +31,7 @@ func New(config DeviceConfig) (*Device, error) {
// Configure configures the pins of the Device
func (d *Device) Configure() {
if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.config()
}
+4 -3
View File
@@ -7,6 +7,7 @@ package ft6336
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/touch"
)
@@ -19,13 +20,13 @@ type Device struct {
}
// New returns FT6336 device for the provided I2C bus using default address.
func New(i2c drivers.I2C, intPin legacy.PinInput) *Device {
func New(i2c drivers.I2C, intPin pin.Input) *Device {
return &Device{
bus: i2c,
buf: make([]byte, 11),
Address: Address,
configurePins: func() {
legacy.ConfigurePinInputPulldown(intPin)
pin.ConfigureInputPulldown(intPin)
},
}
}
@@ -37,7 +38,7 @@ type Config struct {
// Configure the FT6336 device.
func (d *Device) Configure(config Config) error {
if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
return pin.ErrConfigBeforeInstantiated
}
d.write1Byte(0xA4, 0x00)
d.configurePins()
+20 -20
View File
@@ -10,7 +10,7 @@ import (
"errors"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Rotation controls the rotation used by the display.
@@ -22,10 +22,10 @@ type FrameRate uint8
// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
blPin drivers.PinOutput
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
blPin pin.OutputFunc
width int16
height int16
columnOffsetCfg int16
@@ -52,11 +52,11 @@ type Config struct {
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(blPin)
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
pin.ConfigureOutput(resetPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(blPin)
return Device{
bus: bus,
resetPin: resetPin.Set,
@@ -68,11 +68,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device
// Reset the Device
func (d *Device) Reset() {
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
}
@@ -232,14 +232,14 @@ func (d *Device) Tx(data []byte, isCommand bool) {
// Rx reads data from the display
func (d *Device) Rx(command uint8, data []byte) {
d.dcPin(false)
d.csPin(false)
d.dcPin.Low()
d.csPin.Low()
d.bus.Transfer(command)
d.dcPin(true)
d.dcPin.High()
for i := range data {
data[i], _ = d.bus.Transfer(0xFF)
}
d.csPin(true)
d.csPin.High()
}
// Size returns the current size of the display.
@@ -250,9 +250,9 @@ func (d *Device) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight
func (d *Device) EnableBacklight(enable bool) {
if enable {
d.blPin(true)
d.blPin.High()
} else {
d.blPin(false)
d.blPin.Low()
}
}
@@ -563,5 +563,5 @@ func (d *Device) Configure(cfg Config) {
d.Command(DISPON)
time.Sleep(20 * time.Millisecond)
d.blPin(true)
d.blPin.High()
}
+10 -11
View File
@@ -7,27 +7,26 @@ package hcsr04
import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
const TIMEOUT = 23324 // max sensing distance (4m)
// Device holds the pins
type Device struct {
trigger drivers.PinOutput
echo drivers.PinInput
trigger pin.OutputFunc
echo pin.InputFunc
configurePins func()
}
// New returns a new ultrasonic driver given 2 pins
func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
func New(trigger pin.Output, echo pin.Input) Device {
return Device{
trigger: trigger.Set,
echo: echo.Get,
configurePins: func() {
legacy.ConfigurePinOut(trigger)
legacy.ConfigurePinInput(echo)
pin.ConfigureOutput(trigger)
pin.ConfigureInput(echo)
},
}
}
@@ -35,7 +34,7 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
// Configure configures the pins of the Device
func (d *Device) Configure() {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.configurePins()
}
@@ -54,11 +53,11 @@ func (d *Device) ReadDistance() int32 {
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
func (d *Device) ReadPulse() int32 {
t := time.Now()
d.trigger(false)
d.trigger.Low()
time.Sleep(2 * time.Microsecond)
d.trigger(true)
d.trigger.High()
time.Sleep(10 * time.Microsecond)
d.trigger(false)
d.trigger.Low()
i := uint8(0)
for {
if d.echo() {
-2
View File
@@ -2,8 +2,6 @@
package hts221
import "tinygo.org/x/drivers"
// Configure sets up the HTS221 device for communication.
func (d *Device) Configure() {
// read calibration data
-75
View File
@@ -1,75 +0,0 @@
package legacy
import "errors"
// PinOutput 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. Pros and cons
// of both approaches have been discussed in the [relevant issue]. PinOutput should only be used
// to expose an initialization function of a driver that receives pins of this type. Ideally
// driver developers should also expose the initialization with drivers.Pin type:
//
// func New(p1, p2, p3 legacy.PinOutput) *Device {
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
// }
//
// func NewWithPinfuncs(p1, p2, p3 drivers.PinOutput) *Device {
// return &Device{p1:p1, p2:p2, p3:p3}
// }
//
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
type PinOutput interface {
Set(level bool)
}
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
// more information on why this is "legacy".
type PinInput interface {
Get() (level bool)
}
// ConfigurePinOut is a legacy function used to configure pins as outputs.
//
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinOut(po PinOutput) {
configurePinOut(po)
}
// ConfigurePinInput is a legacy function used to configure pins as inputs.
//
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPulldown(pi PinInput) {
configurePinInputPulldown(pi)
}
// ConfigurePinInput is a legacy function used to configure pins as inputs.
//
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInput(pi PinInput) {
configurePinInput(pi)
}
// ConfigurePinInput is a legacy function used to configure pins as inputs.
//
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPullup(pi PinInput) {
configurePinInputPullup(pi)
}
// PinIsNoPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
//
// Deprecated: Drivers do not require pin knowledge from now on.
func PinIsNoPin(pin any) bool {
return pinIsNoPin(pin)
}
var (
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
)
-9
View File
@@ -1,9 +0,0 @@
//go:build !baremetal
package legacy
func configurePinOut(p PinOutput) {}
func configurePinInput(p PinInput) {}
func configurePinInputPulldown(p PinInput) {}
func configurePinInputPullup(p PinInput) {}
func pinIsNoPin(a any) bool { return false }
-33
View File
@@ -1,33 +0,0 @@
//go:build baremetal
package legacy
import "machine"
func configurePinOut(p PinOutput) {
configurePin(p, machine.PinOutput)
}
func configurePinInputPulldown(p PinInput) {
configurePin(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
}
func configurePinInput(p PinInput) {
configurePin(p, machine.PinInput)
}
func configurePinInputPullup(p PinInput) {
configurePin(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
}
func pinIsNoPin(a any) bool {
p, ok := a.(machine.Pin)
return ok && p == machine.NoPin
}
func configurePin(p any, mode machine.PinMode) {
machinePin, ok := p.(machine.Pin)
if ok {
machinePin.Configure(machine.PinConfig{Mode: mode})
}
}
+98
View File
@@ -0,0 +1,98 @@
package pin
import "errors"
// OutputFunc is hardware abstraction for a pin which outputs a
// digital signal (high or low level).
//
// // Code conversion demo: from machine.Pin to pin.OutputFunc
// led := machine.LED
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
// var p pin.OutputFuncFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
type OutputFunc func(level bool)
func (o OutputFunc) High() {
o(true)
}
func (o OutputFunc) Low() {
o(false)
}
// InputFunc is hardware abstraction for a pin which receives a
// digital signal and reads it (high or low level).
//
// // Code conversion demo: from machine.Pin to pin.InputFunc
// input := machine.LED
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput
// var p pin.InputFunc = input.Get // Going from a machine.Pin to a drivers.PinInput
type InputFunc func() (level bool)
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
// This is an wrapper to pin.OutputFunc abstraction which is a function type.
//
// func New(p1, p2, p3 pin.Output) *Device {
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
// }
//
// func NewWithPinfuncs(p1, p2, p3 pin.OutputFunc) *Device {
// return &Device{p1:p1, p2:p2, p3:p3}
// }
//
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
type Output interface {
Set(level bool)
}
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
// more information on why this is "legacy".
type Input interface {
Get() (level bool)
}
// ConfigureOutput is a legacy function used to configure pins as outputs.
//
// Deprecated: You should not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigureOutput(po Output) {
configureOutput(po)
}
// ConfigureInput is a legacy function used to configure pins as inputs.
//
// Deprecated: You should not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigureInputPulldown(pi Input) {
configureInputPulldown(pi)
}
// ConfigureInput is a legacy function used to configure pins as inputs.
//
// Deprecated: You should not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigureInput(pi Input) {
configureInput(pi)
}
// ConfigureInputPullup is a legacy function used to configure pins as inputs.
//
// Deprecated: You should not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigureInputPullup(pi Input) {
configureInputPullup(pi)
}
// IsNotPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
//
// Deprecated: Drivers should not require pin knowledge.
func IsNotPin(pin any) bool {
return isNotPin(pin)
}
var (
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
)
+9
View File
@@ -0,0 +1,9 @@
//go:build !baremetal
package pin
func configureOutput(p Output) {}
func configureInput(p Input) {}
func configureInputPulldown(p Input) {}
func configureInputPullup(p Input) {}
func isNotPin(a any) bool { return false }
@@ -1,6 +1,6 @@
//go:build baremetal && fe310
package legacy
package pin
import "machine"
@@ -1,6 +1,6 @@
//go:build baremetal && !fe310
package legacy
package pin
import "machine"
+33
View File
@@ -0,0 +1,33 @@
//go:build baremetal
package pin
import "machine"
func configureOutput(p Output) {
configure(p, machine.PinOutput)
}
func configureInputPulldown(p Input) {
configure(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
}
func configureInput(p Input) {
configure(p, machine.PinInput)
}
func configureInputPullup(p Input) {
configure(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
}
func isNotPin(a any) bool {
p, ok := a.(machine.Pin)
return ok && p == machine.NoPin
}
func configure(p any, mode machine.PinMode) {
machinePin, ok := p.(machine.Pin)
if ok {
machinePin.Configure(machine.PinConfig{Mode: mode})
}
}
+5 -5
View File
@@ -5,7 +5,7 @@ import (
"errors"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -14,13 +14,13 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
cs pin.OutputFunc
}
// Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum.
func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs.Set,
@@ -34,11 +34,11 @@ func (d *Device) Read() (float32, error) {
value uint16
)
d.cs(false)
d.cs.Low()
if err := d.bus.Tx([]byte{0, 0}, read); err != nil {
return 0, err
}
d.cs(true)
d.cs.High()
// datasheet: Bit D2 is normally low and goes high if the thermocouple input is open.
if read[1]&0x04 == 0x04 {
+5 -5
View File
@@ -4,24 +4,24 @@ package max72xx
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
cs pin.OutputFunc
configurePins func()
}
// NewDriver creates a new max7219 connection. The SPI wire must already be configured
// The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs.Set,
configurePins: func() {
legacy.ConfigurePinOut(cs)
pin.ConfigureOutput(cs)
},
}
}
@@ -29,7 +29,7 @@ func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
// Configure setups the pins.
func (driver *Device) Configure() {
if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
driver.configurePins()
}
+22 -22
View File
@@ -11,13 +11,13 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps MCP2515 SPI CAN Module.
type Device struct {
spi SPI
cs drivers.PinOutput
cs pin.OutputFunc
msg *CANMsg
mcpMode byte
configurePins func()
@@ -37,7 +37,7 @@ const (
)
// New returns a new MCP2515 driver. Pass in a fully configured SPI bus.
func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
func New(b drivers.SPI, csPin pin.Output) *Device {
d := &Device{
spi: SPI{
bus: b,
@@ -47,7 +47,7 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
cs: csPin.Set,
msg: &CANMsg{},
configurePins: func() {
legacy.ConfigurePinOut(csPin)
pin.ConfigureOutput(csPin)
},
}
@@ -57,7 +57,7 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
// Configure sets up the device for communication.
func (d *Device) Configure() {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.configurePins()
}
@@ -166,9 +166,9 @@ func (d *Device) init(speed, clock byte) error {
// Reset resets mcp2515.
func (d *Device) Reset() error {
d.cs(false)
d.cs.Low()
_, err := d.spi.readWrite(mcpReset)
d.cs(true)
d.cs.High()
// time.Sleep(time.Microsecond * 4)
if err != nil {
return err
@@ -456,8 +456,8 @@ func (d *Device) readMsg() error {
func (d *Device) readRxBuffer(loadAddr uint8) error {
msg := d.msg
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(loadAddr)
if err != nil {
return err
@@ -524,8 +524,8 @@ func (d *Device) getNextFreeTxBuf() (uint8, uint8, error) {
}
func (d *Device) writeCANMsg(bufNum uint8, canid uint32, ext, rtrBit, dlc uint8, data []byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(txSidhToLoad(bufNum))
if err != nil {
return err
@@ -544,7 +544,7 @@ func (d *Device) writeCANMsg(bufNum uint8, canid uint32, ext, rtrBit, dlc uint8,
}
// Since cs.Low and cs.High are executed in d.startTransmission,
// it is necessary to set cs.High once to separate the instruction of mcp2515.
d.cs(true)
d.cs.High()
err = d.startTransmission(bufNum)
if err != nil {
@@ -612,9 +612,9 @@ func (s *SPI) setTxBufData(canid uint32, ext, rtrBit, dlc uint8, data []byte) er
}
func (d *Device) startTransmission(bufNum uint8) error {
d.cs(false)
d.cs.Low()
_, err := d.spi.readWrite(txSidhToRTS(bufNum))
d.cs(true)
d.cs.High()
if err != nil {
return err
}
@@ -708,8 +708,8 @@ func txSidhToLoad(i uint8) uint8 {
}
func (d *Device) setRegister(addr, value byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpWrite)
if err != nil {
return err
@@ -728,8 +728,8 @@ func (d *Device) setRegister(addr, value byte) error {
}
func (d *Device) readRegister(addr byte) (byte, error) {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpRead)
if err != nil {
return 0, err
@@ -747,8 +747,8 @@ func (d *Device) readRegister(addr byte) (byte, error) {
}
func (d *Device) modifyRegister(addr, mask, data byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpBitMod)
if err != nil {
return err
@@ -790,8 +790,8 @@ func (d *Device) requestNewMode(newMode byte) error {
}
func (d *Device) readStatus() (byte, error) {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpReadStatus)
if err != nil {
return 0, err
+11 -11
View File
@@ -9,15 +9,15 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin drivers.PinOutput
rstPin drivers.PinOutput
scePin drivers.PinOutput
dcPin pin.OutputFunc
rstPin pin.OutputFunc
scePin pin.OutputFunc
buffer []byte
width int16
height int16
@@ -30,7 +30,7 @@ type Config struct {
}
// New creates a new PCD8544 connection. The SPI bus must already be configured.
func New(bus drivers.SPI, dcPin, rstPin, scePin legacy.PinOutput) *Device {
func New(bus drivers.SPI, dcPin, rstPin, scePin pin.Output) *Device {
return &Device{
bus: bus,
dcPin: dcPin.Set,
@@ -54,9 +54,9 @@ func (d *Device) Configure(cfg Config) {
d.bufferSize = d.width * d.height / 8
d.buffer = make([]byte, d.bufferSize)
d.rstPin(false)
d.rstPin.Low()
time.Sleep(100 * time.Nanosecond)
d.rstPin(true)
d.rstPin.High()
d.SendCommand(FUNCTIONSET | EXTENDEDINSTRUCTION) // H = 1
d.SendCommand(SETVOP | 0x3f) // 0x3f : Vop6 = 0, Vop5 to Vop0 = 1
d.SendCommand(SETTEMP | 0x03) // Experimentally determined
@@ -91,13 +91,13 @@ func (d *Device) Display() error {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dcPin(false)
d.dcPin.Low()
} else {
d.dcPin(true)
d.dcPin.High()
}
d.scePin(false)
d.scePin.Low()
d.bus.Transfer(data)
d.scePin(true)
d.scePin.High()
}
// SetPixel enables or disables a pixel in the buffer
-19
View File
@@ -1,19 +0,0 @@
package drivers
// PinOutput is hardware abstraction for a pin which outputs a
// digital signal (high or low level).
//
// // 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 level).
//
// // 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)
+7 -8
View File
@@ -2,8 +2,7 @@
package shiftregister
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type NumberBit int8
@@ -17,7 +16,7 @@ const (
// Device holds pin number
type Device struct {
latch, clock, out drivers.PinOutput // IC wiring
latch, clock, out pin.OutputFunc // IC wiring
config func()
bits NumberBit // Pin number
mask uint32 // keep all pins state
@@ -31,16 +30,16 @@ type ShiftPin struct {
}
// New returns a new shift output register device
func New(Bits NumberBit, Latch, Clock, Out legacy.PinOutput) *Device {
func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device {
return &Device{
latch: Latch.Set,
clock: Clock.Set,
out: Out.Set,
bits: Bits,
config: func() {
legacy.ConfigurePinOut(Latch)
legacy.ConfigurePinOut(Clock)
legacy.ConfigurePinOut(Out)
pin.ConfigureOutput(Latch)
pin.ConfigureOutput(Clock)
pin.ConfigureOutput(Out)
},
}
}
@@ -48,7 +47,7 @@ func New(Bits NumberBit, Latch, Clock, Out legacy.PinOutput) *Device {
// Configure set hardware configuration
func (d *Device) Configure() {
if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.latch(true)
}
+29 -31
View File
@@ -5,11 +5,9 @@ package ssd1289
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Bus interface {
@@ -17,17 +15,17 @@ type Bus interface {
}
type Device struct {
rs drivers.PinOutput
wr drivers.PinOutput
cs drivers.PinOutput
rst drivers.PinOutput
rs pin.OutputFunc
wr pin.OutputFunc
cs pin.OutputFunc
rst pin.OutputFunc
bus Bus
}
const width = int16(240)
const height = int16(320)
func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bus) Device {
func New(rs pin.Output, wr pin.Output, cs pin.Output, rst pin.Output, bus Bus) Device {
d := Device{
rs: rs.Set,
wr: wr.Set,
@@ -35,25 +33,25 @@ func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bu
rst: rst.Set,
bus: bus,
}
legacy.ConfigurePinOut(rs)
legacy.ConfigurePinOut(wr)
legacy.ConfigurePinOut(cs)
legacy.ConfigurePinOut(rst)
pin.ConfigureOutput(rs)
pin.ConfigureOutput(wr)
pin.ConfigureOutput(cs)
pin.ConfigureOutput(rst)
cs.Set(true)
rst.Set(true)
wr.Set(true)
d.cs.High()
d.rst.High()
d.wr.High()
return d
}
func (d *Device) lcdWriteCom(cmd Command) {
d.rs(false)
d.rs.Low()
d.lcdWriteBusInt(uint16(cmd))
}
func (d *Device) lcdWriteDataInt(data uint16) {
d.rs(true)
d.rs.High()
d.lcdWriteBusInt(data)
}
@@ -63,8 +61,8 @@ func (d *Device) lcdWriteComData(cmd Command, data uint16) {
}
func (d *Device) tx() {
d.wr(false)
d.wr(true)
d.wr.Low()
d.wr.High()
}
func (d *Device) lcdWriteBusInt(data uint16) {
@@ -73,13 +71,13 @@ func (d *Device) lcdWriteBusInt(data uint16) {
}
func (d *Device) Configure() {
d.rst(true)
d.rst.High()
time.Sleep(time.Millisecond * 5)
d.rst(false)
d.rst.Low()
time.Sleep(time.Millisecond * 15)
d.rst(true)
d.rst.High()
time.Sleep(time.Millisecond * 15)
d.cs(false)
d.cs.Low()
//Power supply setting
d.lcdWriteComData(POWERCONTROL1, 0xA8A4)
@@ -139,7 +137,7 @@ func (d *Device) Configure() {
//MUX 319 --> Number of lines in display
d.lcdWriteComData(DRIVEROUTPUTCONTROL, 0x233F)
d.cs(true)
d.cs.High()
}
@@ -169,25 +167,25 @@ func (d *Device) SetPixel(x, y int16, c color.RGBA) {
encoded := encodeColor(c)
d.cs(false)
d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x), uint16(y))
d.rs(true)
d.rs.High()
d.lcdWriteBusInt(encoded)
d.cs(true)
d.cs.High()
}
func (d *Device) FillRect(x, y, w, h int16, c color.RGBA) {
encoded := encodeColor(c)
d.cs(false)
d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x+(w-1)), uint16(y+(h-1)))
d.rs(true)
d.rs.High()
d.bus.Set(encoded)
for i := int64(0); i < int64(w)*int64(h); i++ {
d.tx()
}
d.cs(true)
d.rs(false)
d.cs.High()
d.rs.Low()
}
+10 -10
View File
@@ -11,7 +11,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Model uint8
@@ -20,9 +20,9 @@ type Rotation uint8
// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
width int16
height int16
batchLength int16
@@ -38,9 +38,9 @@ type Config struct {
// New creates a new SSD1331 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin machine.Pin) Device {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(resetPin)
pin.ConfigureOutput(csPin)
return Device{
bus: bus,
dcPin: dcPin.Set,
@@ -70,11 +70,11 @@ func (d *Device) Configure(cfg Config) {
d.batchData = make([]uint8, d.batchLength*2)
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(200 * time.Millisecond)
// Initialization
+21 -21
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
var (
@@ -20,11 +20,11 @@ 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
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
enPin pin.OutputFunc
rwPin pin.OutputFunc
configurePins func()
width int16
height int16
@@ -42,7 +42,7 @@ type Config struct {
}
// New creates a new SSD1351 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
return Device{
bus: bus,
dcPin: dcPin.Set,
@@ -51,11 +51,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput)
enPin: enPin.Set,
rwPin: rwPin.Set,
configurePins: func() {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(enPin)
legacy.ConfigurePinOut(rwPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(resetPin)
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(enPin)
pin.ConfigureOutput(rwPin)
},
}
}
@@ -63,7 +63,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.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
if cfg.Width == 0 {
cfg.Width = 128
@@ -87,16 +87,16 @@ func (d *Device) Configure(cfg Config) {
d.configurePins()
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(200 * time.Millisecond)
d.rwPin(false)
d.dcPin(false)
d.enPin(true)
d.rwPin.Low()
d.dcPin.Low()
d.enPin.High()
// Initialization
d.Command(SET_COMMAND_LOCK)
@@ -286,9 +286,9 @@ func (d *Device) Data(data uint8) {
// Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin(!isCommand)
d.csPin(false)
d.csPin.Low()
d.bus.Tx(data, nil)
d.csPin(true)
d.csPin.High()
}
// Size returns the current size of the display
+15 -15
View File
@@ -10,7 +10,7 @@ import (
"errors"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -39,10 +39,10 @@ type Device = DeviceOf[pixel.RGB565BE]
// formats.
type DeviceOf[T Color] struct {
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
blPin drivers.PinOutput
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
blPin pin.OutputFunc
width int16
height int16
columnOffset int16
@@ -65,17 +65,17 @@ type Config struct {
}
// New creates a new ST7735 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
}
// NewOf creates a new ST7735 connection with a particular pixel format. The SPI
// wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(blPin)
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(resetPin)
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(blPin)
return DeviceOf[T]{
bus: bus,
dcPin: dcPin.Set,
@@ -114,11 +114,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(5 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(20 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(150 * time.Millisecond)
// Common initialization
@@ -226,7 +226,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.SetRotation(d.rotation)
d.blPin(true)
d.blPin.High()
}
// Display does nothing, there's no buffer as it might be too big for some boards
+25 -25
View File
@@ -13,7 +13,7 @@ import (
"errors"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -46,10 +46,10 @@ type Device = DeviceOf[pixel.RGB565BE]
// formats.
type DeviceOf[T Color] struct {
bus drivers.SPI
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
blPin drivers.PinOutput
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
blPin pin.OutputFunc
width int16
height int16
columnOffsetCfg int16
@@ -83,19 +83,19 @@ type Config struct {
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
}
// NewOf creates a new ST7789 connection with a particular pixel format. The SPI
// wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(blPin)
var cs drivers.PinOutput
if !legacy.PinIsNoPin(csPin) {
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(resetPin)
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(blPin)
var cs pin.OutputFunc
if !pin.IsNotPin(csPin) {
cs = csPin.Set
}
return DeviceOf[T]{
@@ -143,11 +143,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchLength += d.batchLength & 1
// Reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(50 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(50 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(50 * time.Millisecond)
// Common initialization
@@ -213,7 +213,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
time.Sleep(10 * time.Millisecond) //
d.endWrite()
d.blPin(true) // Backlight ON
d.blPin.High() // Backlight ON
}
// Send a command with data to the display. It does not change the chip select
@@ -221,9 +221,9 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
// meaning that data can be sent right away.
func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
d.cmdBuf[0] = command
d.dcPin(false)
d.dcPin.Low()
err := d.bus.Tx(d.cmdBuf[:1], nil)
d.dcPin(true)
d.dcPin.High()
if len(data) != 0 {
err = d.bus.Tx(data, nil)
}
@@ -234,7 +234,7 @@ func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
// chip select pin low.
func (d *DeviceOf[T]) startWrite() {
if d.csPin != nil {
d.csPin(false)
d.csPin.Low()
}
}
@@ -242,7 +242,7 @@ func (d *DeviceOf[T]) startWrite() {
// select pin high.
func (d *DeviceOf[T]) endWrite() {
if d.csPin != nil {
d.csPin(true)
d.csPin.High()
}
}
@@ -304,9 +304,9 @@ func (d *DeviceOf[T]) SyncToScanLine(scanline uint16) {
func (d *DeviceOf[T]) GetScanLine() uint16 {
d.startWrite()
data := []uint8{0x00, 0x00}
d.dcPin(false)
d.dcPin.Low()
d.bus.Transfer(GSCAN)
d.dcPin(true)
d.dcPin.High()
for i := range data {
data[i], _ = d.bus.Transfer(0xFF)
}
@@ -545,9 +545,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight
func (d *DeviceOf[T]) EnableBacklight(enable bool) {
if enable {
d.blPin(true)
d.blPin.High()
} else {
d.blPin(false)
d.blPin.Low()
}
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/lora"
)
@@ -22,7 +22,7 @@ const (
// Device wraps an SPI connection to a SX127x device.
type Device struct {
spi drivers.SPI // SPI bus for module communication
rstPin drivers.PinOutput // GPIO for reset
rstPin pin.OutputFunc // GPIO for reset
radioEventChan chan lora.RadioEvent // Channel for Receiving events
loraConf lora.Config // Current Lora configuration
controller RadioController // to manage interactions with the radio
@@ -43,7 +43,7 @@ func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
}
// New creates a new SX127x connection. The SPI bus must already be configured.
func New(spi drivers.SPI, rstPin legacy.PinOutput) *Device {
func New(spi drivers.SPI, rstPin pin.Output) *Device {
k := Device{
spi: spi,
rstPin: rstPin.Set,
+17 -17
View File
@@ -11,7 +11,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -31,9 +31,9 @@ type Config struct {
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
width int16
height int16
@@ -49,11 +49,11 @@ type Device struct {
type Speed uint8
// New returns a new uc8151 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
return Device{
bus: bus,
cs: csPin.Set,
@@ -133,9 +133,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(10 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(10 * time.Millisecond)
d.WaitUntilIdle()
}
@@ -152,18 +152,18 @@ func (d *Device) PowerOn() {
// SendCommand sends a command to the display
func (d *Device) SendCommand(command uint8) {
d.dc(false)
d.cs(false)
d.dc.Low()
d.cs.Low()
d.bus.Transfer(command)
d.cs(true)
d.cs.High()
}
// SendData sends a data byte to the display
func (d *Device) SendData(data ...uint8) {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
d.bus.Tx(data, nil)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
+19 -19
View File
@@ -14,7 +14,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -26,9 +26,9 @@ type Config struct {
type Device struct {
bus *machine.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
configurePins func()
buffer []uint8
@@ -82,7 +82,7 @@ var partialRefresh = [159]uint8{
}
// New returns a new epd1in54 driver. Pass in a fully configured SPI bus.
func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus *machine.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
return Device{
buffer: make([]uint8, (uint32(Width)*uint32(Height))/8),
bus: bus,
@@ -91,17 +91,17 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy
rst: rstPin.Set,
isBusy: busyPin.Get,
configurePins: func() {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
},
}
}
func (d *Device) LDirInit(cfg Config) {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.configurePins()
@@ -160,7 +160,7 @@ func (d *Device) LDirInit(cfg Config) {
func (d *Device) HDirInit(cfg Config) {
if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
panic(pin.ErrConfigBeforeInstantiated)
}
d.configurePins()
@@ -240,11 +240,11 @@ func (d *Device) setLUT(lut [159]uint8) {
// Reset resets the display.
func (d *Device) Reset() {
d.rst(true)
d.rst.High()
time.Sleep(20 * time.Millisecond)
d.rst(false)
d.rst.Low()
time.Sleep(5 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(20 * time.Millisecond)
}
@@ -261,13 +261,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
@@ -429,5 +429,5 @@ func (d *Device) Sleep() {
d.SendData(0x01)
time.Sleep(200 * time.Millisecond)
d.rst(false)
d.rst.Low()
}
+18 -18
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -21,9 +21,9 @@ type Config struct {
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
logicalWidth int16
width int16
@@ -53,11 +53,11 @@ var lutPartialUpdate = [30]uint8{
}
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
return Device{
bus: bus,
cs: csPin.Set,
@@ -91,9 +91,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -119,9 +119,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -155,13 +155,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates
+18 -18
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -20,9 +20,9 @@ type Config struct {
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
width int16
height int16
@@ -33,11 +33,11 @@ type Device struct {
type Color uint8
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
return Device{
bus: bus,
cs: csPin.Set,
@@ -75,9 +75,9 @@ func (d *Device) Configure(cfg Config) {
}
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -126,13 +126,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
+16 -15
View File
@@ -8,6 +8,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)
const (
@@ -19,9 +20,9 @@ const Baudrate = 4_000_000 // 4 MHz
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
blackBuffer []byte
@@ -176,11 +177,11 @@ func (d *Device) setCursor(x, y uint16) error {
}
func (d *Device) hwReset() {
d.rst(true)
d.rst.High()
time.Sleep(50 * time.Millisecond)
d.rst(false)
d.rst.Low()
time.Sleep(2 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(50 * time.Millisecond)
}
@@ -232,26 +233,26 @@ func (d *Device) sendCommandSequence(seq []byte) error {
}
func (d *Device) sendCommandByte(b byte) error {
d.dc(false)
d.cs(false)
d.dc.Low()
d.cs.Low()
_, err := d.bus.Transfer(b)
d.cs(true)
d.cs.High()
return err
}
func (d *Device) sendDataByte(b byte) error {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
_, err := d.bus.Transfer(b)
d.cs(true)
d.cs.High()
return err
}
func (d *Device) sendData(b []byte) error {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
err := d.bus.Tx(b, nil)
d.cs(true)
d.cs.High()
return err
}
+19 -19
View File
@@ -16,7 +16,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -28,9 +28,9 @@ type Config struct {
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
logicalWidth int16
width int16
@@ -61,11 +61,11 @@ var lutPartialUpdate = [30]uint8{
}
// New returns a new epd2in9 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
return Device{
bus: bus,
cs: csPin.Set,
@@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -122,14 +122,14 @@ func (d *Device) Configure(cfg Config) {
d.SendCommand(DATA_ENTRY_MODE_SETTING)
d.SendData(0x03) // X increment; Y increment
d.SetLUT(true)
d.SetLUT.High()
}
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -152,13 +152,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates
+18 -18
View File
@@ -13,7 +13,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -25,9 +25,9 @@ type Config struct {
type Device struct {
bus drivers.SPI
cs drivers.PinOutput
dc drivers.PinOutput
rst drivers.PinOutput
cs pin.OutputFunc
dc pin.OutputFunc
rst pin.OutputFunc
isBusy drivers.PinInput
logicalWidth int16
width int16
@@ -40,11 +40,11 @@ type Device struct {
type Rotation uint8
// New returns a new epd4in2 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
legacy.ConfigurePinInput(busyPin)
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
pin.ConfigureOutput(csPin)
pin.ConfigureOutput(dcPin)
pin.ConfigureOutput(rstPin)
pin.ConfigureInput(busyPin)
return Device{
bus: bus,
cs: csPin.Set,
@@ -78,9 +78,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
d.SendCommand(POWER_SETTING)
@@ -104,9 +104,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -145,13 +145,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates