adxl345: update ADXL345 driver and example to make uniform with other accelerometers

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans
2019-04-26 20:07:36 +02:00
committed by Ayke van Laethem
parent b7fe897b8c
commit 8ac1fa2b22
2 changed files with 34 additions and 31 deletions
+32 -28
View File
@@ -1,7 +1,9 @@
// Package provides a driver for the digital accelerometer ADXL345 // Package adxl345 provides a driver for the ADXL345 digital accelerometer.
// //
// Datasheet EN: http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf // Datasheet EN: http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
//
// Datasheet JP: http://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf // Datasheet JP: http://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf
//
package adxl345 package adxl345
import ( import (
@@ -36,21 +38,20 @@ type bwRate struct {
rate Rate rate Rate
} }
// Device wraps an I2C connection to a BMP180 device. // Device wraps an I2C connection to a ADXL345 device.
type Device struct { type Device struct {
bus machine.I2C bus machine.I2C
Address uint16 Address uint16
powerCtl powerCtl powerCtl powerCtl
dataFormat dataFormat dataFormat dataFormat
bwRate bwRate bwRate bwRate
x, y, z int32
rawX, rawY, rawZ int32
} }
// New creates a new BMP180 connection. The I2C bus must already be // New creates a new ADXL345 connection. The I2C bus must already be
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not init the device.
// To do that you must call the Configure() method on the Device before using it.
func New(bus machine.I2C) Device { func New(bus machine.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
@@ -87,31 +88,34 @@ func (d *Device) Restart() {
d.bus.WriteRegister(uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()}) d.bus.WriteRegister(uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
} }
// Acceleration returns the adjusted x, y and z axis in µG // ReadAcceleration reads the current acceleration from the device and returns
func (d *Device) Acceleration() (x int32, y int32, z int32) { // it in µg (micro-gravity). When one of the axes is pointing straight to Earth
return d.x, d.y, d.z // and the sensor is not moving the returned value will be around 1000000 or
// -1000000.
func (d *Device) ReadAcceleration() (x int32, y int32, z int32, err error) {
rx, ry, rz := d.ReadRawAcceleration()
x = d.dataFormat.convertToIS(rx)
y = d.dataFormat.convertToIS(ry)
z = d.dataFormat.convertToIS(rz)
return
} }
// XYZ returns the raw x, y and z axis from the adxl345 // ReadRawAcceleration reads the sensor values and returns the raw x, y and z axis
func (d *Device) RawXYZ() (x int32, y int32, z int32) { // from the adxl345.
return d.rawX, d.rawY, d.rawZ func (d *Device) ReadRawAcceleration() (x int32, y int32, z int32) {
}
// Update reads the sensor values and stores them in a buffer
func (d *Device) Update() {
data := []byte{0, 0, 0, 0, 0, 0} data := []byte{0, 0, 0, 0, 0, 0}
d.bus.ReadRegister(uint8(d.Address), REG_DATAX0, data) d.bus.ReadRegister(uint8(d.Address), REG_DATAX0, data)
d.rawX = readIntLE(data[0], data[1]) x = readIntLE(data[0], data[1])
d.rawY = readIntLE(data[2], data[3]) y = readIntLE(data[2], data[3])
d.rawZ = readIntLE(data[4], data[5]) z = readIntLE(data[4], data[5])
d.x = d.dataFormat.convertToIS(d.rawX) return
d.y = d.dataFormat.convertToIS(d.rawY)
d.z = d.dataFormat.convertToIS(d.rawZ)
} }
// SetRate change the current rate of the sensor // UseLowPower sets the ADXL345 to use the low power mode.
func (d *Device) UseLowPower(power bool) { func (d *Device) UseLowPower(power bool) {
if power { if power {
d.bwRate.lowPower = 1 d.bwRate.lowPower = 1
+2 -3
View File
@@ -14,11 +14,10 @@ func main() {
println("ADXL345 starts") println("ADXL345 starts")
for { for {
sensor.Update() x, y, z, _ := sensor.ReadAcceleration()
x, y, z := sensor.Acceleration()
println("X:", x, "Y:", y, "Z:", z) println("X:", x, "Y:", y, "Z:", z)
rx, ry, rz := sensor.RawXYZ() rx, ry, rz := sensor.ReadRawAcceleration()
println("X (raw):", rx, "Y (raw):", ry, "Z (raw):", rz) println("X (raw):", rx, "Y (raw):", ry, "Z (raw):", rz)
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)