Skip to content

Commit 38c606d

Browse files
deadprogramysoldak
authored andcommitted
refactor: add display.Pin interface and use it everywhere that is easy to do so
Signed-off-by: deadprogram <[email protected]>
1 parent d02d21e commit 38c606d

File tree

28 files changed

+164
-236
lines changed

28 files changed

+164
-236
lines changed

apa102/apa102.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
55

66
import (
77
"image/color"
8-
"machine"
98

109
"tinygo.org/x/drivers"
1110
)
@@ -37,7 +36,7 @@ func New(b drivers.SPI) *Device {
3736

3837
// NewSoftwareSPI returns a new APA102 driver that will use a software based
3938
// implementation of the SPI protocol.
40-
func NewSoftwareSPI(sckPin, sdoPin machine.Pin, delay uint32) *Device {
39+
func NewSoftwareSPI(sckPin, sdoPin drivers.Pin, delay uint32) *Device {
4140
return New(&bbSPI{SCK: sckPin, SDO: sdoPin, Delay: delay})
4241
}
4342

apa102/softspi.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package apa102
22

3-
import "machine"
3+
import (
4+
"tinygo.org/x/drivers"
5+
)
46

57
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
68
// to mode 0 and ignores trying to receive data. Just enough for the APA102.
79
// Note: making this unexported for now because it is probable not suitable
810
// most purposes other than the APA102 package. It might be desirable to make
911
// this more generic and include it in the TinyGo "machine" package instead.
1012
type bbSPI struct {
11-
SCK machine.Pin
12-
SDO machine.Pin
13+
SCK drivers.Pin
14+
SDO drivers.Pin
1315
Delay uint32
1416
}
1517

16-
// Configure sets up the SCK and SDO pins as outputs and sets them low
18+
// Configure sets the SCK and SDO pins to low.
19+
// Note that the SCK and SDO pins must already be configured as outputs.
1720
func (s *bbSPI) Configure() {
18-
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
19-
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
2021
s.SCK.Low()
2122
s.SDO.Low()
2223
if s.Delay == 0 {

bmi160/bmi160.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bmi160
22

33
import (
4-
"machine"
54
"time"
65

76
"tinygo.org/x/drivers"
@@ -11,7 +10,7 @@ import (
1110
// also an I2C interface, but it is not yet supported.
1211
type DeviceSPI struct {
1312
// Chip select pin
14-
CSB machine.Pin
13+
CSB drivers.Pin
1514

1615
buf [7]byte
1716

@@ -22,18 +21,16 @@ type DeviceSPI struct {
2221
// NewSPI returns a new device driver. The pin and SPI interface are not
2322
// touched, provide a fully configured SPI object and call Configure to start
2423
// using this device.
25-
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
24+
func NewSPI(csb drivers.Pin, spi drivers.SPI) *DeviceSPI {
2625
return &DeviceSPI{
2726
CSB: csb, // chip select
2827
Bus: spi,
2928
}
3029
}
3130

32-
// Configure configures the BMI160 for use. It configures the CSB pin and
33-
// configures the BMI160, but it does not configure the SPI interface (it is
34-
// assumed to be up and running).
31+
// Configure configures the BMI160 for use. The CSB pin anf the SPI interface
32+
// should be configured already. This function configures the BMI160 only.
3533
func (d *DeviceSPI) Configure() error {
36-
d.CSB.Configure(machine.PinConfig{Mode: machine.PinOutput})
3734
d.CSB.High()
3835

3936
// The datasheet recommends doing a register read from address 0x7F to get

buzzer/buzzer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
package buzzer // import "tinygo.org/x/drivers/buzzer"
33

44
import (
5-
"machine"
6-
75
"time"
6+
7+
"tinygo.org/x/drivers"
88
)
99

1010
// Device wraps a GPIO connection to a buzzer.
1111
type Device struct {
12-
pin machine.Pin
12+
pin drivers.Pin
1313
High bool
1414
BPM float64
1515
}
1616

1717
// New returns a new buzzer driver given which pin to use
18-
func New(pin machine.Pin) Device {
18+
func New(pin drivers.Pin) Device {
1919
return Device{
2020
pin: pin,
2121
High: false,

easystepper/easystepper.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package easystepper // import "tinygo.org/x/drivers/easystepper"
33

44
import (
55
"errors"
6-
"machine"
76
"time"
7+
8+
"tinygo.org/x/drivers"
89
)
910

1011
// StepMode determines the coil sequence used to perform a single step
@@ -33,7 +34,7 @@ func (sm StepMode) stepCount() uint {
3334
// DeviceConfig contains the configuration data for a single easystepper driver
3435
type DeviceConfig struct {
3536
// Pin1 ... Pin4 determines the pins to configure and use for the device
36-
Pin1, Pin2, Pin3, Pin4 machine.Pin
37+
Pin1, Pin2, Pin3, Pin4 drivers.Pin
3738
// StepCount is the number of steps required to perform a full revolution of the stepper motor
3839
StepCount uint
3940
// RPM determines the speed of the stepper motor in 'Revolutions per Minute'
@@ -46,12 +47,12 @@ type DeviceConfig struct {
4647
type DualDeviceConfig struct {
4748
DeviceConfig
4849
// Pin5 ... Pin8 determines the pins to configure and use for the second device
49-
Pin5, Pin6, Pin7, Pin8 machine.Pin
50+
Pin5, Pin6, Pin7, Pin8 drivers.Pin
5051
}
5152

5253
// Device holds the pins and the delay between steps
5354
type Device struct {
54-
pins [4]machine.Pin
55+
pins [4]drivers.Pin
5556
stepDelay time.Duration
5657
stepNumber uint8
5758
stepMode StepMode
@@ -68,17 +69,15 @@ func New(config DeviceConfig) (*Device, error) {
6869
return nil, errors.New("config.StepCount and config.RPM must be > 0")
6970
}
7071
return &Device{
71-
pins: [4]machine.Pin{config.Pin1, config.Pin2, config.Pin3, config.Pin4},
72+
pins: [4]drivers.Pin{config.Pin1, config.Pin2, config.Pin3, config.Pin4},
7273
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
7374
stepMode: config.Mode,
7475
}, nil
7576
}
7677

77-
// Configure configures the pins of the Device
78+
// Configure does nothing, as it assumes that the pins of the Device have already
79+
// been configured by the user as outputs.
7880
func (d *Device) Configure() {
79-
for _, pin := range d.pins {
80-
pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
81-
}
8281
}
8382

8483
// NewDual returns a new dual easystepper driver given 8 pins, number of steps and rpm

ft6336/ft6336.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package ft6336
66

77
import (
8-
"machine"
9-
108
"tinygo.org/x/drivers"
119
"tinygo.org/x/drivers/internal/legacy"
1210
"tinygo.org/x/drivers/touch"
@@ -17,11 +15,11 @@ type Device struct {
1715
bus drivers.I2C
1816
buf []byte
1917
Address uint8
20-
intPin machine.Pin
18+
intPin drivers.Pin
2119
}
2220

2321
// New returns FT6336 device for the provided I2C bus using default address.
24-
func New(i2c drivers.I2C, intPin machine.Pin) *Device {
22+
func New(i2c drivers.I2C, intPin drivers.Pin) *Device {
2523
return &Device{
2624
bus: i2c,
2725
buf: make([]byte, 11),
@@ -34,10 +32,10 @@ func New(i2c drivers.I2C, intPin machine.Pin) *Device {
3432
type Config struct {
3533
}
3634

37-
// Configure the FT6336 device.
35+
// Configure the FT6336 device. Note that the interrupt pin must be configured
36+
// separately as an input.
3837
func (d *Device) Configure(config Config) error {
3938
d.write1Byte(0xA4, 0x00)
40-
d.intPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
4139
return nil
4240
}
4341

gc9a01/gc9a01.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package gc9a01 // import "tinygo.org/x/drivers/gc9a01"
55

66
import (
77
"image/color"
8-
"machine"
98
"time"
109

1110
"errors"
@@ -22,10 +21,10 @@ type FrameRate uint8
2221
// Device wraps an SPI connection.
2322
type Device struct {
2423
bus drivers.SPI
25-
dcPin machine.Pin
26-
resetPin machine.Pin
27-
csPin machine.Pin
28-
blPin machine.Pin
24+
dcPin drivers.Pin
25+
resetPin drivers.Pin
26+
csPin drivers.Pin
27+
blPin drivers.Pin
2928
width int16
3029
height int16
3130
columnOffsetCfg int16
@@ -51,12 +50,9 @@ type Config struct {
5150
Height int16
5251
}
5352

54-
// New creates a new ST7789 connection. The SPI wire must already be configured.
55-
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
56-
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
57-
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
58-
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
59-
blPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
53+
// New creates a new ST7789 connection. The SPI wire must already be configured, along with the
54+
// data/command and reset pins as outputs.
55+
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.Pin) Device {
6056
return Device{
6157
bus: bus,
6258
resetPin: resetPin,

hcsr04/hcsr04.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@
55
package hcsr04
66

77
import (
8-
"machine"
98
"time"
9+
10+
"tinygo.org/x/drivers"
1011
)
1112

1213
const TIMEOUT = 23324 // max sensing distance (4m)
1314

1415
// Device holds the pins
1516
type Device struct {
16-
trigger machine.Pin
17-
echo machine.Pin
17+
trigger drivers.Pin
18+
echo drivers.Pin
1819
}
1920

2021
// New returns a new ultrasonic driver given 2 pins
21-
func New(trigger, echo machine.Pin) Device {
22+
func New(trigger, echo drivers.Pin) Device {
2223
return Device{
2324
trigger: trigger,
2425
echo: echo,
2526
}
2627
}
2728

28-
// Configure configures the pins of the Device
29+
// Configure expects that the pins of the Device have already
30+
// been configured by the user. Trigger pin should be an output
31+
// and echo pin should be an input.
2932
func (d *Device) Configure() {
30-
d.trigger.Configure(machine.PinConfig{Mode: machine.PinOutput})
31-
d.echo.Configure(machine.PinConfig{Mode: machine.PinInput})
3233
}
3334

3435
// ReadDistance returns the distance of the object in mm

max6675/max6675.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package max6675
33

44
import (
55
"errors"
6-
"machine"
76

87
"tinygo.org/x/drivers"
98
)
@@ -14,13 +13,13 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
1413

1514
type Device struct {
1615
bus drivers.SPI
17-
cs machine.Pin
16+
cs drivers.Pin
1817
}
1918

2019
// Create a new Device to read from a MAX6675 thermocouple.
2120
// Pins must be configured before use. Frequency for SPI
2221
// should be 4.3MHz maximum.
23-
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
22+
func NewDevice(bus drivers.SPI, cs drivers.Pin) *Device {
2423
return &Device{
2524
bus: bus,
2625
cs: cs,

max72xx/max72xx.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@
33
package max72xx
44

55
import (
6-
"machine"
7-
86
"tinygo.org/x/drivers"
97
)
108

119
type Device struct {
1210
bus drivers.SPI
13-
cs machine.Pin
11+
cs drivers.Pin
1412
}
1513

1614
// NewDriver creates a new max7219 connection. The SPI wire must already be configured
1715
// The SPI frequency must not be higher than 10MHz.
1816
// parameter cs: the datasheet also refers to this pin as "load" pin.
19-
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
17+
func NewDevice(bus drivers.SPI, cs drivers.Pin) *Device {
2018
return &Device{
2119
bus: bus,
2220
cs: cs,
2321
}
2422
}
2523

26-
// Configure setups the pins.
24+
// Configure expects that the pin of the Device has already been configured by the user as output.
2725
func (driver *Device) Configure() {
28-
outPutConfig := machine.PinConfig{Mode: machine.PinOutput}
29-
30-
driver.cs.Configure(outPutConfig)
3126
}
3227

3328
// SetScanLimit sets the scan limit. Maximum is 8.

0 commit comments

Comments
 (0)