Skip to content
Draft
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
31 changes: 15 additions & 16 deletions bmi160/bmi160.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package bmi160

import (
"machine"
"time"

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

// 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 drivers.PinOutput

buf [7]byte

Expand All @@ -22,9 +22,9 @@ type DeviceSPI struct {
// 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 drivers.PinOutput, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
CSB: csb, // chip select
CSB: legacy.PinOutput(csb), // chip select
Bus: spi,
}
}
Expand All @@ -33,8 +33,7 @@ func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
// 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()
d.CSB.Set(true)

// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
Expand Down Expand Up @@ -86,9 +85,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.CSB.Low()
d.CSB.Set(false)
err = d.Bus.Tx(data, data)
d.CSB.High()
d.CSB.Set(true)
if err != nil {
return
}
Expand Down Expand Up @@ -123,9 +122,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()
d.CSB.Set(false)
err = d.Bus.Tx(data, data)
d.CSB.High()
d.CSB.Set(true)
if err != nil {
return
}
Expand Down Expand Up @@ -153,9 +152,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()
d.CSB.Set(false)
err = d.Bus.Tx(data, data)
d.CSB.High()
d.CSB.Set(true)
if err != nil {
return
}
Expand Down Expand Up @@ -201,9 +200,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.CSB.Low()
d.CSB.Set(false)
d.Bus.Tx(data, data)
d.CSB.High()
d.CSB.Set(true)
return data[1]
}

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

d.CSB.Low()
d.CSB.Set(false)
d.Bus.Tx(buf, buf)
d.CSB.High()
d.CSB.Set(true)
}
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"
)

// Device wraps a GPIO connection to a buzzer.
type Device struct {
pin machine.Pin
set drivers.PinSet
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.PinOutput) Device {
return Device{
pin: pin,
set: 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.set(true)
l.High = true
return
}

// Off sets the buzzer to a low state.
func (l *Device) Off() (err error) {
l.pin.Set(false)
l.set(false)
l.High = false
return
}
Expand Down
21 changes: 10 additions & 11 deletions dht/thermometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
package dht // import "tinygo.org/x/drivers/dht"

import (
"machine"
"runtime/interrupt"
"time"

"tinygo.org/x/drivers"
)

// DummyDevice provides a basic interface for DHT devices.
Expand All @@ -30,7 +31,7 @@ type DummyDevice interface {
// Since taking measurements from the sensor is time consuming procedure and blocks interrupts,
// user can avoid any hidden calls to the sensor.
type device struct {
pin machine.Pin
pin drivers.Pin

measurements DeviceType
initialized bool
Expand Down Expand Up @@ -93,14 +94,12 @@ func (t *device) HumidityFloat() (float32, error) {
// Perform initialization of the communication protocol.
// Device lowers the voltage on pin for startingLow=20ms and starts listening for response
// Section 5.2 in [1]
func initiateCommunication(p machine.Pin) {
func initiateCommunication(p drivers.Pin) {
// Send low signal to the device
p.Configure(machine.PinConfig{Mode: machine.PinOutput})
p.Low()
p.Set(false)
time.Sleep(startingLow)
// Set pin to high and wait for reply
p.High()
p.Configure(machine.PinConfig{Mode: machine.PinInput})
p.Set(true)
}

// Measurements returns both measurements: temperature and humidity as they sent by the device.
Expand Down Expand Up @@ -158,7 +157,7 @@ func (t *device) read() error {

// receiveSignals counts number of low and high cycles. The execution is time critical, so the function disables
// interrupts
func receiveSignals(pin machine.Pin, result []counter) {
func receiveSignals(pin drivers.PinInput, result []counter) {
i := uint8(0)
mask := interrupt.Disable()
defer interrupt.Restore(mask)
Expand Down Expand Up @@ -189,7 +188,7 @@ func (t *device) extractData(signals []counter, buf []uint8) error {
// waitForDataTransmission waits for reply from the sensor.
// If no reply received, returns NoSignalError.
// For more details, see section 5.2 in [1]
func waitForDataTransmission(p machine.Pin) error {
func waitForDataTransmission(p drivers.PinInput) error {
// wait for thermometer to pull down
if expectChange(p, true) == timeout {
return NoSignalError
Expand All @@ -209,8 +208,8 @@ func waitForDataTransmission(p machine.Pin) error {
// This device provides full control to the user.
// It does not do any hidden measurements calls and does not check
// for 2 seconds delay between measurements.
func NewDummyDevice(pin machine.Pin, deviceType DeviceType) DummyDevice {
pin.High()
func NewDummyDevice(pin drivers.Pin, deviceType DeviceType) DummyDevice {
pin.Set(true)
return &device{
pin: pin,
measurements: deviceType,
Expand Down
11 changes: 6 additions & 5 deletions dht/timesafethermometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
package dht // import "tinygo.org/x/drivers/dht"

import (
"machine"
"time"

"tinygo.org/x/drivers"
)

// Device interface provides main functionality of the DHTXX sensors.
Expand Down Expand Up @@ -124,8 +125,8 @@ func (m *managedDevice) Configure(policy UpdatePolicy) {

// Constructor of the Device implementation.
// This implementation updates data every 2 seconds during data access.
func New(pin machine.Pin, deviceType DeviceType) Device {
pin.High()
func New(pin drivers.Pin, deviceType DeviceType) Device {
pin.Set(true)
return &managedDevice{
t: device{
pin: pin,
Expand All @@ -141,8 +142,8 @@ func New(pin machine.Pin, deviceType DeviceType) Device {
}

// Constructor of the Device implementation with given UpdatePolicy
func NewWithPolicy(pin machine.Pin, deviceType DeviceType, updatePolicy UpdatePolicy) Device {
pin.High()
func NewWithPolicy(pin drivers.Pin, deviceType DeviceType, updatePolicy UpdatePolicy) Device {
pin.Set(true)
result := &managedDevice{
t: device{
pin: pin,
Expand Down
9 changes: 5 additions & 4 deletions dht/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
package dht // import "tinygo.org/x/drivers/dht"

import (
"machine"
"time"

"tinygo.org/x/drivers"
)

// Check if the pin is disabled
func powerUp(p machine.Pin) bool {
func powerUp(p drivers.Pin) bool {
state := p.Get()
if !state {
p.High()
p.Set(true)
time.Sleep(startTimeout)
}
return state
}

func expectChange(p machine.Pin, oldState bool) counter {
func expectChange(p drivers.PinInput, oldState bool) counter {
cnt := counter(0)
for ; p.Get() == oldState && cnt != timeout; cnt++ {
}
Expand Down
6 changes: 5 additions & 1 deletion examples/dht/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//go:build baremetal && tinygo

package main

import (
"fmt"
"machine"
"time"

"tinygo.org/x/drivers/dht"
"tinygo.org/x/drivers/tinygo"
)

func main() {
pin := machine.D6
pin := tinygo.New(machine.D6)
dhtSensor := dht.New(pin, dht.DHT11)
for {
temp, hum, err := dhtSensor.Measurements()
Expand Down
5 changes: 4 additions & 1 deletion examples/hcsr04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"time"

"tinygo.org/x/drivers/hcsr04"
"tinygo.org/x/drivers/tinygo"
)

func main() {
sensor := hcsr04.New(machine.D10, machine.D9)
trigger := tinygo.New(machine.D10) // automatically configures pin as output
echo := tinygo.New(machine.D9) // automatically configures pin as input
sensor := hcsr04.New(trigger, echo)
sensor.Configure()

println("Ultrasonic starts")
Expand Down
68 changes: 68 additions & 0 deletions examples/rpio/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

// Example program for the ST7735 display (Waveshare 1.44" LCD HAT) using the rpio driver on a Raspberry Pi.

import (
"fmt"
"image/color"
"time"

go_rpio "github.com/stianeikeland/go-rpio/v4"
"tinygo.org/x/drivers/rpio"
"tinygo.org/x/drivers/st7735"
)

var (
black = color.RGBA{0, 0, 0, 255}
colors = [...]color.RGBA{
{255, 0, 0, 255}, // red
{0, 255, 0, 255}, // green
{0, 0, 255, 255}, // blue
{255, 255, 0, 255}, // yellow
}
)

func main() {
if err := go_rpio.Open(); err != nil {
fmt.Println("Error opening GPIO:", err)
return
}
defer go_rpio.Close()

// Initialize SPI and pins
spi := rpio.NewSPI()
resetPin := rpio.NewPin(27)
dcPin := rpio.NewPin(25)
csPin := rpio.NewPin(8)
blPin := rpio.NewPin(24)

// Initialize display
device := st7735.New(spi, resetPin, dcPin, csPin, blPin)
device.Configure(st7735.Config{
Width: 128,
Height: 128,
Model: st7735.GREENTAB,
RowOffset: 3,
ColumnOffset: 2,
})
device.InvertColors(false)
device.EnableBacklight(true)
device.IsBGR(true) // no effect w/o rotation!
device.SetRotation(st7735.NO_ROTATION)

width, height := device.Size()

// Clear display
device.FillScreen(black)

// Draw rectangles in a loop, clockwise rotation of colors
pos := 0
for {
device.FillRectangle(0, 0, width/2, height/2, colors[(pos+0)%len(colors)]) // top left
device.FillRectangle(0, height/2, width/2, height/2, colors[(pos+1)%len(colors)]) // bottom left
device.FillRectangle(width/2, height/2, width/2, height/2, colors[(pos+2)%len(colors)]) // bottom right
device.FillRectangle(width/2, 0, width/2, height/2, colors[(pos+3)%len(colors)]) // top right
pos++
time.Sleep(1 * time.Second)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/orsinium-labs/tinymath v1.1.0
github.com/soypat/natiu-mqtt v0.5.1
github.com/stianeikeland/go-rpio/v4 v4.6.0
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
golang.org/x/net v0.33.0
tinygo.org/x/tinyfont v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/orsinium-labs/tinymath v1.1.0 h1:KomdsyLHB7vE3f1nRAJF2dyf1m/gnM2HxfTe
github.com/orsinium-labs/tinymath v1.1.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
github.com/soypat/natiu-mqtt v0.5.1 h1:rwaDmlvjzD2+3MCOjMZc4QEkDkNwDzbct2TJbpz+TPc=
github.com/soypat/natiu-mqtt v0.5.1/go.mod h1:xEta+cwop9izVCW7xOx2W+ct9PRMqr0gNVkvBPnQTc4=
github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY=
github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
Expand Down
Loading