Skip to content

Commit c7229dd

Browse files
committed
bring #753 changes over to new pin HAL
1 parent 744fda5 commit c7229dd

File tree

33 files changed

+525
-410
lines changed

33 files changed

+525
-410
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
}

easystepper/easystepper.go

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

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

1010
// StepMode determines the coil sequence used to perform a single step
@@ -30,28 +30,10 @@ func (sm StepMode) stepCount() uint {
3030
}
3131
}
3232

33-
// DeviceConfig contains the configuration data for a single easystepper driver
34-
type DeviceConfig struct {
35-
// Pin1 ... Pin4 determines the pins to configure and use for the device
36-
Pin1, Pin2, Pin3, Pin4 machine.Pin
37-
// StepCount is the number of steps required to perform a full revolution of the stepper motor
38-
StepCount uint
39-
// RPM determines the speed of the stepper motor in 'Revolutions per Minute'
40-
RPM uint
41-
// Mode determines the coil sequence used to perform a single step
42-
Mode StepMode
43-
}
44-
45-
// DualDeviceConfig contains the configuration data for a dual easystepper driver
46-
type DualDeviceConfig struct {
47-
DeviceConfig
48-
// Pin5 ... Pin8 determines the pins to configure and use for the second device
49-
Pin5, Pin6, Pin7, Pin8 machine.Pin
50-
}
51-
5233
// Device holds the pins and the delay between steps
5334
type Device struct {
54-
pins [4]machine.Pin
35+
pins [4]pin.OutputFunc
36+
config func()
5537
stepDelay time.Duration
5638
stepNumber uint8
5739
stepMode StepMode
@@ -62,51 +44,6 @@ type DualDevice struct {
6244
devices [2]*Device
6345
}
6446

65-
// New returns a new single easystepper driver given a DeviceConfig
66-
func New(config DeviceConfig) (*Device, error) {
67-
if config.StepCount == 0 || config.RPM == 0 {
68-
return nil, errors.New("config.StepCount and config.RPM must be > 0")
69-
}
70-
return &Device{
71-
pins: [4]machine.Pin{config.Pin1, config.Pin2, config.Pin3, config.Pin4},
72-
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
73-
stepMode: config.Mode,
74-
}, nil
75-
}
76-
77-
// Configure configures the pins of the Device
78-
func (d *Device) Configure() {
79-
for _, pin := range d.pins {
80-
pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
81-
}
82-
}
83-
84-
// NewDual returns a new dual easystepper driver given 8 pins, number of steps and rpm
85-
func NewDual(config DualDeviceConfig) (*DualDevice, error) {
86-
// Create the first device
87-
dev1, err := New(config.DeviceConfig)
88-
if err != nil {
89-
return nil, err
90-
}
91-
// Create the second device
92-
config.DeviceConfig.Pin1 = config.Pin5
93-
config.DeviceConfig.Pin2 = config.Pin6
94-
config.DeviceConfig.Pin3 = config.Pin7
95-
config.DeviceConfig.Pin4 = config.Pin8
96-
dev2, err := New(config.DeviceConfig)
97-
if err != nil {
98-
return nil, err
99-
}
100-
// Return composite dual device
101-
return &DualDevice{devices: [2]*Device{dev1, dev2}}, nil
102-
}
103-
104-
// Configure configures the pins of the DualDevice
105-
func (d *DualDevice) Configure() {
106-
d.devices[0].Configure()
107-
d.devices[1].Configure()
108-
}
109-
11047
// Move rotates the motor the number of given steps
11148
// (negative steps will rotate it the opposite direction)
11249
func (d *Device) Move(steps int32) {

easystepper/easystepper_go.go

Whitespace-only changes.

0 commit comments

Comments
 (0)