Pin HAL to break dependency of drivers from TinyGo's machine package

This commit is contained in:
Yurii Soldak
2025-09-21 04:38:21 +02:00
parent 3fa08112db
commit c6d83b783f
15 changed files with 328 additions and 88 deletions
+16 -21
View File
@@ -5,7 +5,6 @@ package st7735 // import "tinygo.org/x/drivers/st7735"
import (
"image/color"
"machine"
"time"
"errors"
@@ -39,10 +38,10 @@ type Device = DeviceOf[pixel.RGB565BE]
// formats.
type DeviceOf[T Color] struct {
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
blPin machine.Pin
dcPin drivers.PinOutput
resetPin drivers.PinOutput
csPin drivers.PinOutput
blPin drivers.PinOutput
width int16
height int16
columnOffset int16
@@ -65,23 +64,19 @@ 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 machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) 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 machine.Pin) DeviceOf[T] {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
blPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) DeviceOf[T] {
return DeviceOf[T]{
bus: bus,
dcPin: dcPin,
resetPin: resetPin,
csPin: csPin,
blPin: blPin,
dcPin: drivers.SafePinOutput(dcPin),
resetPin: drivers.SafePinOutput(resetPin),
csPin: drivers.SafePinOutput(csPin),
blPin: drivers.SafePinOutput(blPin),
}
}
@@ -114,11 +109,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
// reset the device
d.resetPin.High()
d.resetPin.Set(true)
time.Sleep(5 * time.Millisecond)
d.resetPin.Low()
d.resetPin.Set(false)
time.Sleep(20 * time.Millisecond)
d.resetPin.High()
d.resetPin.Set(true)
time.Sleep(150 * time.Millisecond)
// Common initialization
@@ -226,7 +221,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.SetRotation(d.rotation)
d.blPin.High()
d.blPin.Set(true)
}
// Display does nothing, there's no buffer as it might be too big for some boards
@@ -438,9 +433,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.High()
d.blPin.Set(true)
} else {
d.blPin.Low()
d.blPin.Set(false)
}
}