mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-02 05:57:47 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 453ff96a66 | |||
| c6d83b783f |
+15
-16
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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]
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+7
-7
@@ -2,22 +2,22 @@
|
||||
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,
|
||||
}
|
||||
@@ -25,14 +25,14 @@ func New(pin machine.Pin) Device {
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
+10
-11
@@ -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.
|
||||
@@ -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
|
||||
@@ -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.
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
|
||||
+5
-4
@@ -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++ {
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build tinygo && (rp2040 || rp2350 || stm32 || k210 || esp32c3 || nrf || sam || (avr && (atmega328p || atmega328pb)))
|
||||
//go:build tinygo && (rp2040 || stm32 || k210 || esp32c3 || nrf || sam || (avr && (atmega328p || atmega328pb)))
|
||||
|
||||
// Implementation based on:
|
||||
// https://gist.github.com/aykevl/3fc1683ed77bb0a9c07559dfe857304a
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/honeyhsc"
|
||||
)
|
||||
|
||||
// Data taken from https://github.com/rodan/honeywell_hsc_ssc_i2c/blob/master/hsc_ssc_i2c.cpp
|
||||
// these defaults are valid for the HSCMRNN030PA2A3 chip
|
||||
const (
|
||||
i2cAddress = 0x28
|
||||
// 10%
|
||||
outputMinimum = 0x666
|
||||
// 90% of 2^14 - 1
|
||||
outputMax = 0x399A
|
||||
// min is 0 for sensors that give absolute values
|
||||
pressureMin = 0
|
||||
// 30psi (and we want results in millipascals)
|
||||
// pressureMax = 206842.7
|
||||
pressureMax = 206843 * 1000
|
||||
)
|
||||
|
||||
func main() {
|
||||
bus := machine.I2C0
|
||||
err := bus.Configure(machine.I2CConfig{
|
||||
Frequency: 400_000, // 100kHz minimum and 400kHz I2C maximum clock. 50 to 800 for SPI.
|
||||
SDA: machine.I2C0_SDA_PIN,
|
||||
SCL: machine.I2C0_SCL_PIN,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
sensor := honeyhsc.NewDevI2C(bus, i2cAddress, outputMinimum, outputMax, pressureMin, pressureMax)
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
const measuremask = drivers.Pressure | drivers.Temperature
|
||||
err := sensor.Update(measuremask)
|
||||
if err != nil {
|
||||
println("error updating measurements:", err.Error())
|
||||
continue
|
||||
}
|
||||
P := sensor.Pressure()
|
||||
T := sensor.Temperature()
|
||||
println("pressure:", P, "temperature:", T)
|
||||
}
|
||||
}
|
||||
+3
-12
@@ -14,18 +14,9 @@ func main() {
|
||||
i2c.Configure(machine.I2CConfig{SCL: machine.SCL1_PIN, SDA: machine.SDA1_PIN})
|
||||
|
||||
accel := lis3dh.New(i2c)
|
||||
err := accel.Configure(lis3dh.Config{
|
||||
Address: lis3dh.Address1, // address on the Circuit Playground Express
|
||||
})
|
||||
for err != nil {
|
||||
println("could not configure LIS3DH:", err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
err = accel.SetRange(lis3dh.RANGE_2_G)
|
||||
for err != nil {
|
||||
println("could not set acceleration range:", err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
accel.Address = lis3dh.Address1 // address on the Circuit Playground Express
|
||||
accel.Configure()
|
||||
accel.SetRange(lis3dh.RANGE_2_G)
|
||||
|
||||
println(accel.Connected())
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
+11
-10
@@ -5,20 +5,22 @@
|
||||
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.PinOutput
|
||||
echo drivers.PinInput
|
||||
}
|
||||
|
||||
// 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.
|
||||
// Pins must be configured as output (trigger) and input (echo).
|
||||
func New(trigger drivers.PinOutput, echo drivers.PinInput) Device {
|
||||
return Device{
|
||||
trigger: trigger,
|
||||
echo: echo,
|
||||
@@ -27,8 +29,7 @@ func New(trigger, echo machine.Pin) Device {
|
||||
|
||||
// Configure configures the pins of the Device
|
||||
func (d *Device) Configure() {
|
||||
d.trigger.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.echo.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||
// no-op, left for API compatibility
|
||||
}
|
||||
|
||||
// ReadDistance returns the distance of the object in mm
|
||||
@@ -45,11 +46,11 @@ func (d *Device) ReadDistance() int32 {
|
||||
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
|
||||
func (d *Device) ReadPulse() int32 {
|
||||
t := time.Now()
|
||||
d.trigger.Low()
|
||||
d.trigger.Set(false)
|
||||
time.Sleep(2 * time.Microsecond)
|
||||
d.trigger.High()
|
||||
d.trigger.Set(true)
|
||||
time.Sleep(10 * time.Microsecond)
|
||||
d.trigger.Low()
|
||||
d.trigger.Set(false)
|
||||
i := uint8(0)
|
||||
for {
|
||||
if d.echo.Get() {
|
||||
|
||||
-191
@@ -1,191 +0,0 @@
|
||||
package honeyhsc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
var (
|
||||
errSensorMissing = errors.New("hsc: not connected")
|
||||
errDiagnostic = errors.New("hsc: diagnostic error")
|
||||
)
|
||||
|
||||
const (
|
||||
measuremask = drivers.Pressure | drivers.Temperature
|
||||
statusMask = 0b1100_0000
|
||||
statusOffset = 6
|
||||
)
|
||||
|
||||
// DevI2C is the TruStability® High Accuracy Silicon Ceramic (HSC) Series is a piezoresistive silicon pressure sensor offering a ratiometric
|
||||
// analog or digital output for reading pressure over the specified full scale pressure span and temperature range.
|
||||
type DevI2C struct {
|
||||
bus drivers.I2C
|
||||
dev
|
||||
addr uint8
|
||||
buf [6]byte
|
||||
}
|
||||
|
||||
// NewDevI2C creates and returns a new DevI2C that communicates with an HSC device over the provided I2C bus.
|
||||
// Parameters:
|
||||
// - bus: the I2C bus to use.
|
||||
// - addr: the 7-bit I2C address of the sensor.
|
||||
// - outMin, outMax: raw output code range (counts) corresponding to the pressure span. Depends on sensor model.
|
||||
// - pMin, pMax: pressure range endpoints in millipascals (mPa). Depends on sensor model.
|
||||
//
|
||||
// The returned DevI2C will use these calibration parameters to convert raw bridge counts to pressure.
|
||||
func NewDevI2C(bus drivers.I2C, addr, outMin, outMax uint16, pMin, pMax int32) *DevI2C {
|
||||
h := &DevI2C{
|
||||
bus: bus,
|
||||
addr: uint8(addr),
|
||||
dev: dev{
|
||||
cmin: outMin,
|
||||
cmax: outMax,
|
||||
pmin: pMin,
|
||||
pmax: pMax,
|
||||
},
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// ReadTemperature reads and returns the temperature in milliKelvin (mC) from the I2C-attached HSC device.
|
||||
// It performs an Update internally to get the latest temperature value.
|
||||
func (h *DevI2C) ReadTemperature() (int32, error) {
|
||||
err := h.Update(drivers.Temperature)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return h.Temperature(), nil
|
||||
}
|
||||
|
||||
// Update reads both temperature and pressure data from the I2C-attached HSC device when
|
||||
// the requested measurement mask includes pressure or temperature.
|
||||
// If neither pressure nor temperature is requested, Update is a no-op.
|
||||
func (d *DevI2C) Update(which drivers.Measurement) error {
|
||||
// Update performs an I2C transaction to read 4 bytes, parses the status bits, 14-bit bridge data and
|
||||
// temperature bits, and forwards them to the internal update routine. Any I2C transport error is returned,
|
||||
// as well as errors produced by the internal update (e.g. errSensorMissing, errDiagnostic).
|
||||
if which&measuremask == 0 {
|
||||
return nil
|
||||
}
|
||||
rbuf := d.buf[:4]
|
||||
wbuf := d.buf[4:6]
|
||||
const reg = 0
|
||||
value := (d.addr << 1) | 1
|
||||
wbuf[0] = reg
|
||||
wbuf[1] = value
|
||||
err := d.bus.Tx(uint16(d.addr), wbuf, rbuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
status := (rbuf[0] & statusMask) >> statusOffset
|
||||
bridgeData := (uint16(rbuf[0]&^statusMask) << 8) | uint16(rbuf[1])
|
||||
tempData := uint16(rbuf[2])<<8 | uint16(rbuf[3]&0xe0)>>5
|
||||
return d.dev.update(status, bridgeData, tempData)
|
||||
}
|
||||
|
||||
type pinout func(level bool)
|
||||
|
||||
// DevI2C is the TruStability® High Accuracy Silicon Ceramic (HSC) Series is a piezoresistive silicon pressure sensor offering a ratiometric
|
||||
// analog or digital output for reading pressure over the specified full scale pressure span and temperature range.
|
||||
type DevSPI struct {
|
||||
spi drivers.SPI
|
||||
cs pinout
|
||||
dev
|
||||
buf [4]byte
|
||||
}
|
||||
|
||||
// NewDevSPI creates and returns a new DevSPI that communicates with an HSC device over SPI.
|
||||
// Parameters:
|
||||
// - conn: the SPI connection to use.
|
||||
// - cs: a chip-select function that drives the device select line low/high.
|
||||
// - outMin, outMax: raw output code range (counts) corresponding to the pressure span. Depends on sensor model.
|
||||
// - pMin, pMax: pressure range endpoints in millipascals (mPa). Depends on sensor model.
|
||||
//
|
||||
// The function returns the constructed DevSPI and an error value (currently always nil).
|
||||
func NewDevSPI(conn drivers.SPI, cs pinout, outMin, outMax uint16, pMin, pMax int32) (*DevSPI, error) {
|
||||
h := &DevSPI{
|
||||
spi: conn,
|
||||
cs: cs,
|
||||
dev: dev{
|
||||
cmin: outMin,
|
||||
cmax: outMax,
|
||||
pmin: pMin,
|
||||
pmax: pMax,
|
||||
},
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// ReadTemperature reads and returns the temperature in milliKelvin (mC) from the SPI-attached HSC device.
|
||||
// It performs an Update internally to get the latest temperature value.
|
||||
func (h *DevSPI) ReadTemperature() (int32, error) {
|
||||
err := h.Update(drivers.Temperature)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return h.Temperature(), nil
|
||||
}
|
||||
|
||||
// Update reads pressure and temperature data from the SPI-attached HSC device when the requested measurement mask includes
|
||||
// pressure or temperature. If neither pressure nor temperature is requested, Update is a no-op.
|
||||
func (h *DevSPI) Update(which drivers.Measurement) error {
|
||||
// It toggles the provided chip-select, performs an SPI transfer to read 4 bytes, parses the status bits,
|
||||
// 14-bit bridge data and temperature bits, and forwards them to the internal update routine. Any SPI
|
||||
// transport error is returned, as well as errors produced by the internal update (e.g. errSensorMissing, errDiagnostic).
|
||||
if which&measuremask == 0 {
|
||||
return nil
|
||||
}
|
||||
buf := &h.buf
|
||||
h.cs(false)
|
||||
err := h.spi.Tx(nil, buf[:4])
|
||||
h.cs(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// First two bits are status bits.
|
||||
status := (buf[0] & statusMask) >> statusOffset
|
||||
bridgeData := (uint16(buf[0]&^statusMask) << 8) | uint16(buf[1])
|
||||
|
||||
tempData := uint16(buf[2])<<8 | uint16(buf[3]&0xe0)>>5
|
||||
return h.dev.update(status, bridgeData, tempData)
|
||||
}
|
||||
|
||||
type dev struct {
|
||||
pressure int32
|
||||
temp int32
|
||||
cmin, cmax uint16
|
||||
pmin, pmax int32
|
||||
}
|
||||
|
||||
// Pressure returns the most recently computed pressure value in millipascals (mPa).
|
||||
// The value is taken from the last successful Update.
|
||||
func (d *dev) Pressure() int32 {
|
||||
return d.pressure
|
||||
}
|
||||
|
||||
// Temperature returns the most recently read temperature value in milliKelvin (mC).
|
||||
// The value is taken from the last successful Update.
|
||||
func (d *dev) Temperature() int32 {
|
||||
return d.temp + 273_150
|
||||
}
|
||||
|
||||
// update interprets raw sensor fields (status, bridgeData, tempData) and updates the dev's stored
|
||||
// pressure and temperature. It returns errSensorMissing when the temperature raw value indicates no sensor
|
||||
// (tempData == math.MaxUint16), errDiagnostic when the status indicates a device diagnostic condition
|
||||
// (status == 3), or nil on success. Pressure is computed with integer arithmetic using the configured
|
||||
// cmin/cmax -> pmin/pmax linear mapping in order to avoid overflows.
|
||||
func (d *dev) update(status uint8, bridgeData, tempData uint16) error {
|
||||
if tempData == math.MaxUint16 {
|
||||
return errSensorMissing
|
||||
} else if status == 3 {
|
||||
return errDiagnostic
|
||||
}
|
||||
|
||||
// Take care not to overflow here.
|
||||
p := (int32(bridgeData)-int32(d.cmin))*(d.pmax-d.pmin)/int32(d.cmax-d.cmin) + d.pmin
|
||||
d.temp = int32(tempData)
|
||||
d.pressure = p
|
||||
return nil
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
// The pingconfig group of files serve to abstract away
|
||||
// pin configuration calls on the machine.Pin type.
|
||||
// It was observed this way of developing drivers was
|
||||
// non-portable and unusable on "big" Go projects so
|
||||
// future projects should NOT configure pins in driver code.
|
||||
// Users must configure pins before passing them as arguments
|
||||
// to drivers.
|
||||
|
||||
// ConfigurePinOut is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinOut(po pin.Output) {
|
||||
configurePinOut(po)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPulldown(pi pin.Input) {
|
||||
configurePinInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInput(pi pin.Input) {
|
||||
configurePinInput(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPullup(pi pin.Input) {
|
||||
configurePinInputPullup(pi)
|
||||
}
|
||||
|
||||
// PinIsNoPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers do not require pin knowledge from now on.
|
||||
func PinIsNoPin(pin any) bool {
|
||||
return pinIsNoPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -1,15 +0,0 @@
|
||||
//go:build !tinygo
|
||||
|
||||
package legacy
|
||||
|
||||
import "tinygo.org/x/drivers/internal/pin"
|
||||
|
||||
// This file compiles for non-tinygo builds
|
||||
// for use with "big" or "upstream" Go where
|
||||
// there is no machine package.
|
||||
|
||||
func configurePinOut(p pin.Output) {}
|
||||
func configurePinInput(p pin.Input) {}
|
||||
func configurePinInputPulldown(p pin.Input) {}
|
||||
func configurePinInputPullup(p pin.Input) {}
|
||||
func pinIsNoPin(a any) bool { return false }
|
||||
@@ -1,10 +0,0 @@
|
||||
//go:build baremetal && fe310
|
||||
|
||||
package legacy
|
||||
|
||||
import "machine"
|
||||
|
||||
const (
|
||||
pulldown = machine.PinInput
|
||||
pullup = machine.PinInput
|
||||
)
|
||||
@@ -1,13 +0,0 @@
|
||||
//go:build baremetal && !fe310
|
||||
|
||||
package legacy
|
||||
|
||||
import "machine"
|
||||
|
||||
// If you are getting a build error here you then we missed adding
|
||||
// your CPU build tag to the list of CPUs that do not have pulldown/pullups.
|
||||
// Add it above and in pinhal_nopulls! You should also add a smoketest for it :)
|
||||
const (
|
||||
pulldown = machine.PinInputPulldown
|
||||
pullup = machine.PinInputPullup
|
||||
)
|
||||
@@ -1,37 +0,0 @@
|
||||
//go:build baremetal
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
func configurePinOut(po pin.Output) {
|
||||
configurePin(po, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configurePinInputPulldown(pi pin.Input) {
|
||||
configurePin(pi, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configurePinInput(pi pin.Input) {
|
||||
configurePin(pi, machine.PinInput)
|
||||
}
|
||||
|
||||
func configurePinInputPullup(pi pin.Input) {
|
||||
configurePin(pi, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func pinIsNoPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configurePin(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !(baremetal && tinygo)
|
||||
|
||||
package legacy
|
||||
|
||||
import "tinygo.org/x/drivers"
|
||||
|
||||
func PinOutput(pin drivers.PinOutput) drivers.PinOutput {
|
||||
return pin
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//go:build baremetal && tinygo
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
func PinOutput(pin drivers.PinOutput) drivers.PinOutput {
|
||||
if p, ok := pin.(machine.Pin); ok {
|
||||
p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
}
|
||||
return pin
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// package pin implements a TinyGo Pin HAL.
|
||||
// It serves to eliminate machine.Pin from driver constructors
|
||||
// so that drivers can be used in "big" Go projects where
|
||||
// there is no machine package.
|
||||
// This file contains both function and interface-style Pin HAL definitions.
|
||||
package pin
|
||||
|
||||
// OutputFunc is hardware abstraction for a pin which outputs a
|
||||
// digital signal (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.OutputFunc
|
||||
// led := machine.LED
|
||||
// led.Configure(machine.PinConfig{Mode: machine.Output})
|
||||
// var pin pin.OutputFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
|
||||
//
|
||||
// This is an alternative to [Output] which is an interface type.
|
||||
type OutputFunc func(level bool)
|
||||
|
||||
// High sets the underlying pin's level to high. This is equivalent to calling PinOutput(true).
|
||||
func (setPin OutputFunc) High() {
|
||||
setPin(true)
|
||||
}
|
||||
|
||||
// Low sets the underlying pin's level to low. This is equivalent to calling PinOutput(false).
|
||||
func (setPin OutputFunc) Low() {
|
||||
setPin(false)
|
||||
}
|
||||
|
||||
// InputFunc is hardware abstraction for a pin which receives a
|
||||
// digital signal and reads it (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.InputFunc
|
||||
// input := machine.LED
|
||||
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.Input
|
||||
// var pin pin.InputFunc = input.Get // Going from a machine.Pin to a pin.InputFunc
|
||||
//
|
||||
// This is an alternative to [Input] which is an interface type.
|
||||
type InputFunc func() (level bool)
|
||||
|
||||
// // Below is an example on how to define a input/output pin HAL for a
|
||||
// // pin that must switch between input and output mode:
|
||||
//
|
||||
// var pinIsOutput bool
|
||||
// var po PinOutputFunc = func(b bool) {
|
||||
// if !pinIsOutput {
|
||||
// pin.Configure(outputMode)
|
||||
// pinIsOutput = true
|
||||
// }
|
||||
// pin.Set(b)
|
||||
// }
|
||||
//
|
||||
// var pi PinInputFunc = func() bool {
|
||||
// if pinIsOutput {
|
||||
// pin.Configure(inputMode)
|
||||
// pinIsOutput = false
|
||||
// }
|
||||
// return pin.Get()
|
||||
// }
|
||||
|
||||
// Output interface represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
//
|
||||
// This is an alternative to [OutputFunc] abstraction which is a function type.
|
||||
type Output interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// Input interface represents a pin hardware abstraction layer for a pin that can read a digital signal.
|
||||
//
|
||||
// This is an alternative to [InputFunc] abstraction which is a function type.
|
||||
type Input interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
// Package regmap provides transaction-based interfaces for reading and writing
|
||||
// to device registers over I2C and SPI buses with pre-allocated buffers.
|
||||
package regmap
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
var (
|
||||
// errNotInTx indicates an operation was attempted outside of an active transaction.
|
||||
errNotInTx = errors.New("device not in Tx")
|
||||
|
||||
// errInTx indicates a transaction was started while another is still active.
|
||||
errInTx = errors.New("device already in Tx")
|
||||
|
||||
// errShortWriteBuffer indicates the write buffer is too small for the requested operation.
|
||||
errShortWriteBuffer = errors.New("device write buffer too short")
|
||||
|
||||
// errShortReadBuffer indicates the read buffer is too small for the requested operation.
|
||||
errShortReadBuffer = errors.New("device read buffer too short")
|
||||
)
|
||||
|
||||
// Device8Txer wraps a Device8 to provide buffered transaction support for
|
||||
// I2C and SPI operations. It maintains pre-allocated buffers to avoid heap
|
||||
// allocations during register access operations.
|
||||
//
|
||||
// Users must call SetBuffers to configure the write and read buffers before
|
||||
// initiating transactions.
|
||||
type Device8Txer struct {
|
||||
Device8
|
||||
writeBuf []byte // Pre-allocated buffer for write operations
|
||||
readBuf []byte // Pre-allocated buffer for read operations
|
||||
inTx bool // Tracks whether a transaction is currently active
|
||||
}
|
||||
|
||||
// SetTxBuffers configures the write and read buffers for this device.
|
||||
// These buffers are reused across transactions to avoid heap allocations.
|
||||
//
|
||||
// The writebuf should be large enough to hold the register address plus
|
||||
// all data bytes to be written in a single transaction.
|
||||
func (d *Device8Txer) SetTxBuffers(writebuf, readbuf []byte) {
|
||||
d.readBuf = readbuf
|
||||
d.writeBuf = writebuf
|
||||
}
|
||||
|
||||
// Tx8 represents an active transaction for an 8-bit register device.
|
||||
// It tracks the write buffer and current offset as data is added to the transaction.
|
||||
//
|
||||
// Use AddWriteByte or AddWriteData to add data to the transaction, then call
|
||||
// DoTxI2C or DoTxSPI to execute the transaction over the bus.
|
||||
type Tx8 struct {
|
||||
dw *Device8Txer // Reference to the parent device
|
||||
off int // Current offset in the write buffer
|
||||
}
|
||||
|
||||
// Tx initiates a new transaction for writing to the specified register address.
|
||||
//
|
||||
// Parameters:
|
||||
// - writeAddr: The 8-bit register address to write to
|
||||
//
|
||||
// Returns a Tx8 handle that can be used to add data and execute the transaction.
|
||||
//
|
||||
// Returns an error if:
|
||||
// - A transaction is already active (errInTx)
|
||||
// - The write buffer is too short (errShortWriteBuffer)
|
||||
func (dw *Device8Txer) Tx(writeAddr uint8) (Tx8, error) {
|
||||
if dw.inTx {
|
||||
return Tx8{}, errInTx
|
||||
} else if len(dw.writeBuf) < 1 {
|
||||
return Tx8{}, errShortWriteBuffer
|
||||
}
|
||||
dw.writeBuf[0] = writeAddr
|
||||
return Tx8{dw: dw, off: 1}, nil
|
||||
}
|
||||
|
||||
// AddWriteData appends multiple bytes to the current transaction's write buffer.
|
||||
//
|
||||
// Parameters:
|
||||
// - buf: Variable number of bytes to add to the transaction
|
||||
//
|
||||
// Returns an error if:
|
||||
// - No transaction is active (errNotInTx)
|
||||
// - The write buffer doesn't have enough space (errShortWriteBuffer)
|
||||
func (tx *Tx8) AddWriteData(buf ...byte) error {
|
||||
if !tx.dw.inTx {
|
||||
return errNotInTx
|
||||
}
|
||||
avail := tx.dw.writeBuf[tx.off:]
|
||||
if len(avail) < len(buf) {
|
||||
return errShortWriteBuffer
|
||||
}
|
||||
n := copy(avail, buf)
|
||||
tx.off += n
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddWriteByte appends a single byte to the current transaction's write buffer.
|
||||
//
|
||||
// Parameters:
|
||||
// - b: The byte to add to the transaction
|
||||
//
|
||||
// Returns an error if:
|
||||
// - No transaction is active (errNotInTx)
|
||||
// - The write buffer doesn't have enough space (errShortWriteBuffer)
|
||||
func (tx *Tx8) AddWriteByte(b byte) error {
|
||||
if !tx.dw.inTx {
|
||||
return errNotInTx
|
||||
}
|
||||
avail := tx.dw.writeBuf[tx.off:]
|
||||
if len(avail) < 1 {
|
||||
return errShortWriteBuffer
|
||||
}
|
||||
avail[0] = b
|
||||
tx.off++
|
||||
return nil
|
||||
}
|
||||
|
||||
// DoTxI2C executes the transaction over an I2C bus.
|
||||
//
|
||||
// This performs a combined write-read I2C transaction, first sending the
|
||||
// register address and any data added to the transaction, then reading
|
||||
// the specified number of bytes from the device.
|
||||
//
|
||||
// Parameters:
|
||||
// - bus: The I2C bus to communicate over
|
||||
// - deviceAddr: The I2C address of the target device
|
||||
// - readLength: Number of bytes to read from the device
|
||||
//
|
||||
// Returns the read data as a slice of the internal read buffer, valid until
|
||||
// the next transaction. The transaction is automatically freed after execution.
|
||||
//
|
||||
// Returns an error if:
|
||||
// - No transaction is active (errNotInTx)
|
||||
// - The read buffer is too short (errShortReadBuffer)
|
||||
// - The I2C transaction fails
|
||||
func (tx *Tx8) DoTxI2C(bus drivers.I2C, deviceAddr uint16, readLength int) ([]byte, error) {
|
||||
if tx.off == 0 || !tx.dw.inTx {
|
||||
return nil, errNotInTx
|
||||
}
|
||||
defer tx.freeTx()
|
||||
if len(tx.dw.readBuf) < readLength {
|
||||
return nil, errShortReadBuffer
|
||||
}
|
||||
rbuf := tx.dw.readBuf[:readLength]
|
||||
err := bus.Tx(deviceAddr, tx.dw.writeBuf[:tx.off], rbuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rbuf, err
|
||||
}
|
||||
|
||||
// DoTxSPI executes the transaction over an SPI bus.
|
||||
//
|
||||
// This performs a full-duplex SPI transaction, simultaneously writing the
|
||||
// register address and data while reading the same number of bytes from the device.
|
||||
//
|
||||
// If no read buffer was configured (readBuf is nil), this performs a write-only
|
||||
// transaction and returns nil without error.
|
||||
//
|
||||
// Parameters:
|
||||
// - bus: The SPI bus to communicate over
|
||||
//
|
||||
// Returns the read data as a slice of the internal read buffer (same length as
|
||||
// the write data), valid until the next transaction. The transaction is
|
||||
// automatically freed after execution.
|
||||
//
|
||||
// Returns an error if:
|
||||
// - No transaction is active (errNotInTx)
|
||||
// - The read buffer is too short (errShortReadBuffer)
|
||||
// - The SPI transaction fails
|
||||
func (tx *Tx8) DoTxSPI(bus drivers.SPI) (readBuf []byte, err error) {
|
||||
if tx.off == 0 || !tx.dw.inTx {
|
||||
return nil, errNotInTx
|
||||
}
|
||||
defer tx.freeTx()
|
||||
if tx.dw.readBuf == nil {
|
||||
err = bus.Tx(tx.dw.writeBuf[:tx.off], nil) // Special case, only use write buffer functionality.
|
||||
return nil, err
|
||||
} else if len(readBuf) < tx.off {
|
||||
return nil, errShortReadBuffer
|
||||
}
|
||||
rbuf := tx.dw.readBuf[:tx.off]
|
||||
err = bus.Tx(tx.dw.writeBuf[:tx.off], rbuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rbuf, err
|
||||
}
|
||||
|
||||
// freeTx marks the transaction as complete, allowing a new transaction to be started.
|
||||
// This is called internally by DoTxI2C and DoTxSPI after the transaction completes.
|
||||
func (tx *Tx8) freeTx() {
|
||||
tx.dw.inTx = false
|
||||
}
|
||||
@@ -8,10 +8,6 @@ import (
|
||||
)
|
||||
|
||||
// Device8 implements common logic to most 8-bit peripherals with an I2C or SPI bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8 struct {
|
||||
buf [10]byte
|
||||
}
|
||||
@@ -23,53 +19,41 @@ func (d *Device8) clear() {
|
||||
|
||||
// I2C methods.
|
||||
|
||||
// Read8I2C reads a single byte from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
func (d *Device8) Read8I2C(bus drivers.I2C, i2cAddr uint16, addr uint8) (byte, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:2])
|
||||
return d.buf[1], err
|
||||
}
|
||||
|
||||
// Read16I2C reads a 16-bit value from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Read16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint16, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:3])
|
||||
return order.Uint16(d.buf[1:3]), err
|
||||
}
|
||||
|
||||
// Read32I2C reads a 32-bit value from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Read32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint32, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:5])
|
||||
return order.Uint32(d.buf[1:5]), err
|
||||
}
|
||||
|
||||
// ReadDataI2C reads dataLength bytes from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The data is stored in dataDestination.
|
||||
func (d *Device8) ReadDataI2C(bus drivers.I2C, i2cAddr uint16, addr uint8, dataDestination []byte) error {
|
||||
d.buf[0] = addr
|
||||
return bus.Tx(i2cAddr, d.buf[:1], dataDestination)
|
||||
}
|
||||
|
||||
// Write8I2C writes a single byte value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
func (d *Device8) Write8I2C(bus drivers.I2C, i2cAddr uint16, addr, value uint8) error {
|
||||
d.buf[0] = addr
|
||||
d.buf[1] = value
|
||||
return bus.Tx(i2cAddr, d.buf[:2], nil)
|
||||
}
|
||||
|
||||
// Write16I2C writes a 16-bit value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Write16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint16, order binary.ByteOrder) error {
|
||||
d.buf[0] = addr
|
||||
order.PutUint16(d.buf[1:3], value)
|
||||
return bus.Tx(i2cAddr, d.buf[0:3], nil)
|
||||
}
|
||||
|
||||
// Write32I2C writes a 32-bit value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint32, order binary.ByteOrder) error {
|
||||
d.buf[0] = addr
|
||||
order.PutUint32(d.buf[1:5], value)
|
||||
@@ -78,7 +62,6 @@ func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value
|
||||
|
||||
// SPI methods.
|
||||
|
||||
// Read8SPI reads a single byte from register addr using the provided SPI bus.
|
||||
func (d *Device8) Read8SPI(bus drivers.SPI, addr uint8) (byte, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -86,7 +69,6 @@ func (d *Device8) Read8SPI(bus drivers.SPI, addr uint8) (byte, error) {
|
||||
return d.buf[1], err
|
||||
}
|
||||
|
||||
// Read16SPI reads a 16-bit value from register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Read16SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint16, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -94,7 +76,6 @@ func (d *Device8) Read16SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder)
|
||||
return order.Uint16(d.buf[4:6]), err
|
||||
}
|
||||
|
||||
// Read32SPI reads a 32-bit value from register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Read32SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint32, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -118,7 +99,6 @@ func (d *Device8) ReadDataSPI(bus drivers.SPI, addr uint8, dataLength int, auxil
|
||||
return rbuf[1:], err
|
||||
}
|
||||
|
||||
// Write8SPI writes a single byte value to register addr using the provided SPI bus.
|
||||
func (d *Device8) Write8SPI(bus drivers.SPI, addr, value uint8) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -126,7 +106,6 @@ func (d *Device8) Write8SPI(bus drivers.SPI, addr, value uint8) error {
|
||||
return bus.Tx(d.buf[:2], nil)
|
||||
}
|
||||
|
||||
// Write16SPI writes a 16-bit value to register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Write16SPI(bus drivers.SPI, addr uint8, value uint16, order binary.ByteOrder) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -134,7 +113,6 @@ func (d *Device8) Write16SPI(bus drivers.SPI, addr uint8, value uint16, order bi
|
||||
return bus.Tx(d.buf[:3], nil)
|
||||
}
|
||||
|
||||
// Write32SPI writes a 32-bit value to register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Write32SPI(bus drivers.SPI, addr uint8, value uint32, order binary.ByteOrder) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
@@ -1,123 +0,0 @@
|
||||
package regmap
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
// Device8SPI implements common logic to most 8-bit peripherals with an SPI bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8SPI struct {
|
||||
bus drivers.SPI
|
||||
order binary.ByteOrder
|
||||
d Device8
|
||||
}
|
||||
|
||||
// SetBus sets the SPI bus and byte order for the Device8SPI.
|
||||
//
|
||||
// As a hint, most SPI devices use big-endian (MSB) byte order.
|
||||
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
|
||||
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
|
||||
func (d *Device8SPI) SetBus(bus drivers.SPI, order binary.ByteOrder) {
|
||||
d.bus = bus
|
||||
d.order = order
|
||||
}
|
||||
|
||||
// Read8 reads a single byte from register addr.
|
||||
func (d *Device8SPI) Read8(addr uint8) (byte, error) {
|
||||
return d.d.Read8SPI(d.bus, addr)
|
||||
}
|
||||
|
||||
// Read16 reads a 16-bit value from register addr.
|
||||
func (d *Device8SPI) Read16(addr uint8) (uint16, error) {
|
||||
return d.d.Read16SPI(d.bus, addr, d.order)
|
||||
}
|
||||
|
||||
// Read32 reads a 32-bit value from register addr.
|
||||
func (d *Device8SPI) Read32(addr uint8) (uint32, error) {
|
||||
return d.d.Read32SPI(d.bus, addr, d.order)
|
||||
}
|
||||
|
||||
// ReadData reads dataLength bytes from register addr. Due to the internal functioning of
|
||||
// SPI, an auxiliary buffer must be provided to perform the operation and avoid memory allocation.
|
||||
// The returned slice is a subslice of auxBuffer containing the read data.
|
||||
func (d *Device8SPI) ReadData(addr uint8, datalength int, auxBuffer []byte) ([]byte, error) {
|
||||
return d.d.ReadDataSPI(d.bus, addr, datalength, auxBuffer)
|
||||
}
|
||||
|
||||
// Write8 writes a single byte value to register addr.
|
||||
func (d *Device8SPI) Write8(addr, value uint8) error {
|
||||
return d.d.Write8SPI(d.bus, addr, value)
|
||||
}
|
||||
|
||||
// Write16 writes a 16-bit value to register addr.
|
||||
func (d *Device8SPI) Write16(addr uint8, value uint16) error {
|
||||
return d.d.Write16SPI(d.bus, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Write32 writes a 32-bit value to register addr.
|
||||
func (d *Device8SPI) Write32(addr uint8, value uint32) error {
|
||||
return d.d.Write32SPI(d.bus, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Device8I2C implements common logic to most 8-bit peripherals with an I2C bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8I2C struct {
|
||||
bus drivers.I2C
|
||||
i2cAddr uint16
|
||||
order binary.ByteOrder
|
||||
d Device8
|
||||
}
|
||||
|
||||
// SetBus sets the I2C bus, device address, and byte order for the Device8I2C.
|
||||
//
|
||||
// As a hint, most I2C devices use big-endian (MSB) byte order.
|
||||
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
|
||||
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
|
||||
func (d *Device8I2C) SetBus(bus drivers.I2C, i2cAddr uint16, order binary.ByteOrder) {
|
||||
d.bus = bus
|
||||
d.i2cAddr = i2cAddr
|
||||
d.order = order
|
||||
}
|
||||
|
||||
// Read8 reads a single byte from register addr.
|
||||
func (d *Device8I2C) Read8(addr uint8) (byte, error) {
|
||||
return d.d.Read8I2C(d.bus, d.i2cAddr, addr)
|
||||
}
|
||||
|
||||
// Read16 reads a 16-bit value from register addr.
|
||||
func (d *Device8I2C) Read16(addr uint8) (uint16, error) {
|
||||
return d.d.Read16I2C(d.bus, d.i2cAddr, addr, d.order)
|
||||
}
|
||||
|
||||
// Read32 reads a 32-bit value from register addr.
|
||||
func (d *Device8I2C) Read32(addr uint8) (uint32, error) {
|
||||
return d.d.Read32I2C(d.bus, d.i2cAddr, addr, d.order)
|
||||
}
|
||||
|
||||
// ReadData reads dataLength bytes from register addr.
|
||||
func (d *Device8I2C) ReadData(addr uint8, dataDestination []byte) error {
|
||||
return d.d.ReadDataI2C(d.bus, d.i2cAddr, addr, dataDestination)
|
||||
}
|
||||
|
||||
// Write8 writes a single byte value to register addr.
|
||||
func (d *Device8I2C) Write8(addr, value uint8) error {
|
||||
return d.d.Write8I2C(d.bus, d.i2cAddr, addr, value)
|
||||
}
|
||||
|
||||
// Write16 writes a 16-bit value to register addr.
|
||||
func (d *Device8I2C) Write16(addr uint8, value uint16) error {
|
||||
return d.d.Write16I2C(d.bus, d.i2cAddr, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Write32 writes a 32-bit value to register addr.
|
||||
func (d *Device8I2C) Write32(addr uint8, value uint32) error {
|
||||
return d.d.Write32I2C(d.bus, d.i2cAddr, addr, value, d.order)
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package regmap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
func ExampleDevice8Txer() {
|
||||
// Initialization.
|
||||
var dtx Device8Txer
|
||||
dtx.SetTxBuffers(make([]byte, 256), make([]byte, 256))
|
||||
|
||||
// Usage.
|
||||
const (
|
||||
defaultAddr = 65
|
||||
REG_WRITE = 0x1f
|
||||
IOCTL_CALL = 0xc0
|
||||
)
|
||||
tx, err := dtx.Tx(REG_WRITE)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = tx.AddWriteData(IOCTL_CALL, 0x80, 0x80)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var bus drivers.I2C
|
||||
readData, err := tx.DoTxI2C(bus, defaultAddr, 20)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(readData)
|
||||
}
|
||||
+36
-115
@@ -11,57 +11,37 @@ import (
|
||||
// Device wraps an I2C connection to a LIS3DH device.
|
||||
type Device struct {
|
||||
bus drivers.I2C
|
||||
address uint16
|
||||
r Range
|
||||
accel [6]byte // stored acceleration data (from the Update call)
|
||||
}
|
||||
|
||||
// Driver configuration, used for the Configure call. All fields are optional.
|
||||
type Config struct {
|
||||
Address uint16
|
||||
r Range
|
||||
}
|
||||
|
||||
// New creates a new LIS3DH connection. The I2C bus must already be configured.
|
||||
//
|
||||
// This function only creates the Device object, it does not touch the device.
|
||||
func New(bus drivers.I2C) Device {
|
||||
return Device{bus: bus, address: Address0}
|
||||
return Device{bus: bus, Address: Address0}
|
||||
}
|
||||
|
||||
// Configure sets up the device for communication
|
||||
func (d *Device) Configure(config Config) error {
|
||||
if config.Address != 0 {
|
||||
d.address = config.Address
|
||||
}
|
||||
|
||||
func (d *Device) Configure() {
|
||||
// enable all axes, normal mode
|
||||
err := legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL1, []byte{0x07})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL1, []byte{0x07})
|
||||
|
||||
// 400Hz rate
|
||||
err = d.SetDataRate(DATARATE_400_HZ)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.SetDataRate(DATARATE_400_HZ)
|
||||
|
||||
// High res & BDU enabled
|
||||
err = legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL4, []byte{0x88})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL4, []byte{0x88})
|
||||
|
||||
// get current range
|
||||
d.r, err = d.ReadRange()
|
||||
return err
|
||||
d.r = d.ReadRange()
|
||||
}
|
||||
|
||||
// Connected returns whether a LIS3DH has been found.
|
||||
// It does a "who am I" request and checks the response.
|
||||
func (d *Device) Connected() bool {
|
||||
data := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), WHO_AM_I, data)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), WHO_AM_I, data)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -69,51 +49,46 @@ func (d *Device) Connected() bool {
|
||||
}
|
||||
|
||||
// SetDataRate sets the speed of data collected by the LIS3DH.
|
||||
func (d *Device) SetDataRate(rate DataRate) error {
|
||||
func (d *Device) SetDataRate(rate DataRate) {
|
||||
ctl1 := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL1, ctl1)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL1, ctl1)
|
||||
if err != nil {
|
||||
return err
|
||||
println(err.Error())
|
||||
}
|
||||
// mask off bits
|
||||
ctl1[0] &^= 0xf0
|
||||
ctl1[0] |= (byte(rate) << 4)
|
||||
return legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL1, ctl1)
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL1, ctl1)
|
||||
}
|
||||
|
||||
// SetRange sets the G range for LIS3DH.
|
||||
func (d *Device) SetRange(r Range) error {
|
||||
func (d *Device) SetRange(r Range) {
|
||||
ctl := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
return err
|
||||
println(err.Error())
|
||||
}
|
||||
// mask off bits
|
||||
ctl[0] &^= 0x30
|
||||
ctl[0] |= (byte(r) << 4)
|
||||
err = legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
|
||||
// store the new range
|
||||
d.r = r
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadRange returns the current G range for LIS3DH.
|
||||
func (d *Device) ReadRange() (r Range, err error) {
|
||||
func (d *Device) ReadRange() (r Range) {
|
||||
ctl := []byte{0}
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
println(err.Error())
|
||||
}
|
||||
// mask off bits
|
||||
r = Range(ctl[0] >> 4)
|
||||
r &= 0x03
|
||||
|
||||
return r, nil
|
||||
return r
|
||||
}
|
||||
|
||||
// ReadAcceleration reads the current acceleration from the device and returns
|
||||
@@ -121,17 +96,28 @@ func (d *Device) ReadRange() (r Range, err error) {
|
||||
// and the sensor is not moving the returned value will be around 1000000 or
|
||||
// -1000000.
|
||||
func (d *Device) ReadAcceleration() (int32, int32, int32, error) {
|
||||
rawX, rawY, rawZ := d.ReadRawAcceleration()
|
||||
x, y, z := normalizeRange(rawX, rawY, rawZ, d.r)
|
||||
return x, y, z, nil
|
||||
x, y, z := d.ReadRawAcceleration()
|
||||
divider := float32(1)
|
||||
switch d.r {
|
||||
case RANGE_16_G:
|
||||
divider = 1365
|
||||
case RANGE_8_G:
|
||||
divider = 4096
|
||||
case RANGE_4_G:
|
||||
divider = 8190
|
||||
case RANGE_2_G:
|
||||
divider = 16380
|
||||
}
|
||||
|
||||
return int32(float32(x) / divider * 1000000), int32(float32(y) / divider * 1000000), int32(float32(z) / divider * 1000000), nil
|
||||
}
|
||||
|
||||
// ReadRawAcceleration returns the raw x, y and z axis from the LIS3DH
|
||||
func (d *Device) ReadRawAcceleration() (x int16, y int16, z int16) {
|
||||
legacy.WriteRegister(d.bus, uint8(d.address), REG_OUT_X_L|0x80, nil)
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_OUT_X_L|0x80, nil)
|
||||
|
||||
data := []byte{0, 0, 0, 0, 0, 0}
|
||||
d.bus.Tx(d.address, nil, data)
|
||||
d.bus.Tx(d.Address, nil, data)
|
||||
|
||||
x = int16((uint16(data[1]) << 8) | uint16(data[0]))
|
||||
y = int16((uint16(data[3]) << 8) | uint16(data[2]))
|
||||
@@ -139,68 +125,3 @@ func (d *Device) ReadRawAcceleration() (x int16, y int16, z int16) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Update the sensor values of the 'which' parameter. Only acceleration is
|
||||
// supported at the moment.
|
||||
func (d *Device) Update(which drivers.Measurement) error {
|
||||
if which&drivers.Acceleration != 0 {
|
||||
// Read raw acceleration values and store them in the driver.
|
||||
err := legacy.WriteRegister(d.bus, uint8(d.address), REG_OUT_X_L|0x80, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.bus.Tx(d.address, nil, d.accel[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Acceleration returns the last read acceleration in µg (micro-gravity).
|
||||
// When one of the axes is pointing straight to Earth and the sensor is not
|
||||
// moving the returned value will be around 1000000 or -1000000.
|
||||
func (d *Device) Acceleration() (x, y, z int32) {
|
||||
// Extract the raw 16-bit values.
|
||||
rawX := int16((uint16(d.accel[1]) << 8) | uint16(d.accel[0]))
|
||||
rawY := int16((uint16(d.accel[3]) << 8) | uint16(d.accel[2]))
|
||||
rawZ := int16((uint16(d.accel[5]) << 8) | uint16(d.accel[4]))
|
||||
|
||||
// Normalize these values, to be in µg (micro-gravity).
|
||||
return normalizeRange(rawX, rawY, rawZ, d.r)
|
||||
}
|
||||
|
||||
// Convert raw 16-bit values to normalized 32-bit values while avoiding floats
|
||||
// and divisions.
|
||||
func normalizeRange(rawX, rawY, rawZ int16, r Range) (x, y, z int32) {
|
||||
// We're going to convert the 16-bit raw values to values in the range
|
||||
// -1000_000..1000_000. For now we're going to assume a range of 16G, we'll
|
||||
// adjust that range later.
|
||||
// The formula is derived as follows, and carefully selected to avoid
|
||||
// overflow and integer divisions (the division will be optimized to a
|
||||
// bitshift):
|
||||
// x = x * 1000_000 / 2048
|
||||
// x = x * (1000_000/64) / (2048/64)
|
||||
// x = x * 15625 / 32
|
||||
x = int32(rawX) * 15625 / 32
|
||||
y = int32(rawY) * 15625 / 32
|
||||
z = int32(rawZ) * 15625 / 32
|
||||
|
||||
// Now we need to normalize the three values, since we assumed 16G before.
|
||||
shift := uint32(0)
|
||||
switch r {
|
||||
case RANGE_16_G:
|
||||
shift = 0
|
||||
case RANGE_8_G:
|
||||
shift = 1
|
||||
case RANGE_4_G:
|
||||
shift = 2
|
||||
case RANGE_2_G:
|
||||
shift = 3
|
||||
}
|
||||
x >>= shift
|
||||
y >>= shift
|
||||
z >>= shift
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
+28
-35
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
type AccelRange uint8
|
||||
@@ -27,7 +28,7 @@ type Device struct {
|
||||
accelMultiplier int32
|
||||
gyroMultiplier int32
|
||||
magMultiplier int32
|
||||
buf [7]uint8 // up to 6 bytes for read + 1 byte for the register address
|
||||
buf [6]uint8
|
||||
}
|
||||
|
||||
// Configuration for LSM9DS1 device.
|
||||
@@ -60,15 +61,10 @@ func New(bus drivers.I2C) *Device {
|
||||
// Case of boolean false and error nil means I2C is up,
|
||||
// but "who am I" responses have unexpected values.
|
||||
func (d *Device) Connected() bool {
|
||||
data, err := d.readBytes(d.AccelAddress, WHO_AM_I, 1)
|
||||
if err != nil || data[0] != 0x68 {
|
||||
return false
|
||||
}
|
||||
data, err = d.readBytes(d.MagAddress, WHO_AM_I_M, 1)
|
||||
if err != nil || data[0] != 0x3D {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
data1, data2 := d.buf[:1], d.buf[1:2]
|
||||
legacy.ReadRegister(d.bus, d.AccelAddress, WHO_AM_I, data1)
|
||||
legacy.ReadRegister(d.bus, d.MagAddress, WHO_AM_I_M, data2)
|
||||
return data1[0] == 0x68 && data2[0] == 0x3D
|
||||
}
|
||||
|
||||
// ReadAcceleration reads the current acceleration from the device and returns
|
||||
@@ -76,7 +72,8 @@ func (d *Device) Connected() bool {
|
||||
// and the sensor is not moving the returned value will be around 1000000 or
|
||||
// -1000000.
|
||||
func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_X_L_XL, 6)
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_X_L_XL, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -91,7 +88,8 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
|
||||
// rotation along one axis and while doing so integrate all values over time,
|
||||
// you would get a value close to 360000000.
|
||||
func (d *Device) ReadRotation() (x, y, z int32, err error) {
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_X_L_G, 6)
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_X_L_G, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -104,7 +102,8 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
|
||||
// ReadMagneticField reads the current magnetic field from the device and returns
|
||||
// it in nT (nanotesla). 1 G (gauss) = 100_000 nT (nanotesla).
|
||||
func (d *Device) ReadMagneticField() (x, y, z int32, err error) {
|
||||
data, err := d.readBytes(d.MagAddress, OUT_X_L_M, 6)
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.MagAddress), OUT_X_L_M, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -116,7 +115,8 @@ func (d *Device) ReadMagneticField() (x, y, z int32, err error) {
|
||||
|
||||
// ReadTemperature returns the temperature in Celsius milli degrees (°C/1000)
|
||||
func (d *Device) ReadTemperature() (t int32, err error) {
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_TEMP_L, 2)
|
||||
data := d.buf[:2]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_TEMP_L, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -167,16 +167,20 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
|
||||
d.magMultiplier = 58
|
||||
}
|
||||
|
||||
data := d.buf[:1]
|
||||
|
||||
// Configure accelerometer
|
||||
// Sample rate & measurement range
|
||||
err = d.writeByte(d.AccelAddress, CTRL_REG6_XL, uint8(cfg.AccelSampleRate)<<5|uint8(cfg.AccelRange)<<3)
|
||||
data[0] = uint8(cfg.AccelSampleRate)<<5 | uint8(cfg.AccelRange)<<3
|
||||
err = legacy.WriteRegister(d.bus, d.AccelAddress, CTRL_REG6_XL, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Configure gyroscope
|
||||
// Sample rate & measurement range
|
||||
err = d.writeByte(d.AccelAddress, CTRL_REG1_G, uint8(cfg.GyroSampleRate)<<5|uint8(cfg.GyroRange)<<3)
|
||||
data[0] = uint8(cfg.GyroSampleRate)<<5 | uint8(cfg.GyroRange)<<3
|
||||
err = legacy.WriteRegister(d.bus, d.AccelAddress, CTRL_REG1_G, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -186,44 +190,33 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
|
||||
// Temperature compensation enabled
|
||||
// High-performance mode XY axis
|
||||
// Sample rate
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG1_M, 0b10000000|0b01000000|uint8(cfg.MagSampleRate)<<2)
|
||||
data[0] = 0b10000000 | 0b01000000 | uint8(cfg.MagSampleRate)<<2
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG1_M, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Measurement range
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG2_M, uint8(cfg.MagRange)<<5)
|
||||
data[0] = uint8(cfg.MagRange) << 5
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG2_M, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Continuous-conversion mode
|
||||
// https://electronics.stackexchange.com/questions/237397/continuous-conversion-vs-single-conversion-mode
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG3_M, 0b00000000)
|
||||
data[0] = 0b00000000
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG3_M, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// High-performance mode Z axis
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG4_M, 0b00001000)
|
||||
data[0] = 0b00001000
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG4_M, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) readBytes(addr, reg, size uint8) ([]byte, error) {
|
||||
d.buf[0] = reg
|
||||
err := d.bus.Tx(uint16(addr), d.buf[0:1], d.buf[1:size+1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.buf[1 : size+1], nil
|
||||
}
|
||||
|
||||
func (d *Device) writeByte(addr, reg, value uint8) error {
|
||||
d.buf[0] = reg
|
||||
d.buf[1] = value
|
||||
return d.bus.Tx(uint16(addr), d.buf[0:2], nil)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package drivers
|
||||
|
||||
// Pin types abstract the underlying implementation of a pin,
|
||||
// allowing the use of different hardware or libraries without changing driver code.
|
||||
//
|
||||
// Note, pin mode functionality is not part of the Pin interface.
|
||||
// Client code is responsible for configuring pin modes correctly.
|
||||
// This can be done either before passing the pin to a driver constructor
|
||||
// or by ensuring correct pin mode is set when Get or Set methods are called.
|
||||
// See rpio package for an example of a pin implementation that does this.
|
||||
//
|
||||
// Drivers that used to configure output pin mode in their constructors can use
|
||||
// legacy.PinOutput() wrapper to keep the same behavior for machine.Pin.
|
||||
// See internal/legacy/pinlegacy_tinygo.go and internal/legacy/pinlegacy_generic.go for details.
|
||||
//
|
||||
// All new drivers are encouraged to not configure pin modes in their constructors and
|
||||
// do not depend on either machine package or legacy wrappers.
|
||||
|
||||
// PinInput is an interface for reading the state of a pin.
|
||||
type PinInput interface {
|
||||
Get() bool
|
||||
}
|
||||
|
||||
// PinOutput is an interface for setting the state of a pin.
|
||||
type PinOutput interface {
|
||||
Set(bool)
|
||||
}
|
||||
|
||||
// Pin is an interface for a pin that can be both input and output.
|
||||
type Pin interface {
|
||||
PinInput
|
||||
PinOutput
|
||||
}
|
||||
|
||||
// PinGet is a function type for getting the state of a pin
|
||||
// Shall be used in high-performance drivers to avoid interface call overhead.
|
||||
type PinGet func() bool
|
||||
|
||||
// PinSet is a function type for setting the state of a pin.
|
||||
// Shall be used in high-performance drivers to avoid interface call overhead.
|
||||
type PinSet func(bool)
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// Implementation of the Pin interface for the Raspberry Pi GPIO pins.
|
||||
// Depends on the go-rpio library.
|
||||
// Configures pin modes automatically when Get or Set methods are called.
|
||||
|
||||
package rpio // import "tinygo.org/x/drivers/rpio"
|
||||
|
||||
import go_rpio "github.com/stianeikeland/go-rpio/v4"
|
||||
|
||||
type Pin struct {
|
||||
modeSet bool
|
||||
Mode go_rpio.Mode
|
||||
Pin go_rpio.Pin
|
||||
}
|
||||
|
||||
func NewPin(pinNumber int) *Pin {
|
||||
return &Pin{Pin: go_rpio.Pin(pinNumber)}
|
||||
}
|
||||
|
||||
func (p *Pin) Get() bool {
|
||||
if !p.modeSet || p.Mode != go_rpio.Input {
|
||||
p.Pin.Input()
|
||||
p.Mode = go_rpio.Input
|
||||
}
|
||||
return p.Pin.Read() == go_rpio.High
|
||||
}
|
||||
|
||||
func (p *Pin) Set(high bool) {
|
||||
if !p.modeSet || p.Mode != go_rpio.Output {
|
||||
p.Pin.Output()
|
||||
p.Mode = go_rpio.Output
|
||||
}
|
||||
state := go_rpio.Low
|
||||
if high {
|
||||
state = go_rpio.High
|
||||
}
|
||||
p.Pin.Write(state)
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package rpio // import "tinygo.org/x/drivers/rpio"
|
||||
|
||||
import go_rpio "github.com/stianeikeland/go-rpio/v4"
|
||||
|
||||
type SPI struct {
|
||||
}
|
||||
|
||||
func NewSPI() (s *SPI) {
|
||||
if err := go_rpio.SpiBegin(go_rpio.Spi0); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go_rpio.SpiSpeed(25_000_000) // 25 MHz
|
||||
go_rpio.SpiChipSelect(0)
|
||||
return &SPI{}
|
||||
}
|
||||
|
||||
func (s *SPI) Tx(w, r []byte) error {
|
||||
data := make([]byte, len(w))
|
||||
copy(data, w)
|
||||
go_rpio.SpiExchange(data)
|
||||
copy(r, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SPI) Transfer(b byte) (byte, error) {
|
||||
w := []byte{b}
|
||||
r := make([]byte, len(w))
|
||||
err := s.Tx(w, r)
|
||||
return r[0], err
|
||||
}
|
||||
+17
-20
@@ -1,46 +1,43 @@
|
||||
package ssd1306
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
type SPIBus struct {
|
||||
wire drivers.SPI
|
||||
dcPin machine.Pin
|
||||
resetPin machine.Pin
|
||||
csPin machine.Pin
|
||||
dcPin drivers.PinOutput
|
||||
resetPin drivers.PinOutput
|
||||
csPin drivers.PinOutput
|
||||
buffer []byte // buffer to avoid heap allocations
|
||||
}
|
||||
|
||||
// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
|
||||
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) *Device {
|
||||
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin drivers.PinOutput) *Device {
|
||||
return &Device{
|
||||
bus: &SPIBus{
|
||||
wire: bus,
|
||||
dcPin: dcPin,
|
||||
resetPin: resetPin,
|
||||
csPin: csPin,
|
||||
dcPin: legacy.PinOutput(dcPin),
|
||||
resetPin: legacy.PinOutput(resetPin),
|
||||
csPin: legacy.PinOutput(csPin),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// configure pins with the SPI bus and allocate the buffer
|
||||
func (b *SPIBus) configure(address uint16, size int16) []byte {
|
||||
b.csPin.Low()
|
||||
b.dcPin.Low()
|
||||
b.resetPin.Low()
|
||||
b.csPin.Set(false)
|
||||
b.dcPin.Set(false)
|
||||
b.resetPin.Set(false)
|
||||
|
||||
b.resetPin.High()
|
||||
b.resetPin.Set(true)
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
b.resetPin.Low()
|
||||
b.resetPin.Set(false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
b.resetPin.High()
|
||||
b.resetPin.Set(true)
|
||||
|
||||
b.buffer = make([]byte, size+1) // +1 for a command
|
||||
return b.buffer[1:] // return the image buffer
|
||||
@@ -59,10 +56,10 @@ func (b *SPIBus) flush() error {
|
||||
|
||||
// tx sends data to the display
|
||||
func (b *SPIBus) tx(data []byte, isCommand bool) error {
|
||||
b.csPin.High()
|
||||
b.csPin.Set(true)
|
||||
b.dcPin.Set(!isCommand)
|
||||
b.csPin.Low()
|
||||
b.csPin.Set(false)
|
||||
err := b.wire.Tx(data, nil)
|
||||
b.csPin.High()
|
||||
b.csPin.Set(true)
|
||||
return err
|
||||
}
|
||||
|
||||
+17
-21
@@ -5,12 +5,12 @@ package st7735 // import "tinygo.org/x/drivers/st7735"
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
|
||||
@@ -39,10 +39,10 @@ type Device = DeviceOf[pixel.RGB565BE]
|
||||
// formats.
|
||||
type DeviceOf[T Color] struct {
|
||||
bus drivers.SPI
|
||||
dcPin machine.Pin
|
||||
resetPin machine.Pin
|
||||
csPin machine.Pin
|
||||
blPin machine.Pin
|
||||
dcPin drivers.PinOutput
|
||||
resetPin drivers.PinOutput
|
||||
csPin drivers.PinOutput
|
||||
blPin drivers.PinOutput
|
||||
width int16
|
||||
height int16
|
||||
columnOffset int16
|
||||
@@ -65,23 +65,19 @@ type Config struct {
|
||||
}
|
||||
|
||||
// New creates a new ST7735 connection. The SPI wire must already be configured.
|
||||
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
|
||||
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) Device {
|
||||
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
|
||||
}
|
||||
|
||||
// NewOf creates a new ST7735 connection with a particular pixel format. The SPI
|
||||
// wire must already be configured.
|
||||
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin machine.Pin) DeviceOf[T] {
|
||||
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
blPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) DeviceOf[T] {
|
||||
return DeviceOf[T]{
|
||||
bus: bus,
|
||||
dcPin: dcPin,
|
||||
resetPin: resetPin,
|
||||
csPin: csPin,
|
||||
blPin: blPin,
|
||||
dcPin: legacy.PinOutput(dcPin),
|
||||
resetPin: legacy.PinOutput(resetPin),
|
||||
csPin: legacy.PinOutput(csPin),
|
||||
blPin: legacy.PinOutput(blPin),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,11 +110,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
|
||||
d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
|
||||
|
||||
// reset the device
|
||||
d.resetPin.High()
|
||||
d.resetPin.Set(true)
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
d.resetPin.Low()
|
||||
d.resetPin.Set(false)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
d.resetPin.High()
|
||||
d.resetPin.Set(true)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
|
||||
// Common initialization
|
||||
@@ -226,7 +222,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
|
||||
|
||||
d.SetRotation(d.rotation)
|
||||
|
||||
d.blPin.High()
|
||||
d.blPin.Set(true)
|
||||
}
|
||||
|
||||
// Display does nothing, there's no buffer as it might be too big for some boards
|
||||
@@ -438,9 +434,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
|
||||
// EnableBacklight enables or disables the backlight
|
||||
func (d *DeviceOf[T]) EnableBacklight(enable bool) {
|
||||
if enable {
|
||||
d.blPin.High()
|
||||
d.blPin.Set(true)
|
||||
} else {
|
||||
d.blPin.Low()
|
||||
d.blPin.Set(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//go:build baremetal && tinygo
|
||||
|
||||
package tinygo // import "tinygo.org/x/drivers/tinygo"
|
||||
|
||||
import "machine"
|
||||
|
||||
// tinygo.Pin wraps machine.Pin to ensure correct pin mode is set when Get or Set methods are called.
|
||||
|
||||
type Pin struct {
|
||||
pin machine.Pin
|
||||
mode machine.PinMode
|
||||
|
||||
modeSet bool
|
||||
inputMode machine.PinMode
|
||||
}
|
||||
|
||||
func New(pin machine.Pin) *Pin {
|
||||
return &Pin{pin: pin, inputMode: machine.PinInput}
|
||||
}
|
||||
|
||||
func NewWithInputMode(pin machine.Pin, inputMode machine.PinMode) *Pin {
|
||||
return &Pin{pin: pin, inputMode: inputMode}
|
||||
}
|
||||
|
||||
func (p *Pin) Get() bool {
|
||||
if !p.modeSet || p.mode != p.inputMode {
|
||||
p.pin.Configure(machine.PinConfig{Mode: p.inputMode})
|
||||
p.mode = machine.PinInput
|
||||
}
|
||||
return p.pin.Get()
|
||||
}
|
||||
|
||||
func (p *Pin) Set(high bool) {
|
||||
if !p.modeSet || p.mode != machine.PinOutput {
|
||||
p.pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
p.mode = machine.PinOutput
|
||||
}
|
||||
p.pin.Set(high)
|
||||
}
|
||||
+17
-23
@@ -8,11 +8,10 @@ package uc8151 // import "tinygo.org/x/drivers/uc8151"
|
||||
import (
|
||||
"errors"
|
||||
"image/color"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
|
||||
@@ -32,10 +31,10 @@ type Config struct {
|
||||
|
||||
type Device struct {
|
||||
bus drivers.SPI
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy pin.InputFunc
|
||||
cs machine.Pin
|
||||
dc machine.Pin
|
||||
rst machine.Pin
|
||||
busy machine.Pin
|
||||
width int16
|
||||
height int16
|
||||
buffer []uint8
|
||||
@@ -50,22 +49,17 @@ type Device struct {
|
||||
type Speed uint8
|
||||
|
||||
// New returns a new uc8151 driver. Pass in a fully configured SPI bus.
|
||||
// Pins passed in must be configured beforehand.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
// For backwards compatibility.
|
||||
// This driver used to configure pins,
|
||||
// so leave in to not break users.
|
||||
// May be removed in future so try not to depend on it!
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
|
||||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
busyPin.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
dc: dcPin.Set,
|
||||
rst: rstPin.Set,
|
||||
isBusy: busyPin.Get,
|
||||
bus: bus,
|
||||
cs: csPin,
|
||||
dc: dcPin,
|
||||
rst: rstPin,
|
||||
busy: busyPin,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,14 +313,14 @@ func (d *Device) ClearDisplay() {
|
||||
|
||||
// WaitUntilIdle waits until the display is ready
|
||||
func (d *Device) WaitUntilIdle() {
|
||||
for !d.isBusy() {
|
||||
for !d.busy.Get() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// IsBusy returns the busy status of the display
|
||||
func (d *Device) IsBusy() bool {
|
||||
return d.isBusy()
|
||||
return d.busy.Get()
|
||||
}
|
||||
|
||||
// ClearBuffer sets the buffer to 0xFF (white)
|
||||
|
||||
Reference in New Issue
Block a user