#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>

// 定义 LCD2004 地址 (通常是 0x27 或 0x3F)
LiquidCrystal_I2C lcd(0x27, 20, 4);

// DS18B20 连接引脚
#define ONE_WIRE_BUS PA0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// 继电器控制引脚
#define RELAY_PIN PA4

// 按键引脚
#define BTN_SELECT PA1  // 选择调整 a 或 b
#define BTN_UP PA2      // 增加数值
#define BTN_DOWN PA3    // 减少数值  

// 阈值变量 (a: 开启温差, b: 关闭温差)
float thresholdA = 5.0;
float thresholdB = 2.0;
bool adjustingA = true;  // 默认调整 thresholdA

// EEPROM 存储地址
#define EEPROM_ADDR_A 0
#define EEPROM_ADDR_B 4

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

    sensors.begin();
    
    pinMode(RELAY_PIN, OUTPUT);
    pinMode(BTN_SELECT, INPUT_PULLUP);
    pinMode(BTN_UP, INPUT_PULLUP);
    pinMode(BTN_DOWN, INPUT_PULLUP);

    EEPROM.get(EEPROM_ADDR_A, thresholdA);
    EEPROM.get(EEPROM_ADDR_B, thresholdB);

    if (thresholdA < 0 || thresholdA > 50) thresholdA = 5.0;
    if (thresholdB < 0 || thresholdB > 50) thresholdB = 2.0;
}

void loop() {
    sensors.requestTemperatures();
    float temp1 = sensors.getTempCByIndex(0);  // 探头 1
    float temp2 = sensors.getTempCByIndex(1);  // 探头 2

    float deltaT = temp1 - temp2;

    if (deltaT > thresholdA) {
        digitalWrite(RELAY_PIN, HIGH);
    } else if (deltaT < thresholdB) {
        digitalWrite(RELAY_PIN, LOW);
    }

    handleButtonInput();
    updateLCD(temp1, temp2, deltaT);

    delay(500);
}

void handleButtonInput() {
    static unsigned long lastDebounceTime = 0;
    if (millis() - lastDebounceTime < 200) return;

    if (digitalRead(BTN_SELECT) == LOW) {
        adjustingA = !adjustingA;
        delay(200);
    }

    if (digitalRead(BTN_UP) == LOW) {
        if (adjustingA) {
            thresholdA += 0.5;
            EEPROM.put(EEPROM_ADDR_A, thresholdA);
        } else {
            thresholdB += 0.5;
            EEPROM.put(EEPROM_ADDR_B, thresholdB);
        }
        delay(200);
    }

    if (digitalRead(BTN_DOWN) == LOW) {
        if (adjustingA) {
            thresholdA -= 0.5;
            EEPROM.put(EEPROM_ADDR_A, thresholdA);
        } else {
            thresholdB -= 0.5;
            EEPROM.put(EEPROM_ADDR_B, thresholdB);
        }
        delay(200);
    }
    
    lastDebounceTime = millis();
}

void updateLCD(float temp1, float temp2, float deltaT) {
    lcd.clear();
    
    lcd.setCursor(0, 0);
    lcd.print("T1: ");
    lcd.print(temp1, 1);
    lcd.print("C");
    
    lcd.setCursor(10, 0);
    lcd.print("T2: ");
    lcd.print(temp2, 1);
    lcd.print("C");

    lcd.setCursor(0, 1);
    lcd.print("dT: ");
    lcd.print(deltaT, 1);
    lcd.print("C");

    lcd.setCursor(10, 1);
    lcd.print(adjustingA ? "A: " : "B: ");
    lcd.print(adjustingA ? thresholdA : thresholdB, 1);
    lcd.print("C");

    lcd.setCursor(0, 2);
    lcd.print("Relay: ");
    lcd.print(digitalRead(RELAY_PIN) ? "ON " : "OFF");

    lcd.setCursor(0, 3);
    lcd.print("BTN: SEL UP DOWN");
}
nucleo:D1
nucleo:D0
nucleo:RST.1
nucleo:GND.1
nucleo:D2
nucleo:D3
nucleo:D4
nucleo:D5
nucleo:D6
nucleo:D7
nucleo:D8
nucleo:D9
nucleo:D10
nucleo:D11
nucleo:D12
nucleo:VIN
nucleo:GND.2
nucleo:RST.2
nucleo:5V
nucleo:A7
nucleo:A6
nucleo:A5
nucleo:A4
nucleo:A3
nucleo:A2
nucleo:A1
nucleo:A0
nucleo:REF
nucleo:3V3
nucleo:D13
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
led1:A
led1:C
led2:A
led2:C
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
temp1:GND
temp1:DQ
temp1:VCC
temp2:GND
temp2:DQ
temp2:VCC
r1:1
r1:2
r2:1
r2:2
vcc1:VCC
gnd1:GND
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL