mirror of https://github.com/periph/devices
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
1.9 KiB
Go
97 lines
1.9 KiB
Go
// Copyright 2023 The Periph Authors. All rights reserved.
|
|
// Use of this source code is governed under the Apache License, Version 2.0
|
|
// that can be found in the LICENSE file.
|
|
|
|
package example
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"periph.io/x/conn/v3/i2c/i2creg"
|
|
"periph.io/x/conn/v3/spi/spireg"
|
|
"periph.io/x/devices/v3/adxl345"
|
|
"periph.io/x/host/v3"
|
|
"time"
|
|
)
|
|
|
|
// I2C uses an adxl345 device connected by I²C.
|
|
// it reads the acceleration values every 30ms for 30 seconds.
|
|
// You can i use `i2dctools` to find the I²C bus number
|
|
// e.g : sudo apt-get install i2c-tools
|
|
//
|
|
// sudo i2cdetect -y 1
|
|
func I2C(addr uint16) {
|
|
|
|
mustInitHost()
|
|
|
|
// Use i2creg to find the first available I²C bus.
|
|
// Generally I2C1 on raspberry pi.
|
|
p, err := i2creg.Open("")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Print(p.String())
|
|
|
|
defer p.Close()
|
|
|
|
d, err := adxl345.NewI2C(p, addr, &adxl345.DefaultOpts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
measure(d, 30*time.Second)
|
|
}
|
|
|
|
// Spi uses an adxl345 device connected by SPI.
|
|
// it reads the acceleration values every 30ms for 30 seconds.
|
|
func Spi() {
|
|
|
|
mustInitHost()
|
|
|
|
// Use spireg SPI port registry to find the first available SPI bus.
|
|
p, err := spireg.Open("")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
d, err := adxl345.NewSpi(p, &adxl345.DefaultOpts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
measure(d, 30*time.Second)
|
|
}
|
|
|
|
// mustInitHost Make sure host is initialized.
|
|
func mustInitHost() {
|
|
|
|
if _, err := host.Init(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// measure reads the acceleration values every 30ms for <duration> seconds
|
|
func measure(d *adxl345.Dev, duration time.Duration) {
|
|
|
|
fmt.Println(d.String())
|
|
|
|
mode := d.Mode()
|
|
// use a ticker to read the acceleration values every 200ms
|
|
ticker := time.NewTicker(30 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
// stop after 3 seconds
|
|
stop := time.After(duration)
|
|
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
case <-ticker.C:
|
|
a := d.Update()
|
|
fmt.Println(mode, a)
|
|
}
|
|
}
|
|
}
|