#define BLINKER_WIFI
#include <Arduino.h>
#include <Blinker.h>
#include <TFT_eSPI.h>
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
MAX30105 particleSensor;
// 存储红外光和红光数据的缓冲区
uint32_t irBuffer[100];
uint32_t redBuffer[100];
// 测量得到的血氧饱和度和心率值
int32_t spo2;
int8_t validSPO2;
int32_t heartRate;
int8_t validHeartRate;
// 引脚定义
byte pulseLED = 21;
byte readLED = 22;

char auth[] = "74f197d54ce2";   //点灯科技 秘钥
char ssid[] = "webserver";      //输入你的WIFI名
char pswd[] = "www.360xyz.cn";  //输入你的WIFI密码

BlinkerNumber Number1("num-auq");  //心率
BlinkerNumber Number2("num-2lc");  //血氧

TFT_eSPI tft = TFT_eSPI();  //屏幕操作对象

void setup() {
  Serial.begin(115200);
  pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);
  pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);

  // 初始化传感器
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
    Serial.println("MAX30105 was not found. Please check wiring/power.");
    while (1)
      ;
  }
  // 设置传感器参数
  byte ledBrightness = 60;
  byte sampleAverage = 4;
  byte ledMode = 2;
  byte sampleRate = 100;
  int pulseWidth = 411;
  int adcRange = 4096;
  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);


  Blinker.begin(auth, ssid, pswd);   //连接WIFI
  Blinker.attachData(dataRead);      //绑定dataRead
  delay(500);

  tft.init();                    //屏幕初始化
  tft.fillScreen(TFT_BLACK);     //屏幕颜色
  tft.setTextColor(TFT_YELLOW);  //设置文本颜色为黄色
  //tft.setTextSize(1);            //设置文字的大小 (1~7)
}

void loop() {
  Blinker.run();

  // 读取 30 个样本
  int bufferLength = 30;
  for (byte i = 0; i < bufferLength; i++) {
    while (particleSensor.available() == false) {
      particleSensor.check();
    }
    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample();
    //Serial.print("red=");
    //Serial.print(redBuffer[i], DEC);
    //Serial.print(", ir=");
    //Serial.println(irBuffer[i], DEC);
  }
  // 计算血氧饱和度和心率
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  // 输出结果
  if (validHeartRate == 1 && validSPO2 == 1) {
    int32_t temHR = heartRate / 2.4;
    Serial.print("心率 ");
    Serial.print(temHR);
    Serial.print(", 血氧 ");
    Serial.println(spo2, DEC);

    Number1.print(temHR);
    Number2.print(spo2);
    tft.fillScreen(TFT_BLACK);
    tft.setCursor(30, 20, 2);             //设置起始坐标(10, 10)，1 号字M体
    tft.println("HR: " + String(temHR));  //显示文字

    tft.setCursor(30, 100, 2);  //设置起始坐标(10, 10)，1 号字体
    tft.println("S2: " + String(spo2));
  } else {
    Serial.println("检测失败,请手指放好位置");
    //Number1.print(0);
    //Number2.print(0);
    // tft.fillScreen(TFT_BLACK);
    // tft.setCursor(30, 20, 2);   //设置起始坐标(10, 10)，1 号字M体
    // tft.println("HR: 0");       //显示文字
    // tft.setCursor(30, 100, 2);  //设置起始坐标(10, 10)，1 号字体
    // tft.println("S2: 0");
  }
}

void dataRead(const String& data) {
  BLINKER_LOG("Blinker readString: ", data);
}
