esp32_BNO08x/source/BNO08xRpt.cpp

160 lines
4.0 KiB
C++
Raw Normal View History

2024-11-23 01:05:03 +00:00
#include "BNO08xRpt.hpp"
#include "BNO08x.hpp"
/**
* @brief Enables a sensor report such that the BNO08x begins sending it.
*
* @param report_period_us The period/interval of the report in microseconds.
* @param sensor_cfg Sensor special configuration (optional, see BNO08xRpt::default_sensor_cfg for defaults).
2024-11-23 01:05:03 +00:00
*
* @return True if report was successfully enabled.
2024-11-23 01:05:03 +00:00
*/
bool BNO08xRpt::enable(uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg)
{
2024-11-24 04:08:15 +00:00
int sh2_res = SH2_OK;
xEventGroupClearBits(imu->evt_grp_report_en, rpt_bit); // Set the event group bit
sensor_cfg.reportInterval_us = time_between_reports;
imu->lock_sh2_HAL();
2024-11-24 04:08:15 +00:00
sh2_res = sh2_setSensorConfig(ID, &sensor_cfg);
imu->unlock_sh2_HAL();
2024-11-23 01:05:03 +00:00
2024-11-24 04:08:15 +00:00
if (sh2_res != SH2_OK)
2024-11-23 01:05:03 +00:00
{
2024-11-24 04:08:15 +00:00
return false;
}
else
{
flush();
period_us = time_between_reports; // Update the period
xEventGroupSetBits(imu->evt_grp_report_en, rpt_bit); // Set the event group bit
return true;
2024-11-23 01:05:03 +00:00
}
}
/**
* @brief Disables a sensor report by setting its period to 0us such that the BNO08x stops sending it.
*
* @param sensor_ID The ID of the sensor for the respective report to be disabled.
* @param sensor_cfg Sensor special configuration.
*
* @return ESP_OK if report was successfully disabled.
*/
bool BNO08xRpt::disable(sh2_SensorConfig_t sensor_cfg)
{
EventBits_t evt_grp_report_en_bits = xEventGroupGetBits(imu->evt_grp_report_en);
if (evt_grp_report_en_bits & rpt_bit)
{
2024-11-24 04:08:15 +00:00
if (!enable(0UL, sensor_cfg))
2024-11-23 01:05:03 +00:00
return false;
else
xEventGroupClearBits(imu->evt_grp_report_en, rpt_bit); // Set the event group bit
}
return true;
}
/**
* @brief Registers a callback to execute when new data from a specific report is received.
*
* @param cb_fxn Pointer to the call-back function should be of void return type void input param.
*
* @return void, nothing to return
*/
void BNO08xRpt::register_cb(std::function<void(void)> cb_fxn)
{
imu->cb_list.push_back({ID, cb_fxn});
2024-11-24 04:08:15 +00:00
}
/**
* @brief Checks if a new report has been received since the last time this function was called.
*
*
* @return True if a new report was received since the last time this function was called.
*/
bool BNO08xRpt::has_new_data()
{
bool new_data = false;
if (xEventGroupGetBits(imu->evt_grp_report_data_available) & rpt_bit)
{
new_data = true;
xEventGroupClearBits(imu->evt_grp_report_data_available, rpt_bit);
}
return new_data;
}
2024-11-24 04:08:15 +00:00
/**
* @brief Flush all buffered reports for this sensor/report module.
*
* @return True if flush operation succeeded.
*/
bool BNO08xRpt::flush()
{
int success = SH2_OK;
imu->lock_sh2_HAL();
success = sh2_flush(ID);
imu->unlock_sh2_HAL();
return (success != SH2_OK) ? false : true;
}
/**
* @brief Gets sample counts for this sensor (see SH-2 ref manual 6.4.3.1)
*
* @param Struct to store requested data.
*
* @return True get counts operation succeeded.
*/
bool BNO08xRpt::get_sample_counts(bno08x_sample_counts_t& sample_counts)
{
int success = SH2_OK;
sh2_Counts_t pCounts;
imu->lock_sh2_HAL();
success = sh2_getCounts(ID, &pCounts);
imu->unlock_sh2_HAL();
if (success != SH2_OK)
{
return false;
}
else
{
sample_counts = pCounts;
return true;
}
}
/**
* @brief Clears BNO08x internal sample counts for this sensor. (see SH-2 ref manual 6.4.3.1)
*
* @return True clear counts operation succeeded.
*/
bool BNO08xRpt::clear_sample_counts()
{
int success = SH2_OK;
imu->lock_sh2_HAL();
success = sh2_clearCounts(ID);
imu->unlock_sh2_HAL();
return (success != SH2_OK) ? false : true;
}
2024-11-24 04:08:15 +00:00
/**
* @brief Signals to BNO08x::data_available() that a new report has arrived.
*
* @return void, nothing to return
*/
void BNO08xRpt::signal_data_available()
{
xEventGroupSetBits(imu->evt_grp_report_data_available, rpt_bit);
2024-11-24 04:08:15 +00:00
xEventGroupSetBits(imu->evt_grp_bno08x_task, BNO08x::EVT_GRP_BNO08x_TASK_DATA_AVAILABLE);
}