This commit is contained in:
mark9064 2024-08-06 20:15:52 +10:00 committed by GitHub
commit 92d9ba7dc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 38 deletions

View file

@ -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;
} }

View file

@ -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;

View file

@ -67,40 +67,27 @@ 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)); uint8_t buf[8];
auto h = ReadRegister(static_cast<uint8_t>(Registers::C0DataH)); uint8_t base = static_cast<uint8_t>(Registers::C1dataM);
auto l = ReadRegister(static_cast<uint8_t>(Registers::C0dataL)); Hrs3300::PackedHrsAls res;
return ((l & 0x30) << 12) | (m << 8) | ((h & 0x0f) << 4) | (l & 0x0f); auto ret = twiMaster.Read(twiAddress, base, buf, 8);
} if (ret != TwiMaster::ErrorCodes::NoError) {
NRF_LOG_INFO("READ ERROR");
uint32_t Hrs3300::ReadAls() {
auto m = ReadRegister(static_cast<uint8_t>(Registers::C1dataM));
auto h = ReadRegister(static_cast<uint8_t>(Registers::C1dataH));
auto l = ReadRegister(static_cast<uint8_t>(Registers::C1dataL));
return ((h & 0x3f) << 11) | (m << 3) | (l & 0x07);
}
void Hrs3300::SetGain(uint8_t gain) {
constexpr uint8_t maxGain = 64U;
gain = std::min(gain, maxGain);
uint8_t hgain = 0;
while ((1 << hgain) < gain) {
++hgain;
} }
// hrs
uint8_t m = static_cast<uint8_t>(Registers::C0DataM) - base;
uint8_t h = static_cast<uint8_t>(Registers::C0DataH) - base;
uint8_t l = static_cast<uint8_t>(Registers::C0dataL) - base;
res.hrs = (buf[m] << 8) | ((buf[h] & 0x0f) << 4) | (buf[l] & 0x0f);
WriteRegister(static_cast<uint8_t>(Registers::Hgain), hgain << 2); // als
} m = static_cast<uint8_t>(Registers::C1dataM) - base;
h = static_cast<uint8_t>(Registers::C1dataH) - base;
l = static_cast<uint8_t>(Registers::C1dataL) - base;
res.als = ((buf[h] & 0x3f) << 11) | (buf[m] << 3) | (buf[l] & 0x07);
void Hrs3300::SetDrive(uint8_t drive) { return res;
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) {

View file

@ -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;

View file

@ -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)