From d3ad1aac1e54381766cde130a522e22ea48bbc14 Mon Sep 17 00:00:00 2001 From: bpds Date: Thu, 28 Dec 2023 11:31:41 +0100 Subject: [PATCH] Added sensitivity option. --- adxl345/adxl345.go | 46 +++++++++++++++++++++++++++++++------- adxl345/example/example.go | 2 ++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/adxl345/adxl345.go b/adxl345/adxl345.go index 3d5c129..8cd5e54 100644 --- a/adxl345/adxl345.go +++ b/adxl345/adxl345.go @@ -7,10 +7,15 @@ import ( "periph.io/x/conn/v3/spi" ) +type Sensitivity byte + const ( DeviceID = 0x00 // Device ID, expected to be 0xE5 when using ADXL345 - // 0x01 to 0x1C are reserved for future use + S2G Sensitivity = 0x00 // Sensitivity at 2g + S4G Sensitivity = 0x01 // Sensitivity at 4g + S8G Sensitivity = 0x02 // Sensitivity at 8g + S16G Sensitivity = 0x03 // Sensitivity at 16g ThreshTap = 0x1D // Tap threshold OfsX = 0x1E // X-axis offset @@ -60,13 +65,15 @@ var ( ) var DefaultOpts = Opts{ - TurnOnOnStart: true, - ExpectedDevid: 0xE5, + TurnOnOnStart: true, + ExpectedDeviceID: 0xE5, + Sensitivity: S2G, } type Opts struct { - TurnOnOnStart bool // Turn on the device in measurement mode on start. - ExpectedDevid byte // Expected device ID used to verify that the device is an ADXL345. + TurnOnOnStart bool // Turn on the device in measurement mode on start. + ExpectedDeviceID byte // Expected device ID used to verify that the device is an ADXL345. + Sensitivity Sensitivity // Sensitivity of the device (2G, 4G, 8G, 16G) } // Dev is a driver for the ADXL345 accelerometer @@ -77,7 +84,7 @@ type Dev struct { } func (d *Dev) String() string { - return fmt.Sprintf("ADXL345") + return fmt.Sprintf("ADXL345{Sensitivity:%d}", d.Sensitivity()) } // New creates a new ADXL345 Dev or returns an error. @@ -101,14 +108,37 @@ func New(p spi.Port, o *Opts) (*Dev, error) { return nil, err } } + if o.Sensitivity != S2G { // default + err = d.setSensitivity(o.Sensitivity) + if err != nil { + return nil, err + } + } // Verify that the device is an ADXL345. rx, _ := d.RawReadRegister(DeviceID) - if rx[1] != o.ExpectedDevid { - return nil, fmt.Errorf("wrong device connected should be an adxl345 should be\"%#x\" rx0=\"%#x\" rx1=\"%#x\"", o.ExpectedDevid, rx[0], rx[1]) + if rx[1] != o.ExpectedDeviceID { + return nil, fmt.Errorf("wrong device connected should be an adxl345 should be\"%#x\" rx0=\"%#x\" rx1=\"%#x\"", o.ExpectedDeviceID, rx[0], rx[1]) } return d, nil } +// SetSensitivity sets the sensitivity of the ADXL345. +// The sensitivity parameter should be one of 2, 4, 8, or 16, representing ±2g, ±4g, ±8g, or ±16g respectively. +func (d *Dev) setSensitivity(sensitivity Sensitivity) error { + switch sensitivity { + case S2G, S4G, S8G, S16G: + // Write to the DataFormat register + return d.WriteRegister(DataFormat, byte(sensitivity)) + default: + return fmt.Errorf("invalid sensitivity: %d. Valid values are 2, 4, 8, 16", sensitivity) + } +} + +func (d *Dev) Sensitivity() Sensitivity { + rx, _ := d.RawReadRegister(DataFormat) + return Sensitivity(rx[1]) +} + // TurnOn turns on the measurement mode of the ADXL345. // This is required before reading data from the device. func (d *Dev) TurnOn() error { diff --git a/adxl345/example/example.go b/adxl345/example/example.go index b87341a..fea5c47 100644 --- a/adxl345/example/example.go +++ b/adxl345/example/example.go @@ -32,6 +32,8 @@ func Example() { 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()