Added sensitivity option.

pull/66/head
bpds 2 years ago
parent a0f580fcea
commit d3ad1aac1e
No known key found for this signature in database
GPG Key ID: B666D5C1144EADD6

@ -7,10 +7,15 @@ import (
"periph.io/x/conn/v3/spi" "periph.io/x/conn/v3/spi"
) )
type Sensitivity byte
const ( const (
DeviceID = 0x00 // Device ID, expected to be 0xE5 when using ADXL345 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 ThreshTap = 0x1D // Tap threshold
OfsX = 0x1E // X-axis offset OfsX = 0x1E // X-axis offset
@ -60,13 +65,15 @@ var (
) )
var DefaultOpts = Opts{ var DefaultOpts = Opts{
TurnOnOnStart: true, TurnOnOnStart: true,
ExpectedDevid: 0xE5, ExpectedDeviceID: 0xE5,
Sensitivity: S2G,
} }
type Opts struct { type Opts struct {
TurnOnOnStart bool // Turn on the device in measurement mode on start. 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. 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 // Dev is a driver for the ADXL345 accelerometer
@ -77,7 +84,7 @@ type Dev struct {
} }
func (d *Dev) String() string { 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. // 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 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. // Verify that the device is an ADXL345.
rx, _ := d.RawReadRegister(DeviceID) rx, _ := d.RawReadRegister(DeviceID)
if rx[1] != o.ExpectedDevid { if rx[1] != o.ExpectedDeviceID {
return nil, fmt.Errorf("wrong device connected should be an adxl345 should be\"%#x\" rx0=\"%#x\" rx1=\"%#x\"", o.ExpectedDevid, rx[0], rx[1]) 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 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. // TurnOn turns on the measurement mode of the ADXL345.
// This is required before reading data from the device. // This is required before reading data from the device.
func (d *Dev) TurnOn() error { func (d *Dev) TurnOn() error {

@ -32,6 +32,8 @@ func Example() {
panic(err) panic(err)
} }
fmt.Println(d.String())
// use a ticker to read the acceleration values every 200ms // use a ticker to read the acceleration values every 200ms
ticker := time.NewTicker(30 * time.Millisecond) ticker := time.NewTicker(30 * time.Millisecond)
defer ticker.Stop() defer ticker.Stop()

Loading…
Cancel
Save