app.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "app.h"
  2. #include "main.h"
  3. #include "usart.h"
  4. #include "sensor.h"
  5. #include "para.h"
  6. #include "rtc.h"
  7. #include "pm.h"
  8. #include "rtu.h"
  9. #include "wdt.h"
  10. #include "rec.h"
  11. #include "app_rtc.h"
  12. static void led_task(void)
  13. {
  14. static uint32_t ticks = 0;
  15. uint32_t now = HAL_GetTick();
  16. uint32_t diff = now - ticks;
  17. if (diff < 100)
  18. {
  19. HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_SET);
  20. HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_RESET);
  21. }
  22. else if (diff < 1000)
  23. {
  24. HAL_GPIO_WritePin(LED_SYS_GPIO_Port, LED_SYS_Pin, GPIO_PIN_RESET);
  25. HAL_GPIO_WritePin(LED_DBG_GPIO_Port, LED_DBG_Pin, GPIO_PIN_SET);
  26. }
  27. else
  28. {
  29. ticks = now;
  30. }
  31. }
  32. void app_init(void)
  33. {
  34. #ifdef __DBG_LOG__
  35. MX_USART1_UART_Init();
  36. #endif
  37. para_init();
  38. pm_init();
  39. wdt_init(30 * 1000);
  40. }
  41. static struct
  42. {
  43. int flag; // 上次光照强度是否大于阈值
  44. uint32_t start; // 模式开始时间
  45. } light_mode;
  46. int app_chk_rtu_work_condition(rtc_time_t *now)
  47. {
  48. uint32_t now_min = now->tm_hour * 60 + now->tm_min;
  49. // 检查参数是否合法
  50. if (para.photo_interval == 0)
  51. {
  52. return -1;
  53. }
  54. // 检查是否是0点整(00:00:xx)
  55. // 250930修改,解决0点时,rtu每分钟都会被唤醒的问题
  56. if (now->tm_hour == 0 && now->tm_min == 0 && now->tm_sec < PM_WUT_PERIOD)
  57. {
  58. light_mode.flag = 0;
  59. return 0;
  60. }
  61. if (para.mode == 0)
  62. {
  63. // 光控模式
  64. rec_data_t last_rec;
  65. // 检查光照强度
  66. if (rec_get_last(&last_rec) == 0)
  67. {
  68. if (last_rec.illuminance > 8000)
  69. {
  70. if (light_mode.flag == 0)
  71. {
  72. // 设置光照强度允许标记
  73. // 允许工作时长结束后清除或每天0点清除
  74. light_mode.flag = 1;
  75. light_mode.start = now->tm_hour * 60 + now->tm_min;
  76. }
  77. }
  78. }
  79. else
  80. {
  81. light_mode.flag = 0;
  82. }
  83. // 如果光照条件允许工作,检查时间条件是否符合要求
  84. if (light_mode.flag)
  85. {
  86. uint32_t allowed_end = light_mode.start + para.light_mode_hours * 60;
  87. // 防止跨天情况,简单限制最大值
  88. if (allowed_end > 1440)
  89. {
  90. allowed_end = 1440;
  91. }
  92. // 首先需要在允许的工作时长范围内
  93. if (now_min >= light_mode.start && now_min < allowed_end)
  94. {
  95. if (((now_min - light_mode.start) % para.photo_interval) == 0 &&
  96. now->tm_sec < PM_WUT_PERIOD)
  97. {
  98. return 1;
  99. }
  100. }
  101. }
  102. }
  103. else
  104. {
  105. // 时控模式
  106. // 启动条件
  107. // 1. 时间位于(para.time_mode_start, para.time_mode_end]
  108. // 2. 达到间隔时间要求
  109. uint32_t start_min, end_min;
  110. start_min = para.time_mode_start * 60;
  111. end_min = para.time_mode_end * 60 + 59;
  112. if (now_min > start_min && now_min <= end_min)
  113. {
  114. if ((now_min - start_min) % para.photo_interval == 0 &&
  115. now->tm_sec < PM_WUT_PERIOD)
  116. {
  117. return 1;
  118. }
  119. }
  120. }
  121. return -1;
  122. }
  123. void print_reset_source()
  124. {
  125. uint32_t reset_source = HAL_RCC_GetResetSource();
  126. if (reset_source == 0)
  127. {
  128. dbg_log("Reset: UNKNOWN (flags cleared or first boot)");
  129. return;
  130. }
  131. dbg_log("Reset source: 0x%08lX", (unsigned long)reset_source);
  132. }
  133. void app_run(void)
  134. {
  135. #ifndef __debug__
  136. uint32_t last_timestamp = 0;
  137. #endif
  138. #ifdef __DBG_LOG__
  139. print_reset_source();
  140. #endif
  141. while (1)
  142. {
  143. /*LL_USART_TransmitData8(COM_DBG, 0x01); //调试口发
  144. while (!LL_USART_IsActiveFlag_TC(COM_DBG));*/
  145. led_task();
  146. sensor_task();
  147. rtu_task();
  148. if (pm_get_wakeup_events() & PM_WAKUP_EVT_KEY)
  149. {
  150. pm_clr_wakeup_events(PM_WAKUP_EVT_KEY);
  151. // 按键唤醒,开始测试流程
  152. // 传感器采集一次数据
  153. // 开启rtu的工作流程
  154. sensor_init(0);
  155. rtu_init(2);
  156. }
  157. if (pm_get_wakeup_events() & PM_WAKUP_EVT_WUT)
  158. {
  159. /*LL_USART_TransmitData8(COM_DBG, 0x50); //调试口发
  160. while (!LL_USART_IsActiveFlag_TC(COM_DBG));*/
  161. rtc_time_t time;
  162. uint32_t timestamp;
  163. rtc_get_time(&time);
  164. timestamp = rtc_mktime(&time);
  165. time.tm_year = RTC_Bcd2ToByte(time.tm_year);
  166. time.tm_mon = RTC_Bcd2ToByte(time.tm_mon);
  167. time.tm_mday = RTC_Bcd2ToByte(time.tm_mday);
  168. time.tm_hour = RTC_Bcd2ToByte(time.tm_hour);
  169. time.tm_min = RTC_Bcd2ToByte(time.tm_min);
  170. time.tm_sec = RTC_Bcd2ToByte(time.tm_sec);
  171. pm_clr_wakeup_events(PM_WAKUP_EVT_WUT);
  172. #ifndef __debug__
  173. // 每个小时触发一次采集
  174. if (time.tm_min == 0 && time.tm_sec < PM_WUT_PERIOD)
  175. {
  176. // 启动传感器
  177. // 避免时间回调导致的重复记录
  178. if ((timestamp > last_timestamp) && (timestamp - last_timestamp > PM_WUT_PERIOD))
  179. {
  180. sensor_init(timestamp);
  181. last_timestamp = timestamp;
  182. }
  183. }
  184. // 检查是否需要启动RTU
  185. int reason = app_chk_rtu_work_condition(&time);
  186. if (reason >= 0)
  187. {
  188. rtu_init(reason);
  189. }
  190. else
  191. {
  192. // 10:00:xx强制唤醒,防止时控或光控模式时间段没有覆盖10点窗口期
  193. if(time.tm_hour == 10 && time.tm_min == 0 && time.tm_sec < PM_WUT_PERIOD)
  194. {
  195. rtu_init(2);
  196. }
  197. }
  198. #else
  199. // 调试模式下每个唤醒周期都采集一次数据
  200. sensor_init(timestamp);
  201. // 调试模式下每5min上报一次数据
  202. if ((time.tm_min % 5) == 0 && time.tm_sec < PM_WUT_PERIOD)
  203. {
  204. // 上报数据
  205. rtu_init(1);
  206. }
  207. #endif
  208. }
  209. wdt_feed();
  210. pm_enter_low_power_mode();
  211. }
  212. }