#include "Save_Time.h"


void Time_Save(void)
{
    // 保存直排模式参数
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_PUMP_ON, (uint8_t*)&DIRECT_HIGH_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_PUMP_OFF, (uint8_t*)&DIRECT_HIGH_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_FAN_ON, (uint8_t*)&DIRECT_HIGH_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_FAN_OFF, (uint8_t*)&DIRECT_HIGH_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_LOW_FAN_ON, (uint8_t*)&DIRECT_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_LOW_FAN_OFF, (uint8_t*)&DIRECT_LOW_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_FIRST_LOW_ON, (uint8_t*)&DIRECT_FIRST_LOW_FAN_ON, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_DIRECT_FIRST_LOW_OFF, (uint8_t*)&DIRECT_FIRST_LOW_FAN_OFF, sizeof(uint32_t));
    
    // 保存冲厕模式参数
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_PUMP_ON, (uint8_t*)&FLUSH_HIGH_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_PUMP_OFF, (uint8_t*)&FLUSH_HIGH_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_FAN_ON, (uint8_t*)&FLUSH_HIGH_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_FAN_OFF, (uint8_t*)&FLUSH_HIGH_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_LOW_FAN_ON, (uint8_t*)&FLUSH_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_LOW_FAN_OFF, (uint8_t*)&FLUSH_LOW_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_FIRST_LOW_ON, (uint8_t*)&FLUSH_FIRST_LOW_FAN_ON, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_FLUSH_FIRST_LOW_OFF, (uint8_t*)&FLUSH_FIRST_LOW_FAN_OFF, sizeof(uint32_t));
    
    // 保存灌溉模式参数
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_PUMP_ON, (uint8_t*)&HIGH_LEVEL_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_PUMP_OFF, (uint8_t*)&HIGH_LEVEL_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_FAN_ON, (uint8_t*)&HIGH_LEVEL_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_FAN_OFF, (uint8_t*)&HIGH_LEVEL_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_LOW_FAN_ON, (uint8_t*)&LOW_LEVEL_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_LOW_FAN_OFF, (uint8_t*)&LOW_LEVEL_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_FIRST_LOW_ON, (uint8_t*)&FIRST_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_WriteBytes(TIME_ADDR_IRRIG_FIRST_LOW_OFF, (uint8_t*)&FIRST_LOW_FAN_OFF_TIME, sizeof(uint32_t));
}

void Time_Load(void)
{
    // 读取直排模式参数
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_PUMP_ON, (uint8_t*)&DIRECT_HIGH_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_PUMP_OFF, (uint8_t*)&DIRECT_HIGH_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_FAN_ON, (uint8_t*)&DIRECT_HIGH_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_FAN_OFF, (uint8_t*)&DIRECT_HIGH_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_LOW_FAN_ON, (uint8_t*)&DIRECT_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_LOW_FAN_OFF, (uint8_t*)&DIRECT_LOW_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_FIRST_LOW_ON, (uint8_t*)&DIRECT_FIRST_LOW_FAN_ON, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_DIRECT_FIRST_LOW_OFF, (uint8_t*)&DIRECT_FIRST_LOW_FAN_OFF, sizeof(uint32_t));
    
    // 读取冲厕模式参数
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_PUMP_ON, (uint8_t*)&FLUSH_HIGH_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_PUMP_OFF, (uint8_t*)&FLUSH_HIGH_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_FAN_ON, (uint8_t*)&FLUSH_HIGH_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_FAN_OFF, (uint8_t*)&FLUSH_HIGH_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_LOW_FAN_ON, (uint8_t*)&FLUSH_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_LOW_FAN_OFF, (uint8_t*)&FLUSH_LOW_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_FIRST_LOW_ON, (uint8_t*)&FLUSH_FIRST_LOW_FAN_ON, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_FLUSH_FIRST_LOW_OFF, (uint8_t*)&FLUSH_FIRST_LOW_FAN_OFF, sizeof(uint32_t));
    
    // 读取灌溉模式参数
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_PUMP_ON, (uint8_t*)&HIGH_LEVEL_PUMP_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_PUMP_OFF, (uint8_t*)&HIGH_LEVEL_PUMP_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_FAN_ON, (uint8_t*)&HIGH_LEVEL_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_FAN_OFF, (uint8_t*)&HIGH_LEVEL_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_LOW_FAN_ON, (uint8_t*)&LOW_LEVEL_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_LOW_FAN_OFF, (uint8_t*)&LOW_LEVEL_FAN_OFF_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_FIRST_LOW_ON, (uint8_t*)&FIRST_LOW_FAN_ON_TIME, sizeof(uint32_t));
    Inf_AT24C02_ReadBytes(TIME_ADDR_IRRIG_FIRST_LOW_OFF, (uint8_t*)&FIRST_LOW_FAN_OFF_TIME, sizeof(uint32_t));

    // 检查值是否有效，如果无效则设置默认值
    if(DIRECT_HIGH_PUMP_ON_TIME == 0xFFFFFFFF || DIRECT_HIGH_PUMP_ON_TIME == 0)
    {
        // 直排模式默认值
           DIRECT_HIGH_PUMP_ON_TIME      = 1;    // 1分钟
           DIRECT_HIGH_PUMP_OFF_TIME     = 150;  // 150分钟
           DIRECT_HIGH_FAN_ON_TIME       = 40;   // 40分钟
           DIRECT_HIGH_FAN_OFF_TIME      = 2;    // 2分钟
           DIRECT_LOW_FAN_ON_TIME        = 10;   // 10分钟
           DIRECT_LOW_FAN_OFF_TIME       = 20;   // 20分钟
           DIRECT_FIRST_LOW_FAN_ON       = 40;   // 40分钟
           DIRECT_FIRST_LOW_FAN_OFF      = 2;    // 2分钟
           
           // 冲厕模式默认值
           FLUSH_HIGH_PUMP_ON_TIME       = 1;    // 1分钟
           FLUSH_HIGH_PUMP_OFF_TIME      = 150;  // 150分钟
           FLUSH_HIGH_FAN_ON_TIME        = 40;   // 40分钟
           FLUSH_HIGH_FAN_OFF_TIME       = 2;    // 2分钟
           FLUSH_LOW_FAN_ON_TIME         = 5;    // 5分钟
           FLUSH_LOW_FAN_OFF_TIME        = 25;   // 25分钟
           FLUSH_FIRST_LOW_FAN_ON        = 40;   // 40分钟
           FLUSH_FIRST_LOW_FAN_OFF       = 2;    // 2分钟
           
           // 灌溉模式默认值
           HIGH_LEVEL_PUMP_ON_TIME       = 1;    // 1分钟
           HIGH_LEVEL_PUMP_OFF_TIME      = 120;  // 120分钟
           HIGH_LEVEL_FAN_ON_TIME        = 20;   // 20分钟
           HIGH_LEVEL_FAN_OFF_TIME       = 5;    // 5分钟
           FIRST_LOW_FAN_ON_TIME         = 10;   // 10分钟
           FIRST_LOW_FAN_OFF_TIME        = 10;   // 10分钟
           LOW_LEVEL_FAN_ON_TIME         = 5;    // 5分钟
           LOW_LEVEL_FAN_OFF_TIME        = 40;   // 40分钟
           
           // 特殊模式默认值
           SPECIAL_INITIAL_STOP_TIME     = 1440; // 24小时 = 1440分钟
           SPECIAL_FIRST_PHASE_TIME      = 2880; // 48小时 = 2880分钟
           SPECIAL_PUMP_RUN_TIME         = 1;    // 1分钟
           SPECIAL_FIRST_STOP_TIME       = 360;  // 360分钟
           SPECIAL_SECOND_PHASE_TIME     = 1;    // 1分钟（如需48小时请改为2880）
           SPECIAL_SECOND_STOP_TIME      = 240;  // 240分钟
        
       
        
        // 保存默认值到EEPROM
        Time_Save();
    }
}

