Skip to content

Commit

Permalink
namespace clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
aldwinhermanudin committed Dec 30, 2023
1 parent ba30a89 commit e882a93
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
33 changes: 29 additions & 4 deletions src/mbedos/test-bme688/source/bme688/BME688.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define BSEC_CHECK_INPUT(x, shift) (x & (1 << (shift-1)))

using namespace std::chrono;
using namespace teapotlabs::utils;

namespace teapotlabs {
namespace sensors {
Expand All @@ -14,6 +15,12 @@ namespace bme688 {
// *********************************************************************
static int8_t SensorInternalReadRegisterCb( uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr )
{
// check user intf_ptr object
if( intf_ptr == nullptr )
{
return 0xFF; // return error
}

BME688* bme688 = (BME688*) intf_ptr;
if( bme688->i2c_local == nullptr )
{
Expand All @@ -37,8 +44,14 @@ static int8_t SensorInternalReadRegisterCb( uint8_t reg_addr, uint8_t *reg_data,
return rslt;
}

static int8_t SensorInternalWriteRegisterCb(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
static int8_t SensorInternalWriteRegisterCb( uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr )
{
// check user intf_ptr object
if( intf_ptr == nullptr )
{
return 0xFF; // return error
}

// check callback obeject pointer
BME688* bme688 = (BME688*) intf_ptr;
if( bme688->i2c_local == nullptr )
Expand Down Expand Up @@ -74,7 +87,6 @@ static void SensorInternalDelayUsCb(uint32_t time_us, void *intf_ptr)
wait_us ( time_us );
}


// *********************************************************************
// PUBLIC
// *********************************************************************
Expand Down Expand Up @@ -105,7 +117,16 @@ ReturnCode BME688::Initialise( bsec_virtual_sensor_t* sensor_list, uint8_t n_sen
this->bsec.n_sensors = n_sensors;
memcpy(this->bsec.sensor_list, sensor_list, n_sensors);

// TODO: check chip ID
uint8_t chip_id;
result = this->GetChipId( chip_id );
if( result != ReturnCode::kOk )
{
return result;
}
if( chip_id != kBme688ChipId )
{
return ReturnCode::kSensorChipIdUnknown;
}

/* initialise bme688 sensor */
result = this->InitialiseSensor();
Expand Down Expand Up @@ -628,7 +649,11 @@ ReturnCode BME688::ProcessData(const int64_t curr_time_ns, const bme68x_data &da

ReturnCode BME688::GetChipId( uint8_t& chip_id_out )
{
return ReadRegister( kBme688ChipIdAddr, &chip_id_out, 1 );
if( ReadRegister( kBme688ChipIdAddr, &chip_id_out, 1 ) != ReturnCode::kOk )
{
return ReturnCode::kSensorGetChipIdFail;
}
return ReturnCode::kOk;
}

ReturnCode BME688::ReadRegister( const uint8_t reg_addr, uint8_t* const reg_data, uint32_t length )
Expand Down
45 changes: 24 additions & 21 deletions src/mbedos/test-bme688/source/bme688/BME688.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __BME688__H
#define __BME688__H
#ifndef BME688_H
#define BME688_H

#include "mbed.h"
#include "bme68x_defs.h"
Expand All @@ -12,29 +12,32 @@ namespace sensors {
namespace bme688 {

/* defining constexpr in header might not be the best way to replace macro */
constexpr uint8_t kBme688ChipId = 0x61;
constexpr uint8_t kBme688ChipIdAddr = 0xD0;
constexpr uint16_t kBsecTotalHeatDur = UINT16_C(140);

enum class ReturnCode
{
// success code
kOk = 0,
// error code
kError,
kSensorStructureFail,
kSensorConfigFail,
kSensorHeaterFail,
kSensorSetOperationFail,
kSensorOperationSeqFail,
kSensorBsecFail,
kSensorBsecSubscriptionFail,
kSensorBsecProcessFail,
kSensorGetDataFail,
kBsecInitFail,
kBsecRunFail,
kSensorInitFail,
kNullPointer,
kSensorReadRegisterFail
// success code
kOk = 0,
// error code
kError,
kSensorStructureFail,
kSensorConfigFail,
kSensorHeaterFail,
kSensorSetOperationFail,
kSensorOperationSeqFail,
kSensorBsecFail,
kSensorBsecSubscriptionFail,
kSensorBsecProcessFail,
kSensorGetDataFail,
kBsecInitFail,
kBsecRunFail,
kSensorInitFail,
kNullPointer,
kSensorReadRegisterFail,
kSensorChipIdUnknown,
kSensorGetChipIdFail
};

using Callback = void (*)( const bme68x_data data, bsec_output_t* const outputs, const uint8_t n_outputs );
Expand Down Expand Up @@ -72,7 +75,7 @@ class BME688{

private:

// enum and struct definition
// local struct definition
struct Bme688FetchedData
{
bme68x_data data[3];
Expand Down
10 changes: 8 additions & 2 deletions src/mbedos/test-bme688/source/bme688/utils/Defer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef DEFER_H
#define DEFER_H
#ifndef UTILS_DEFER_H
#define UTILS_DEFER_H

namespace teapotlabs{
namespace utils {

template <class T>
class Defer
{
Expand All @@ -15,4 +18,7 @@ class Defer
};
};

} // namespace utils
} // namespace teapotlabs

#endif

0 comments on commit e882a93

Please sign in to comment.