Reduce code size for accelerometer support (#288)

* Reduce the LIS2DH12 driver's code size.
* Reduce the MMA8652FC driver's code size.
* Make orientation detection smaller.


* Inlined C++ class constructor.
* De-unroll I2C register writes.
* Removed unused setSensitivity method.
This commit is contained in:
Alessandro Gatti
2018-05-10 02:02:29 +02:00
committed by Ben V. Brown
parent 4718efe79b
commit 215fe8e9e8
6 changed files with 76 additions and 75 deletions

View File

@@ -7,39 +7,32 @@
#include <LIS2DH12.hpp>
#include "cmsis_os.h"
LIS2DH12::LIS2DH12(FRToSI2C* i2cHandle) {
i2c = i2cHandle;
}
typedef struct {
const uint8_t reg;
const uint8_t value;
} LIS_REG;
static const LIS_REG i2c_registers[] = {
{LIS_CTRL_REG1, 0x17}, // 25Hz
{LIS_CTRL_REG2, 0b00001000}, // Highpass filter off
{LIS_CTRL_REG3, 0b01100000}, // Setup interrupt pins
{LIS_CTRL_REG4, 0b00001000}, // Block update mode off, HR on
{LIS_CTRL_REG5, 0b00000010},
{LIS_CTRL_REG6, 0b01100010},
//Basically setup the unit to run, and enable 4D orientation detection
{LIS_INT2_CFG, 0b01111110}, //setup for movement detection
{LIS_INT2_THS, 0x28},
{LIS_INT2_DURATION, 64},
{LIS_INT1_CFG, 0b01111110},
{LIS_INT1_THS, 0x28},
{LIS_INT1_DURATION, 64}
};
void LIS2DH12::initalize() {
I2C_RegisterWrite(LIS_CTRL_REG1, 0x17); //25Hz
I2C_RegisterWrite(LIS_CTRL_REG2, 0b00001000); //Highpass filter off
I2C_RegisterWrite(LIS_CTRL_REG3, 0b01100000); //Setup interrupt pins
I2C_RegisterWrite(LIS_CTRL_REG4, 0b00001000); //Block update mode off,HR on
I2C_RegisterWrite(LIS_CTRL_REG5, 0b00000010);
I2C_RegisterWrite(LIS_CTRL_REG6, 0b01100010);
//Basically setup the unit to run, and enable 4D orientation detection
I2C_RegisterWrite(LIS_INT2_CFG, 0b01111110); //setup for movement detection
I2C_RegisterWrite(LIS_INT2_THS, 0x28);
I2C_RegisterWrite(LIS_INT2_DURATION, 64);
I2C_RegisterWrite(LIS_INT1_CFG, 0b01111110); //setup for movement detection
I2C_RegisterWrite(LIS_INT1_THS, 0x28);
I2C_RegisterWrite(LIS_INT1_DURATION, 64);
}
//0=no change, 1= right handed, 2= left handed
uint8_t LIS2DH12::getOrientation() {
// 8=right handed,4=left,16=flat
//So we ignore if not 8/4
uint8_t pos = I2C_RegisterRead(LIS_INT2_SRC);
if (pos == 8)
return 1;
else if (pos == 4)
return 2;
else
return 0;
for (size_t index = 0; index < (sizeof(i2c_registers) / sizeof(i2c_registers[0])); index++) {
I2C_RegisterWrite(i2c_registers[index].reg, i2c_registers[index].value);
}
}
void LIS2DH12::getAxisReadings(int16_t* x, int16_t* y, int16_t* z) {
@@ -52,9 +45,6 @@ void LIS2DH12::getAxisReadings(int16_t* x, int16_t* y, int16_t* z) {
(*z) = ((uint16_t) (tempArr[5] << 8 | tempArr[4]));
}
void LIS2DH12::setSensitivity(uint8_t threshold, uint8_t filterTime) {
}
void LIS2DH12::I2C_RegisterWrite(uint8_t reg, uint8_t data) {
i2c->Mem_Write(LIS2DH_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &data, 1);