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

#define REPORTING_PERIOD_MS     1000

PulseOximeter pox;
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

uint32_t tsLastReport = 0;

void onBeatDetected()
{
    Serial.println("Urganish!");
}

void setup()
{
    Serial.begin(115200);
    lcd.init();                      // initialize the lcd 
    lcd.backlight();

    if (!pox.begin()) {
        Serial.println("Puls oksimetri sensorini boshlashda xatolik");
        lcd.print("Puls oksimetri sensorini boshlashda xatolik");
    }

    pox.setOnBeatDetectedCallback(onBeatDetected);

    pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
}

void loop()
{
    pox.update();

    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Yurak tezligi:");
        lcd.print(pox.getHeartRate());
        lcd.setCursor(0,1);
        lcd.print("SpO2:");
        lcd.print(pox.getSpO2());
        lcd.print("%");

        tsLastReport = millis();
    }
}