Skip to content

Commit 09fd013

Browse files
soypatdeadprogram
authored andcommitted
add simplest driver ports
1 parent 23833e6 commit 09fd013

File tree

10 files changed

+107
-74
lines changed

10 files changed

+107
-74
lines changed

apa102/apa102.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
55

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

109
"tinygo.org/x/drivers"
10+
"tinygo.org/x/drivers/internal/legacy"
11+
"tinygo.org/x/drivers/internal/pin"
1112
)
1213

1314
const (
@@ -37,8 +38,11 @@ func New(b drivers.SPI) *Device {
3738

3839
// NewSoftwareSPI returns a new APA102 driver that will use a software based
3940
// implementation of the SPI protocol.
40-
func NewSoftwareSPI(sckPin, sdoPin machine.Pin, delay uint32) *Device {
41-
return New(&bbSPI{SCK: sckPin, SDO: sdoPin, Delay: delay})
41+
func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
42+
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
43+
legacy.ConfigurePinOut(sckPin)
44+
legacy.ConfigurePinOut(sdoPin)
45+
}})
4246
}
4347

4448
// WriteColors writes the given RGBA color slice out using the APA102 protocol.

apa102/softspi.go

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

3-
import "machine"
3+
import (
4+
"tinygo.org/x/drivers/internal/legacy"
5+
"tinygo.org/x/drivers/internal/pin"
6+
)
47

58
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
69
// to mode 0 and ignores trying to receive data. Just enough for the APA102.
710
// Note: making this unexported for now because it is probable not suitable
811
// most purposes other than the APA102 package. It might be desirable to make
912
// this more generic and include it in the TinyGo "machine" package instead.
1013
type bbSPI struct {
11-
SCK machine.Pin
12-
SDO machine.Pin
13-
Delay uint32
14+
SCK pin.OutputFunc
15+
SDO pin.OutputFunc
16+
Delay uint32
17+
configurePins func()
1418
}
1519

1620
// Configure sets up the SCK and SDO pins as outputs and sets them low
1721
func (s *bbSPI) Configure() {
18-
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
19-
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
22+
if s.configurePins == nil {
23+
panic(legacy.ErrConfigBeforeInstantiated)
24+
}
25+
s.configurePins()
2026
s.SCK.Low()
2127
s.SDO.Low()
2228
if s.Delay == 0 {

bmi160/bmi160.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
package bmi160
22

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

76
"tinygo.org/x/drivers"
7+
"tinygo.org/x/drivers/internal/legacy"
8+
"tinygo.org/x/drivers/internal/pin"
89
)
910

1011
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
1112
// also an I2C interface, but it is not yet supported.
1213
type DeviceSPI struct {
1314
// Chip select pin
14-
CSB machine.Pin
15+
csb pin.OutputFunc
1516

1617
buf [7]byte
1718

1819
// SPI bus (requires chip select to be usable).
19-
Bus drivers.SPI
20+
bus drivers.SPI
21+
configurePins func()
2022
}
2123

2224
// NewSPI returns a new device driver. The pin and SPI interface are not
2325
// touched, provide a fully configured SPI object and call Configure to start
2426
// using this device.
25-
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
27+
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
2628
return &DeviceSPI{
27-
CSB: csb, // chip select
28-
Bus: spi,
29+
csb: csb.Set, // chip select
30+
bus: spi,
31+
configurePins: func() {
32+
legacy.ConfigurePinOut(csb)
33+
},
2934
}
3035
}
3136

3237
// Configure configures the BMI160 for use. It configures the CSB pin and
3338
// configures the BMI160, but it does not configure the SPI interface (it is
3439
// assumed to be up and running).
3540
func (d *DeviceSPI) Configure() error {
36-
d.CSB.Configure(machine.PinConfig{Mode: machine.PinOutput})
37-
d.CSB.High()
38-
41+
if d.configurePins == nil {
42+
return legacy.ErrConfigBeforeInstantiated
43+
}
44+
d.configurePins()
45+
d.csb.High()
3946
// The datasheet recommends doing a register read from address 0x7F to get
4047
// SPI communication going:
4148
// > If CSB sees a rising edge after power-up, the BMI160 interface switches
@@ -86,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
8693
data[0] = 0x80 | reg_TEMPERATURE_0
8794
data[1] = 0
8895
data[2] = 0
89-
d.CSB.Low()
90-
err = d.Bus.Tx(data, data)
91-
d.CSB.High()
96+
d.csb.Low()
97+
err = d.bus.Tx(data, data)
98+
d.csb.High()
9299
if err != nil {
93100
return
94101
}
@@ -123,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
123130
for i := 1; i < len(data); i++ {
124131
data[i] = 0
125132
}
126-
d.CSB.Low()
127-
err = d.Bus.Tx(data, data)
128-
d.CSB.High()
133+
d.csb.Low()
134+
err = d.bus.Tx(data, data)
135+
d.csb.High()
129136
if err != nil {
130137
return
131138
}
@@ -153,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
153160
for i := 1; i < len(data); i++ {
154161
data[i] = 0
155162
}
156-
d.CSB.Low()
157-
err = d.Bus.Tx(data, data)
158-
d.CSB.High()
163+
d.csb.Low()
164+
err = d.bus.Tx(data, data)
165+
d.csb.High()
159166
if err != nil {
160167
return
161168
}
@@ -201,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
201208
data := d.buf[:2]
202209
data[0] = 0x80 | address
203210
data[1] = 0
204-
d.CSB.Low()
205-
d.Bus.Tx(data, data)
206-
d.CSB.High()
211+
d.csb.Low()
212+
d.bus.Tx(data, data)
213+
d.csb.High()
207214
return data[1]
208215
}
209216

@@ -217,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
217224
buf[0] = address
218225
buf[1] = data
219226

220-
d.CSB.Low()
221-
d.Bus.Tx(buf, buf)
222-
d.CSB.High()
227+
d.csb.Low()
228+
d.bus.Tx(buf, buf)
229+
d.csb.High()
223230
}

buzzer/buzzer.go

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

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

1010
// Device wraps a GPIO connection to a buzzer.
1111
type Device struct {
12-
pin machine.Pin
12+
pin pin.OutputFunc
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 pin.Output) Device {
1919
return Device{
20-
pin: pin,
20+
pin: pin.Set,
2121
High: false,
2222
BPM: 96.0,
2323
}
2424
}
2525

2626
// On sets the buzzer to a high state.
2727
func (l *Device) On() (err error) {
28-
l.pin.Set(true)
28+
l.pin.High()
2929
l.High = true
3030
return
3131
}
3232

3333
// Off sets the buzzer to a low state.
3434
func (l *Device) Off() (err error) {
35-
l.pin.Set(false)
35+
l.pin.Low()
3636
l.High = false
3737
return
3838
}

ft6336/ft6336.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Guarded because still unsure of how to deal with interrupt drivers.
2+
//go:build tinygo
3+
14
// Package ft6336 provides a driver for the FT6336 I2C Self-Capacitive touch
25
// panel controller.
36
//

hd44780/hd44780.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (d *Device) SendCommand(command byte) {
210210
d.bus.SetCommandMode(true)
211211
d.bus.Write([]byte{command})
212212

213-
for d.busy(command == DISPLAY_CLEAR || command == CURSOR_HOME) {
213+
for d.isBusy(command == DISPLAY_CLEAR || command == CURSOR_HOME) {
214214
}
215215
}
216216

@@ -219,7 +219,7 @@ func (d *Device) sendData(data byte) {
219219
d.bus.SetCommandMode(false)
220220
d.bus.Write([]byte{data})
221221

222-
for d.busy(false) {
222+
for d.isBusy(false) {
223223
}
224224
}
225225

@@ -231,9 +231,9 @@ func (d *Device) CreateCharacter(cgramAddr uint8, data []byte) {
231231
}
232232
}
233233

234-
// busy returns true when hd447890 is busy
234+
// isBusy returns true when hd447890 is isBusy
235235
// or after the timeout specified
236-
func (d *Device) busy(longDelay bool) bool {
236+
func (d *Device) isBusy(longDelay bool) bool {
237237
if d.bus.WriteOnly() {
238238
// Can't read busy flag if write only, so sleep a bit then return
239239
if longDelay {
@@ -261,7 +261,7 @@ func (d *Device) busy(longDelay bool) bool {
261261

262262
// Busy returns true when hd447890 is busy
263263
func (d *Device) Busy() bool {
264-
return d.busy(false)
264+
return d.isBusy(false)
265265
}
266266

267267
// Size returns the current size of the display.

max6675/max6675.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package max6675
33

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

87
"tinygo.org/x/drivers"
8+
"tinygo.org/x/drivers/internal/pin"
99
)
1010

1111
// ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -14,16 +14,16 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
1414

1515
type Device struct {
1616
bus drivers.SPI
17-
cs machine.Pin
17+
cs pin.OutputFunc
1818
}
1919

2020
// Create a new Device to read from a MAX6675 thermocouple.
2121
// Pins must be configured before use. Frequency for SPI
2222
// should be 4.3MHz maximum.
23-
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
23+
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
2424
return &Device{
2525
bus: bus,
26-
cs: cs,
26+
cs: cs.Set,
2727
}
2828
}
2929

max72xx/max72xx.go

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

55
import (
6-
"machine"
7-
86
"tinygo.org/x/drivers"
7+
"tinygo.org/x/drivers/internal/legacy"
8+
"tinygo.org/x/drivers/internal/pin"
99
)
1010

1111
type Device struct {
12-
bus drivers.SPI
13-
cs machine.Pin
12+
bus drivers.SPI
13+
cs pin.OutputFunc
14+
configurePins func()
1415
}
1516

1617
// NewDriver creates a new max7219 connection. The SPI wire must already be configured
1718
// The SPI frequency must not be higher than 10MHz.
1819
// parameter cs: the datasheet also refers to this pin as "load" pin.
19-
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
20+
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
2021
return &Device{
2122
bus: bus,
22-
cs: cs,
23+
cs: cs.Set,
24+
configurePins: func() {
25+
legacy.ConfigurePinOut(cs)
26+
},
2327
}
2428
}
2529

2630
// Configure setups the pins.
2731
func (driver *Device) Configure() {
28-
outPutConfig := machine.PinConfig{Mode: machine.PinOutput}
29-
30-
driver.cs.Configure(outPutConfig)
32+
if driver.configurePins == nil {
33+
panic(legacy.ErrConfigBeforeInstantiated)
34+
}
35+
driver.configurePins()
3136
}
3237

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

0 commit comments

Comments
 (0)