Atomic HRS reads (#1845)
- Combine the reading of all `HRS3300` registers into one I2C read so data is not partial - Downsizes both HRS and ALS to 16bit as the sensor does not generate larger than 16bit values in its current configuration - Increasing the resolution by 1 bit doubles the sensor acquisition time, since we are already at 10Hz we are never going to use a higher resolution - The PPG algorithm buffers for ALS/HRS are already 16bit anyway - Remove functions for setting gain / drive that are unused throughout the codebase - Calculate constants with constexpr
This commit is contained in:
parent
7ca0418c82
commit
ad3bf49c7b
|
@ -142,7 +142,7 @@ Ppg::Ppg() {
|
||||||
spectrum.fill(0.0f);
|
spectrum.fill(0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t Ppg::Preprocess(uint32_t hrs, uint32_t als) {
|
int8_t Ppg::Preprocess(uint16_t hrs, uint16_t als) {
|
||||||
if (dataIndex < dataLength) {
|
if (dataIndex < dataLength) {
|
||||||
dataHRS[dataIndex++] = hrs;
|
dataHRS[dataIndex++] = hrs;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Pinetime {
|
||||||
class Ppg {
|
class Ppg {
|
||||||
public:
|
public:
|
||||||
Ppg();
|
Ppg();
|
||||||
int8_t Preprocess(uint32_t hrs, uint32_t als);
|
int8_t Preprocess(uint16_t hrs, uint16_t als);
|
||||||
int HeartRate();
|
int HeartRate();
|
||||||
void Reset(bool resetDaqBuffer);
|
void Reset(bool resetDaqBuffer);
|
||||||
static constexpr int deltaTms = 100;
|
static constexpr int deltaTms = 100;
|
||||||
|
|
|
@ -67,40 +67,37 @@ void Hrs3300::Disable() {
|
||||||
WriteRegister(static_cast<uint8_t>(Registers::PDriver), 0);
|
WriteRegister(static_cast<uint8_t>(Registers::PDriver), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Hrs3300::ReadHrs() {
|
Hrs3300::PackedHrsAls Hrs3300::ReadHrsAls() {
|
||||||
auto m = ReadRegister(static_cast<uint8_t>(Registers::C0DataM));
|
constexpr Registers dataRegisters[] =
|
||||||
auto h = ReadRegister(static_cast<uint8_t>(Registers::C0DataH));
|
{Registers::C1dataM, Registers::C0DataM, Registers::C0DataH, Registers::C1dataH, Registers::C1dataL, Registers::C0dataL};
|
||||||
auto l = ReadRegister(static_cast<uint8_t>(Registers::C0dataL));
|
// Calculate smallest register address
|
||||||
return ((l & 0x30) << 12) | (m << 8) | ((h & 0x0f) << 4) | (l & 0x0f);
|
constexpr uint8_t baseOffset = static_cast<uint8_t>(*std::min_element(std::begin(dataRegisters), std::end(dataRegisters)));
|
||||||
|
// Calculate largest address to determine length of read needed
|
||||||
|
// Add one to largest relative index to find the length
|
||||||
|
constexpr uint8_t length = static_cast<uint8_t>(*std::max_element(std::begin(dataRegisters), std::end(dataRegisters))) - baseOffset + 1;
|
||||||
|
|
||||||
|
Hrs3300::PackedHrsAls res;
|
||||||
|
uint8_t buf[length];
|
||||||
|
auto ret = twiMaster.Read(twiAddress, baseOffset, buf, length);
|
||||||
|
if (ret != TwiMaster::ErrorCodes::NoError) {
|
||||||
|
NRF_LOG_INFO("READ ERROR");
|
||||||
}
|
}
|
||||||
|
// hrs
|
||||||
|
uint8_t m = static_cast<uint8_t>(Registers::C0DataM) - baseOffset;
|
||||||
|
uint8_t h = static_cast<uint8_t>(Registers::C0DataH) - baseOffset;
|
||||||
|
uint8_t l = static_cast<uint8_t>(Registers::C0dataL) - baseOffset;
|
||||||
|
// There are two extra bits (17 and 18) but they are not read here
|
||||||
|
// as resolutions >16bit aren't practically useful (too slow) and
|
||||||
|
// all hrs values throughout InfiniTime are 16bit
|
||||||
|
res.hrs = (buf[m] << 8) | ((buf[h] & 0x0f) << 4) | (buf[l] & 0x0f);
|
||||||
|
|
||||||
uint32_t Hrs3300::ReadAls() {
|
// als
|
||||||
auto m = ReadRegister(static_cast<uint8_t>(Registers::C1dataM));
|
m = static_cast<uint8_t>(Registers::C1dataM) - baseOffset;
|
||||||
auto h = ReadRegister(static_cast<uint8_t>(Registers::C1dataH));
|
h = static_cast<uint8_t>(Registers::C1dataH) - baseOffset;
|
||||||
auto l = ReadRegister(static_cast<uint8_t>(Registers::C1dataL));
|
l = static_cast<uint8_t>(Registers::C1dataL) - baseOffset;
|
||||||
return ((h & 0x3f) << 11) | (m << 3) | (l & 0x07);
|
res.als = ((buf[h] & 0x3f) << 11) | (buf[m] << 3) | (buf[l] & 0x07);
|
||||||
}
|
|
||||||
|
|
||||||
void Hrs3300::SetGain(uint8_t gain) {
|
return res;
|
||||||
constexpr uint8_t maxGain = 64U;
|
|
||||||
gain = std::min(gain, maxGain);
|
|
||||||
uint8_t hgain = 0;
|
|
||||||
while ((1 << hgain) < gain) {
|
|
||||||
++hgain;
|
|
||||||
}
|
|
||||||
|
|
||||||
WriteRegister(static_cast<uint8_t>(Registers::Hgain), hgain << 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Hrs3300::SetDrive(uint8_t drive) {
|
|
||||||
auto en = ReadRegister(static_cast<uint8_t>(Registers::Enable));
|
|
||||||
auto pd = ReadRegister(static_cast<uint8_t>(Registers::PDriver));
|
|
||||||
|
|
||||||
en = (en & 0xf7) | ((drive & 2) << 2);
|
|
||||||
pd = (pd & 0xbf) | ((drive & 1) << 6);
|
|
||||||
|
|
||||||
WriteRegister(static_cast<uint8_t>(Registers::Enable), en);
|
|
||||||
WriteRegister(static_cast<uint8_t>(Registers::PDriver), pd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hrs3300::WriteRegister(uint8_t reg, uint8_t data) {
|
void Hrs3300::WriteRegister(uint8_t reg, uint8_t data) {
|
||||||
|
|
|
@ -21,6 +21,11 @@ namespace Pinetime {
|
||||||
Hgain = 0x17
|
Hgain = 0x17
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PackedHrsAls {
|
||||||
|
uint16_t hrs;
|
||||||
|
uint16_t als;
|
||||||
|
};
|
||||||
|
|
||||||
Hrs3300(TwiMaster& twiMaster, uint8_t twiAddress);
|
Hrs3300(TwiMaster& twiMaster, uint8_t twiAddress);
|
||||||
Hrs3300(const Hrs3300&) = delete;
|
Hrs3300(const Hrs3300&) = delete;
|
||||||
Hrs3300& operator=(const Hrs3300&) = delete;
|
Hrs3300& operator=(const Hrs3300&) = delete;
|
||||||
|
@ -30,10 +35,7 @@ namespace Pinetime {
|
||||||
void Init();
|
void Init();
|
||||||
void Enable();
|
void Enable();
|
||||||
void Disable();
|
void Disable();
|
||||||
uint32_t ReadHrs();
|
PackedHrsAls ReadHrsAls();
|
||||||
uint32_t ReadAls();
|
|
||||||
void SetGain(uint8_t gain);
|
|
||||||
void SetDrive(uint8_t drive);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TwiMaster& twiMaster;
|
TwiMaster& twiMaster;
|
||||||
|
|
|
@ -70,7 +70,8 @@ void HeartRateTask::Work() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (measurementStarted) {
|
if (measurementStarted) {
|
||||||
int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls());
|
auto sensorData = heartRateSensor.ReadHrsAls();
|
||||||
|
int8_t ambient = ppg.Preprocess(sensorData.hrs, sensorData.als);
|
||||||
int bpm = ppg.HeartRate();
|
int bpm = ppg.HeartRate();
|
||||||
|
|
||||||
// If ambient light detected or a reset requested (bpm < 0)
|
// If ambient light detected or a reset requested (bpm < 0)
|
||||||
|
|
Loading…
Reference in a new issue