You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
11 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdbool.h> //定义布尔
  2. #include <string.h>
  3. #include "board.h"
  4. //
  5. #include "zes8p5066lib/basic.h"
  6. #include "zes8p5066lib/gpio.h"
  7. #include "zes8p5066lib/key.h"
  8. #include "zes8p5066lib/systicket.h"
  9. #include "zes8p5066lib/uart0.h"
  10. //
  11. #include "service/human_computer_interaction_service.h"
  12. #include "service/ozone_control_service.h"
  13. #include "service/thisdevice.h"
  14. #include "test.h"
  15. #include "zsimple_timer/zsimple_timer.h"
  16. /***********************************************************************************************************************
  17. * =========================================================================================================== *
  18. ***********************************************************************************************************************/
  19. void onkey(zkey_t* key, zkey_state_t key_state);
  20. /***********************************************************************************************************************
  21. * =========================================================================================================== *
  22. ***********************************************************************************************************************/
  23. static zkey_t s_keys[] = {
  24. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  25. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  26. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  27. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  28. };
  29. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  30. /***********************************************************************************************************************
  31. * =============================================================================================================== *
  32. ***********************************************************************************************************************/
  33. /***********************************************************************************************************************
  34. * ===================================================COUNT_COMPUTE=================================================== *
  35. ***********************************************************************************************************************/
  36. static uint32_t compute_countdown_num(int countdowns) {
  37. if (countdowns) return countdowns / kconst_countdown_step_s + !!(countdowns % kconst_countdown_step_s);
  38. }
  39. static void increase_and_assign_countdonwnum() {
  40. if (thisDevice.countdonwnum == 4) {
  41. thisDevice.countdonwnum = 0;
  42. thisDevice.countdonwnum_s = 0;
  43. thisDevice.countdonw_setting_num = 0;
  44. } else {
  45. thisDevice.countdonwnum_s = (thisDevice.countdonwnum + 1) * kconst_countdown_step_s;
  46. thisDevice.countdonwnum = thisDevice.countdonwnum + 1;
  47. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  48. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  49. }
  50. }
  51. static void mf_set_countdown(int countdownnum) {
  52. thisDevice.countdonwnum = countdownnum;
  53. thisDevice.countdonwnum_s = thisDevice.countdonwnum * kconst_countdown_step_s;
  54. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  55. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  56. }
  57. /***********************************************************************************************************************
  58. * ===================================================POWER_CONTROL=================================================== *
  59. ***********************************************************************************************************************/
  60. //设备开始工作
  61. static void startwork() {
  62. port_fan_set(true);
  63. ozone_control_start();
  64. thisDevice.working = true;
  65. }
  66. //停止设备工作
  67. static void stopwork() {
  68. ozone_control_stop();
  69. port_fan_set(false);
  70. thisDevice.working = false;
  71. }
  72. //关机
  73. static void shutdwon() {
  74. printf("power off\n");
  75. thisDevice.poweron = false;
  76. stopwork();
  77. }
  78. //开机
  79. static void poweron() {
  80. printf("power on\n");
  81. thisDevice.poweron = true;
  82. thisDevice.level = klevel1;
  83. thisDevice.mode = knormal;
  84. startwork();
  85. }
  86. /***********************************************************************************************************************
  87. * =================================================KEY_EVENT_PROCESS================================================= *
  88. ***********************************************************************************************************************/
  89. //电源按键处理
  90. static void mf_process_poweron_key(zkey_t* key) {
  91. printf("on %s \n", key->name);
  92. if (!thisDevice.poweron) {
  93. poweron();
  94. } else {
  95. shutdwon();
  96. }
  97. return;
  98. }
  99. //等级按键处理
  100. static void mf_process_level_key(zkey_t* key) {
  101. if (!thisDevice.poweron) return;
  102. printf("on %s \n", key->name);
  103. /**
  104. * @brief
  105. */
  106. if (thisDevice.level == klevel1) {
  107. printf("changet level to level2\n");
  108. thisDevice.level = klevel2;
  109. //更改臭氧状态
  110. } else if (thisDevice.level == klevel2) {
  111. printf("changet level to level1\n");
  112. thisDevice.level = klevel1;
  113. //更改臭氧状态
  114. }
  115. hcis_active_input(kchange_level_input);
  116. return;
  117. }
  118. //定时按键处理
  119. static void mf_process_timer_key(zkey_t* key) {
  120. if (!thisDevice.poweron) return;
  121. printf("on %s \n", key->name);
  122. if (!thisDevice.working) {
  123. startwork();
  124. }
  125. if (thisDevice.mode != ktimingMode) {
  126. mf_set_countdown(0);
  127. }
  128. increase_and_assign_countdonwnum();
  129. if (thisDevice.countdonwnum_s == 0) {
  130. hcis_active_input(knone_active);
  131. thisDevice.mode = knormal;
  132. } else {
  133. hcis_active_input(kchange_countdonw_time_input);
  134. thisDevice.mode = ktimingMode;
  135. }
  136. }
  137. //间隔按键处理
  138. static void mf_process_interval_key(zkey_t* key) {
  139. if (!thisDevice.poweron) return;
  140. printf("on %s \n", key->name);
  141. if (!thisDevice.working) {
  142. startwork();
  143. }
  144. if (thisDevice.mode != kintermittentMode) {
  145. mf_set_countdown(0);
  146. }
  147. increase_and_assign_countdonwnum();
  148. if (thisDevice.countdonwnum_s == 0) {
  149. hcis_active_input(knone_active);
  150. thisDevice.mode = knormal;
  151. } else {
  152. hcis_active_input(kchange_countdonw_time_input);
  153. thisDevice.mode = kintermittentMode;
  154. }
  155. }
  156. static void onkey(zkey_t* key, zkey_state_t key_state) {
  157. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  158. mf_process_poweron_key(key);
  159. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  160. mf_process_level_key(key);
  161. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  162. mf_process_timer_key(key);
  163. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  164. mf_process_interval_key(key);
  165. }
  166. }
  167. /***********************************************************************************************************************
  168. * ===============================================THIS_MODULE_SCHEDULE================================================ *
  169. ***********************************************************************************************************************/
  170. static void mf_try_autoshutdown() {
  171. if (thisDevice.poweron) {
  172. if (thisDevice.countdonwnum_s == 0) {
  173. shutdwon();
  174. }
  175. }
  176. }
  177. static void mf_try_autostop() {
  178. //间歇模式下,定时到达定时时间,停止工作
  179. if (thisDevice.countdonwnum_s == 0) stopwork();
  180. }
  181. static void mf_try_auto_restart() {
  182. //间歇模式下,设备工作一定时间后,休息一定时间
  183. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > //
  184. (/*设备工作时间:*/ thisDevice.countdonw_setting_num * kconst_countdown_step_s + //
  185. /*设备休息时间*/ thisDevice.countdonw_setting_num * kconst_countdown_step_s)) {
  186. mf_set_countdown(thisDevice.countdonw_setting_num);
  187. startwork();
  188. }
  189. }
  190. static void mf_update_countdown() {
  191. if /* */ (thisDevice.mode == knormal) {
  192. thisDevice.countdonwnum = 0;
  193. thisDevice.countdonwnum_s = 0;
  194. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  195. if (thisDevice.countdonwnum_s != 0) {
  196. thisDevice.countdonwnum_s = //
  197. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  198. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  199. }
  200. }
  201. }
  202. void this_module_schedule() {
  203. /**
  204. * @brief
  205. */
  206. if (thisDevice.mode == ktimingMode) {
  207. //定时自动关机
  208. mf_update_countdown();
  209. mf_try_autoshutdown();
  210. }
  211. /**
  212. * @brief ,
  213. */
  214. if (thisDevice.mode == kintermittentMode) {
  215. mf_update_countdown();
  216. if (thisDevice.working) {
  217. mf_try_autostop();
  218. } else {
  219. mf_try_auto_restart();
  220. }
  221. }
  222. };
  223. /***********************************************************************************************************************
  224. * =======================================================MAIN======================================================== *
  225. ***********************************************************************************************************************/
  226. static void mf_do_debug_light_state() {
  227. static uint8_t debug_led_state = 1;
  228. debug_led_state = !debug_led_state;
  229. port_debug_set(debug_led_state);
  230. }
  231. static void mf_init_all_subdevice_state() {
  232. port_debug_set(false);
  233. port_fan_set(false);
  234. port_led0_set(false);
  235. port_led1_set(false);
  236. port_led2_set(false);
  237. port_led3_set(false);
  238. port_led_r_set(false);
  239. port_led_g_set(false);
  240. port_led_b_set(false);
  241. }
  242. void WDTInit(void) {
  243. IWDT_InitStruType x;
  244. x.WDT_Tms = 10000;
  245. x.WDT_IE = Enable; /* IWDT中断使能 */
  246. x.WDT_Rst = Enable; /* IWDT复位使能 */
  247. x.WDT_Clock = IWDT_CLOCK_WDT; /* LRC */
  248. IWDT_Init(&x);
  249. /* 使能IWDT */
  250. IWDT_Enable();
  251. }
  252. int main(void) {
  253. SystemInit(); //配置系统时钟
  254. DeviceClockAllEnable(); //打开所有外设时钟
  255. systicket_init();
  256. /*系统初始化*/
  257. zgpio_init_all_gpio(); //
  258. port_init(); //
  259. mf_init_all_subdevice_state(); //
  260. printf("==========OZONE_GENERATOR==========\n"); //
  261. printf("= manufactor: iflytop\n"); //
  262. printf("= version : %s\n", VERSION); //
  263. printf("=\n"); //
  264. WDTInit();
  265. /*组件初始化*/
  266. zkey_init(&key_module); //按键初始化
  267. ozone_control_init();
  268. /**
  269. * @brief
  270. * 20k起步50hz100ms计算功率
  271. */
  272. while (true) {
  273. //按键扫描逻辑
  274. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  275. END();
  276. //调试指示灯
  277. DO_IT_EACH_MS(150) { mf_do_debug_light_state(); }
  278. END();
  279. //臭氧控制逻辑Schedule
  280. ozone_control_schedule();
  281. //人机交互逻辑Schedule
  282. hcis_shcedule();
  283. //当前模块逻辑Schedule
  284. this_module_schedule();
  285. //喂狗
  286. if (0x01 == IWDT_GetFlagStatus()) IWDT_Clear();
  287. }
  288. }