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" "image/color"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
const ( const (
@@ -37,10 +37,10 @@ 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 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() { return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin) pin.ConfigureOutput(sckPin)
legacy.ConfigurePinOut(sdoPin) pin.ConfigureOutput(sdoPin)
}}) }})
} }
+10 -11
View File
@@ -1,8 +1,7 @@
package apa102 package apa102
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded // 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 // 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 pin.OutputFunc
SDO drivers.PinOutput SDO pin.OutputFunc
Delay uint32 Delay uint32
configurePins func() configurePins func()
} }
@@ -20,11 +19,11 @@ type bbSPI struct {
// Configure sets up the SCK and SDO pins as outputs and sets them low // Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() { func (s *bbSPI) Configure() {
if s.configurePins == nil { if s.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
s.configurePins() s.configurePins()
s.SCK(false) s.SCK.Low()
s.SDO(false) s.SDO.Low()
if s.Delay == 0 { if s.Delay == 0 {
s.Delay = 1 s.Delay = 1
} }
@@ -53,19 +52,19 @@ func (s *bbSPI) Transfer(b byte) (byte, error) {
for i := uint8(0); i < 8; i++ { for i := uint8(0); i < 8; i++ {
// half clock cycle high to start // half clock cycle high to start
s.SCK(true) s.SCK.High()
s.delay() s.delay()
// write the value to SDO (MSB first) // write the value to SDO (MSB first)
if b&(1<<(7-i)) == 0 { if b&(1<<(7-i)) == 0 {
s.SDO(false) s.SDO.Low()
} else { } else {
s.SDO(true) s.SDO.High()
} }
s.delay() s.delay()
// half clock cycle low // half clock cycle low
s.SCK(false) s.SCK.Low()
s.delay() s.delay()
// for actual SPI would try to read the SDI value here // for actual SPI would try to read the SDI value here
+16 -16
View File
@@ -4,14 +4,14 @@ import (
"time" "time"
"tinygo.org/x/drivers" "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 // 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 pin.OutputFunc
buf [7]byte buf [7]byte
@@ -23,12 +23,12 @@ 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 legacy.PinOutput, spi drivers.SPI) *DeviceSPI { func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{ return &DeviceSPI{
csb: csb.Set, // chip select csb: csb.Set, // chip select
bus: spi, bus: spi,
configurePins: func() { 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). // assumed to be up and running).
func (d *DeviceSPI) Configure() error { func (d *DeviceSPI) Configure() error {
if d.configurePins == nil { if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated return pin.ErrConfigBeforeInstantiated
} }
d.configurePins() d.configurePins()
d.csb(true) d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get // The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going: // SPI communication going:
@@ -93,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0 data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0 data[1] = 0
data[2] = 0 data[2] = 0
d.csb(false) d.csb.Low()
err = d.bus.Tx(data, data) err = d.bus.Tx(data, data)
d.csb(true) d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -130,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ { for i := 1; i < len(data); i++ {
data[i] = 0 data[i] = 0
} }
d.csb(false) d.csb.Low()
err = d.bus.Tx(data, data) err = d.bus.Tx(data, data)
d.csb(true) d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -160,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ { for i := 1; i < len(data); i++ {
data[i] = 0 data[i] = 0
} }
d.csb(false) d.csb.Low()
err = d.bus.Tx(data, data) err = d.bus.Tx(data, data)
d.csb(true) d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -208,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2] data := d.buf[:2]
data[0] = 0x80 | address data[0] = 0x80 | address
data[1] = 0 data[1] = 0
d.csb(false) d.csb.Low()
d.bus.Tx(data, data) d.bus.Tx(data, data)
d.csb(true) d.csb.High()
return data[1] return data[1]
} }
@@ -224,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address buf[0] = address
buf[1] = data buf[1] = data
d.csb(false) d.csb.Low()
d.bus.Tx(buf, buf) 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 ( import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
// 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 pin.OutputFunc
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 legacy.PinOutput) Device { func New(pin pin.Output) Device {
return Device{ return Device{
pin: pin.Set, pin: pin.Set,
High: false, High: false,
@@ -26,14 +25,14 @@ func New(pin legacy.PinOutput) Device {
// On sets the buzzer to a high state. // On sets the buzzer to a high state.
func (l *Device) On() (err error) { func (l *Device) On() (err error) {
l.pin(true) l.pin.High()
l.High = true l.High = true
return return
} }
// Off sets the buzzer to a low state. // Off sets the buzzer to a low state.
func (l *Device) Off() (err error) { func (l *Device) Off() (err error) {
l.pin(false) l.pin.Low()
l.High = false l.High = false
return return
} }
+52 -52
View File
@@ -4,7 +4,7 @@ package easystepper // import "tinygo.org/x/drivers/easystepper"
import ( import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
) )
// StepMode determines the coil sequence used to perform a single step // 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 // Device holds the pins and the delay between steps
type Device struct { type Device struct {
pins [4]drivers.PinOutput pins [4]pin.OutputFunc
config func() config func()
stepDelay time.Duration stepDelay time.Duration
stepNumber uint8 stepNumber uint8
@@ -62,8 +62,8 @@ func (d *Device) Move(steps int32) {
// Off turns off all motor pins // Off turns off all motor pins
func (d *Device) Off() { func (d *Device) Off() {
for _, pin := range d.pins { for _, p := range d.pins {
pin(false) p.Low()
} }
} }
@@ -124,28 +124,28 @@ func (d *Device) stepMotor(step uint8) {
func (d *Device) stepMotor4(step uint8) { func (d *Device) stepMotor4(step uint8) {
switch step { switch step {
case 0: case 0:
d.pins[0](true) d.pins[0].High()
d.pins[1](false) d.pins[1].Low()
d.pins[2](true) d.pins[2].High()
d.pins[3](false) d.pins[3].Low()
break break
case 1: case 1:
d.pins[0](false) d.pins[0].Low()
d.pins[1](true) d.pins[1].High()
d.pins[2](true) d.pins[2].High()
d.pins[3](false) d.pins[3].Low()
break break
case 2: case 2:
d.pins[0](false) d.pins[0].Low()
d.pins[1](true) d.pins[1].High()
d.pins[2](false) d.pins[2].Low()
d.pins[3](true) d.pins[3].High()
break break
case 3: case 3:
d.pins[0](true) d.pins[0].High()
d.pins[1](false) d.pins[1].Low()
d.pins[2](false) d.pins[2].Low()
d.pins[3](true) d.pins[3].High()
break break
} }
d.stepNumber = step d.stepNumber = step
@@ -155,45 +155,45 @@ func (d *Device) stepMotor4(step uint8) {
func (d *Device) stepMotor8(step uint8) { func (d *Device) stepMotor8(step uint8) {
switch step { switch step {
case 0: case 0:
d.pins[0](true) d.pins[0].High()
d.pins[2](false) d.pins[2].Low()
d.pins[1](false) d.pins[1].Low()
d.pins[3](false) d.pins[3].Low()
case 1: case 1:
d.pins[0](true) d.pins[0].High()
d.pins[2](true) d.pins[2].High()
d.pins[1](false) d.pins[1].Low()
d.pins[3](false) d.pins[3].Low()
case 2: case 2:
d.pins[0](false) d.pins[0].Low()
d.pins[2](true) d.pins[2].High()
d.pins[1](false) d.pins[1].Low()
d.pins[3](false) d.pins[3].Low()
case 3: case 3:
d.pins[0](false) d.pins[0].Low()
d.pins[2](true) d.pins[2].High()
d.pins[1](true) d.pins[1].High()
d.pins[3](false) d.pins[3].Low()
case 4: case 4:
d.pins[0](false) d.pins[0].Low()
d.pins[2](false) d.pins[2].Low()
d.pins[1](true) d.pins[1].High()
d.pins[3](false) d.pins[3].Low()
case 5: case 5:
d.pins[0](false) d.pins[0].Low()
d.pins[2](false) d.pins[2].Low()
d.pins[1](true) d.pins[1].High()
d.pins[3](true) d.pins[3].High()
case 6: case 6:
d.pins[0](false) d.pins[0].Low()
d.pins[2](false) d.pins[2].Low()
d.pins[1](false) d.pins[1].Low()
d.pins[3](true) d.pins[3].High()
case 7: case 7:
d.pins[0](true) d.pins[0].High()
d.pins[2](false) d.pins[2].Low()
d.pins[1](false) d.pins[1].Low()
d.pins[3](true) d.pins[3].High()
} }
d.stepNumber = step d.stepNumber = step
} }
+6 -3
View File
@@ -4,20 +4,23 @@ import (
"errors" "errors"
"time" "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 { if stepcount == 0 || rpm == 0 {
return nil, errors.New("zero rpm and/or stepcount") return nil, errors.New("zero rpm and/or stepcount")
} }
var ps [4]pin.OutputFunc
for i := range pins { for i := range pins {
if pins[i] == nil { if pins[i] == nil {
return nil, errors.New("nil pin") return nil, errors.New("nil pin")
} }
ps[i] = pins[i].Set
} }
d := &Device{ d := &Device{
pins: pins, pins: ps,
stepDelay: time.Second * 60 / time.Duration((stepcount * rpm)), stepDelay: time.Second * 60 / time.Duration((stepcount * rpm)),
stepMode: mode, stepMode: mode,
config: func() {}, config: func() {},
+7 -8
View File
@@ -7,8 +7,7 @@ import (
"machine" "machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
// New returns a new single easystepper driver given a DeviceConfig // 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 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]pin.OutputFunc{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() {
legacy.ConfigurePinOut(config.Pin1) pin.ConfigureOutput(config.Pin1)
legacy.ConfigurePinOut(config.Pin2) pin.ConfigureOutput(config.Pin2)
legacy.ConfigurePinOut(config.Pin3) pin.ConfigureOutput(config.Pin3)
legacy.ConfigurePinOut(config.Pin4) pin.ConfigureOutput(config.Pin4)
}, },
}, nil }, nil
} }
@@ -32,7 +31,7 @@ func New(config DeviceConfig) (*Device, error) {
// Configure configures the pins of the Device // Configure configures the pins of the Device
func (d *Device) Configure() { func (d *Device) Configure() {
if d.config == nil { if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.config() d.config()
} }
+4 -3
View File
@@ -7,6 +7,7 @@ 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"
) )
@@ -19,13 +20,13 @@ 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 legacy.PinInput) *Device { func New(i2c drivers.I2C, intPin pin.Input) *Device {
return &Device{ return &Device{
bus: i2c, bus: i2c,
buf: make([]byte, 11), buf: make([]byte, 11),
Address: Address, Address: Address,
configurePins: func() { configurePins: func() {
legacy.ConfigurePinInputPulldown(intPin) pin.ConfigureInputPulldown(intPin)
}, },
} }
} }
@@ -37,7 +38,7 @@ type Config struct {
// Configure the FT6336 device. // Configure the FT6336 device.
func (d *Device) Configure(config Config) error { func (d *Device) Configure(config Config) error {
if d.configurePins == nil { if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated return pin.ErrConfigBeforeInstantiated
} }
d.write1Byte(0xA4, 0x00) d.write1Byte(0xA4, 0x00)
d.configurePins() d.configurePins()
+20 -20
View File
@@ -10,7 +10,7 @@ import (
"errors" "errors"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"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.
@@ -22,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 pin.OutputFunc
resetPin drivers.PinOutput resetPin pin.OutputFunc
csPin drivers.PinOutput csPin pin.OutputFunc
blPin drivers.PinOutput blPin pin.OutputFunc
width int16 width int16
height int16 height int16
columnOffsetCfg int16 columnOffsetCfg int16
@@ -52,11 +52,11 @@ 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 legacy.PinOutput) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
legacy.ConfigurePinOut(resetPin) pin.ConfigureOutput(resetPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(blPin) pin.ConfigureOutput(blPin)
return Device{ return Device{
bus: bus, bus: bus,
resetPin: resetPin.Set, resetPin: resetPin.Set,
@@ -68,11 +68,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device
// Reset the Device // Reset the Device
func (d *Device) Reset() { func (d *Device) Reset() {
d.resetPin(true) d.resetPin.High()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(false) d.resetPin.Low()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(true) d.resetPin.High()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
@@ -232,14 +232,14 @@ func (d *Device) Tx(data []byte, isCommand bool) {
// Rx reads data from the display // Rx reads data from the display
func (d *Device) Rx(command uint8, data []byte) { func (d *Device) Rx(command uint8, data []byte) {
d.dcPin(false) d.dcPin.Low()
d.csPin(false) d.csPin.Low()
d.bus.Transfer(command) d.bus.Transfer(command)
d.dcPin(true) d.dcPin.High()
for i := range data { for i := range data {
data[i], _ = d.bus.Transfer(0xFF) data[i], _ = d.bus.Transfer(0xFF)
} }
d.csPin(true) d.csPin.High()
} }
// Size returns the current size of the display. // 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 // EnableBacklight enables or disables the backlight
func (d *Device) EnableBacklight(enable bool) { func (d *Device) EnableBacklight(enable bool) {
if enable { if enable {
d.blPin(true) d.blPin.High()
} else { } else {
d.blPin(false) d.blPin.Low()
} }
} }
@@ -563,5 +563,5 @@ func (d *Device) Configure(cfg Config) {
d.Command(DISPON) d.Command(DISPON)
time.Sleep(20 * time.Millisecond) time.Sleep(20 * time.Millisecond)
d.blPin(true) d.blPin.High()
} }
+10 -11
View File
@@ -7,27 +7,26 @@ package hcsr04
import ( import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
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 pin.OutputFunc
echo drivers.PinInput echo pin.InputFunc
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 legacy.PinOutput, echo legacy.PinInput) Device { func New(trigger pin.Output, echo pin.Input) Device {
return Device{ return Device{
trigger: trigger.Set, trigger: trigger.Set,
echo: echo.Get, echo: echo.Get,
configurePins: func() { configurePins: func() {
legacy.ConfigurePinOut(trigger) pin.ConfigureOutput(trigger)
legacy.ConfigurePinInput(echo) pin.ConfigureInput(echo)
}, },
} }
} }
@@ -35,7 +34,7 @@ func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
// Configure configures the pins of the Device // Configure configures the pins of the Device
func (d *Device) Configure() { func (d *Device) Configure() {
if d.configurePins == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.configurePins() d.configurePins()
} }
@@ -54,11 +53,11 @@ func (d *Device) ReadDistance() int32 {
// ReadPulse returns the time of the pulse (roundtrip) in microseconds // ReadPulse returns the time of the pulse (roundtrip) in microseconds
func (d *Device) ReadPulse() int32 { func (d *Device) ReadPulse() int32 {
t := time.Now() t := time.Now()
d.trigger(false) d.trigger.Low()
time.Sleep(2 * time.Microsecond) time.Sleep(2 * time.Microsecond)
d.trigger(true) d.trigger.High()
time.Sleep(10 * time.Microsecond) time.Sleep(10 * time.Microsecond)
d.trigger(false) d.trigger.Low()
i := uint8(0) i := uint8(0)
for { for {
if d.echo() { if d.echo() {
-2
View File
@@ -2,8 +2,6 @@
package hts221 package hts221
import "tinygo.org/x/drivers"
// Configure sets up the HTS221 device for communication. // Configure sets up the HTS221 device for communication.
func (d *Device) Configure() { func (d *Device) Configure() {
// read calibration data // 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 //go:build baremetal && fe310
package legacy package pin
import "machine" import "machine"
@@ -1,6 +1,6 @@
//go:build baremetal && !fe310 //go:build baremetal && !fe310
package legacy package pin
import "machine" 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" "errors"
"tinygo.org/x/drivers" "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. // ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -14,13 +14,13 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
} }
// 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 legacy.PinOutput) *Device { func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs.Set, cs: cs.Set,
@@ -34,11 +34,11 @@ func (d *Device) Read() (float32, error) {
value uint16 value uint16
) )
d.cs(false) d.cs.Low()
if err := d.bus.Tx([]byte{0, 0}, read); err != nil { if err := d.bus.Tx([]byte{0, 0}, read); err != nil {
return 0, err return 0, err
} }
d.cs(true) d.cs.High()
// datasheet: Bit D2 is normally low and goes high if the thermocouple input is open. // datasheet: Bit D2 is normally low and goes high if the thermocouple input is open.
if read[1]&0x04 == 0x04 { if read[1]&0x04 == 0x04 {
+5 -5
View File
@@ -4,24 +4,24 @@ package max72xx
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"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 pin.OutputFunc
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 legacy.PinOutput) *Device { func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs.Set, cs: cs.Set,
configurePins: func() { 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. // Configure setups the pins.
func (driver *Device) Configure() { func (driver *Device) Configure() {
if driver.configurePins == nil { if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
driver.configurePins() driver.configurePins()
} }
+22 -22
View File
@@ -11,13 +11,13 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"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 pin.OutputFunc
msg *CANMsg msg *CANMsg
mcpMode byte mcpMode byte
configurePins func() configurePins func()
@@ -37,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 legacy.PinOutput) *Device { func New(b drivers.SPI, csPin pin.Output) *Device {
d := &Device{ d := &Device{
spi: SPI{ spi: SPI{
bus: b, bus: b,
@@ -47,7 +47,7 @@ func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
cs: csPin.Set, cs: csPin.Set,
msg: &CANMsg{}, msg: &CANMsg{},
configurePins: func() { 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. // Configure sets up the device for communication.
func (d *Device) Configure() { func (d *Device) Configure() {
if d.configurePins == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.configurePins() d.configurePins()
} }
@@ -166,9 +166,9 @@ func (d *Device) init(speed, clock byte) error {
// Reset resets mcp2515. // Reset resets mcp2515.
func (d *Device) Reset() error { func (d *Device) Reset() error {
d.cs(false) d.cs.Low()
_, err := d.spi.readWrite(mcpReset) _, err := d.spi.readWrite(mcpReset)
d.cs(true) d.cs.High()
// time.Sleep(time.Microsecond * 4) // time.Sleep(time.Microsecond * 4)
if err != nil { if err != nil {
return err return err
@@ -456,8 +456,8 @@ func (d *Device) readMsg() error {
func (d *Device) readRxBuffer(loadAddr uint8) error { func (d *Device) readRxBuffer(loadAddr uint8) error {
msg := d.msg msg := d.msg
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(loadAddr) _, err := d.spi.readWrite(loadAddr)
if err != nil { if err != nil {
return err 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 { func (d *Device) writeCANMsg(bufNum uint8, canid uint32, ext, rtrBit, dlc uint8, data []byte) error {
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(txSidhToLoad(bufNum)) _, err := d.spi.readWrite(txSidhToLoad(bufNum))
if err != nil { if err != nil {
return err 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, // 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. // it is necessary to set cs.High once to separate the instruction of mcp2515.
d.cs(true) d.cs.High()
err = d.startTransmission(bufNum) err = d.startTransmission(bufNum)
if err != nil { 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 { func (d *Device) startTransmission(bufNum uint8) error {
d.cs(false) d.cs.Low()
_, err := d.spi.readWrite(txSidhToRTS(bufNum)) _, err := d.spi.readWrite(txSidhToRTS(bufNum))
d.cs(true) d.cs.High()
if err != nil { if err != nil {
return err return err
} }
@@ -708,8 +708,8 @@ func txSidhToLoad(i uint8) uint8 {
} }
func (d *Device) setRegister(addr, value byte) error { func (d *Device) setRegister(addr, value byte) error {
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(mcpWrite) _, err := d.spi.readWrite(mcpWrite)
if err != nil { if err != nil {
return err return err
@@ -728,8 +728,8 @@ func (d *Device) setRegister(addr, value byte) error {
} }
func (d *Device) readRegister(addr byte) (byte, error) { func (d *Device) readRegister(addr byte) (byte, error) {
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(mcpRead) _, err := d.spi.readWrite(mcpRead)
if err != nil { if err != nil {
return 0, err return 0, err
@@ -747,8 +747,8 @@ func (d *Device) readRegister(addr byte) (byte, error) {
} }
func (d *Device) modifyRegister(addr, mask, data byte) error { func (d *Device) modifyRegister(addr, mask, data byte) error {
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(mcpBitMod) _, err := d.spi.readWrite(mcpBitMod)
if err != nil { if err != nil {
return err return err
@@ -790,8 +790,8 @@ func (d *Device) requestNewMode(newMode byte) error {
} }
func (d *Device) readStatus() (byte, error) { func (d *Device) readStatus() (byte, error) {
d.cs(false) d.cs.Low()
defer d.cs(true) defer d.cs.High()
_, err := d.spi.readWrite(mcpReadStatus) _, err := d.spi.readWrite(mcpReadStatus)
if err != nil { if err != nil {
return 0, err return 0, err
+11 -11
View File
@@ -9,15 +9,15 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "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 pin.OutputFunc
rstPin drivers.PinOutput rstPin pin.OutputFunc
scePin drivers.PinOutput scePin pin.OutputFunc
buffer []byte buffer []byte
width int16 width int16
height int16 height int16
@@ -30,7 +30,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 legacy.PinOutput) *Device { func New(bus drivers.SPI, dcPin, rstPin, scePin pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
@@ -54,9 +54,9 @@ func (d *Device) Configure(cfg Config) {
d.bufferSize = d.width * d.height / 8 d.bufferSize = d.width * d.height / 8
d.buffer = make([]byte, d.bufferSize) d.buffer = make([]byte, d.bufferSize)
d.rstPin(false) d.rstPin.Low()
time.Sleep(100 * time.Nanosecond) time.Sleep(100 * time.Nanosecond)
d.rstPin(true) d.rstPin.High()
d.SendCommand(FUNCTIONSET | EXTENDEDINSTRUCTION) // H = 1 d.SendCommand(FUNCTIONSET | EXTENDEDINSTRUCTION) // H = 1
d.SendCommand(SETVOP | 0x3f) // 0x3f : Vop6 = 0, Vop5 to Vop0 = 1 d.SendCommand(SETVOP | 0x3f) // 0x3f : Vop6 = 0, Vop5 to Vop0 = 1
d.SendCommand(SETTEMP | 0x03) // Experimentally determined 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dcPin(false) d.dcPin.Low()
} else { } else {
d.dcPin(true) d.dcPin.High()
} }
d.scePin(false) d.scePin.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.scePin(true) d.scePin.High()
} }
// SetPixel enables or disables a pixel in the buffer // 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 package shiftregister
import ( import (
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
type NumberBit int8 type NumberBit int8
@@ -17,7 +16,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 pin.OutputFunc // 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
@@ -31,16 +30,16 @@ 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 legacy.PinOutput) *Device { func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device {
return &Device{ return &Device{
latch: Latch.Set, latch: Latch.Set,
clock: Clock.Set, clock: Clock.Set,
out: Out.Set, out: Out.Set,
bits: Bits, bits: Bits,
config: func() { config: func() {
legacy.ConfigurePinOut(Latch) pin.ConfigureOutput(Latch)
legacy.ConfigurePinOut(Clock) pin.ConfigureOutput(Clock)
legacy.ConfigurePinOut(Out) pin.ConfigureOutput(Out)
}, },
} }
} }
@@ -48,7 +47,7 @@ func New(Bits NumberBit, Latch, Clock, Out legacy.PinOutput) *Device {
// Configure set hardware configuration // Configure set hardware configuration
func (d *Device) Configure() { func (d *Device) Configure() {
if d.config == nil { if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.latch(true) d.latch(true)
} }
+29 -31
View File
@@ -5,11 +5,9 @@ package ssd1289
import ( import (
"image/color" "image/color"
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/internal/legacy"
) )
type Bus interface { type Bus interface {
@@ -17,17 +15,17 @@ type Bus interface {
} }
type Device struct { type Device struct {
rs drivers.PinOutput rs pin.OutputFunc
wr drivers.PinOutput wr pin.OutputFunc
cs drivers.PinOutput cs pin.OutputFunc
rst drivers.PinOutput 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 pin.Output, wr pin.Output, cs pin.Output, rst pin.Output, bus Bus) Device {
d := Device{ d := Device{
rs: rs.Set, rs: rs.Set,
wr: wr.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, rst: rst.Set,
bus: bus, bus: bus,
} }
legacy.ConfigurePinOut(rs) pin.ConfigureOutput(rs)
legacy.ConfigurePinOut(wr) pin.ConfigureOutput(wr)
legacy.ConfigurePinOut(cs) pin.ConfigureOutput(cs)
legacy.ConfigurePinOut(rst) pin.ConfigureOutput(rst)
cs.Set(true) d.cs.High()
rst.Set(true) d.rst.High()
wr.Set(true) d.wr.High()
return d return d
} }
func (d *Device) lcdWriteCom(cmd Command) { func (d *Device) lcdWriteCom(cmd Command) {
d.rs(false) d.rs.Low()
d.lcdWriteBusInt(uint16(cmd)) d.lcdWriteBusInt(uint16(cmd))
} }
func (d *Device) lcdWriteDataInt(data uint16) { func (d *Device) lcdWriteDataInt(data uint16) {
d.rs(true) d.rs.High()
d.lcdWriteBusInt(data) d.lcdWriteBusInt(data)
} }
@@ -63,8 +61,8 @@ func (d *Device) lcdWriteComData(cmd Command, data uint16) {
} }
func (d *Device) tx() { func (d *Device) tx() {
d.wr(false) d.wr.Low()
d.wr(true) d.wr.High()
} }
func (d *Device) lcdWriteBusInt(data uint16) { func (d *Device) lcdWriteBusInt(data uint16) {
@@ -73,13 +71,13 @@ func (d *Device) lcdWriteBusInt(data uint16) {
} }
func (d *Device) Configure() { func (d *Device) Configure() {
d.rst(true) d.rst.High()
time.Sleep(time.Millisecond * 5) time.Sleep(time.Millisecond * 5)
d.rst(false) d.rst.Low()
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
d.rst(true) d.rst.High()
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
d.cs(false) d.cs.Low()
//Power supply setting //Power supply setting
d.lcdWriteComData(POWERCONTROL1, 0xA8A4) d.lcdWriteComData(POWERCONTROL1, 0xA8A4)
@@ -139,7 +137,7 @@ func (d *Device) Configure() {
//MUX 319 --> Number of lines in display //MUX 319 --> Number of lines in display
d.lcdWriteComData(DRIVEROUTPUTCONTROL, 0x233F) 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) encoded := encodeColor(c)
d.cs(false) d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x), uint16(y)) d.setXY(uint16(x), uint16(y), uint16(x), uint16(y))
d.rs(true) d.rs.High()
d.lcdWriteBusInt(encoded) d.lcdWriteBusInt(encoded)
d.cs(true) d.cs.High()
} }
func (d *Device) FillRect(x, y, w, h int16, c color.RGBA) { func (d *Device) FillRect(x, y, w, h int16, c color.RGBA) {
encoded := encodeColor(c) encoded := encodeColor(c)
d.cs(false) d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x+(w-1)), uint16(y+(h-1))) d.setXY(uint16(x), uint16(y), uint16(x+(w-1)), uint16(y+(h-1)))
d.rs(true) d.rs.High()
d.bus.Set(encoded) d.bus.Set(encoded)
for i := int64(0); i < int64(w)*int64(h); i++ { for i := int64(0); i < int64(w)*int64(h); i++ {
d.tx() d.tx()
} }
d.cs(true) d.cs.High()
d.rs(false) d.rs.Low()
} }
+10 -10
View File
@@ -11,7 +11,7 @@ import (
"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
@@ -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 pin.OutputFunc
resetPin drivers.PinOutput resetPin pin.OutputFunc
csPin drivers.PinOutput csPin pin.OutputFunc
width int16 width int16
height int16 height int16
batchLength int16 batchLength int16
@@ -38,9 +38,9 @@ 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 machine.Pin) Device {
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(resetPin) pin.ConfigureOutput(resetPin)
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
return Device{ return Device{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
@@ -70,11 +70,11 @@ func (d *Device) Configure(cfg Config) {
d.batchData = make([]uint8, d.batchLength*2) d.batchData = make([]uint8, d.batchLength*2)
// reset the device // reset the device
d.resetPin(true) d.resetPin.High()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(false) d.resetPin.Low()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(true) d.resetPin.High()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
// Initialization // Initialization
+21 -21
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
var ( var (
@@ -20,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 pin.OutputFunc
resetPin drivers.PinOutput resetPin pin.OutputFunc
csPin drivers.PinOutput csPin pin.OutputFunc
enPin drivers.PinOutput enPin pin.OutputFunc
rwPin drivers.PinOutput rwPin pin.OutputFunc
configurePins func() configurePins func()
width int16 width int16
height int16 height int16
@@ -42,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 legacy.PinOutput) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
return Device{ return Device{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
@@ -51,11 +51,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput)
enPin: enPin.Set, enPin: enPin.Set,
rwPin: rwPin.Set, rwPin: rwPin.Set,
configurePins: func() { configurePins: func() {
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(resetPin) pin.ConfigureOutput(resetPin)
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(enPin) pin.ConfigureOutput(enPin)
legacy.ConfigurePinOut(rwPin) 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 // Configure initializes the display with default configuration
func (d *Device) Configure(cfg Config) { func (d *Device) Configure(cfg Config) {
if d.configurePins == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
if cfg.Width == 0 { if cfg.Width == 0 {
cfg.Width = 128 cfg.Width = 128
@@ -87,16 +87,16 @@ func (d *Device) Configure(cfg Config) {
d.configurePins() d.configurePins()
// reset the device // reset the device
d.resetPin(true) d.resetPin.High()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(false) d.resetPin.Low()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
d.resetPin(true) d.resetPin.High()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rwPin(false) d.rwPin.Low()
d.dcPin(false) d.dcPin.Low()
d.enPin(true) d.enPin.High()
// Initialization // Initialization
d.Command(SET_COMMAND_LOCK) d.Command(SET_COMMAND_LOCK)
@@ -286,9 +286,9 @@ 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(!isCommand) d.dcPin(!isCommand)
d.csPin(false) d.csPin.Low()
d.bus.Tx(data, nil) d.bus.Tx(data, nil)
d.csPin(true) d.csPin.High()
} }
// Size returns the current size of the display // Size returns the current size of the display
+15 -15
View File
@@ -10,7 +10,7 @@ import (
"errors" "errors"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -39,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 pin.OutputFunc
resetPin drivers.PinOutput resetPin pin.OutputFunc
csPin drivers.PinOutput csPin pin.OutputFunc
blPin drivers.PinOutput blPin pin.OutputFunc
width int16 width int16
height int16 height int16
columnOffset int16 columnOffset int16
@@ -65,17 +65,17 @@ 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 legacy.PinOutput) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) 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 legacy.PinOutput) DeviceOf[T] { func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(resetPin) pin.ConfigureOutput(resetPin)
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(blPin) pin.ConfigureOutput(blPin)
return DeviceOf[T]{ return DeviceOf[T]{
bus: bus, bus: bus,
dcPin: dcPin.Set, dcPin: dcPin.Set,
@@ -114,11 +114,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchData = pixel.NewImage[T](int(d.batchLength), 1) d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
// reset the device // reset the device
d.resetPin(true) d.resetPin.High()
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
d.resetPin(false) d.resetPin.Low()
time.Sleep(20 * time.Millisecond) time.Sleep(20 * time.Millisecond)
d.resetPin(true) d.resetPin.High()
time.Sleep(150 * time.Millisecond) time.Sleep(150 * time.Millisecond)
// Common initialization // Common initialization
@@ -226,7 +226,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.SetRotation(d.rotation) 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 // 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" "errors"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -46,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 pin.OutputFunc
resetPin drivers.PinOutput resetPin pin.OutputFunc
csPin drivers.PinOutput csPin pin.OutputFunc
blPin drivers.PinOutput blPin pin.OutputFunc
width int16 width int16
height int16 height int16
columnOffsetCfg int16 columnOffsetCfg int16
@@ -83,19 +83,19 @@ 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 legacy.PinOutput) Device { func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) 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 legacy.PinOutput) DeviceOf[T] { func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(resetPin) pin.ConfigureOutput(resetPin)
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(blPin) pin.ConfigureOutput(blPin)
var cs drivers.PinOutput var cs pin.OutputFunc
if !legacy.PinIsNoPin(csPin) { if !pin.IsNotPin(csPin) {
cs = csPin.Set cs = csPin.Set
} }
return DeviceOf[T]{ return DeviceOf[T]{
@@ -143,11 +143,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchLength += d.batchLength & 1 d.batchLength += d.batchLength & 1
// Reset the device // Reset the device
d.resetPin(true) d.resetPin.High()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
d.resetPin(false) d.resetPin.Low()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
d.resetPin(true) d.resetPin.High()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
// Common initialization // Common initialization
@@ -213,7 +213,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
time.Sleep(10 * time.Millisecond) // time.Sleep(10 * time.Millisecond) //
d.endWrite() 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 // 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. // meaning that data can be sent right away.
func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error { func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
d.cmdBuf[0] = command d.cmdBuf[0] = command
d.dcPin(false) d.dcPin.Low()
err := d.bus.Tx(d.cmdBuf[:1], nil) err := d.bus.Tx(d.cmdBuf[:1], nil)
d.dcPin(true) d.dcPin.High()
if len(data) != 0 { if len(data) != 0 {
err = d.bus.Tx(data, nil) err = d.bus.Tx(data, nil)
} }
@@ -234,7 +234,7 @@ func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
// chip select pin low. // chip select pin low.
func (d *DeviceOf[T]) startWrite() { func (d *DeviceOf[T]) startWrite() {
if d.csPin != nil { if d.csPin != nil {
d.csPin(false) d.csPin.Low()
} }
} }
@@ -242,7 +242,7 @@ func (d *DeviceOf[T]) startWrite() {
// select pin high. // select pin high.
func (d *DeviceOf[T]) endWrite() { func (d *DeviceOf[T]) endWrite() {
if d.csPin != nil { 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 { func (d *DeviceOf[T]) GetScanLine() uint16 {
d.startWrite() d.startWrite()
data := []uint8{0x00, 0x00} data := []uint8{0x00, 0x00}
d.dcPin(false) d.dcPin.Low()
d.bus.Transfer(GSCAN) d.bus.Transfer(GSCAN)
d.dcPin(true) d.dcPin.High()
for i := range data { for i := range data {
data[i], _ = d.bus.Transfer(0xFF) data[i], _ = d.bus.Transfer(0xFF)
} }
@@ -545,9 +545,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight // EnableBacklight enables or disables the backlight
func (d *DeviceOf[T]) EnableBacklight(enable bool) { func (d *DeviceOf[T]) EnableBacklight(enable bool) {
if enable { if enable {
d.blPin(true) d.blPin.High()
} else { } else {
d.blPin(false) d.blPin.Low()
} }
} }
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/lora" "tinygo.org/x/drivers/lora"
) )
@@ -22,7 +22,7 @@ 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 pin.OutputFunc // 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
@@ -43,7 +43,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 legacy.PinOutput) *Device { func New(spi drivers.SPI, rstPin pin.Output) *Device {
k := Device{ k := Device{
spi: spi, spi: spi,
rstPin: rstPin.Set, rstPin: rstPin.Set,
+17 -17
View File
@@ -11,7 +11,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel" "tinygo.org/x/drivers/pixel"
) )
@@ -31,9 +31,9 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
width int16 width int16
height int16 height int16
@@ -49,11 +49,11 @@ 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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
return Device{ return Device{
bus: bus, bus: bus,
cs: csPin.Set, cs: csPin.Set,
@@ -133,9 +133,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device // Reset resets the device
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(false) d.rst.Low()
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
d.WaitUntilIdle() d.WaitUntilIdle()
} }
@@ -152,18 +152,18 @@ func (d *Device) PowerOn() {
// SendCommand sends a command to the display // SendCommand sends a command to the display
func (d *Device) SendCommand(command uint8) { func (d *Device) SendCommand(command uint8) {
d.dc(false) d.dc.Low()
d.cs(false) d.cs.Low()
d.bus.Transfer(command) d.bus.Transfer(command)
d.cs(true) d.cs.High()
} }
// SendData sends a data byte to the display // SendData sends a data byte to the display
func (d *Device) SendData(data ...uint8) { func (d *Device) SendData(data ...uint8) {
d.dc(true) d.dc.High()
d.cs(false) d.cs.Low()
d.bus.Tx(data, nil) d.bus.Tx(data, nil)
d.cs(true) d.cs.High()
} }
// SetPixel modifies the internal buffer in a single pixel. // SetPixel modifies the internal buffer in a single pixel.
+19 -19
View File
@@ -14,7 +14,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -26,9 +26,9 @@ type Config struct {
type Device struct { type Device struct {
bus *machine.SPI bus *machine.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
configurePins func() configurePins func()
buffer []uint8 buffer []uint8
@@ -82,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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus *machine.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) 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,
@@ -91,17 +91,17 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy
rst: rstPin.Set, rst: rstPin.Set,
isBusy: busyPin.Get, isBusy: busyPin.Get,
configurePins: func() { configurePins: func() {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
}, },
} }
} }
func (d *Device) LDirInit(cfg Config) { func (d *Device) LDirInit(cfg Config) {
if d.configurePins == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.configurePins() d.configurePins()
@@ -160,7 +160,7 @@ func (d *Device) LDirInit(cfg Config) {
func (d *Device) HDirInit(cfg Config) { func (d *Device) HDirInit(cfg Config) {
if d.configurePins == nil { if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated) panic(pin.ErrConfigBeforeInstantiated)
} }
d.configurePins() d.configurePins()
@@ -240,11 +240,11 @@ func (d *Device) setLUT(lut [159]uint8) {
// Reset resets the display. // Reset resets the display.
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(true) d.rst.High()
time.Sleep(20 * time.Millisecond) time.Sleep(20 * time.Millisecond)
d.rst(false) d.rst.Low()
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(20 * time.Millisecond) 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dc(false) d.dc.Low()
} else { } else {
d.dc(true) d.dc.High()
} }
d.cs(false) d.cs.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.cs(true) d.cs.High()
} }
// SetPixel modifies the internal buffer in a single pixel. // SetPixel modifies the internal buffer in a single pixel.
@@ -429,5 +429,5 @@ func (d *Device) Sleep() {
d.SendData(0x01) d.SendData(0x01)
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rst(false) d.rst.Low()
} }
+18 -18
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -21,9 +21,9 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
logicalWidth int16 logicalWidth int16
width int16 width int16
@@ -53,11 +53,11 @@ 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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
return Device{ return Device{
bus: bus, bus: bus,
cs: csPin.Set, cs: csPin.Set,
@@ -91,9 +91,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF d.buffer[i] = 0xFF
} }
d.cs(false) d.cs.Low()
d.dc(false) d.dc.Low()
d.rst(false) d.rst.Low()
d.Reset() d.Reset()
@@ -119,9 +119,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device // Reset resets the device
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(false) d.rst.Low()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(200 * time.Millisecond) 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dc(false) d.dc.Low()
} else { } else {
d.dc(true) d.dc.High()
} }
d.cs(false) d.cs.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.cs(true) d.cs.High()
} }
// SetLUT sets the look up tables for full or partial updates // SetLUT sets the look up tables for full or partial updates
+18 -18
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -20,9 +20,9 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
width int16 width int16
height int16 height int16
@@ -33,11 +33,11 @@ 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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
return Device{ return Device{
bus: bus, bus: bus,
cs: csPin.Set, cs: csPin.Set,
@@ -75,9 +75,9 @@ func (d *Device) Configure(cfg Config) {
} }
} }
d.cs(false) d.cs.Low()
d.dc(false) d.dc.Low()
d.rst(false) d.rst.Low()
d.Reset() d.Reset()
@@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device // Reset resets the device
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(false) d.rst.Low()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(200 * time.Millisecond) 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dc(false) d.dc.Low()
} else { } else {
d.dc(true) d.dc.High()
} }
d.cs(false) d.cs.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.cs(true) d.cs.High()
} }
// SetPixel modifies the internal buffer in a single pixel. // SetPixel modifies the internal buffer in a single pixel.
+16 -15
View File
@@ -8,6 +8,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
const ( const (
@@ -19,9 +20,9 @@ const Baudrate = 4_000_000 // 4 MHz
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
blackBuffer []byte blackBuffer []byte
@@ -176,11 +177,11 @@ func (d *Device) setCursor(x, y uint16) error {
} }
func (d *Device) hwReset() { func (d *Device) hwReset() {
d.rst(true) d.rst.High()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
d.rst(false) d.rst.Low()
time.Sleep(2 * time.Millisecond) time.Sleep(2 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
} }
@@ -232,26 +233,26 @@ func (d *Device) sendCommandSequence(seq []byte) error {
} }
func (d *Device) sendCommandByte(b byte) error { func (d *Device) sendCommandByte(b byte) error {
d.dc(false) d.dc.Low()
d.cs(false) d.cs.Low()
_, err := d.bus.Transfer(b) _, err := d.bus.Transfer(b)
d.cs(true) d.cs.High()
return err return err
} }
func (d *Device) sendDataByte(b byte) error { func (d *Device) sendDataByte(b byte) error {
d.dc(true) d.dc.High()
d.cs(false) d.cs.Low()
_, err := d.bus.Transfer(b) _, err := d.bus.Transfer(b)
d.cs(true) d.cs.High()
return err return err
} }
func (d *Device) sendData(b []byte) error { func (d *Device) sendData(b []byte) error {
d.dc(true) d.dc.High()
d.cs(false) d.cs.Low()
err := d.bus.Tx(b, nil) err := d.bus.Tx(b, nil)
d.cs(true) d.cs.High()
return err return err
} }
+19 -19
View File
@@ -16,7 +16,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -28,9 +28,9 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
logicalWidth int16 logicalWidth int16
width int16 width int16
@@ -61,11 +61,11 @@ 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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
return Device{ return Device{
bus: bus, bus: bus,
cs: csPin.Set, cs: csPin.Set,
@@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF d.buffer[i] = 0xFF
} }
d.cs(false) d.cs.Low()
d.dc(false) d.dc.Low()
d.rst(false) d.rst.Low()
d.Reset() d.Reset()
@@ -122,14 +122,14 @@ func (d *Device) Configure(cfg Config) {
d.SendCommand(DATA_ENTRY_MODE_SETTING) d.SendCommand(DATA_ENTRY_MODE_SETTING)
d.SendData(0x03) // X increment; Y increment d.SendData(0x03) // X increment; Y increment
d.SetLUT(true) d.SetLUT.High()
} }
// Reset resets the device // Reset resets the device
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(false) d.rst.Low()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(200 * time.Millisecond) 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dc(false) d.dc.Low()
} else { } else {
d.dc(true) d.dc.High()
} }
d.cs(false) d.cs.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.cs(true) d.cs.High()
} }
// SetLUT sets the look up tables for full or partial updates // SetLUT sets the look up tables for full or partial updates
+18 -18
View File
@@ -13,7 +13,7 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/internal/pin"
) )
type Config struct { type Config struct {
@@ -25,9 +25,9 @@ type Config struct {
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs drivers.PinOutput cs pin.OutputFunc
dc drivers.PinOutput dc pin.OutputFunc
rst drivers.PinOutput rst pin.OutputFunc
isBusy drivers.PinInput isBusy drivers.PinInput
logicalWidth int16 logicalWidth int16
width int16 width int16
@@ -40,11 +40,11 @@ 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 legacy.PinOutput, busyPin legacy.PinInput) Device { func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin) pin.ConfigureOutput(csPin)
legacy.ConfigurePinOut(dcPin) pin.ConfigureOutput(dcPin)
legacy.ConfigurePinOut(rstPin) pin.ConfigureOutput(rstPin)
legacy.ConfigurePinInput(busyPin) pin.ConfigureInput(busyPin)
return Device{ return Device{
bus: bus, bus: bus,
cs: csPin.Set, cs: csPin.Set,
@@ -78,9 +78,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF d.buffer[i] = 0xFF
} }
d.cs(false) d.cs.Low()
d.dc(false) d.dc.Low()
d.rst(false) d.rst.Low()
d.Reset() d.Reset()
d.SendCommand(POWER_SETTING) d.SendCommand(POWER_SETTING)
@@ -104,9 +104,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device // Reset resets the device
func (d *Device) Reset() { func (d *Device) Reset() {
d.rst(false) d.rst.Low()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
d.rst(true) d.rst.High()
time.Sleep(200 * time.Millisecond) 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 // sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) { func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand { if isCommand {
d.dc(false) d.dc.Low()
} else { } else {
d.dc(true) d.dc.High()
} }
d.cs(false) d.cs.Low()
d.bus.Transfer(data) d.bus.Transfer(data)
d.cs(true) d.cs.High()
} }
// SetLUT sets the look up tables for full or partial updates // SetLUT sets the look up tables for full or partial updates