Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pin: implement driver.Pin interface #215

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ endif
@md5sum ./build/test.hex

DRIVERS = $(wildcard */)
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x
NOTESTS = build examples flash semihosting pcd8544 microphone mcp3008 gps microbitmatrix \
ws2812 thermistor ili9341 wifinina shifter hd44780 espat l9110x bmi160 l293x touch
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))

unit-test:
@go test -v $(addprefix ./,$(TESTS))
@go test -v $(addprefix ./,$(TESTS)) ./waveshare-epd/...

test: clean fmt-check unit-test smoke-test
3 changes: 1 addition & 2 deletions apa102/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package apa102 // import "tinygo.org/x/drivers/apa102"

import (
"image/color"
"machine"

"tinygo.org/x/drivers"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func New(b drivers.SPI) Device {

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

Expand Down
15 changes: 15 additions & 0 deletions apa102/apa102_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package apa102

import (
"testing"

qt "github.com/frankban/quicktest"
"tinygo.org/x/drivers/tester"
)

func TestDefaultAPA102(t *testing.T) {
c := qt.New(t)
bus := tester.NewSPIBus(c)
dev := New(bus)
c.Assert(dev, qt.Not(qt.IsNil))
}
13 changes: 7 additions & 6 deletions apa102/softspi.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package apa102

import "machine"
import "tinygo.org/x/drivers"

// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
// to mode 0 and ignores trying to receive data. Just enough for the APA102.
// Note: making this unexported for now because it is probable not suitable
// 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.
type bbSPI struct {
SCK machine.Pin
SDO machine.Pin
SCK drivers.Pin
SDO drivers.Pin
Delay uint32
}

// Configure sets up the SCK and SDO pins as outputs and sets them low
// Configure sets up the SCK and SDO low.
// The pins must already be set as outputs in the main program that uses this driver.
func (s *bbSPI) Configure() {
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
// s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
// s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
s.SCK.Low()
s.SDO.Low()
if s.Delay == 0 {
Expand Down
8 changes: 4 additions & 4 deletions buzzer/buzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
package buzzer // import "tinygo.org/x/drivers/buzzer"

import (
"machine"

"time"

"tinygo.org/x/drivers"
)

// Device wraps a GPIO connection to a buzzer.
type Device struct {
pin machine.Pin
pin drivers.Pin
High bool
BPM float64
}

// New returns a new buzzer driver given which pin to use
func New(pin machine.Pin) Device {
func New(pin drivers.Pin) Device {
return Device{
pin: pin,
High: false,
Expand Down
15 changes: 15 additions & 0 deletions buzzer/buzzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package buzzer

import (
"testing"

qt "github.com/frankban/quicktest"
"tinygo.org/x/drivers/tester"
)

func TestDefaultBuzzer(t *testing.T) {
c := qt.New(t)
pin := tester.NewPin(c)
dev := New(pin)
c.Assert(dev, qt.Not(qt.IsNil))
}
20 changes: 9 additions & 11 deletions easystepper/easystepper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
package easystepper // import "tinygo.org/x/drivers/easystepper"

import (
"machine"
"time"

"tinygo.org/x/drivers"
)

// Device holds the pins and the delay between steps
type Device struct {
pins [4]machine.Pin
pins [4]drivers.Pin
stepDelay int32
stepNumber uint8
}
Expand All @@ -19,29 +20,26 @@ type DualDevice struct {
}

// New returns a new easystepper driver given 4 pins, number of steps and rpm
func New(pin1, pin2, pin3, pin4 machine.Pin, steps int32, rpm int32) Device {
func New(pin1, pin2, pin3, pin4 drivers.Pin, steps int32, rpm int32) Device {
return Device{
pins: [4]machine.Pin{pin1, pin2, pin3, pin4},
pins: [4]drivers.Pin{pin1, pin2, pin3, pin4},
stepDelay: 60000000 / (steps * rpm),
}
}

// Configure configures the pins of the Device
// Configure does not do much. Pins must already be configured as output.
func (d *Device) Configure() {
for _, pin := range d.pins {
pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
}
}

// NewDual returns a new dual easystepper driver given 8 pins, number of steps and rpm
func NewDual(pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin8 machine.Pin, steps int32, rpm int32) DualDevice {
func NewDual(pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin8 drivers.Pin, steps int32, rpm int32) DualDevice {
var dual DualDevice
dual.devices[0] = Device{
pins: [4]machine.Pin{pin1, pin2, pin3, pin4},
pins: [4]drivers.Pin{pin1, pin2, pin3, pin4},
stepDelay: 60000000 / (steps * rpm),
}
dual.devices[1] = Device{
pins: [4]machine.Pin{pin5, pin6, pin7, pin8},
pins: [4]drivers.Pin{pin5, pin6, pin7, pin8},
stepDelay: 60000000 / (steps * rpm),
}
return dual
Expand Down
18 changes: 18 additions & 0 deletions easystepper/easystepper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package easystepper

import (
"testing"

qt "github.com/frankban/quicktest"
"tinygo.org/x/drivers/tester"
)

func TestDefaultEasyStepper(t *testing.T) {
c := qt.New(t)
pin1 := tester.NewPin(c)
pin2 := tester.NewPin(c)
pin3 := tester.NewPin(c)
pin4 := tester.NewPin(c)
dev := New(pin1, pin2, pin3, pin4, 100, 100)
c.Assert(dev, qt.Not(qt.IsNil))
}
5 changes: 5 additions & 0 deletions examples/amg88xx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func main() {
})
machine.I2C0.Configure(machine.I2CConfig{SCL: machine.SCL_PIN, SDA: machine.SDA_PIN})

machine.TFT_RST.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.TFT_DC.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.TFT_CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.TFT_LITE.Configure(machine.PinConfig{Mode: machine.PinOutput})

display := st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
display.Configure(st7735.Config{
Rotation: st7735.ROTATION_90,
Expand Down
5 changes: 5 additions & 0 deletions examples/easystepper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
)

func main() {
machine.P13.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P15.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P14.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P16.Configure(machine.PinConfig{Mode: machine.PinOutput})

motor := easystepper.New(machine.P13, machine.P15, machine.P14, machine.P16, 200, 75)
motor.Configure()

Expand Down
3 changes: 3 additions & 0 deletions examples/hcsr04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func main() {
machine.D10.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D9.Configure(machine.PinConfig{Mode: machine.PinInput})

sensor := hcsr04.New(machine.D10, machine.D9)
sensor.Configure()

Expand Down
16 changes: 15 additions & 1 deletion examples/hub75/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ func main() {
Mode: 0},
)

display = hub75.New(machine.SPI0, 11, 12, 6, 10, 18, 20)
p11 := machine.Pin(11)
p12 := machine.Pin(12)
p6 := machine.Pin(6)
p10 := machine.Pin(10)
p18 := machine.Pin(18)
p20 := machine.Pin(20)

p11.Configure(machine.PinConfig{Mode: machine.PinOutput})
p12.Configure(machine.PinConfig{Mode: machine.PinOutput})
p6.Configure(machine.PinConfig{Mode: machine.PinOutput})
p10.Configure(machine.PinConfig{Mode: machine.PinOutput})
p18.Configure(machine.PinConfig{Mode: machine.PinOutput})
p20.Configure(machine.PinConfig{Mode: machine.PinOutput})

display = hub75.New(machine.SPI0, p11, p12, p6, p10, p18, p20)
display.Configure(hub75.Config{
Width: 64,
Height: 32,
Expand Down
4 changes: 4 additions & 0 deletions examples/shiftregister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func main() {
machine.PA6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.PA7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.PB6.Configure(machine.PinConfig{Mode: machine.PinOutput})

d := shiftregister.New(
shiftregister.EIGHT_BITS,
machine.PA6, // D12 Pin latch connected to ST_CP of 74HC595 (12)
Expand Down
4 changes: 4 additions & 0 deletions examples/ssd1306/spi_128x64/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

func main() {
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
})
Expand Down
4 changes: 4 additions & 0 deletions examples/ssd1331/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

func main() {
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
})
Expand Down
6 changes: 6 additions & 0 deletions examples/ssd1351/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
)

func main() {
machine.D18.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D17.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D16.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D4.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D19.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI1.Configure(machine.SPIConfig{
Frequency: 2000000,
})
Expand Down
5 changes: 5 additions & 0 deletions examples/st7735/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func main() {
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
})
Expand Down
4 changes: 4 additions & 0 deletions examples/st7789/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func main() {
// machine.TFT_DC,
// machine.TFT_CS,
// machine.TFT_LITE)
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
Expand Down
5 changes: 5 additions & 0 deletions examples/waveshare-epd/epd2in13/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
var display epd2in13.Device

func main() {
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
Mode: 0,
Expand Down
5 changes: 5 additions & 0 deletions examples/waveshare-epd/epd2in13x/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
var display epd2in13x.Device

func main() {
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
Mode: 0,
Expand Down
5 changes: 5 additions & 0 deletions examples/waveshare-epd/epd4in2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
var display epd4in2.Device

func main() {
machine.P6.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P7.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P8.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.P9.Configure(machine.PinConfig{Mode: machine.PinOutput})

machine.SPI0.Configure(machine.SPIConfig{
Frequency: 8000000,
Mode: 0,
Expand Down
17 changes: 9 additions & 8 deletions hcsr04/hcsr04.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
package hcsr04

import (
"machine"
"time"

"tinygo.org/x/drivers"
)

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

// Device holds the pins
type Device struct {
trigger machine.Pin
echo machine.Pin
trigger drivers.Pin
echo drivers.Pin
}

// New returns a new ultrasonic driver given 2 pins
func New(trigger, echo machine.Pin) Device {
// New returns a new ultrasonic driver given 2 pins.
// trigger needs to be configured as an output pin,
// echo needs to be configured as an input pin.
func New(trigger, echo drivers.Pin) Device {
return Device{
trigger: trigger,
echo: echo,
}
}

// Configure configures the pins of the Device
// Configures the Device
func (d *Device) Configure() {
d.trigger.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.echo.Configure(machine.PinConfig{Mode: machine.PinInput})
}

// ReadDistance returns the distance of the object in mm
Expand Down
Loading