#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#include <FastLED.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"


#define DHTPIN 4  // 数字引脚连接DHT传感器
#define DHTTYPE DHT11  // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
float humidity;
float temperature;

#define LED_PIN 18    // LED串连接的引脚
#define NUM_LEDS 16   // LED数量
CRGB leds[NUM_LEDS];  // 用于存储LED颜色数据的数组


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;
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);  // 初始化LED串

  Serial.begin(115200);
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);

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

void loop() {
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  } else {
    temperature = event.temperature;
  }
  delay(500);
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  } else {
    humidity = event.relative_humidity;
  }
  String th = String(temperature) + "," + String(humidity);
  Serial.println(th);
  if (temperature <= 5) {
    //紫色
    fill_solid(leds, NUM_LEDS, CRGB(CHSV(160, 255, 255)));
    FastLED.show();
    delay(100);
  } else {
    if (temperature <= 10) {
      //靛
      fill_solid(leds, NUM_LEDS, CRGB(CHSV(180, 255, 255)));
      FastLED.show();
      delay(100);
    } else {
      if (temperature <= 15) {
        //蓝
       fill_solid(leds, NUM_LEDS, CRGB(CHSV(192, 255, 255)));
        FastLED.show();
        delay(100);
      } else {
        if (temperature <= 20) {
          //绿
          fill_solid(leds, NUM_LEDS, CRGB(CHSV(96, 255, 255)));
          FastLED.show();
          delay(100);
        } else {
          if (temperature <= 25) {
            //黄
            fill_solid(leds, NUM_LEDS, CRGB(CHSV(32, 255, 255)));
            FastLED.show();
            delay(100);
          } else {
            if (temperature <= 30) {
              //橙
              fill_solid(leds, NUM_LEDS, CRGB(CHSV(24, 255, 255)));
              FastLED.show();
              delay(100);
            } else {
              //红
              fill_solid(leds, NUM_LEDS, CRGB(CHSV(0, 255, 255)));
              FastLED.show();
              delay(100);
            }
          }
        }
      }
    }
  }

  
  /////// 读取 30 个样本
  int bufferLength = 50;

  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);

  } else {
    Serial.println("检测失败,请手指放好位置");
    //Number1.print(0);
    //Number2.print(0);

  }
}
