1
0
forked from me/IronOS

Move Accel probe into class

This commit is contained in:
Ben V. Brown
2020-05-29 20:46:19 +10:00
parent 3b0fc9c6f8
commit ef5ba8b650
5 changed files with 232 additions and 203 deletions

View File

@@ -12,12 +12,16 @@
#include "LIS2DH12_defines.hpp"
#include "hardware.h"
#ifdef MODEL_TS80
#define LIS_ORI_FLIP
#endif
class LIS2DH12 {
public:
static bool detect();
static void initalize();
//1 = rh, 2,=lh, 8=flat
static Orientation getOrientation() {
#ifdef MODEL_TS80
#ifdef LIS_ORI_FLIP
uint8_t val = (FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS,
LIS_INT2_SRC) >> 2);
if (val == 8)

View File

@@ -15,11 +15,12 @@
class MMA8652FC {
public:
static void initalize(); // Initalize the system
static Orientation getOrientation();// Reads the I2C register and returns the orientation (true == left)
static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z);
//Returns true if this accelerometer is detected
static bool detect();
//Init any internal state
static void initalize();
static Orientation getOrientation(); // Reads the I2C register and returns the orientation (true == left)
static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z);
private:
};

View File

@@ -15,29 +15,28 @@ typedef struct {
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},
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}
};
{ 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() {
for (size_t index = 0; index < (sizeof(i2c_registers) / sizeof(i2c_registers[0])); index++) {
FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].value);
for (size_t index = 0;
index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]));
index++) {
FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,
i2c_registers[index].reg, i2c_registers[index].value);
}
}
void LIS2DH12::getAxisReadings(int16_t& x, int16_t& y, int16_t& z) {
void LIS2DH12::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
std::array<int16_t, 3> sensorData;
FRToSI2C::Mem_Read(LIS2DH_I2C_ADDRESS, 0xA8, I2C_MEMADD_SIZE_8BIT,
@@ -48,3 +47,14 @@ void LIS2DH12::getAxisReadings(int16_t& x, int16_t& y, int16_t& z) {
y = sensorData[1];
z = sensorData[2];
}
bool LIS2DH12::detect() {
uint8_t buffer[1];
if (HAL_I2C_Mem_Read(&hi2c1, 25 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
//ACK'd
return true;
}
//NAK'd
return false;
}

View File

@@ -30,28 +30,31 @@ static const MMA_REG i2c_registers[] = { { CTRL_REG2, 0 }, //Normal mode
{ CTRL_REG1, 0x19 } // ODR=12 Hz, Active mode
};
void MMA8652FC::initalize() {
size_t index = 0;
//send all the init commands to the unit
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val);
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS, i2c_registers[index].reg,
i2c_registers[index].val);
index++;
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val);
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS, i2c_registers[index].reg,
i2c_registers[index].val);
index++;
HAL_Delay(2); // ~1ms delay
while (index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]))) {
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val);
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,
i2c_registers[index].reg, i2c_registers[index].val);
index++;
}
}
Orientation MMA8652FC::getOrientation() {
//First read the PL_STATUS register
uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,PL_STATUS_REG);
uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,
PL_STATUS_REG);
if ((plStatus & 0b10000000) == 0b10000000) {
plStatus >>= 1; //We don't need the up/down bit
plStatus &= 0x03; //mask to the two lower bits
@@ -65,14 +68,28 @@ Orientation MMA8652FC::getOrientation() {
return ORIENTATION_FLAT;
}
void MMA8652FC::getAxisReadings(int16_t& x, int16_t& y, int16_t& z) {
void MMA8652FC::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
std::array<int16_t, 3> sensorData;
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, I2C_MEMADD_SIZE_8BIT,
reinterpret_cast<uint8_t*>(sensorData.begin()),
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG,
I2C_MEMADD_SIZE_8BIT, reinterpret_cast<uint8_t*>(sensorData.begin()),
sensorData.size() * sizeof(int16_t));
x = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[0])));
y = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[1])));
z = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[2])));
x = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[0])));
y = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[1])));
z = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[2])));
}
bool MMA8652FC::detect() {
uint8_t buffer[1];
if (HAL_I2C_Mem_Read(&hi2c1, 29 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
//The device ACK'd
return true;
}
//NAK'd
return false;
}

View File

@@ -55,14 +55,11 @@ int main(void) {
OLED::initialize(); // start up the LCD
OLED::setFont(0); // default to bigger font
// Testing for which accelerometer is mounted
uint8_t buffer[1];
HAL_IWDG_Refresh(&hiwdg);
if (HAL_I2C_Mem_Read(&hi2c1, 29 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
resetWatchdog();
if (MMA8652FC::detect()) {
PCBVersion = 1;
MMA8652FC::initalize(); // this sets up the I2C registers
} else if (HAL_I2C_Mem_Read(&hi2c1, 25 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT,
buffer, 1, 1000) == HAL_OK) {
} else if (LIS2DH12::detect()) {
PCBVersion = 2;
// Setup the ST Accelerometer
LIS2DH12::initalize(); // startup the accelerometer