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.

287 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
  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. increase_and_assign_countdonwnum();
  126. if (thisDevice.countdonwnum_s == 0) {
  127. hcis_active_input(knone_active);
  128. thisDevice.mode = knormal;
  129. } else {
  130. hcis_active_input(kchange_countdonw_time_input);
  131. thisDevice.mode = ktimingMode;
  132. }
  133. }
  134. //间隔按键处理
  135. static void mf_process_interval_key(zkey_t* key) {
  136. if (!thisDevice.poweron) return;
  137. printf("on %s \n", key->name);
  138. if (!thisDevice.working) {
  139. startwork();
  140. }
  141. increase_and_assign_countdonwnum();
  142. if (thisDevice.countdonwnum_s == 0) {
  143. hcis_active_input(knone_active);
  144. thisDevice.mode = knormal;
  145. } else {
  146. hcis_active_input(kchange_countdonw_time_input);
  147. thisDevice.mode = kintermittentMode;
  148. }
  149. }
  150. static void onkey(zkey_t* key, zkey_state_t key_state) {
  151. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  152. mf_process_poweron_key(key);
  153. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  154. mf_process_level_key(key);
  155. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  156. mf_process_timer_key(key);
  157. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  158. mf_process_interval_key(key);
  159. }
  160. }
  161. /***********************************************************************************************************************
  162. * ===============================================THIS_MODULE_SCHEDULE================================================ *
  163. ***********************************************************************************************************************/
  164. static void mf_try_autoshutdown() {
  165. if (thisDevice.poweron) {
  166. if (thisDevice.countdonwnum_s == 0) {
  167. shutdwon();
  168. }
  169. }
  170. }
  171. static void mf_try_autostop() {
  172. //间歇模式下,定时到达定时时间,停止工作
  173. if (thisDevice.countdonwnum_s == 0) stopwork();
  174. }
  175. static void mf_try_auto_restart() {
  176. //间歇模式下,再次到达工作时间,再次工作
  177. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > INTERMITTENTMODE_PERIOD_S) {
  178. mf_set_countdown(thisDevice.countdonw_setting_num);
  179. startwork();
  180. }
  181. }
  182. static void mf_update_countdown() {
  183. if /* */ (thisDevice.mode == knormal) {
  184. thisDevice.countdonwnum = 0;
  185. thisDevice.countdonwnum_s = 0;
  186. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  187. if (thisDevice.countdonwnum_s != 0) {
  188. thisDevice.countdonwnum_s = //
  189. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  190. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  191. }
  192. }
  193. }
  194. void this_module_schedule() {
  195. /**
  196. * @brief
  197. */
  198. if (thisDevice.mode == ktimingMode) {
  199. //定时自动关机
  200. mf_update_countdown();
  201. mf_try_autoshutdown();
  202. }
  203. /**
  204. * @brief ,
  205. */
  206. if (thisDevice.mode == kintermittentMode) {
  207. mf_update_countdown();
  208. if (thisDevice.working) {
  209. mf_try_autostop();
  210. } else {
  211. mf_try_auto_restart();
  212. }
  213. }
  214. };
  215. /***********************************************************************************************************************
  216. * =======================================================MAIN======================================================== *
  217. ***********************************************************************************************************************/
  218. static void mf_do_debug_light_state() {
  219. static uint8_t debug_led_state = 1;
  220. debug_led_state = !debug_led_state;
  221. port_debug_set(debug_led_state);
  222. }
  223. static void mf_init_all_subdevice_state() {
  224. port_debug_set(false);
  225. port_fan_set(false);
  226. port_led0_set(false);
  227. port_led1_set(false);
  228. port_led2_set(false);
  229. port_led3_set(false);
  230. port_led_r_set(false);
  231. port_led_g_set(false);
  232. port_led_b_set(false);
  233. }
  234. int main(void) {
  235. SystemInit(); //配置系统时钟
  236. DeviceClockAllEnable(); //打开所有外设时钟
  237. systicket_init();
  238. /*系统初始化*/
  239. zgpio_init_all_gpio(); //
  240. port_init(); //
  241. mf_init_all_subdevice_state(); //
  242. printf("==========OZONE_GENERATOR==========\n"); //
  243. printf("= manufactor: iflytop\n"); //
  244. printf("= version : %s\n", VERSION); //
  245. printf("=\n"); //
  246. /*组件初始化*/
  247. zkey_init(&key_module); //按键初始化
  248. ozone_control_init();
  249. /**
  250. * @brief
  251. * 20k起步50hz100ms计算功率
  252. */
  253. while (true) {
  254. //按键扫描逻辑
  255. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  256. END();
  257. //调试指示灯
  258. DO_IT_EACH_MS(150) { mf_do_debug_light_state(); }
  259. END();
  260. //臭氧控制逻辑Schedule
  261. ozone_control_schedule();
  262. //人机交互逻辑Schedule
  263. hcis_shcedule();
  264. //当前模块逻辑Schedule
  265. this_module_schedule();
  266. }
  267. }