mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-11 18:33:43 +00:00
Add support for CEVA BNO08x 9DoF sensor (#809)
* Add support for CEVA BNO08x 9DoF sensor. Also includes implementation of CEVA SH-2 and SHTP protocols.
* Replace machine.I2C with drivers.I2C interface to remove dependency on machine package
* Replace Pin functionality with that provided by tinygo.org/x/drivers/internal/pin
* Unexport fields on SensorValue and replace with accessor methods. Add check for correct SensorID validation
* Add example to smoketest.sh
* Change build target for smoketest to match development environment.. Probably not important, but matches reality.
* Fix decoding of some sensor data: Step Counter, Tap Detector, Flip Detector. These are experimental.
* Refactor to allow SPI/UART etc. SPI is currently under development, but is omitted from this commit.
Example code has been moved to i2c subdirectory.
This commit introduces some major refactoring changes. It introduces a "Buser" interface and tries to remove any I2C specific code from the core. It still retains a couple of I2C specific fields in the "Config" struct ("Address" and "ReadChunk") but they are ignored in the as yet uncommited SPI code.
* Fix CRLF -> LF for gofmt
* Update smoketest to point to new example file
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// Package main provides a basic example of using the BNO08x driver
|
||||
// to read rotation vector (quaternion) data from the sensor.
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/bno08x"
|
||||
)
|
||||
|
||||
func main() {
|
||||
time.Sleep(2 * time.Second) // Wait for sensor to power up
|
||||
// Initialize I2C bus
|
||||
i2c := machine.I2C0
|
||||
err := i2c.Configure(machine.I2CConfig{
|
||||
Frequency: 400 * machine.KHz,
|
||||
})
|
||||
if err != nil {
|
||||
println("Failed to configure I2C:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
println("Initializing BNO08x sensor...")
|
||||
|
||||
// Create and configure sensor using I2C
|
||||
sensor := bno08x.NewI2C(i2c)
|
||||
err = sensor.Configure(bno08x.Config{})
|
||||
if err != nil {
|
||||
println("Failed to configure sensor:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
println("Sensor initialized successfully")
|
||||
|
||||
// Enable Game Rotation Vector reports at 100Hz (10000 microseconds = 10ms interval)
|
||||
// Using Game Rotation Vector (0x08) to match the working channel_debug test
|
||||
err = sensor.EnableReport(bno08x.SensorGameRotationVector, 10000)
|
||||
if err != nil {
|
||||
println("Failed to enable game rotation vector:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
println("Reading rotation vectors...")
|
||||
println("Format: Real I J K Accuracy")
|
||||
|
||||
// Add a delay after enabling reports (Arduino does this)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Main loop - read and display quaternion data
|
||||
for {
|
||||
event, ok := sensor.GetSensorEvent()
|
||||
if ok && (event.ID() == bno08x.SensorRotationVector || event.ID() == bno08x.SensorGameRotationVector) {
|
||||
q := event.Quaternion()
|
||||
if event.ID() == bno08x.SensorRotationVector {
|
||||
println(q.Real, q.I, q.J, q.K, event.QuaternionAccuracy())
|
||||
} else {
|
||||
// GameRotationVector doesn't have accuracy
|
||||
println(q.Real, q.I, q.J, q.K)
|
||||
}
|
||||
}
|
||||
|
||||
// Arduino uses 10ms delay in loop
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user