Skip to content

Commit

Permalink
Cosmetic change
Browse files Browse the repository at this point in the history
  • Loading branch information
GOB52 committed Aug 13, 2024
1 parent 76bcd6f commit 5880009
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ body:
label: Issue checklist
description: Please double-check that you have done each of the following things before submitting the issue.
options:
- label: I searched for previous reports in [the issue tracker](https://github.com/m5stack/M5Stack/issues?q=)
- label: I searched for previous reports in [the issue tracker](https://github.com/m5stack/M5Unit-HEART/issues?q=)
required: true
- label: My report contains all necessary details
required: true
12 changes: 6 additions & 6 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
strategy:
matrix:
path:
- check: './' # path to include
exclude: '' # path to exclude
# - check: 'src'
# exclude: '(Fonts)' # Exclude file paths containing "Fonts"
# - check: 'examples'
# exclude: ''
- check: 'src'
exclude: ''
- check: 'test'
exclude: ''
- check: 'examples'
exclude: ''
steps:
- uses: actions/[email protected]
- name: Run clang-format style check for C/C++/Protobuf programs.
Expand Down
9 changes: 3 additions & 6 deletions examples/UnitUnified/UnitHeart/UnitHeart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ m5::unit::UnitMAX30100 unitMAX30100;
m5::max30100::HeartRate heartRate(100);

uint32_t getSamplingRate(const m5::unit::max30100::Sampling rate) {
static constexpr uint32_t table[] = {50, 100, 167, 200,
400, 600, 800, 1000};
static constexpr uint32_t table[] = {50, 100, 167, 200, 400, 600, 800, 1000};
return table[m5::stl::to_underlying(rate)];
}

Expand Down Expand Up @@ -58,8 +57,7 @@ void setup() {
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
M5_LOGI("Bus:%d", i2c_bus.has_value());
if (!Units.add(unitMAX30100, i2c_bus ? i2c_bus.value() : nullptr) ||
!Units.begin()) {
if (!Units.add(unitMAX30100, i2c_bus ? i2c_bus.value() : nullptr) || !Units.begin()) {
M5_LOGE("Failed to begin");
lcd.clear(TFT_RED);
while (true) {
Expand Down Expand Up @@ -92,8 +90,7 @@ void loop() {
while (unitMAX30100.available()) {
// M5_LOGI("\n>IR:%u\n>RED:%u", unitMAX30100.ir(),
// unitMAX30100.red());
bool beat = heartRate.push_back((float)unitMAX30100.ir(),
(float)unitMAX30100.red());
bool beat = heartRate.push_back((float)unitMAX30100.ir(), (float)unitMAX30100.red());
if (beat) {
M5_LOGI("Beat!");
}
Expand Down
1 change: 0 additions & 1 deletion src/M5UnitHEART.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ namespace unit {} // namespace unit
} // namespace m5

#endif

31 changes: 10 additions & 21 deletions src/unit/unit_MAX30100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace {
constexpr uint8_t partId{0x11};
// Sampling rate to interval
constexpr elapsed_time_t interval_table[] = {
1000 / 50, 1000 / 100, 1000 / 167, 1000 / 200,
1000 / 400, 1000 / 600, 1000 / 800, 1000 / 1000,
1000 / 50, 1000 / 100, 1000 / 167, 1000 / 200, 1000 / 400, 1000 / 600, 1000 / 800, 1000 / 1000,
};

constexpr uint32_t MEASURE_TEMPERATURE_DURATION{29}; // 29ms
Expand Down Expand Up @@ -56,8 +55,7 @@ const types::uid_t UnitMAX30100::uid{"UnitMAX30100"_mmh3};
const types::uid_t UnitMAX30100::attr{0};

bool UnitMAX30100::begin() {
assert(_cfg.stored_size >= max30100::MAX_FIFO_DEPTH &&
"stored_size must be greater than MAX_FIFO_DEPT");
assert(_cfg.stored_size >= max30100::MAX_FIFO_DEPTH && "stored_size must be greater than MAX_FIFO_DEPT");
if (_cfg.stored_size != _data->capacity()) {
_data.reset(new m5::container::CircularBuffer<Data>(_cfg.stored_size));
if (!_data) {
Expand Down Expand Up @@ -171,17 +169,15 @@ bool UnitMAX30100::setLedConfiguration(const max30100::LedConfiguration lc) {
return set_led_configration(lc.value);
}

bool UnitMAX30100::setLedCurrent(const max30100::CurrentControl ir,
const max30100::CurrentControl red) {
bool UnitMAX30100::setLedCurrent(const max30100::CurrentControl ir, const max30100::CurrentControl red) {
max30100::LedConfiguration lc{};
lc.irLed(ir);
lc.redLed(red);
return set_led_configration(lc.value);
}

bool UnitMAX30100::resetFIFO() {
return writeRegister8(FIFO_WRITE_POINTER, 0) &&
writeRegister8(FIFO_OVERFLOW_COUNTER, 0) &&
return writeRegister8(FIFO_WRITE_POINTER, 0) && writeRegister8(FIFO_OVERFLOW_COUNTER, 0) &&
writeRegister8(FIFO_READ_POINTER, 0);
}

Expand All @@ -190,8 +186,7 @@ bool UnitMAX30100::measureTemperatureSingleshot(TemperatureData& td) {
if (read_mode_configration(mc.value)) {
mc.temperature(true);
if (set_mode_configration(mc.value)) {
auto timeout_at =
m5::utility::millis() + MEASURE_TEMPERATURE_DURATION * 2;
auto timeout_at = m5::utility::millis() + MEASURE_TEMPERATURE_DURATION * 2;
bool done{};
do {
m5::utility::delay(MEASURE_TEMPERATURE_DURATION);
Expand All @@ -206,16 +201,13 @@ bool UnitMAX30100::measureTemperatureSingleshot(TemperatureData& td) {
//
bool UnitMAX30100::read_FIFO() {
uint8_t wptr{}, rptr{};
if (!read_register8(FIFO_WRITE_POINTER, wptr) ||
!read_register8(FIFO_READ_POINTER, rptr) ||
if (!read_register8(FIFO_WRITE_POINTER, wptr) || !read_register8(FIFO_READ_POINTER, rptr) ||
!read_register8(FIFO_OVERFLOW_COUNTER, _overflow)) {
M5_LIB_LOGE("Failed to read ptrs");
return false;
}

uint_fast8_t readCount =
_overflow ? max30100::MAX_FIFO_DEPTH
: (wptr - rptr) & (max30100::MAX_FIFO_DEPTH - 1);
uint_fast8_t readCount = _overflow ? max30100::MAX_FIFO_DEPTH : (wptr - rptr) & (max30100::MAX_FIFO_DEPTH - 1);

// M5_LIB_LOGI(">>cnt:%u of:%u", readCount, _overflow);

Expand All @@ -225,8 +217,7 @@ bool UnitMAX30100::read_FIFO() {
Data d{};
for (uint_fast8_t i = 0; i < readCount; ++i) {
// //Note that FIFO_DATA_REGISTER cannot be burst read.
if (!read_register(FIFO_DATA_REGISTER, d.raw.data(),
d.raw.size())) {
if (!read_register(FIFO_DATA_REGISTER, d.raw.data(), d.raw.size())) {
// Recover the reading position
M5_LIB_LOGE("Failed to read");
if (!writeRegister8(FIFO_READ_POINTER, rptr + i)) {
Expand Down Expand Up @@ -278,8 +269,7 @@ bool UnitMAX30100::set_spo2_configration(const uint8_t c) {
// in the configuration. However, the value is not actually set, so
// check it.
uint8_t chk{};
if (writeRegister8(SPO2_CONFIGURATION, c) && read_spo2_configration(chk) &&
(chk == c)) {
if (writeRegister8(SPO2_CONFIGURATION, c) && read_spo2_configration(chk) && (chk == c)) {
max30100::SpO2Configuration sc;
sc.value = c;
_samplingRate = sc.samplingRate();
Expand Down Expand Up @@ -310,8 +300,7 @@ bool UnitMAX30100::read_register8(const uint8_t reg, uint8_t& v) {
return readRegister8(reg, v, 0, false /*stop*/);
}

bool UnitMAX30100::read_register(const uint8_t reg, uint8_t* buf,
const size_t len) {
bool UnitMAX30100::read_register(const uint8_t reg, uint8_t* buf, const size_t len) {
return readRegister(reg, buf, len, 0, false /*stop*/);
}

Expand Down
33 changes: 11 additions & 22 deletions src/unit/unit_MAX30100.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ struct SpO2Configuration {
value = (value & ~(1U << 6)) | ((b ? 1 : 0) << 6);
}
void samplingRate(const Sampling rate) {
value = (value & ~(0x07 << 2)) |
((m5::stl::to_underlying(rate) & 0x07) << 2);
value = (value & ~(0x07 << 2)) | ((m5::stl::to_underlying(rate) & 0x07) << 2);
}
void ledPulseWidth(const LedPulseWidth width) {
value = (value & ~0x03) | (m5::stl::to_underlying(width) & 0x03);
Expand Down Expand Up @@ -198,8 +197,7 @@ struct LedConfiguration {
///@name Setter
///@{
void redLed(const CurrentControl cc) {
value =
(value & ~(0x0F << 4)) | ((m5::stl::to_underlying(cc) & 0x0F) << 4);
value = (value & ~(0x0F << 4)) | ((m5::stl::to_underlying(cc) & 0x0F) << 4);
}
void irLed(const CurrentControl cc) {
value = (value & ~0x0F) | (m5::stl::to_underlying(cc) & 0x0F);
Expand Down Expand Up @@ -243,9 +241,7 @@ struct TemperatureData {
@note The only single measurement is temperature; other data is constantly
measured and stored
*/
class UnitMAX30100
: public Component,
public PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data> {
class UnitMAX30100 : public Component, public PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data> {
M5_UNIT_COMPONENT_HPP_BUILDER(UnitMAX30100, 0x57);

public:
Expand All @@ -259,24 +255,19 @@ class UnitMAX30100
//! @brief Sampling rate
max30100::Sampling samplingRate{m5::unit::max30100::Sampling::Rate100};
//! @brief Led pulse width
max30100::LedPulseWidth pulseWidth{
m5::unit::max30100::LedPulseWidth::PW1600};
max30100::LedPulseWidth pulseWidth{m5::unit::max30100::LedPulseWidth::PW1600};
//! @brief The SpO2 ADC resolution
bool highResolution{true};
//! @brief Led current for IR
m5::unit::max30100::CurrentControl irCurrent{
// m5::unit::max30100::CurrentControl::mA7_6};
m5::unit::max30100::CurrentControl::mA27_1};
m5::unit::max30100::CurrentControl irCurrent{// m5::unit::max30100::CurrentControl::mA7_6};
m5::unit::max30100::CurrentControl::mA27_1};
//! @brief Led current for Red
m5::unit::max30100::CurrentControl redCurrent{
// m5::unit::max30100::CurrentControl::mA7_6};
m5::unit::max30100::CurrentControl::mA27_1};
m5::unit::max30100::CurrentControl redCurrent{// m5::unit::max30100::CurrentControl::mA7_6};
m5::unit::max30100::CurrentControl::mA27_1};
};

explicit UnitMAX30100(const uint8_t addr = DEFAULT_ADDRESS)
: Component(addr),
_data{new m5::container::CircularBuffer<max30100::Data>(
max30100::MAX_FIFO_DEPTH)} {
: Component(addr), _data{new m5::container::CircularBuffer<max30100::Data>(max30100::MAX_FIFO_DEPTH)} {
auto cfg = config();
cfg.stored_size = max30100::MAX_FIFO_DEPTH;
config(cfg);
Expand Down Expand Up @@ -408,8 +399,7 @@ class UnitMAX30100
*/
bool setLedConfiguration(const max30100::LedConfiguration lc);
//! @brief Set IR/RED current
bool setLedCurrent(const max30100::CurrentControl ir,
const max30100::CurrentControl red);
bool setLedCurrent(const max30100::CurrentControl ir, const max30100::CurrentControl red);
///@}

///@note The temperature sensor data can be used to compensate the SpO2
Expand Down Expand Up @@ -453,8 +443,7 @@ class UnitMAX30100
bool read_FIFO();
bool read_measurement_temperature(max30100::TemperatureData& td);

M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitMAX30100,
max30100::Data);
M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitMAX30100, max30100::Data);

bool read_mode_configration(uint8_t& c);
bool set_mode_configration(const uint8_t c);
Expand Down
35 changes: 12 additions & 23 deletions src/utility/heart_rate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ namespace {

constexpr float ALPHA{0.95f}; // for removeDC

void calculateButterworthCoefficients(float Fs, float Fc, float& a0, float& a1,
float& b1) {
void calculateButterworthCoefficients(float Fs, float Fc, float& a0, float& a1, float& b1) {
float tanWc = std::tan(M_PI * Fc / Fs);
float sqrt2 = std::sqrt(2.0f);

Expand All @@ -29,8 +28,7 @@ void calculateButterworthCoefficients(float Fs, float Fc, float& a0, float& a1,
b1 = (tanWc - sqrt2) / (tanWc + sqrt2);
}

inline dcFilter_t removeDC(const float x, const float prev_w,
const float alpha) {
inline dcFilter_t removeDC(const float x, const float prev_w, const float alpha) {
dcFilter_t filtered;
filtered.w = x + alpha * prev_w;
filtered.result = filtered.w - prev_w;
Expand All @@ -45,8 +43,7 @@ float meanDiff(const float M, meanDiffFilter_t& filterValues) {
filterValues.sum += filterValues.values[filterValues.index];

filterValues.index++;
filterValues.index =
filterValues.index % meanDiffFilter_t::MEAN_FILTER_SIZE;
filterValues.index = filterValues.index % meanDiffFilter_t::MEAN_FILTER_SIZE;

if (filterValues.count < meanDiffFilter_t::MEAN_FILTER_SIZE) {
filterValues.count++;
Expand Down Expand Up @@ -77,23 +74,18 @@ std::pair<float /*AC*/, float /*DC*/> calculateACDC(
namespace m5 {
namespace max30100 {

HeartRate::HeartRate(const uint32_t srate, const float threshold,
const size_t max_data_size)
: _samplingRate{(float)srate},
_threshold(threshold),
_maxDataSize{max_data_size} {
HeartRate::HeartRate(const uint32_t srate, const float threshold, const size_t max_data_size)
: _samplingRate{(float)srate}, _threshold(threshold), _maxDataSize{max_data_size} {
assert(srate && "SamplingRate must not be zero");
if (!max_data_size) {
_maxDataSize = (size_t)srate * 30U;
}
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1,
_bwfIR.b1);
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1, _bwfIR.b1);
}

void HeartRate::setSamplingRate(const uint32_t sr) {
_samplingRate = sr;
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1,
_bwfIR.b1);
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1, _bwfIR.b1);
clear();
}

Expand All @@ -104,8 +96,7 @@ void HeartRate::clear() {
_dcIR = dcFilter_t{};
_mdIR = meanDiffFilter_t{};
_bwfIR = butterworthFilter_t{};
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1,
_bwfIR.b1);
calculateButterworthCoefficients(_samplingRate, 10.0f, _bwfIR.a0, _bwfIR.a1, _bwfIR.b1);
}

bool HeartRate::push_back(const float ir, const float red) {
Expand Down Expand Up @@ -135,10 +126,9 @@ bool HeartRate::push_back(const float ir, const float red) {
_acSqRED = _dcRED.result * _dcRED.result;
++_count;
if (_beat) {
float rr = std::log(std::sqrt(_acSqRED / _count)) /
std::log(std::sqrt(_acSqIR / _count));
auto s = 110.0f - _coeffSpO2 * rr;
_SpO2 = std::fmin(std::fmax(s, 80.f), 100.f);
float rr = std::log(std::sqrt(_acSqRED / _count)) / std::log(std::sqrt(_acSqIR / _count));
auto s = 110.0f - _coeffSpO2 * rr;
_SpO2 = std::fmin(std::fmax(s, 80.f), 100.f);
_acSqIR = _acSqRED = 0.0f;
_count = 0;
}
Expand Down Expand Up @@ -167,8 +157,7 @@ bool HeartRate::detect_beat() {
auto d2 = _dataIR[_dataIR.size() - 2];
auto d1 = _dataIR[_dataIR.size() - 3];
auto d0 = _dataIR[_dataIR.size() - 4];
if (!_incrasing && (d0 > _threshold) && (d1 > d0) && (d2 > d1) &&
(d3 > d2)) {
if (!_incrasing && (d0 > _threshold) && (d1 > d0) && (d2 > d1) && (d3 > d2)) {
_incrasing = true;
} else if (_incrasing && (d1 < d0) && (d2 < d1) && (d3 < d2)) {
_incrasing = false;
Expand Down
3 changes: 1 addition & 2 deletions src/utility/heart_rate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class HeartRate {
@param threshold Threshold for detect beat (depends on ir/redCUrrent)
@param store_size Stored data size(0U means auto)
*/
HeartRate(const uint32_t srate, const float threshold = 125.f,
const size_t max_data_size = 0U);
HeartRate(const uint32_t srate, const float threshold = 125.f, const size_t max_data_size = 0U);

///@name Settings
///@{
Expand Down
12 changes: 5 additions & 7 deletions test/embedded/embedded_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
#if __has_include(<esp_idf_version.h>)
#include <esp_idf_version.h>
#else // esp_idf_version.h has been introduced in Arduino 1.0.5 (ESP-IDF3.3)
#define ESP_IDF_VERSION_VAL(major, minor, patch) \
((major << 16) | (minor << 8) | (patch))
#define ESP_IDF_VERSION ESP_IDF_VERSION_VAL(3, 2, 0)
#define ESP_IDF_VERSION_VAL(major, minor, patch) ((major << 16) | (minor << 8) | (patch))
#define ESP_IDF_VERSION ESP_IDF_VERSION_VAL(3, 2, 0)
#endif

namespace {
auto& lcd = M5.Display;
} // namespace

void test() {
lcd.fillRect(0, 0, lcd.width() >> 1, lcd.height(),
RUN_ALL_TESTS() ? TFT_RED : TFT_GREEN);
lcd.fillRect(0, 0, lcd.width() >> 1, lcd.height(), RUN_ALL_TESTS() ? TFT_RED : TFT_GREEN);
}

void setup() {
Expand All @@ -35,8 +33,8 @@ void setup() {
M5.begin();

M5_LOGI("CPP %ld", __cplusplus);
M5_LOGI("ESP-IDF Version %d.%d.%d", (ESP_IDF_VERSION >> 16) & 0xFF,
(ESP_IDF_VERSION >> 8) & 0xFF, ESP_IDF_VERSION & 0xFF);
M5_LOGI("ESP-IDF Version %d.%d.%d", (ESP_IDF_VERSION >> 16) & 0xFF, (ESP_IDF_VERSION >> 8) & 0xFF,
ESP_IDF_VERSION & 0xFF);
M5_LOGI("BOARD:%X", M5.getBoard());
M5_LOGI("Heap: %u", esp_get_free_heap_size());

Expand Down
Loading

0 comments on commit 5880009

Please sign in to comment.