mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-18 13:43:58 +00:00
Use int16 for ADXL345 readings (#656)
ADXL345: fix: use int16 for ADXL345 readings
This commit is contained in:
+7
-7
@@ -95,16 +95,16 @@ func (d *Device) Restart() {
|
|||||||
func (d *Device) ReadAcceleration() (x int32, y int32, z int32, err error) {
|
func (d *Device) ReadAcceleration() (x int32, y int32, z int32, err error) {
|
||||||
rx, ry, rz := d.ReadRawAcceleration()
|
rx, ry, rz := d.ReadRawAcceleration()
|
||||||
|
|
||||||
x = d.dataFormat.convertToIS(rx)
|
x = int32(d.dataFormat.convertToIS(rx))
|
||||||
y = d.dataFormat.convertToIS(ry)
|
y = int32(d.dataFormat.convertToIS(ry))
|
||||||
z = d.dataFormat.convertToIS(rz)
|
z = int32(d.dataFormat.convertToIS(rz))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadRawAcceleration reads the sensor values and returns the raw x, y and z axis
|
// ReadRawAcceleration reads the sensor values and returns the raw x, y and z axis
|
||||||
// from the adxl345.
|
// from the adxl345.
|
||||||
func (d *Device) ReadRawAcceleration() (x int32, y int32, z int32) {
|
func (d *Device) ReadRawAcceleration() (x int16, y int16, z int16) {
|
||||||
data := []byte{0, 0, 0, 0, 0, 0}
|
data := []byte{0, 0, 0, 0, 0, 0}
|
||||||
legacy.ReadRegister(d.bus, uint8(d.Address), REG_DATAX0, data)
|
legacy.ReadRegister(d.bus, uint8(d.Address), REG_DATAX0, data)
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ func (d *Device) SetRange(sensorRange Range) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// convertToIS adjusts the raw values from the adxl345 with the range configuration
|
// convertToIS adjusts the raw values from the adxl345 with the range configuration
|
||||||
func (d *dataFormat) convertToIS(rawValue int32) int32 {
|
func (d *dataFormat) convertToIS(rawValue int16) int16 {
|
||||||
switch d.sensorRange {
|
switch d.sensorRange {
|
||||||
case RANGE_2G:
|
case RANGE_2G:
|
||||||
return rawValue * 4 // rawValue * 2 * 1000 / 512
|
return rawValue * 4 // rawValue * 2 * 1000 / 512
|
||||||
@@ -190,6 +190,6 @@ func (b *bwRate) toByte() (bits uint8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// readInt converts two bytes to int16
|
// readInt converts two bytes to int16
|
||||||
func readIntLE(msb byte, lsb byte) int32 {
|
func readIntLE(msb byte, lsb byte) int16 {
|
||||||
return int32(uint16(msb) | uint16(lsb)<<8)
|
return int16(uint16(msb) | uint16(lsb)<<8)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user