#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <LiquidCrystal_I2C.h>

#define REPORTING_PERIOD_MS     1000

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

// Create a PulseOximeter object
PulseOximeter pox;

// Time at which the last beat occurred
uint32_t tsLastReport = 0;

// Callback routine is executed when a pulse is detected
void onBeatDetected() {
    Serial.println("Beat!");
  lcd.print("Beat");
}

void setup() {
    lcd.init();
  lcd.backlight();.  lcd.setCursor(0, 0);

    Serial.begin(9600);

    Serial.print("Initializing pulse oximeter..");
  lcd.print("Initializing pulse oximeter..");

    // Initialize sensor
    if (!pox.begin()) {
        lcd.println("FAILED");
        for(;;);
    } else {
        lcd.println("SUCCESS");
    }

	// Configure sensor to use 7.6mA for LED drive
	pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);

    // Register a callback routine
    pox.setOnBeatDetectedCallback(onBeatDetected);
}

void loop() {
    // Read from the sensor
    pox.update();

    // Grab the updated heart rate and SpO2 levels
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        lcd.print("Heart rate:");
        lcd.print(pox.getHeartRate());
        lcd.print("bpm / SpO2:");
        lcd.print(pox.getSpO2());
        lcd.println("%");

        tsLastReport = millis();
    }
}