#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_SHT4x.h"

#define SCREEN_WIDTH 128 // OLED 显示屏的宽度，单位像素
#define SCREEN_HEIGHT 64 // OLED 显示屏的高度，单位像素
#define OLED_RESET     -1 // OLED 的重置引脚，不使用时设为 -1
#define SHT4x_Address 0x44

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

#define I2C_SDA 10
#define I2C_SCL 8

void setup() {
  // 初始化串口
  Serial.begin(115200);
  Wire.begin(I2C_SDA,I2C_SCL);          // Reset IIC SDA SCL

  while (!Serial)
    delay(10);     // will pause Zero, Leonardo, etc until serial console opens
  Serial.println("Adafruit SHT4x test");
  if (! sht4.begin()) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(1);
  }
  Serial.println("Found SHT4x sensor");
  Serial.print("Serial number 0x");
  Serial.println(sht4.readSerial(), HEX);

  // You can have 3 different precisions, higher precision takes longer
  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  switch (sht4.getPrecision()) {
     case SHT4X_HIGH_PRECISION: 
       Serial.println("High precision");
       break;
     case SHT4X_MED_PRECISION: 
       Serial.println("Med precision");
       break;
     case SHT4X_LOW_PRECISION: 
       Serial.println("Low precision");
       break;
  }

  // You can have 6 different heater settings
  // higher heat and longer times uses more power
  // and reads will take longer too!
  sht4.setHeater(SHT4X_NO_HEATER);
  switch (sht4.getHeater()) {
     case SHT4X_NO_HEATER: 
       Serial.println("No heater");
       break;
     case SHT4X_HIGH_HEATER_1S: 
       Serial.println("High heat for 1 second");
       break;
     case SHT4X_HIGH_HEATER_100MS: 
       Serial.println("High heat for 0.1 second");
       break;
     case SHT4X_MED_HEATER_1S: 
       Serial.println("Medium heat for 1 second");
       break;
     case SHT4X_MED_HEATER_100MS: 
       Serial.println("Medium heat for 0.1 second");
       break;
     case SHT4X_LOW_HEATER_1S: 
       Serial.println("Low heat for 1 second");
       break;
     case SHT4X_LOW_HEATER_100MS: 
       Serial.println("Low heat for 0.1 second");
       break;
  }
  // 初始化 OLED 显示屏
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C 是 OLED 的 I2C 地址
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // 进入无限循环
  }
  display.display(); // 显示启动画面
  delay(1000);
  display.setTextSize(1);   // Draw 1X-scale text
  display.setTextColor(SSD1306_WHITE); // Set OLED Text Color is White

}

void loop() {

  sensors_event_t humidity, temp;
  
  uint32_t timestamp = millis();
  // uint32_t temperature;
  // uint32_t humidity;
  sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  timestamp = millis() - timestamp;

  Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
  Serial.print("Read duration (ms): ");
  Serial.println(timestamp);

 // Display data on OLED
    display.clearDisplay();             // Clear OLED Display
    display.setTextSize(2);             // Set Text Size
    display.setCursor(0, 0);           // Set Text Cursor (x,y) x=width y=hight
    display.print("SHT4x ");
    display.printf("0x%02X", SHT4x_Address); // 格式化为两位十六进制数
    display.setTextSize(2);             // Set Text Size
    // display.print("Temperature: ");
    display.setCursor(0, 20);
    display.print("Tem");
    display.setCursor(40, 20);
    display.print(temp.temperature);
    display.print(" C");
    // display.print("Humidity: ");
    display.setCursor(0, 40);
    display.print("Hum");
    display.setCursor(40, 40);
    display.print(humidity.relative_humidity);
    display.print(" %");
    display.display();

  // 延时一段时间
  delay(2000);
}