#include "app.h" #include "main.h" #include "usart.h" #include "sensor.h" #include "para.h" #include "rtc.h" #include "pm.h" #include "rtu.h" #include "wdt.h" #include "rec.h" #include "app_rtc.h" static void led_task(void) { static uint32_t ticks = 0; uint32_t now = HAL_GetTick(); uint32_t diff = now - ticks; if (diff < 100) { HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_RESET); } else if (diff < 1000) { HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_SET); } else { ticks = now; } } void app_init(void) { #ifdef __DBG_LOG__ MX_USART1_UART_Init(); #endif para_init(); pm_init(); wdt_init(30 * 1000); } static struct { int flag; // 上次光照强度是否大于阈值 uint32_t start; // 模式开始时间 } light_mode; int app_chk_rtu_work_condition(rtc_time_t *now) { uint32_t now_min = now->tm_hour * 60 + now->tm_min; // 检查参数是否合法 if (para.photo_interval == 0) { return -1; } // 检查是否是0点整(00:00:xx) // 250930修改,解决0点时,rtu每分钟都会被唤醒的问题 if (now->tm_hour == 0 && now->tm_min == 0 && now->tm_sec < PM_WUT_PERIOD) { light_mode.flag = 0; return 0; } if (para.mode == 0) { // 光控模式 rec_data_t last_rec; // 检查光照强度 if (rec_get_last(&last_rec) == 0) { if (last_rec.illuminance > 8000) { if (light_mode.flag == 0) { // 设置光照强度允许标记 // 允许工作时长结束后清除或每天0点清除 light_mode.flag = 1; light_mode.start = now->tm_hour * 60 + now->tm_min; } } } else { light_mode.flag = 0; } // 如果光照条件允许工作,检查时间条件是否符合要求 if (light_mode.flag) { uint32_t allowed_end = light_mode.start + para.light_mode_hours * 60; // 防止跨天情况,简单限制最大值 if (allowed_end > 1440) { allowed_end = 1440; } // 首先需要在允许的工作时长范围内 if (now_min >= light_mode.start && now_min < allowed_end) { if (((now_min - light_mode.start) % para.photo_interval) == 0 && now->tm_sec < PM_WUT_PERIOD) { return 1; } } } } else { // 时控模式 // 启动条件 // 1. 时间位于(para.time_mode_start, para.time_mode_end] // 2. 达到间隔时间要求 uint32_t start_min, end_min; start_min = para.time_mode_start * 60; end_min = para.time_mode_end * 60 + 59; if (now_min > start_min && now_min <= end_min) { if ((now_min - start_min) % para.photo_interval == 0 && now->tm_sec < PM_WUT_PERIOD) { return 1; } } } return -1; } void print_reset_source() { uint32_t reset_source = HAL_RCC_GetResetSource(); if (reset_source == 0) { dbg_log("Reset: UNKNOWN (flags cleared or first boot)"); return; } dbg_log("Reset source: 0x%08lX", (unsigned long)reset_source); } void app_run(void) { #ifndef __debug__ uint32_t last_timestamp = 0; #endif #ifdef __DBG_LOG__ print_reset_source(); #endif while (1) { /*LL_USART_TransmitData8(COM_DBG, 0x01); //调试口发 while (!LL_USART_IsActiveFlag_TC(COM_DBG));*/ led_task(); sensor_task(); rtu_task(); if (pm_get_wakeup_events() & PM_WAKUP_EVT_KEY) { pm_clr_wakeup_events(PM_WAKUP_EVT_KEY); // 按键唤醒,开始测试流程 // 传感器采集一次数据 // 开启rtu的工作流程 sensor_init(0); rtu_init(2); } if (pm_get_wakeup_events() & PM_WAKUP_EVT_WUT) { /*LL_USART_TransmitData8(COM_DBG, 0x50); //调试口发 while (!LL_USART_IsActiveFlag_TC(COM_DBG));*/ rtc_time_t time; uint32_t timestamp; rtc_get_time(&time); timestamp = rtc_mktime(&time); time.tm_year = RTC_Bcd2ToByte(time.tm_year); time.tm_mon = RTC_Bcd2ToByte(time.tm_mon); time.tm_mday = RTC_Bcd2ToByte(time.tm_mday); time.tm_hour = RTC_Bcd2ToByte(time.tm_hour); time.tm_min = RTC_Bcd2ToByte(time.tm_min); time.tm_sec = RTC_Bcd2ToByte(time.tm_sec); pm_clr_wakeup_events(PM_WAKUP_EVT_WUT); #ifndef __debug__ // 每个小时触发一次采集 if (time.tm_min == 0 && time.tm_sec < PM_WUT_PERIOD) { // 启动传感器 // 避免时间回调导致的重复记录 if ((timestamp > last_timestamp) && (timestamp - last_timestamp > PM_WUT_PERIOD)) { sensor_init(timestamp); last_timestamp = timestamp; } } // 检查是否需要启动RTU int reason = app_chk_rtu_work_condition(&time); if (reason >= 0) { rtu_init(reason); } else { // 10:00:xx强制唤醒,防止时控或光控模式时间段没有覆盖10点窗口期 if(time.tm_hour == 10 && time.tm_min == 0 && time.tm_sec < PM_WUT_PERIOD) { rtu_init(2); } } #else // 调试模式下每个唤醒周期都采集一次数据 sensor_init(timestamp); // 调试模式下每5min上报一次数据 if ((time.tm_min % 5) == 0 && time.tm_sec < PM_WUT_PERIOD) { // 上报数据 rtu_init(1); } #endif } wdt_feed(); pm_enter_low_power_mode(); } }