Skip to content

Commit

Permalink
Updated prototypes and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aeraterta committed Jan 20, 2025
1 parent e28a2c1 commit 959f62c
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 36 deletions.
162 changes: 147 additions & 15 deletions src/adxl345.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ int8_t adxl345_set_scale(adxl345_dev *device, adxl345_scale scale) {
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t adxl345_acc_set_resolution(adxl345_dev *device,
adxl345_resolution resolution) {
int8_t adxl345_set_resolution(adxl345_dev *device,
adxl345_resolution resolution) {
uint8_t val = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_DATA_FORMAT, &val) !=
Expand Down Expand Up @@ -365,6 +365,25 @@ int8_t adxl345_set_tap_window(adxl345_dev *device, int8_t window) {
return i2c_write_bytes(ADXL345_I2C_ADDRESS, data_buffer);
}

// ADD DOXYGEN
int8_t adxl345_tap_axes_enable(adxl345_dev *device,
adxl345_axes_enable tap_en) {
uint8_t val = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_TAP_AXES, &val) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

val &= ~0x07;
val = val | tap_en << ADXL345_TAP_EN_MASK;

device->tap_config.tap_en = tap_en;

uint8_t data_buffer[] = {ADXL345_REG_TAP_AXES, val};
return i2c_write_bytes(ADXL345_I2C_ADDRESS, data_buffer);
}

// ADD DOXYGEN
int8_t adxl345_set_activity_threshold(adxl345_dev *device, int8_t threshold) {
device->activity_config.activity_threshold = threshold;
Expand All @@ -382,8 +401,8 @@ int8_t adxl345_set_inactivity_threshold(adxl345_dev *device, int8_t threshold) {
}

// ADD DOXYGEN
int8_t adxl345_enable_axes_activity(adxl345_dev *device,
adxl345_axes_enable activity_en) {
int8_t adxl345_set_activity_axes_enable(adxl345_dev *device,
adxl345_axes_enable activity_en) {
uint8_t val = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_ACT_INACT_CTL, &val) !=
Expand All @@ -401,8 +420,8 @@ int8_t adxl345_enable_axes_activity(adxl345_dev *device,
}

// ADD DOXYGEN
int8_t adxl345_enable_axes_inactivity(adxl345_dev *device,
adxl345_axes_enable inactivity_en) {
int8_t adxl345_set_inactivity_axes_enable(adxl345_dev *device,
adxl345_axes_enable inactivity_en) {
uint8_t val = 0x00;

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_ACT_INACT_CTL, &val) !=
Expand Down Expand Up @@ -436,29 +455,142 @@ int8_t adxl345_set_freefall_timeout(adxl345_dev *device, int8_t timeout) {
}

// ADD DOXYGEN
int8_t adxl345_tap_axes_enable(adxl345_dev *device,
adxl345_axes_enable tap_en) {
int8_t adxl345_get_activity_tap_status(adxl345_dev *device) {
uint8_t val = 0x00;
if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_ACT_TAP_STATUS, &val) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_TAP_AXES, &val) !=
device->activity_config.activity_status.x = (bool)(val & (1 << 6));
device->activity_config.activity_status.y = (bool)(val & (1 << 5));
device->activity_config.activity_status.z = (bool)(val & (1 << 4));

device->tap_config.tap_status.x = (bool)(val & (1 << 2));
device->tap_config.tap_status.y = (bool)(val & (1 << 1));
device->tap_config.tap_status.z = (bool)(val & 1);

return ADXL345_STATUS_SUCCESS;
}

// ADD DOXYGEN
int8_t adxl345_set_interrupt_status(adxl345_dev *device, uint8_t interrupt,
int enable) {
uint8_t val = 0x00;
if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_INT_ENABLE, &val) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

val &= ~0x07;
val = val | tap_en << ADXL345_TAP_EN_MASK;
val = val | enable << interrupt;

device->tap_config.tap_en = tap_en;
uint8_t data_buffer[] = {ADXL345_REG_INT_ENABLE, val};
return i2c_write_bytes(ADXL345_I2C_ADDRESS, data_buffer);
}

uint8_t data_buffer[] = {ADXL345_REG_TAP_AXES, val};
// ADD DOXYGEN
int8_t adxl345_set_interrupt_map(adxl345_dev *device, uint8_t interrupt,
int map) {
uint8_t val = 0x00;
if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_INT_MAP, &val) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

val = val | map << interrupt;

uint8_t data_buffer[] = {ADXL345_REG_INT_MAP, val};
return i2c_write_bytes(ADXL345_I2C_ADDRESS, data_buffer);
}

int8_t adxl345_get_trigger_source(adxl345_dev *device) {
// ADD DOXYGEN
int8_t adxl345_get_interrupt_status(adxl345_dev *device) {
uint8_t val = 0x00;
if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_ACT_TAP_STATUS, &val) !=
if (i2c_read_byte(ADXL345_I2C_ADDRESS, ADXL345_REG_INT_SOURCE, &val) !=
ADXL345_STATUS_SUCCESS) {
return ADXL345_STATUS_API_ERR;
}

device->interrupt_status.data_ready = (bool)(val & (1 << 7));
device->interrupt_status.single_tap = (bool)(val & (1 << 6));
device->interrupt_status.double_tap = (bool)(val & (1 << 5));
device->interrupt_status.activity = (bool)(val & (1 << 4));
device->interrupt_status.inactivity = (bool)(val & (1 << 3));
device->interrupt_status.free_fall = (bool)(val & (1 << 2));
device->interrupt_status.watermark = (bool)(val & (1 << 1));
device->interrupt_status.overrun = (bool)(val & 1);

return ADXL345_STATUS_SUCCESS;
}

int8_t adxl345_get_axes_data_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;
}

if (device->resolution == ADXL345_RES_10BIT){
data->x = ((val_h << 8) | val_l) >> 6;
}
else {
data->x = (val_h << 8) | val_l;
}

return ADXL345_STATUS_SUCCESS;
}

int8_t adxl345_get_axes_data_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;
}

if (device->resolution == ADXL345_RES_10BIT){
data->y = ((val_h << 8) | val_l) >> 6;
}
else {
data->y = (val_h << 8) | val_l;
}

return ADXL345_STATUS_SUCCESS;
}

int8_t adxl345_get_axes_data_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;
}

if (device->resolution == ADXL345_RES_10BIT){
data->z = ((val_h << 8) | val_l) >> 6;
}
else {
data->z = (val_h << 8) | val_l;
}

return ADXL345_STATUS_SUCCESS;
}
87 changes: 68 additions & 19 deletions src/adxl345.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

#include "utils/i2c.h"

typedef struct {
int16_t x;
int16_t y;
int16_t z;
} adxl345_axes_data;

/*******************************STATUSES***************************************/
typedef enum {
ADXL345_STATUS_SUCCESS = 0,
Expand All @@ -51,6 +57,18 @@ typedef enum {
#define ADXL345_INACTIVITY_EN_MASK 0x00
#define ADXL345_TAP_EN_MASK 0x00

#define ADXL345_INT_DATA_READY 0x07
#define ADXL345_INT_SINGLE_TAP 0x06
#define ADXL345_INT_DOUBLE_TAP 0x05
#define ADXL345_INT_ACTIVITY 0x04
#define ADXL345_INT_INACTIVITY 0x03
#define ADXL345_INT_FREE_FALL 0x02
#define ADXL345_INT_WATERMARK 0x01
#define ADXL345_INT_OVERRUNY 0x00

#define ADXL345_INT1_PIN 0x00
#define ADXL345_INT2_PIN 0x01

/*********************************REGISTERS************************************/

#define ADXL345_REG_DEV_ID 0x00
Expand Down Expand Up @@ -128,14 +146,14 @@ typedef enum {
} adxl345_resolution;

typedef enum {
ACC_AXES_DISABLE_ALL = 0x00,
ACC_AXES_ENABLE_X = 0x01,
ACC_AXES_ENABLE_Y = 0x02,
ACC_AXES_ENABLE_Z = 0x04,
ACC_AXES_ENABLE_XY = 0x03,
ACC_AXES_ENABLE_XZ = 0x05,
ACC_AXES_ENABLE_YZ = 0x06,
ACC_AXES_ENABLE_XYZ = 0x07
AXES_DISABLE_ALL = 0x00,
AXES_ENABLE_X = 0x01,
AXES_ENABLE_Y = 0x02,
AXES_ENABLE_Z = 0x04,
AXES_ENABLE_XY = 0x03,
AXES_ENABLE_XZ = 0x05,
AXES_ENABLE_YZ = 0x06,
AXES_ENABLE_XYZ = 0x07
} adxl345_axes_enable;

typedef struct {
Expand All @@ -144,6 +162,17 @@ typedef struct {
bool z;
} axes_status;

typedef struct {
bool data_ready;
bool single_tap;
bool double_tap;
bool activity;
bool inactivity;
bool free_fall;
bool watermark;
bool overrun;
} interrupt_status;

typedef struct {
int8_t x;
int8_t y;
Expand All @@ -157,6 +186,7 @@ typedef struct {
uint8_t window;
adxl345_axes_enable tap_en;
axes_status axes_status;
axes_status tap_status;
} tap_config;

typedef struct {
Expand All @@ -165,6 +195,7 @@ typedef struct {
adxl345_axes_enable activity_en;
adxl345_axes_enable inactivity_en;
axes_status axes_status;
axes_status activity_status;
} activity_config;

typedef struct {
Expand All @@ -180,6 +211,7 @@ typedef struct {
adxl345_measure_mode measure;
adxl345_scale scale;
adxl345_odr odr;
interrupt_status interrupt_status;

activity_config activity_config;
freefall_config freefall_config;
Expand All @@ -195,6 +227,7 @@ typedef struct {
adxl345_measure_mode measure;
adxl345_scale scale;
adxl345_odr odr;
interrupt_status interrupt_status;

activity_config activity_config;
freefall_config freefall_config;
Expand All @@ -216,15 +249,15 @@ bool adxl345_online(void);

int8_t adxl345_set_power_mode(adxl345_dev *device, adxl345_power_mode mode);

int8_t adxl345_set_measure_mode(adxl345_dev *device,
adxl345_measure_mode measure);

int8_t adxl345_set_odr(adxl345_dev *device, adxl345_odr odr);

int8_t adxl345_set_scale(adxl345_dev *device, adxl345_scale scale);

int8_t adxl345_acc_set_resolution(adxl345_dev *device,
adxl345_resolution resolution);

int8_t adxl345_set_measure_mode(adxl345_dev *device,
adxl345_measure_mode measure);
int8_t adxl345_set_resolution(adxl345_dev *device,
adxl345_resolution resolution);

int8_t adxl345_set_offset_x(adxl345_dev *device, int8_t offset);

Expand All @@ -240,17 +273,33 @@ int8_t adxl345_set_tap_latency(adxl345_dev *device, int8_t latency);

int8_t adxl345_set_tap_window(adxl345_dev *device, int8_t window);

int8_t adxl345_tap_axes_enable(adxl345_dev *device, adxl345_axes_enable tap_en);

int8_t adxl345_set_activity_threshold(adxl345_dev *device, int8_t threshold);

int8_t adxl345_enable_axes_activity(adxl345_dev *device,
adxl345_axes_enable activity_en);
int8_t adxl345_set_inactivity_threshold(adxl345_dev *device, int8_t threshold);

int8_t adxl345_enable_axes_inactivity(adxl345_dev *device,
adxl345_axes_enable inactivity_en);
int8_t adxl345_set_activity_axes_enable(adxl345_dev *device,
adxl345_axes_enable activity_en);

int8_t adxl345_set_inactivity_axes_enable(adxl345_dev *device,
adxl345_axes_enable inactivity_en);

int8_t adxl345_set_freefall_threshold(adxl345_dev *device, int8_t threshold);

int8_t adxl345_tap_axes_enable(adxl345_dev *device, adxl345_axes_enable tap_en);
int8_t adxl345_set_freefall_timeout(adxl345_dev *device, int8_t timeout);

int8_t adxl345_get_activity_tap_status(adxl345_dev *device);

int8_t adxl345_set_interrupt_status(adxl345_dev *device, uint8_t interrupt,
int enable);

int8_t adxl345_set_interrupt_map(adxl345_dev *device, uint8_t interrupt,
int map);

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

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

int8_t adxl345_get_trigger_source(adxl345_dev *device);
int8_t adxl345_get_axes_data_z(adxl345_dev *device, adxl345_axes_data *data);
#endif
4 changes: 2 additions & 2 deletions test/test_adxl345.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void test_adxl345_set_scale(void) {
TEST_ASSERT_EQUAL(dev.scale, ADXL345_SCALE_8G);
}

void test_adxl345_acc_set_resolution(void) {
void test_adxl345_set_resolution(void) {
uint8_t read_data_result = 0x00;
i2c_read_byte_ExpectAndReturn(ADXL345_I2C_ADDRESS, ADXL345_REG_DATA_FORMAT,
NULL, ADXL345_STATUS_SUCCESS);
Expand All @@ -110,7 +110,7 @@ void test_adxl345_acc_set_resolution(void) {
i2c_write_bytes_IgnoreArg_data_buffer();

TEST_ASSERT_EQUAL(ADXL345_STATUS_SUCCESS,
adxl345_acc_set_resolution(&dev, ADXL345_RES_16BIT));
adxl345_set_resolution(&dev, ADXL345_RES_16BIT));
TEST_ASSERT_EQUAL(dev.resolution, ADXL345_RES_16BIT);
}

Expand Down

0 comments on commit 959f62c

Please sign in to comment.