ssd1xxx: break dependency from machine package (#812)

* ssd1xxx: break dependency from machine package
* fix ssd1289 example
* simplify
This commit is contained in:
Yurii Soldak
2025-11-12 09:40:34 +01:00
committed by GitHub
parent 4b831af52b
commit c710cc8236
6 changed files with 92 additions and 88 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"machine" "machine"
"math/rand" "math/rand"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/ssd1289" "tinygo.org/x/drivers/ssd1289"
) )
@@ -16,7 +17,7 @@ func main() {
//consider creating a more efficient bus implementation that uses //consider creating a more efficient bus implementation that uses
//your microcontrollers built in "ports" //your microcontrollers built in "ports"
//see rp2040bus.go for an example for the rapsberry pi pico //see rp2040bus.go for an example for the rapsberry pi pico
bus := ssd1289.NewPinBus([16]machine.Pin{ bus := ssd1289.NewPinBus([16]pin.Output{
machine.GP4, //DB0 machine.GP4, //DB0
machine.GP5, //DB1 machine.GP5, //DB1
machine.GP6, //DB2 machine.GP6, //DB2
+11 -20
View File
@@ -1,15 +1,19 @@
package ssd1289 package ssd1289
import "machine" import (
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type pinBus struct { type pinBus struct {
pins [16]machine.Pin pins [16]pin.Output
} }
func NewPinBus(pins [16]machine.Pin) pinBus { func NewPinBus(pins [16]pin.Output) pinBus {
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
pins[i].Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(pins[i])
} }
return pinBus{ return pinBus{
@@ -18,20 +22,7 @@ func NewPinBus(pins [16]machine.Pin) pinBus {
} }
func (b pinBus) Set(data uint16) { func (b pinBus) Set(data uint16) {
b.pins[15].Set((data & (1 << 15)) != 0) for i := 15; i >= 0; i-- {
b.pins[14].Set((data & (1 << 14)) != 0) b.pins[i].Set((data & (1 << i)) != 0)
b.pins[13].Set((data & (1 << 13)) != 0) }
b.pins[12].Set((data & (1 << 12)) != 0)
b.pins[11].Set((data & (1 << 11)) != 0)
b.pins[10].Set((data & (1 << 10)) != 0)
b.pins[9].Set((data & (1 << 9)) != 0)
b.pins[8].Set((data & (1 << 8)) != 0)
b.pins[7].Set((data & (1 << 7)) != 0)
b.pins[6].Set((data & (1 << 6)) != 0)
b.pins[5].Set((data & (1 << 5)) != 0)
b.pins[4].Set((data & (1 << 4)) != 0)
b.pins[3].Set((data & (1 << 3)) != 0)
b.pins[2].Set((data & (1 << 2)) != 0)
b.pins[1].Set((data & (1 << 1)) != 0)
b.pins[0].Set((data & (1 << 0)) != 0)
} }
+21 -18
View File
@@ -5,8 +5,10 @@ package ssd1289
import ( import (
"image/color" "image/color"
"machine"
"time" "time"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Bus interface { type Bus interface {
@@ -14,33 +16,34 @@ type Bus interface {
} }
type Device struct { type Device struct {
rs machine.Pin rs pin.OutputFunc
wr machine.Pin wr pin.OutputFunc
cs machine.Pin cs pin.OutputFunc
rst machine.Pin rst pin.OutputFunc
bus Bus bus Bus
} }
const width = int16(240) const width = int16(240)
const height = int16(320) const height = int16(320)
func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bus) Device { func New(rs, wr, cs, rst pin.Output, bus Bus) *Device {
d := Device{ d := &Device{
rs: rs, rs: rs.Set,
wr: wr, wr: wr.Set,
cs: cs, cs: cs.Set,
rst: rst, rst: rst.Set,
bus: bus, bus: bus,
} }
rs.Configure(machine.PinConfig{Mode: machine.PinOutput}) // configure GPIO pins (only on baremetal targets, for backwards compatibility)
wr.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(rs)
cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(wr)
rst.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(cs)
legacy.ConfigurePinOut(rst)
cs.High() d.cs.High()
rst.High() d.rst.High()
wr.High() d.wr.High()
return d return d
} }
+14 -12
View File
@@ -1,31 +1,33 @@
package ssd1306 package ssd1306
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type SPIBus struct { type SPIBus struct {
wire drivers.SPI wire drivers.SPI
dcPin machine.Pin dcPin pin.OutputFunc
resetPin machine.Pin resetPin pin.OutputFunc
csPin machine.Pin csPin pin.OutputFunc
buffer []byte // buffer to avoid heap allocations buffer []byte // buffer to avoid heap allocations
} }
// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured. // NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) *Device { func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin pin.Output) *Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) // configure GPIO pins (on baremetal targets only, for backwards compatibility)
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(dcPin)
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
return &Device{ return &Device{
bus: &SPIBus{ bus: &SPIBus{
wire: bus, wire: bus,
dcPin: dcPin, dcPin: dcPin.Set,
resetPin: resetPin, resetPin: resetPin.Set,
csPin: csPin, csPin: csPin.Set,
}, },
} }
} }
@@ -60,7 +62,7 @@ func (b *SPIBus) flush() error {
// tx sends data to the display // tx sends data to the display
func (b *SPIBus) tx(data []byte, isCommand bool) error { func (b *SPIBus) tx(data []byte, isCommand bool) error {
b.csPin.High() b.csPin.High()
b.dcPin.Set(!isCommand) b.dcPin(!isCommand)
b.csPin.Low() b.csPin.Low()
err := b.wire.Tx(data, nil) err := b.wire.Tx(data, nil)
b.csPin.High() b.csPin.High()
+14 -12
View File
@@ -5,12 +5,13 @@ package ssd1331 // import "tinygo.org/x/drivers/ssd1331"
import ( import (
"image/color" "image/color"
"machine"
"errors" "errors"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Model uint8 type Model uint8
@@ -19,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 machine.Pin dcPin pin.OutputFunc
resetPin machine.Pin resetPin pin.OutputFunc
csPin machine.Pin csPin pin.OutputFunc
width int16 width int16
height int16 height int16
batchLength int16 batchLength int16
@@ -36,15 +37,16 @@ type Config struct {
} }
// New creates a new SSD1331 connection. The SPI wire must already be configured. // New creates a new SSD1331 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin machine.Pin) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin pin.Output) Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) // configure GPIO pins (on baremetal targets only, for backwards compatibility)
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(dcPin)
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
return Device{ return Device{
bus: bus, bus: bus,
dcPin: dcPin, dcPin: dcPin.Set,
resetPin: resetPin, resetPin: resetPin.Set,
csPin: csPin, csPin: csPin.Set,
} }
} }
@@ -251,7 +253,7 @@ func (d *Device) Data(data uint8) {
// Tx sends data to the display // Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) { func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin.Set(!isCommand) d.dcPin(!isCommand)
d.bus.Tx(data, nil) d.bus.Tx(data, nil)
} }
+30 -25
View File
@@ -6,10 +6,11 @@ package ssd1351 // import "tinygo.org/x/drivers/ssd1351"
import ( import (
"errors" "errors"
"image/color" "image/color"
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
var ( var (
@@ -19,17 +20,18 @@ var (
// Device wraps an SPI connection. // Device wraps an SPI connection.
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
dcPin machine.Pin dcPin pin.OutputFunc
resetPin machine.Pin resetPin pin.OutputFunc
csPin machine.Pin csPin pin.OutputFunc
enPin machine.Pin enPin pin.OutputFunc
rwPin machine.Pin rwPin pin.OutputFunc
width int16 width int16
height int16 height int16
rowOffset int16 rowOffset int16
columnOffset int16 columnOffset int16
bufferLength int16 bufferLength int16
configurePins func()
} }
// Config is the configuration for the display // Config is the configuration for the display
@@ -41,14 +43,21 @@ 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 machine.Pin) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
return Device{ return Device{
bus: bus, bus: bus,
dcPin: dcPin, dcPin: dcPin.Set,
resetPin: resetPin, resetPin: resetPin.Set,
csPin: csPin, csPin: csPin.Set,
enPin: enPin, enPin: enPin.Set,
rwPin: rwPin, rwPin: rwPin.Set,
configurePins: func() {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(enPin)
legacy.ConfigurePinOut(rwPin)
},
} }
} }
@@ -72,12 +81,8 @@ func (d *Device) Configure(cfg Config) {
d.bufferLength = d.height d.bufferLength = d.height
} }
// configure GPIO pins // configure GPIO pins (on baremetal targets only, for backwards compatibility)
d.dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) d.configurePins()
d.resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.enPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.rwPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
// reset the device // reset the device
d.resetPin.High() d.resetPin.High()
@@ -278,7 +283,7 @@ func (d *Device) Data(data uint8) {
// Tx sends data to the display // Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) { func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin.Set(!isCommand) d.dcPin(!isCommand)
d.csPin.Low() d.csPin.Low()
d.bus.Tx(data, nil) d.bus.Tx(data, nil)
d.csPin.High() d.csPin.High()