Skip to content

Commit

Permalink
Some precisions
Browse files Browse the repository at this point in the history
* Add datasheet in README.md
* Explain in commment the bitwise operation
* Replace ternary operator with if/else
* Replace "typedef enum" with "enum class"
* Replace the variable name "success" with "outputStatus" in main and
  "ret" for the rest
  • Loading branch information
YannLocatelli committed May 19, 2020
1 parent 1ec5825 commit 34c9ea8
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 179 deletions.
4 changes: 3 additions & 1 deletion LKExp-Sensors-LSM6DSOX_Examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Construction relies on examples provided by ST.
* [Driver](https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lsm6dsox_STdC/driver) -> commit `3fca83f875bbcbb6a428a7deb0ff01c0e5a2b033` (30 April 2020)
* [Examples](https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lsm6dsox_STdC/example)

## Abreviation
[LSM6DSOX datasheet](https://www.st.com/resource/en/datasheet/lsm6dsox.pdf)

## Abbreviation

* FS : Full Scale
* FSM : Finite State Machine
Expand Down
91 changes: 50 additions & 41 deletions LKExp-Sensors-LSM6DSOX_Examples/lib/accelerometer/accelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,44 @@ Accelerometer::Accelerometer(I2C *i2c, uint8_t address) : _i2c(i2c), _address(ad
* @param init pointer to device specific initalization structure
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXAccStatusTypeDef Accelerometer::init(void *init)
AccStatus Accelerometer::init(void *init)
{
/* Initialize the device for driver usage */
if (lsm6dsox_init_set(&_reg_ctx, LSM6DSOX_DRV_RDY) != LSM6DSOX_Acc_OK)
if (lsm6dsox_init_set(&_reg_ctx, LSM6DSOX_DRV_RDY) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

//Get current status
if (lsm6dsox_mode_get(&_reg_ctx, NULL, &_status) != LSM6DSOX_Acc_OK)
if (lsm6dsox_mode_get(&_reg_ctx, NULL, &_status) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

/* Replace output data rate selection and full scale selection. */
_status.ui.xl.odr = _status.ui.xl.LSM6DSOX_XL_UI_52Hz_LP;
_status.ui.xl.fs = _status.ui.xl.LSM6DSOX_XL_UI_4g;
if (lsm6dsox_mode_set(&_reg_ctx, NULL, &_status) != LSM6DSOX_Acc_OK)
if (lsm6dsox_mode_set(&_reg_ctx, NULL, &_status) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

return LSM6DSOX_Acc_OK;
return AccStatus::OK;
}

/**
* @brief Read component ID
* @param id the WHO_AM_I value
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXAccStatusTypeDef Accelerometer::read_id(uint8_t *id)
AccStatus Accelerometer::read_id(uint8_t *id)
{
if (lsm6dsox_device_id_get(&_reg_ctx, id) != LSM6DSOX_Acc_OK)
if (lsm6dsox_device_id_get(&_reg_ctx, id) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

return LSM6DSOX_Acc_OK;
return AccStatus::OK;
}

/**
Expand All @@ -77,15 +77,15 @@ LSM6DSOXAccStatusTypeDef Accelerometer::read_id(uint8_t *id)
* @param fullScale is full scal in hexadecimal value
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXAccStatusTypeDef Accelerometer::get_status(PowerModeAcc *powerMode, float *dataRate, uint16_t *fullScale)
AccStatus Accelerometer::get_status(AccPowerMode *powerMode, float *dataRate, uint16_t *fullScale)
{
LSM6DSOXAccStatusTypeDef success = LSM6DSOX_Acc_OK;
if (lsm6dsox_mode_get(&_reg_ctx, NULL, &_status) != LSM6DSOX_Acc_OK)
AccStatus ret = AccStatus::OK;
if (lsm6dsox_mode_get(&_reg_ctx, NULL, &_status) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

switch((_status.ui.xl.odr & 0x0F))
switch((_status.ui.xl.odr & 0x0F)) //Value of odr is 0xYZ with Y the power mode and Z an enum frequence
{
case 0:
*dataRate = 0;
Expand Down Expand Up @@ -124,21 +124,30 @@ LSM6DSOXAccStatusTypeDef Accelerometer::get_status(PowerModeAcc *powerMode, floa
*dataRate = 1.6f;
break;
default:
success = LSM6DSOX_Acc_ERROR;
ret = AccStatus::ERROR;
break;
}
if (success == LSM6DSOX_Acc_ERROR)
if (ret == AccStatus::ERROR)
{
return success;
return ret;
}


*powerMode = (*dataRate == 0) ? Power_Off_Acc
: ((_status.ui.xl.odr & 0x20) >> 5) ? Ultra_Low_Power_Acc
: (not(_status.ui.xl.odr & 0x10) >> 4) ? High_Performance_Acc
: (*dataRate > 100.0f) ? Normal_Power_Acc
: Low_Power_Acc;

//Value of odr is 0xYZ with Y the power mode and Z an enum frequence
if (*dataRate == 0) {

This comment has been minimized.

Copy link
@ladislas

ladislas May 19, 2020

Member

beaucoup plus clair! :)
mais de même que L88, il faut expliquer les & et >>.

Il faut même les encapsuler dans des utility function de la classe accelerometer qui cache le détail de ça à l'utilisateur.

*powerMode = AccPowerMode::OFF;
} else {
if ((_status.ui.xl.odr & 0x20) >> 5) {
*powerMode = AccPowerMode::ULTRA_LOW;
} else if ((_status.ui.gy.odr & 0x10) >> 4) {
if(*dataRate < 100.0f){
*powerMode = AccPowerMode::LOW;
} else {
*powerMode = AccPowerMode::NORMAL;
}
} else {
*powerMode = AccPowerMode::HIGH_PERFORMANCE;
}
}

switch((_status.ui.xl.fs))
{
Expand All @@ -155,27 +164,27 @@ LSM6DSOXAccStatusTypeDef Accelerometer::get_status(PowerModeAcc *powerMode, floa
*fullScale = 8;
break;
default:
success = LSM6DSOX_Acc_ERROR;
ret = AccStatus::ERROR;
break;
}

return success;
return ret;
}

/**
* @brief Read component interrupt status
* @param dataReady flag
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXAccStatusTypeDef Accelerometer::get_int_status(uint8_t *dataReady)
AccStatus Accelerometer::get_int_status(uint8_t *dataReady)
{
if (lsm6dsox_all_sources_get(&_reg_ctx, &_int_status) != LSM6DSOX_Acc_OK)
if (lsm6dsox_all_sources_get(&_reg_ctx, &_int_status) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}
*dataReady = _int_status.drdy_xl;

return LSM6DSOX_Acc_OK;
return AccStatus::OK;
}

/**
Expand All @@ -185,23 +194,23 @@ LSM6DSOXAccStatusTypeDef Accelerometer::get_int_status(uint8_t *dataReady)
* @param mg_Z data value of acceleration on Z axis in mg
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOXAccStatusTypeDef Accelerometer::get_data(float *mg_X, float *mg_Y, float *mg_Z)
AccStatus Accelerometer::get_data(float *mg_X, float *mg_Y, float *mg_Z)
{
//if (lsm6dsox_data_get(&_reg_ctx, NULL, &_status, &_data) != LSM6DSOX_Acc_OK) //Lot of mistakes in the driver's function
//if (lsm6dsox_data_get(&_reg_ctx, NULL, &_status, &_data) != AccStatus::OK) //Lot of mistakes in the driver's function
//{
// return LSM6DSOX_Acc_ERROR;
// return AccStatus::ERROR;
//}
//*mg_X = _data.ui.xl.mg[0];
//*mg_Y = _data.ui.xl.mg[1];
//*mg_Z = _data.ui.xl.mg[2];

LSM6DSOXAccStatusTypeDef success = LSM6DSOX_Acc_OK;
AccStatus ret = AccStatus::OK;
uint8_t data_raw[6];

/* Read raw data values. */
if (lsm6dsox_acceleration_raw_get(&_reg_ctx, data_raw) != LSM6DSOX_Acc_OK)
if (lsm6dsox_acceleration_raw_get(&_reg_ctx, data_raw) != (int32_t)AccStatus::OK)
{
return LSM6DSOX_Acc_ERROR;
return AccStatus::ERROR;
}

float_t (* pConversionFunction) (int16_t raw_value);
Expand All @@ -220,15 +229,15 @@ LSM6DSOXAccStatusTypeDef Accelerometer::get_data(float *mg_X, float *mg_Y, float
pConversionFunction = &lsm6dsox_from_fs8_to_mg;
break;
default:
success = LSM6DSOX_Acc_ERROR;
ret = AccStatus::ERROR;
break;
}

*mg_X = (float)(* pConversionFunction)((uint16_t)((uint16_t)data_raw[1] << 8) | data_raw[0]);
*mg_Y = (float)(* pConversionFunction)((uint16_t)((uint16_t)data_raw[3] << 8) | data_raw[2]);
*mg_Z = (float)(* pConversionFunction)((uint16_t)((uint16_t)data_raw[5] << 8) | data_raw[4]);

return success;
return ret;
}

int32_t LSM6DSOX_acc_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
Expand Down
59 changes: 27 additions & 32 deletions LKExp-Sensors-LSM6DSOX_Examples/lib/accelerometer/accelerometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,24 @@

/* Typedefs ------------------------------------------------------------------*/

typedef enum
{
LSM6DSOX_Acc_OK = 0,
LSM6DSOX_Acc_ERROR = -1
} LSM6DSOXAccStatusTypeDef;
enum class AccStatus : int32_t {
OK = 0,
ERROR = -1,
};

typedef enum
{
High_Performance_Acc = 0,
Normal_Power_Acc = 1,
Low_Power_Acc = 2,
Ultra_Low_Power_Acc = 3,
Power_Off_Acc = 4
}PowerModeAcc;

typedef enum
{
Transmission_Acc_OK = 0,
Transmission_Acc_ERROR = -1,
Transmission_Acc_OVERFLOW = -2
} TransmissionAccStatusTypeDef;
enum class AccPowerMode {
HIGH_PERFORMANCE,
NORMAL,
LOW,
ULTRA_LOW,
OFF,
};

enum class AccTransmissionStatus : uint8_t {
OK = 0,
ERROR = 1,
overflow = 2,
};

/* Class Declaration ---------------------------------------------------------*/

Expand All @@ -54,11 +51,11 @@ class Accelerometer
{
public:
Accelerometer(I2C *i2c, uint8_t address=LSM6DSOX_I2C_ADD_L);
LSM6DSOXAccStatusTypeDef init(void *init);
LSM6DSOXAccStatusTypeDef read_id(uint8_t *id);
LSM6DSOXAccStatusTypeDef get_status(PowerModeAcc *powerMode, float *dataRate, uint16_t *fullScale);
LSM6DSOXAccStatusTypeDef get_int_status(uint8_t *dataReady);
LSM6DSOXAccStatusTypeDef get_data(float *mg_X, float *mg_Y, float *mg_Z);
AccStatus init(void *init);
AccStatus read_id(uint8_t *id);
AccStatus get_status(AccPowerMode *powerMode, float *dataRate, uint16_t *fullScale);
AccStatus get_int_status(uint8_t *dataReady);
AccStatus get_data(float *mg_X, float *mg_Y, float *mg_Z);

/**
* @brief Utility function to read data.
Expand All @@ -69,8 +66,6 @@ class Accelerometer
*/
uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
{
//if (!_i2c) return Transmission_ERROR;

int ret;

/* Send device address, with no STOP condition */
Expand All @@ -80,8 +75,8 @@ class Accelerometer
ret = _i2c->read(_address, (char*)pBuffer, NumByteToRead, false);
}

if(ret) return Transmission_Acc_ERROR;
return Transmission_Acc_OK;
if(ret) return (uint8_t)AccTransmissionStatus::ERROR;
return (uint8_t)AccTransmissionStatus::OK;
}

/**
Expand All @@ -98,16 +93,16 @@ class Accelerometer
int ret;
uint8_t tmp[TEMP_BUF_SIZE];

if(NumByteToWrite >= TEMP_BUF_SIZE) return Transmission_Acc_OVERFLOW;
if(NumByteToWrite >= TEMP_BUF_SIZE) return (uint8_t)AccTransmissionStatus::overflow;

/* First, send device address. Then, send data and STOP condition */
tmp[0] = RegisterAddr;
memcpy(tmp+1, pBuffer, NumByteToWrite);

ret = _i2c->write(_address, (const char*)tmp, NumByteToWrite+1, false);

if(ret) return Transmission_Acc_ERROR;
return Transmission_Acc_OK;
if(ret) return (uint8_t)AccTransmissionStatus::ERROR;
return (uint8_t)AccTransmissionStatus::OK;
}

private:
Expand Down
Loading

0 comments on commit 34c9ea8

Please sign in to comment.