Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/ssd1289/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"machine"
"math/rand"

"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/ssd1289"
)

Expand All @@ -16,7 +17,7 @@ func main() {
//consider creating a more efficient bus implementation that uses
//your microcontrollers built in "ports"
//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.GP5, //DB1
machine.GP6, //DB2
Expand Down
31 changes: 11 additions & 20 deletions ssd1289/pinbus.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package ssd1289

import "machine"
import (
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

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++ {
pins[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
legacy.ConfigurePinOut(pins[i])
}

return pinBus{
Expand All @@ -18,20 +22,7 @@ func NewPinBus(pins [16]machine.Pin) pinBus {
}

func (b pinBus) Set(data uint16) {
b.pins[15].Set((data & (1 << 15)) != 0)
b.pins[14].Set((data & (1 << 14)) != 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)
for i := 15; i >= 0; i-- {
b.pins[i].Set((data & (1 << i)) != 0)
}
}
39 changes: 21 additions & 18 deletions ssd1289/ssd1289.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,45 @@ package ssd1289

import (
"image/color"
"machine"
"time"

"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

type Bus interface {
Set(data uint16)
}

type Device struct {
rs machine.Pin
wr machine.Pin
cs machine.Pin
rst machine.Pin
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 {
d := Device{
rs: rs,
wr: wr,
cs: cs,
rst: rst,
func New(rs, wr, cs, rst pin.Output, bus Bus) *Device {
d := &Device{
rs: rs.Set,
wr: wr.Set,
cs: cs.Set,
rst: rst.Set,
bus: bus,
}

rs.Configure(machine.PinConfig{Mode: machine.PinOutput})
wr.Configure(machine.PinConfig{Mode: machine.PinOutput})
cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
rst.Configure(machine.PinConfig{Mode: machine.PinOutput})
// configure GPIO pins (only on baremetal targets, for backwards compatibility)
legacy.ConfigurePinOut(rs)
legacy.ConfigurePinOut(wr)
legacy.ConfigurePinOut(cs)
legacy.ConfigurePinOut(rst)

cs.High()
rst.High()
wr.High()
d.cs.High()
d.rst.High()
d.wr.High()

return d
}
Expand Down
26 changes: 14 additions & 12 deletions ssd1306/ssd1306_spi.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package ssd1306

import (
"machine"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

type SPIBus struct {
wire drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
buffer []byte // buffer to avoid heap allocations
}

// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) *Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin pin.Output) *Device {
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
return &Device{
bus: &SPIBus{
wire: bus,
dcPin: dcPin,
resetPin: resetPin,
csPin: csPin,
dcPin: dcPin.Set,
resetPin: resetPin.Set,
csPin: csPin.Set,
},
}
}
Expand Down Expand Up @@ -60,7 +62,7 @@ func (b *SPIBus) flush() error {
// tx sends data to the display
func (b *SPIBus) tx(data []byte, isCommand bool) error {
b.csPin.High()
b.dcPin.Set(!isCommand)
b.dcPin(!isCommand)
b.csPin.Low()
err := b.wire.Tx(data, nil)
b.csPin.High()
Expand Down
26 changes: 14 additions & 12 deletions ssd1331/ssd1331.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package ssd1331 // import "tinygo.org/x/drivers/ssd1331"

import (
"image/color"
"machine"

"errors"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

type Model uint8
Expand All @@ -19,9 +20,9 @@ type Rotation uint8
// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
width int16
height int16
batchLength int16
Expand All @@ -36,15 +37,16 @@ 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 {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
func New(bus drivers.SPI, resetPin, dcPin, csPin pin.Output) Device {
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
return Device{
bus: bus,
dcPin: dcPin,
resetPin: resetPin,
csPin: csPin,
dcPin: dcPin.Set,
resetPin: resetPin.Set,
csPin: csPin.Set,
}
}

Expand Down Expand Up @@ -251,7 +253,7 @@ func (d *Device) Data(data uint8) {

// Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin.Set(!isCommand)
d.dcPin(!isCommand)
d.bus.Tx(data, nil)
}

Expand Down
55 changes: 30 additions & 25 deletions ssd1351/ssd1351.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package ssd1351 // import "tinygo.org/x/drivers/ssd1351"
import (
"errors"
"image/color"
"machine"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

var (
Expand All @@ -19,17 +20,18 @@ var (

// Device wraps an SPI connection.
type Device struct {
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
enPin machine.Pin
rwPin machine.Pin
width int16
height int16
rowOffset int16
columnOffset int16
bufferLength int16
bus drivers.SPI
dcPin pin.OutputFunc
resetPin pin.OutputFunc
csPin pin.OutputFunc
enPin pin.OutputFunc
rwPin pin.OutputFunc
width int16
height int16
rowOffset int16
columnOffset int16
bufferLength int16
configurePins func()
}

// Config is the configuration for the display
Expand All @@ -41,14 +43,21 @@ 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 machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
return Device{
bus: bus,
dcPin: dcPin,
resetPin: resetPin,
csPin: csPin,
enPin: enPin,
rwPin: rwPin,
dcPin: dcPin.Set,
resetPin: resetPin.Set,
csPin: csPin.Set,
enPin: enPin.Set,
rwPin: rwPin.Set,
configurePins: func() {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(enPin)
legacy.ConfigurePinOut(rwPin)
},
}
}

Expand All @@ -72,12 +81,8 @@ func (d *Device) Configure(cfg Config) {
d.bufferLength = d.height
}

// configure GPIO pins
d.dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
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})
// configure GPIO pins (on baremetal targets only, for backwards compatibility)
d.configurePins()

// reset the device
d.resetPin.High()
Expand Down Expand Up @@ -278,7 +283,7 @@ func (d *Device) Data(data uint8) {

// Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin.Set(!isCommand)
d.dcPin(!isCommand)
d.csPin.Low()
d.bus.Tx(data, nil)
d.csPin.High()
Expand Down