BH1790GLC Heart rate calculation on NANO 33 BLE

Hi,

I am trying to use the heart rate sensor BH1790 GLC on a nano 33 BLE. The problem is that the code given by the constructor (see code below) uses the flexitimer2 library that is not available on the nano 33 BLE. I am trying to find an alternative code without this library but nothing works.

Does anyone have an idea of how I can solve the problem?
Thanks in advance

#include <Wire.h>
#include <BH1790GLC.h>
#include <FlexiTimer2.h>

extern "C" {
#include <hr_bh1790.h>
}

#define SAMPLING_CNT_32HZ   (32)

BH1790GLC bh1790glc;
volatile bool timer_flg;
// void timer_isr(void);

static uint8_t    s_cnt_freq = 0;




void timer_isr(void) {
  timer_flg = true;
}

void setup() {

  uint16_t ret16 = ERROR_NONE;

  timer_flg = false;
  Serial.begin(115200);
  while (!Serial);

  Wire.begin();
  Wire.setClock(400000L);

  s_cnt_freq = 0;

  ret16 = hr_bh1790_Init();
  if (ret16 == ERROR_NONE) {
    Serial.println(F("BPM, wearing"));

    
    FlexiTimer2::stop();
    FlexiTimer2::set(250, 1.0/8000, timer_isr);  // 32Hz timer
    FlexiTimer2::start();
    
    

    ret16 = hr_bh1790_StartMeasure();
    if (ret16 != ERROR_NONE) {
      Serial.println(F("Error: hr_bh1790_StartMeasure function"));
      Serial.print(F("ret16 = "));
      Serial.println(ret16, HEX);      
    }
  } else {
    Serial.println(F("Error: hr_bh1790_Init function"));
    Serial.print(F("ret16 = "));
    Serial.println(ret16, HEX);
  }
}

void loop() {

  uint8_t  bpm     = 0U;
  uint8_t  wearing = 0U;
  uint16_t ret16   = ERROR_NONE;

  if (timer_flg) {
    ret16 = hr_bh1790_Calc(s_cnt_freq);
    if (ret16 == ERROR_NONE) {
      s_cnt_freq++;
      if (s_cnt_freq >= SAMPLING_CNT_32HZ) {
        s_cnt_freq = 0;
        hr_bh1790_GetData(&bpm, &wearing);
        Serial.print(bpm, DEC);
        Serial.print(F(","));
        Serial.println(wearing, DEC);
      }
    } else {
      Serial.println(F("Error: hr_bh1790_Calc function"));
      Serial.print(F("ret16 = "));
      Serial.println(ret16, HEX);
    }
    timer_flg = false;
  }
}

int8_t bh1790_Write(uint8_t adr, uint8_t *data, uint8_t size)
{
  byte   rc  = 0;
  int8_t ret = 0;
  
  rc = bh1790glc.write(adr, data, size);
  if (rc == 0) {
    ret = BH1790_RC_OK;
  } else {
    ret = BH1790_RC_I2C_ERR;
  }

  return (ret);
}

int8_t bh1790_Read(uint8_t adr, uint8_t *data, uint8_t size)
{
  byte   rc  = 0;
  int8_t ret = 0;

  rc = bh1790glc.read(adr, data, size);
  if (rc == 0) {
    ret = BH1790_RC_OK;
  } else {
    ret = BH1790_RC_I2C_ERR;
  }
  
  return (ret);
}

It looks like the timer library is used to set an interrupt flag for a 32 Hz measurement cycle time. If so, it is easy enough to replace all that using the built in timing function millis().

I would get rid of everything related to the timer library, declare a global unsigned long variable named last_timeout, and then replace this line:

 if (timer_flg) {

with something like:

if (millis() - last_timeout >= 31UL) {
     last_timeout = millis();

Use of micros() instead of millis() would lead to slightly higher accuracy.