app.c 5.8 KB

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