mirror of https://github.com/periph/devices
Added I²C support.
- Removed useless ReadRaw - Changed the scope of ReadAndCombine to private readAndCombinepull/67/head
parent
9ddc05227d
commit
5eba749b5f
@ -1,56 +0,0 @@
|
||||
// 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/spi/spireg"
|
||||
"periph.io/x/devices/v3/adxl345"
|
||||
"periph.io/x/host/v3"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Example reads the acceleration values every 30ms for 3 seconds.
|
||||
func Example() {
|
||||
|
||||
// Initialize the host
|
||||
// Make sure periph is initialized.
|
||||
if _, err := host.Init(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
fmt.Println(d.String())
|
||||
|
||||
// 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(3 * time.Second)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-ticker.C:
|
||||
a := d.Update()
|
||||
fmt.Println(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue