setupDevice method

void setupDevice({
  1. double ledPower = 6.4,
  2. int sampleAverage = 4,
  3. int ledsEnabled = 2,
  4. int sampleRate = 100,
  5. int pulseWidth = 215,
  6. int adcRange = 16384,
  7. int timeoutMillis = 500,
})

Implementation

void setupDevice(
    {double ledPower = 6.4,
    int sampleAverage = 4,
    int ledsEnabled = 2,
    int sampleRate = 100,
    int pulseWidth = 215,
    int adcRange = 16384,
    int timeoutMillis = 500}) {
  softReset(timeoutMillis: timeoutMillis);

  int fifoConfigValue = readRegister('FIFO_CONFIG', true);
  fifoConfigValue = setBits(fifoConfigValue, 'FIFO_CONFIG', 'sample_average', sampleAverage);
  fifoConfigValue = setBits(fifoConfigValue, 'FIFO_CONFIG', 'fifo_rollover_en', true);
  writeRegister('FIFO_CONFIG', fifoConfigValue, true);

  //Check table 8 in datasheet on page 19. You can't just throw in sample rate and pulse width randomly. 100hz + 1600us is max for that resolution
  int spo2ConfigValue = readRegister('SPO2_CONFIG', true);
  spo2ConfigValue = setBits(spo2ConfigValue, 'SPO2_CONFIG', 'sample_rate_sps', sampleRate);
  spo2ConfigValue = setBits(spo2ConfigValue, 'SPO2_CONFIG', 'adc_range_nA', adcRange);
  spo2ConfigValue = setBits(spo2ConfigValue, 'SPO2_CONFIG', 'led_pw_us', pulseWidth);
  writeRegister('SPO2_CONFIG', spo2ConfigValue, true);

  int modeConfigValue = readRegister('MODE_CONFIG', true);
  modeConfigValue = setBits(modeConfigValue, 'MODE_CONFIG', 'mode', ledsEnabled);
  writeRegister('MODE_CONFIG', modeConfigValue, true);

  int ledModeValue = readRegister('LED_MODE_CONTROL_SLOTS_1_2', true);
  ledModeValue = setBits(ledModeValue, 'LED_MODE_CONTROL_SLOTS_1_2', 'slot1', 'red');
  if (ledsEnabled >= 2) {
    ledModeValue = setBits(ledModeValue, 'LED_MODE_CONTROL_SLOTS_1_2', 'slot2', 'ir');
  } else {
    ledModeValue = setBits(ledModeValue, 'LED_MODE_CONTROL_SLOTS_1_2', 'slot2', 'off');
  }
  writeRegister('LED_MODE_CONTROL_SLOTS_1_2', ledModeValue, true);

  if (ledsEnabled >= 3) {
    ledModeValue = readRegister('LED_MODE_CONTROL_SLOTS_3_4', true);
    ledModeValue = setBits(ledModeValue, 'LED_MODE_CONTROL_SLOTS_3_4', 'slot3', 'green');
    writeRegister('LED_MODE_CONTROL_SLOTS_3_4', ledModeValue, true);
  }

  lastREDLedCurrentCheck = 0;
  redLEDCurrent = startingRedLEDCurrent;
  irLEDCurrent = startingIRLEDCurrent;
  setLEDCurrents(redLEDCurrent, irLEDCurrent);

  clearFIFO(true);
}