10#ifndef M5_UNIT_HEART_UNIT_MAX30100_HPP
11#define M5_UNIT_HEART_UNIT_MAX30100_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/stl/extension.hpp>
15#include <m5_utility/container/circular_buffer.hpp>
44 return value & (1U << 7);
48 return value & (1U << 6);
52 return value & (1U << 3);
56 return static_cast<Mode>(value & 0x07);
64 value = (value & ~(1U << 7)) | ((b ? 1 : 0) << 7);
68 value = (value & ~(1U << 6)) | ((b ? 1 : 0) << 6);
72 value = (value & ~(1U << 3)) | ((b ? 1 : 0) << 3);
76 value = (value & ~0x07) | (m5::stl::to_underlying(m) & 0x07);
104uint32_t getSamplingRate(
const Sampling rate);
149 bool highResolution()
const
151 return value & (1U << 6);
155 return static_cast<Sampling>((value >> 2) & 0x07);
165 void highResolution(
const bool b)
167 value = (value & ~(1U << 6)) | ((b ? 1 : 0) << 6);
169 void samplingRate(
const Sampling rate)
171 value = (value & ~(0x07 << 2)) | ((m5::stl::to_underlying(rate) & 0x07) << 2);
175 value = (value & ~0x03) | (m5::stl::to_underlying(width) & 0x03);
225 value = (value & ~(0x0F << 4)) | ((m5::stl::to_underlying(cc) & 0x0F) << 4);
229 value = (value & ~0x0F) | (m5::stl::to_underlying(cc) & 0x0F);
243 std::array<uint8_t, 4> raw{};
245 uint16_t
red()
const;
253 std::array<uint8_t, 2> raw{};
271class UnitMAX30100 :
public Component,
public PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data> {
297 explicit UnitMAX30100(
const uint8_t addr = DEFAULT_ADDRESS)
298 : Component(addr), _data{new
m5::container::CircularBuffer<
max30100::Data>(
max30100::MAX_FIFO_DEPTH)}
300 auto ccfg = component_config();
301 ccfg.clock = 400 * 1000U;
302 ccfg.stored_size = max30100::MAX_FIFO_DEPTH;
303 component_config(ccfg);
305 virtual ~UnitMAX30100()
309 virtual bool begin()
override;
310 virtual void update(
const bool force =
false)
override;
329 inline uint16_t
ir()
const
331 return !empty() ? oldest().ir() : 0;
334 inline uint16_t
red()
const
336 return !empty() ? oldest().red() : 0;
366 return PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data>::startPeriodicMeasurement();
384 return PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data>::startPeriodicMeasurement(
385 mode, sample_rate, pulse_width, ir_current, high_resolution, red_current);
393 return PeriodicMeasurementAdapter<UnitMAX30100, max30100::Data>::stopPeriodicMeasurement();
417 return enable_power_save(
true);
422 return enable_power_save(
false);
449 return enable_high_resolution(
true);
454 return enable_high_resolution(
false);
505 bool start_periodic_measurement();
510 bool stop_periodic_measurement();
517 bool read_mode_configration(uint8_t& c);
518 bool write_mode_configration(
const uint8_t c);
519 bool enable_power_save(
const bool enabled);
520 bool read_spo2_configration(uint8_t& c);
521 bool write_spo2_configration(
const uint8_t c);
522 bool enable_high_resolution(
const bool enabled);
523 bool read_led_configration(uint8_t& c);
524 bool write_led_configration(
const uint8_t c);
526 bool read_register(
const uint8_t reg, uint8_t* buf,
const size_t len);
527 bool read_register8(
const uint8_t reg, uint8_t& v);
531 uint8_t _retrived{}, _overflow{};
532 std::unique_ptr<m5::container::CircularBuffer<max30100::Data>> _data{};
541constexpr uint8_t READ_INTERRUPT_STATUS{0x00};
542constexpr uint8_t INTERRUPT_ENABLE{0x01};
544constexpr uint8_t FIFO_WRITE_POINTER{0x02};
545constexpr uint8_t FIFO_OVERFLOW_COUNTER{0x03};
546constexpr uint8_t FIFO_READ_POINTER{0x04};
548constexpr uint8_t FIFO_DATA_REGISTER{0x05};
550constexpr uint8_t MODE_CONFIGURATION{0x06};
551constexpr uint8_t SPO2_CONFIGURATION{0x07};
552constexpr uint8_t LED_CONFIGURATION{0x09};
554constexpr uint8_t TEMP_INTEGER{0x16};
555constexpr uint8_t TEMP_FRACTION{0x17};
557constexpr uint8_t READ_REVISION_ID{0xFE};
558constexpr uint8_t PART_ID{0xFF};
Pulse oximetry and heart-rate sensor.
Definition unit_MAX30100.hpp:271
void config(const config_t &cfg)
Set the configration.
Definition unit_MAX30100.hpp:320
uint8_t overflow() const
The number of samples lost.
Definition unit_MAX30100.hpp:352
bool readSpO2Configuration(max30100::SpO2Configuration &sc)
Read SpO2 configrartion.
Definition unit_MAX30100.cpp:224
bool writeSamplingRate(const max30100::Sampling rate)
Write sample rate.
Definition unit_MAX30100.cpp:234
bool writePowerSaveDisable()
Write power save mode to disable.
Definition unit_MAX30100.hpp:420
bool measureTemperatureSingleshot(max30100::TemperatureData &td)
Measure tempeature single shot.
Definition unit_MAX30100.cpp:278
bool writeHighResolutionDisable()
Write SpO2 high resolution mode to disable.
Definition unit_MAX30100.hpp:452
bool writeSpO2Configuration(const max30100::SpO2Configuration sc)
Write SpO2 configrartion.
Definition unit_MAX30100.cpp:229
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_MAX30100.hpp:364
bool writeMode(const max30100::Mode mode)
Write Mode.
Definition unit_MAX30100.cpp:196
uint8_t retrived() const
Number of data last retrieved.
Definition unit_MAX30100.hpp:344
bool startPeriodicMeasurement(const max30100::Mode mode, const max30100::Sampling sample_rate, const max30100::LedPulseWidth pulse_width, const max30100::CurrentControl ir_current, const bool high_resolution=false, const max30100::CurrentControl red_current=max30100::CurrentControl::mA0_0)
Start periodic measurement.
Definition unit_MAX30100.hpp:379
config_t config()
Gets the configration.
Definition unit_MAX30100.hpp:315
bool writeModeConfiguration(const max30100::ModeConfiguration mc)
Write Mode configuration.
Definition unit_MAX30100.cpp:191
bool readModeConfiguration(max30100::ModeConfiguration &mc)
Read Mode configuration.
Definition unit_MAX30100.cpp:186
bool writeHighResolutionEnable()
Writee SpO2 high resolution mode to enable.
Definition unit_MAX30100.hpp:447
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_MAX30100.hpp:391
bool writeLedConfiguration(const max30100::LedConfiguration lc)
Write Led configrartion.
Definition unit_MAX30100.cpp:259
bool reset()
Reset.
Definition unit_MAX30100.cpp:206
bool writeLedCurrent(const max30100::CurrentControl ir, const max30100::CurrentControl red)
Write IR/RED current.
Definition unit_MAX30100.cpp:264
bool resetFIFO()
Reset FIFO buffer.
Definition unit_MAX30100.cpp:272
bool writeLedPulseWidth(const max30100::LedPulseWidth width)
Write LED pulse width.
Definition unit_MAX30100.cpp:244
uint16_t red() const
Oldest Red.
Definition unit_MAX30100.hpp:334
uint16_t ir() const
Oldest IR.
Definition unit_MAX30100.hpp:329
bool writePowerSaveEnable()
Write power save mode to enable.
Definition unit_MAX30100.hpp:415
bool readLedConfiguration(max30100::LedConfiguration &lc)
Read Led configrartion.
Definition unit_MAX30100.cpp:254
Top level namespace of M5stack.
Settings for begin.
Definition unit_MAX30100.hpp:280
max30100::LedPulseWidth pulse_width
Led pulse width if start on begin.
Definition unit_MAX30100.hpp:288
m5::unit::max30100::CurrentControl red_current
Led current for Red if start on begin (only SpO2)
Definition unit_MAX30100.hpp:294
m5::unit::max30100::CurrentControl ir_current
Led current for IR if start on begin.
Definition unit_MAX30100.hpp:290
bool start_periodic
Start periodic measurement on begin?
Definition unit_MAX30100.hpp:282
max30100::Mode mode
Operating mode if start on begin.
Definition unit_MAX30100.hpp:284
max30100::Sampling sampling_rate
Sampling rate if start on begin.
Definition unit_MAX30100.hpp:286
bool high_resolution
The SpO2 ADC resolution if start on begin (only SpO2)
Definition unit_MAX30100.hpp:292
Measurement data group.
Definition unit_MAX30100.hpp:242
uint16_t red() const
RED.
Definition unit_MAX30100.cpp:62
uint16_t ir() const
IR.
Definition unit_MAX30100.cpp:58
Accessor for LedConfiguration.
Definition unit_MAX30100.hpp:209
Accessor for ModeConfiguration.
Definition unit_MAX30100.hpp:39
void mode(const Mode m)
Mode control.
Definition unit_MAX30100.hpp:74
bool reset() const
Reset control.
Definition unit_MAX30100.hpp:46
void shdn(const bool b)
Shutdown Control (SHDN)
Definition unit_MAX30100.hpp:62
bool shdn() const
Shutdown Control (SHDN)
Definition unit_MAX30100.hpp:42
void temperature(const bool b)
Temperature enable.
Definition unit_MAX30100.hpp:70
bool temperature() const
Temperature enable.
Definition unit_MAX30100.hpp:50
Mode mode() const
Mode control.
Definition unit_MAX30100.hpp:54
void reset(const bool b)
Reset control.
Definition unit_MAX30100.hpp:66
Accessor for SpO2Configuration.
Definition unit_MAX30100.hpp:146
Measurement data group for temperature.
Definition unit_MAX30100.hpp:252
float fahrenheit() const
temperature (Fahrenheit)
Definition unit_MAX30100.cpp:72
float celsius() const
temperature (Celsius)
Definition unit_MAX30100.cpp:67
float temperature() const
temperature (Celsius)
Definition unit_MAX30100.hpp:255
Mode
Operation mode.
Definition unit_MAX30100.hpp:29
@ SPO2
SPO2 and HR enabled.
constexpr uint8_t MAX_FIFO_DEPTH
FIFO depth.
Definition unit_MAX30100.hpp:236
CurrentControl
Current level for Led.
Definition unit_MAX30100.hpp:186
LedPulseWidth
LED pulse width (the IR and RED have the same pulse width)
Definition unit_MAX30100.hpp:110
@ PW200
200 us (ADC 13 bits)
@ PW1600
1600 us (ADC 16 bits)
@ PW800
800 us (ADC 15 bits)
@ PW400
400 us (ADC 14 bits)
Sampling
Sampling rate for pulse.
Definition unit_MAX30100.hpp:88