Skip to content

Commit 8f0fbaa

Browse files
committed
more drivers are now cross platform
1 parent f3945b1 commit 8f0fbaa

File tree

20 files changed

+481
-325
lines changed

20 files changed

+481
-325
lines changed

apa102/softspi.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apa102
22

33
import (
44
"tinygo.org/x/drivers"
5+
"tinygo.org/x/drivers/internal/legacy"
56
)
67

78
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
@@ -18,6 +19,9 @@ type bbSPI struct {
1819

1920
// Configure sets up the SCK and SDO pins as outputs and sets them low
2021
func (s *bbSPI) Configure() {
22+
if s.config == nil {
23+
panic(legacy.ErrConfigBeforeInstantiated)
24+
}
2125
s.config()
2226
s.SCK(false)
2327
s.SDO(false)

bmi160/bmi160.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
// also an I2C interface, but it is not yet supported.
1212
type DeviceSPI struct {
1313
// Chip select pin
14-
CSB drivers.PinOutput
14+
csb drivers.PinOutput
1515

1616
buf [7]byte
1717

1818
// SPI bus (requires chip select to be usable).
19-
Bus drivers.SPI
19+
bus drivers.SPI
2020
config func()
2121
}
2222

@@ -25,8 +25,8 @@ type DeviceSPI struct {
2525
// using this device.
2626
func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
2727
return &DeviceSPI{
28-
CSB: csb.Set, // chip select
29-
Bus: spi,
28+
csb: csb.Set, // chip select
29+
bus: spi,
3030
config: func() {
3131
legacy.ConfigurePinOut(csb)
3232
},
@@ -37,7 +37,11 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
3737
// configures the BMI160, but it does not configure the SPI interface (it is
3838
// assumed to be up and running).
3939
func (d *DeviceSPI) Configure() error {
40-
d.CSB(true)
40+
if d.config == nil {
41+
return legacy.ErrConfigBeforeInstantiated
42+
}
43+
d.config()
44+
d.csb(true)
4145

4246
// The datasheet recommends doing a register read from address 0x7F to get
4347
// SPI communication going:
@@ -89,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
8993
data[0] = 0x80 | reg_TEMPERATURE_0
9094
data[1] = 0
9195
data[2] = 0
92-
d.CSB(false)
93-
err = d.Bus.Tx(data, data)
94-
d.CSB(true)
96+
d.csb(false)
97+
err = d.bus.Tx(data, data)
98+
d.csb(true)
9599
if err != nil {
96100
return
97101
}
@@ -126,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
126130
for i := 1; i < len(data); i++ {
127131
data[i] = 0
128132
}
129-
d.CSB(false)
130-
err = d.Bus.Tx(data, data)
131-
d.CSB(true)
133+
d.csb(false)
134+
err = d.bus.Tx(data, data)
135+
d.csb(true)
132136
if err != nil {
133137
return
134138
}
@@ -156,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
156160
for i := 1; i < len(data); i++ {
157161
data[i] = 0
158162
}
159-
d.CSB(false)
160-
err = d.Bus.Tx(data, data)
161-
d.CSB(true)
163+
d.csb(false)
164+
err = d.bus.Tx(data, data)
165+
d.csb(true)
162166
if err != nil {
163167
return
164168
}
@@ -204,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
204208
data := d.buf[:2]
205209
data[0] = 0x80 | address
206210
data[1] = 0
207-
d.CSB(false)
208-
d.Bus.Tx(data, data)
209-
d.CSB(true)
211+
d.csb(false)
212+
d.bus.Tx(data, data)
213+
d.csb(true)
210214
return data[1]
211215
}
212216

@@ -220,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
220224
buf[0] = address
221225
buf[1] = data
222226

223-
d.CSB(false)
224-
d.Bus.Tx(buf, buf)
225-
d.CSB(true)
227+
d.csb(false)
228+
d.bus.Tx(buf, buf)
229+
d.csb(true)
226230
}

easystepper/easystepper.go

Lines changed: 55 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
//go:build baremetal
2+
13
// Package easystepper provides a simple driver to rotate a 4-wire stepper motor.
24
package easystepper // import "tinygo.org/x/drivers/easystepper"
35

46
import (
5-
"errors"
6-
"machine"
77
"time"
8+
9+
"tinygo.org/x/drivers"
810
)
911

1012
// StepMode determines the coil sequence used to perform a single step
@@ -30,28 +32,10 @@ func (sm StepMode) stepCount() uint {
3032
}
3133
}
3234

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-
5235
// Device holds the pins and the delay between steps
5336
type Device struct {
54-
pins [4]machine.Pin
37+
pins [4]drivers.PinOutput
38+
config func()
5539
stepDelay time.Duration
5640
stepNumber uint8
5741
stepMode StepMode
@@ -62,51 +46,6 @@ type DualDevice struct {
6246
devices [2]*Device
6347
}
6448

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-
11049
// Move rotates the motor the number of given steps
11150
// (negative steps will rotate it the opposite direction)
11251
func (d *Device) Move(steps int32) {
@@ -126,7 +65,7 @@ func (d *Device) Move(steps int32) {
12665
// Off turns off all motor pins
12766
func (d *Device) Off() {
12867
for _, pin := range d.pins {
129-
pin.Low()
68+
pin(false)
13069
}
13170
}
13271

@@ -187,28 +126,28 @@ func (d *Device) stepMotor(step uint8) {
187126
func (d *Device) stepMotor4(step uint8) {
188127
switch step {
189128
case 0:
190-
d.pins[0].High()
191-
d.pins[1].Low()
192-
d.pins[2].High()
193-
d.pins[3].Low()
129+
d.pins[0](true)
130+
d.pins[1](false)
131+
d.pins[2](true)
132+
d.pins[3](false)
194133
break
195134
case 1:
196-
d.pins[0].Low()
197-
d.pins[1].High()
198-
d.pins[2].High()
199-
d.pins[3].Low()
135+
d.pins[0](false)
136+
d.pins[1](true)
137+
d.pins[2](true)
138+
d.pins[3](false)
200139
break
201140
case 2:
202-
d.pins[0].Low()
203-
d.pins[1].High()
204-
d.pins[2].Low()
205-
d.pins[3].High()
141+
d.pins[0](false)
142+
d.pins[1](true)
143+
d.pins[2](false)
144+
d.pins[3](true)
206145
break
207146
case 3:
208-
d.pins[0].High()
209-
d.pins[1].Low()
210-
d.pins[2].Low()
211-
d.pins[3].High()
147+
d.pins[0](true)
148+
d.pins[1](false)
149+
d.pins[2](false)
150+
d.pins[3](true)
212151
break
213152
}
214153
d.stepNumber = step
@@ -218,45 +157,45 @@ func (d *Device) stepMotor4(step uint8) {
218157
func (d *Device) stepMotor8(step uint8) {
219158
switch step {
220159
case 0:
221-
d.pins[0].High()
222-
d.pins[2].Low()
223-
d.pins[1].Low()
224-
d.pins[3].Low()
160+
d.pins[0](true)
161+
d.pins[2](false)
162+
d.pins[1](false)
163+
d.pins[3](false)
225164
case 1:
226-
d.pins[0].High()
227-
d.pins[2].High()
228-
d.pins[1].Low()
229-
d.pins[3].Low()
165+
d.pins[0](true)
166+
d.pins[2](true)
167+
d.pins[1](false)
168+
d.pins[3](false)
230169
case 2:
231-
d.pins[0].Low()
232-
d.pins[2].High()
233-
d.pins[1].Low()
234-
d.pins[3].Low()
170+
d.pins[0](false)
171+
d.pins[2](true)
172+
d.pins[1](false)
173+
d.pins[3](false)
235174
case 3:
236-
d.pins[0].Low()
237-
d.pins[2].High()
238-
d.pins[1].High()
239-
d.pins[3].Low()
175+
d.pins[0](false)
176+
d.pins[2](true)
177+
d.pins[1](true)
178+
d.pins[3](false)
240179
case 4:
241-
d.pins[0].Low()
242-
d.pins[2].Low()
243-
d.pins[1].High()
244-
d.pins[3].Low()
180+
d.pins[0](false)
181+
d.pins[2](false)
182+
d.pins[1](true)
183+
d.pins[3](false)
245184
case 5:
246-
d.pins[0].Low()
247-
d.pins[2].Low()
248-
d.pins[1].High()
249-
d.pins[3].High()
185+
d.pins[0](false)
186+
d.pins[2](false)
187+
d.pins[1](true)
188+
d.pins[3](true)
250189
case 6:
251-
d.pins[0].Low()
252-
d.pins[2].Low()
253-
d.pins[1].Low()
254-
d.pins[3].High()
190+
d.pins[0](false)
191+
d.pins[2](false)
192+
d.pins[1](false)
193+
d.pins[3](true)
255194
case 7:
256-
d.pins[0].High()
257-
d.pins[2].Low()
258-
d.pins[1].Low()
259-
d.pins[3].High()
195+
d.pins[0](true)
196+
d.pins[2](false)
197+
d.pins[1](false)
198+
d.pins[3](true)
260199
}
261200
d.stepNumber = step
262201
}

0 commit comments

Comments
 (0)