Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions 32blit-stm32/Src/32blit/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ static uint16_t accel_address = LIS3DH_DEVICE_ADDRESS;
//
// Local variables for this file
//
static RunningAverage<float> accel_x(8);
static RunningAverage<float> accel_y(8);
static RunningAverage<float> accel_z(8);
static RunningAverage<int16_t> accel_x(8);
static RunningAverage<int16_t> accel_y(8);
static RunningAverage<int16_t> accel_z(8);

static I2CState i2c_state = SEND_ACL;
static uint8_t i2c_buffer[6] = {0};
Expand Down Expand Up @@ -119,24 +119,18 @@ namespace i2c {
i2c_state = PROC_ACL;
break;
case PROC_ACL:
// LIS3DH & MSA301 - 12-bit left-justified
accel_x.add(((int8_t)i2c_buffer[1] << 6) | (i2c_buffer[0] >> 2));
accel_y.add(((int8_t)i2c_buffer[3] << 6) | (i2c_buffer[2] >> 2));
accel_z.add(((int8_t)i2c_buffer[5] << 6) | (i2c_buffer[4] >> 2));

if(is_beta_unit){
blit::tilt = Vec3(
accel_x.average(),
accel_y.average(),
-accel_z.average()
);
} else {
blit::tilt = Vec3(
-accel_x.average(),
-accel_y.average(),
-accel_z.average()
);
}
// LIS3DH - 12-bit left-justified
// MSA301 - 14-bit left-justified
// shift it down later so the average drops less bits
accel_x.add(((int8_t)i2c_buffer[1] << 8) | (i2c_buffer[0]));
accel_y.add(((int8_t)i2c_buffer[3] << 8) | (i2c_buffer[2]));
accel_z.add(((int8_t)i2c_buffer[5] << 8) | (i2c_buffer[4]));

blit::tilt = Vec3(
-accel_x.average() >> 2,
-accel_y.average() >> 2,
-accel_z.average() >> 2
);

blit::tilt.normalize();

Expand Down
7 changes: 3 additions & 4 deletions 32blit-stm32/Src/i2c-msa301.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

static void _i2c_send_8(I2C_HandleTypeDef *i2c_port, uint16_t address, uint8_t reg, uint8_t data);

//functions
//functions
void msa301_init(I2C_HandleTypeDef *i2c_port, uint8_t power_mode, uint8_t low_power_bandwidth, uint8_t update_rate){
_i2c_send_8(i2c_port, MSA301_DEVICE_ADDRESS, MSA301_CONTROL1_REGISTER, update_rate);
_i2c_send_8(i2c_port, MSA301_DEVICE_ADDRESS, MSA301_CONTROL2_REGISTER, (power_mode | low_power_bandwidth));
_i2c_send_8(i2c_port, MSA301_DEVICE_ADDRESS, MSA301_SWAP_REGISTER, 0b00001111); // Reverse X, Y and Z axis polarity (and X/Y swap also??)
_i2c_send_8(i2c_port, MSA301_DEVICE_ADDRESS, MSA301_SWAP_REGISTER, 0b00000011); // Reverse Z axis polarity and X/Y swap
}

static void _i2c_send_8(I2C_HandleTypeDef *i2c_port, uint16_t address, uint8_t reg, uint8_t data){
Expand All @@ -17,6 +17,5 @@ static void _i2c_send_8(I2C_HandleTypeDef *i2c_port, uint16_t address, uint8_t r
data_buffer[1] = data;
HAL_I2C_Master_Transmit(i2c_port, address, &data_buffer[0] , 2 ,HAL_TIMEOUT);
}

/*****************************END OF FILE****/

6 changes: 4 additions & 2 deletions 32blit/engine/running_average.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class RunningAverage
{
public:
using Data = std::vector<T>;
RunningAverage(std::size_t uSize) : m_uSize(uSize), m_uIndex(0), m_average(0), m_bFull(false) {};
RunningAverage(std::size_t uSize) : m_uSize(uSize), m_uIndex(0), m_average(0), m_bFull(false) {
m_data.reserve(uSize);
};

const T &operator[] (std::size_t i) const
{
Expand Down Expand Up @@ -91,7 +93,7 @@ class RunningAverage

private:
Data m_data;
std::size_t m_uSize;
const std::size_t m_uSize;
std::size_t m_uIndex;
T m_average;
bool m_bFull;
Expand Down