app.c 5.7 KB

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