Skip to content
Merged
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
10 changes: 7 additions & 3 deletions apa102/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package apa102 // import "tinygo.org/x/drivers/apa102"

import (
"image/color"
"machine"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

const (
Expand Down Expand Up @@ -37,8 +38,11 @@ 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 {
return New(&bbSPI{SCK: sckPin, SDO: sdoPin, Delay: delay})
func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin)
legacy.ConfigurePinOut(sdoPin)
}})
}

// WriteColors writes the given RGBA color slice out using the APA102 protocol.
Expand Down
18 changes: 12 additions & 6 deletions apa102/softspi.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package apa102

import "machine"
import (
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

// 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
Delay uint32
SCK pin.OutputFunc
SDO pin.OutputFunc
Delay uint32
configurePins func()
}

// Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() {
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput})
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput})
if s.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
s.configurePins()
s.SCK.Low()
s.SDO.Low()
if s.Delay == 0 {
Expand Down
55 changes: 31 additions & 24 deletions bmi160/bmi160.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
package bmi160

import (
"machine"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported.
type DeviceSPI struct {
// Chip select pin
CSB machine.Pin
csb pin.OutputFunc

buf [7]byte

// SPI bus (requires chip select to be usable).
Bus drivers.SPI
bus drivers.SPI
configurePins func()
}

// NewSPI returns a new device driver. The pin and SPI interface are not
// touched, provide a fully configured SPI object and call Configure to start
// using this device.
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
CSB: csb, // chip select
Bus: spi,
csb: csb.Set, // chip select
bus: spi,
configurePins: func() {
legacy.ConfigurePinOut(csb)
},
}
}

// Configure configures the BMI160 for use. It configures the CSB pin and
// configures the BMI160, but it does not configure the SPI interface (it is
// assumed to be up and running).
func (d *DeviceSPI) Configure() error {
d.CSB.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.CSB.High()

if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
}
d.configurePins()
d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
// > If CSB sees a rising edge after power-up, the BMI160 interface switches
Expand Down Expand Up @@ -86,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb.High()
if err != nil {
return
}
Expand Down Expand Up @@ -123,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb.High()
if err != nil {
return
}
Expand Down Expand Up @@ -153,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb.High()
if err != nil {
return
}
Expand Down Expand Up @@ -201,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.CSB.Low()
d.Bus.Tx(data, data)
d.CSB.High()
d.csb.Low()
d.bus.Tx(data, data)
d.csb.High()
return data[1]
}

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

d.CSB.Low()
d.Bus.Tx(buf, buf)
d.CSB.High()
d.csb.Low()
d.bus.Tx(buf, buf)
d.csb.High()
}
14 changes: 7 additions & 7 deletions buzzer/buzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
package buzzer // import "tinygo.org/x/drivers/buzzer"

import (
"machine"

"time"

"tinygo.org/x/drivers/internal/pin"
)

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

// New returns a new buzzer driver given which pin to use
func New(pin machine.Pin) Device {
func New(pin pin.Output) Device {
return Device{
pin: pin,
pin: pin.Set,
High: false,
BPM: 96.0,
}
}

// On sets the buzzer to a high state.
func (l *Device) On() (err error) {
l.pin.Set(true)
l.pin.High()
l.High = true
return
}

// Off sets the buzzer to a low state.
func (l *Device) Off() (err error) {
l.pin.Set(false)
l.pin.Low()
l.High = false
return
}
Expand Down
3 changes: 3 additions & 0 deletions ft6336/ft6336.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Guarded because still unsure of how to deal with interrupt drivers.
//go:build tinygo

// Package ft6336 provides a driver for the FT6336 I2C Self-Capacitive touch
// panel controller.
//
Expand Down
10 changes: 5 additions & 5 deletions hd44780/hd44780.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (d *Device) SendCommand(command byte) {
d.bus.SetCommandMode(true)
d.bus.Write([]byte{command})

for d.busy(command == DISPLAY_CLEAR || command == CURSOR_HOME) {
for d.isBusy(command == DISPLAY_CLEAR || command == CURSOR_HOME) {
}
}

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

for d.busy(false) {
for d.isBusy(false) {
}
}

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

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

// Busy returns true when hd447890 is busy
func (d *Device) Busy() bool {
return d.busy(false)
return d.isBusy(false)
}

// Size returns the current size of the display.
Expand Down
8 changes: 4 additions & 4 deletions max6675/max6675.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package max6675

import (
"errors"
"machine"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
)

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

type Device struct {
bus drivers.SPI
cs machine.Pin
cs pin.OutputFunc
}

// Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum.
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs,
cs: cs.Set,
}
}

Expand Down
23 changes: 14 additions & 9 deletions max72xx/max72xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@
package max72xx

import (
"machine"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)

type Device struct {
bus drivers.SPI
cs machine.Pin
bus drivers.SPI
cs pin.OutputFunc
configurePins func()
}

// NewDriver creates a new max7219 connection. The SPI wire must already be configured
// The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs,
cs: cs.Set,
configurePins: func() {
legacy.ConfigurePinOut(cs)
},
}
}

// Configure setups the pins.
func (driver *Device) Configure() {
outPutConfig := machine.PinConfig{Mode: machine.PinOutput}

driver.cs.Configure(outPutConfig)
if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
driver.configurePins()
}

// SetScanLimit sets the scan limit. Maximum is 8.
Expand Down
Loading