Skip to content

Commit

Permalink
Removed individual functions to get x, y, and z axes data. Added unit…
Browse files Browse the repository at this point in the history
…-tests
  • Loading branch information
aeraterta committed Jan 24, 2025
1 parent 2b36985 commit 611a1a3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 116 deletions.
110 changes: 5 additions & 105 deletions src/adxl345.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,108 +778,6 @@ int8_t adxl345_get_interrupt_status(adxl345_dev *device) {
return ADXL345_STATUS_SUCCESS;
}

/**
* @brief Get the raw X-axis acceleration data from the ADXL345 accelerometer.
*
* Reads the X-axis acceleration data registers and updates the raw data in the
* provided `adxl345_axes_data` structure. The data is right-shifted according
* to the device's resolution setting.
*
* @param device Pointer to the ADXL345 device structure.
* @param data Pointer to the structure where the raw X-axis data will be
* stored.
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t adxl345_get_raw_x(adxl345_dev *device, adxl345_axes_data *data) {
uint8_t val_l = 0x00;
uint8_t val_h = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAX1, &val_h) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAX0, &val_l) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

data->raw_data.x = ((val_h << 8) | val_l) >> device->resolution.mask;

return ADXL345_STATUS_SUCCESS;
}

/**
* @brief Get the raw Y-axis acceleration data from the ADXL345 accelerometer.
*
* Reads the Y-axis acceleration data registers and updates the raw data in the
* provided `adxl345_axes_data` structure. The data is right-shifted according
* to the device's resolution setting.
*
* @param device Pointer to the ADXL345 device structure.
* @param data Pointer to the structure where the raw Y-axis data will be
* stored.
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t adxl345_get_raw_y(adxl345_dev *device, adxl345_axes_data *data) {
uint8_t val_l = 0x00;
uint8_t val_h = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAY1, &val_h) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAY0, &val_l) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

data->raw_data.y = ((val_h << 8) | val_l) >> device->resolution.mask;

return ADXL345_STATUS_SUCCESS;
}

/**
* @brief Get the raw Z-axis acceleration data from the ADXL345 accelerometer.
*
* Reads the Z-axis acceleration data registers and updates the raw data in the
* provided `adxl345_axes_data` structure. The data is right-shifted according
* to the device's resolution setting.
*
* @param device Pointer to the ADXL345 device structure.
* @param data Pointer to the structure where the raw Z-axis data will be
* stored.
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t adxl345_get_raw_z(adxl345_dev *device, adxl345_axes_data *data) {
uint8_t val_l = 0x00;
uint8_t val_h = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAZ1, &val_h) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAZ0, &val_l) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

data->raw_data.z = ((val_h << 8) | val_l) >> device->resolution.mask;

return ADXL345_STATUS_SUCCESS;
}

/**
* @brief Get the raw X, Y, and Z-axis acceleration data from the ADXL345
* accelerometer.
Expand Down Expand Up @@ -955,11 +853,13 @@ int8_t adxl345_get_raw_xyz(adxl345_dev *device, adxl345_axes_data *data) {
*/
void adxl345_get_acc_xyz(adxl345_dev *device, adxl345_axes_data *data) {

data->acc_data.x = (float)(data->raw_data.x * device->scale.fs) /
float full_scale_range = device->scale.fs / 1.0f;

data->acc_data.x = (float)(data->raw_data.x * full_scale_range) /
(float)(1 << device->resolution.bits);
data->acc_data.y = (float)(data->raw_data.y * device->scale.fs) /
data->acc_data.y = (float)(data->raw_data.y * full_scale_range) /
(float)(1 << device->resolution.bits);
data->acc_data.z = (float)(data->raw_data.z * device->scale.fs) /
data->acc_data.z = (float)(data->raw_data.z * full_scale_range) /
(float)(1 << device->resolution.bits);
}

Expand Down
6 changes: 0 additions & 6 deletions src/adxl345.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,6 @@ int8_t adxl345_set_interrupt_map(adxl345_dev *device, uint8_t interrupt,

int8_t adxl345_get_interrupt_status(adxl345_dev *device);

int8_t adxl345_get_raw_x(adxl345_dev *device, adxl345_axes_data *data);

int8_t adxl345_get_raw_y(adxl345_dev *device, adxl345_axes_data *data);

int8_t adxl345_get_raw_z(adxl345_dev *device, adxl345_axes_data *data);

int8_t adxl345_get_raw_xyz(adxl345_dev *device, adxl345_axes_data *data);

void adxl345_get_acc_xyz(adxl345_dev *device, adxl345_axes_data *data);
Expand Down
64 changes: 59 additions & 5 deletions test/test_adxl345.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

static adxl345_dev dev;
static adxl345_init_param init_param;
adxl345_axes_data adxl345_data;

void test_adxl345_setup(void) {}

Expand Down Expand Up @@ -82,7 +83,7 @@ void test_adxl345_set_odr_invalid_odr(void) {
void test_adxl345_set_scale(void) {
uint8_t read_data_result = 0x00;
adxl345_scale_config scale;
scale.scale = ADXL345_SCALE_8G;
scale.scale = ADXL345_SCALE_2G;

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATA_FORMAT,
NULL, ADXL345_STATUS_SUCCESS);
Expand All @@ -94,8 +95,8 @@ void test_adxl345_set_scale(void) {
i2c_write_byte_IgnoreArg_data_buffer();

TEST_ASSERT_EQUAL(ADXL345_STATUS_SUCCESS, adxl345_set_scale(&dev, scale));
TEST_ASSERT_EQUAL(dev.scale.scale, ADXL345_SCALE_8G);
TEST_ASSERT_EQUAL(dev.scale.fs, ADXL345_FULL_SCALE_8G);
TEST_ASSERT_EQUAL(dev.scale.scale, ADXL345_SCALE_2G);
TEST_ASSERT_EQUAL(dev.scale.fs, ADXL345_FULL_SCALE_2G);
}

void test_adxl345_set_resolution(void) {
Expand All @@ -115,8 +116,8 @@ void test_adxl345_set_resolution(void) {
TEST_ASSERT_EQUAL(ADXL345_STATUS_SUCCESS,
adxl345_set_resolution(&dev, resolution));
TEST_ASSERT_EQUAL(dev.resolution.resolution, ADXL345_RES_FULL);
TEST_ASSERT_EQUAL(dev.resolution.bits, 12);
TEST_ASSERT_EQUAL(dev.resolution.mask, 4);
TEST_ASSERT_EQUAL(dev.resolution.bits, 10);
TEST_ASSERT_EQUAL(dev.resolution.mask, 6);
}

void test_adxl345_set_tap_threshold(void) {
Expand Down Expand Up @@ -311,4 +312,57 @@ void test_adxl345_get_interrupt_status(void) {
TEST_ASSERT_FALSE(dev.interrupt_status.free_fall);
TEST_ASSERT_TRUE(dev.interrupt_status.watermark);
TEST_ASSERT_TRUE(dev.interrupt_status.overrun);
}

void test_adxl345_get_raw_xyz(void) {
uint8_t val[6];
val[0] = 0x40; // X LSB
val[1] = 0xFC; // X MSB
val[2] = 0xFD; // Y LSB
val[3] = 0xFD; // Y MSB
val[4] = 0x80; // Z LSB
val[5] = 0x3A; // Z MSB

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAX1, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[1]);

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAX0, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[0]);

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAY1, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[3]);

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAY0, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[2]);

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAZ1, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[5]);

i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATAZ0, NULL,
ADXL345_STATUS_SUCCESS);
i2c_read_byte_IgnoreArg_read_data();
i2c_read_byte_ReturnThruPtr_read_data(&val[4]);

TEST_ASSERT_EQUAL(ADXL345_STATUS_SUCCESS,
adxl345_get_raw_xyz(&dev, &adxl345_data));
TEST_ASSERT_EQUAL(adxl345_data.raw_data.x, -15);
TEST_ASSERT_EQUAL(adxl345_data.raw_data.y, -9);
TEST_ASSERT_EQUAL(adxl345_data.raw_data.z, 234);
}

void test_adxl345_get_acc_xyz(void) {
adxl345_get_acc_xyz(&dev, &adxl345_data);
TEST_ASSERT_FLOAT_WITHIN(0.01, -0.06, adxl345_data.acc_data.x);
TEST_ASSERT_FLOAT_WITHIN(0.01, -0.04, adxl345_data.acc_data.y);
TEST_ASSERT_FLOAT_WITHIN(0.01, 0.91, adxl345_data.acc_data.z);
}

0 comments on commit 611a1a3

Please sign in to comment.