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.

259 lines
8.8 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
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. static uint32_t compute_countdown_num(int countdowns) {
  34. if (countdowns) return countdowns / kconst_countdown_step_s + !!(countdowns % kconst_countdown_step_s);
  35. }
  36. static void startwork() {
  37. port_fan_set(true);
  38. ozone_control_start();
  39. thisDevice.working = true;
  40. }
  41. static void stopwork() {
  42. ozone_control_stop();
  43. port_fan_set(false);
  44. thisDevice.working = false;
  45. }
  46. static void shutdwon() {
  47. printf("power off\n");
  48. thisDevice.poweron = false;
  49. stopwork();
  50. }
  51. static void increase_and_assign_countdonwnum() {
  52. if (thisDevice.countdonwnum == 4) {
  53. thisDevice.countdonwnum = 0;
  54. thisDevice.countdonwnum_s = 0;
  55. thisDevice.countdonw_setting_num = 0;
  56. hcis_active_input(knone_active);
  57. thisDevice.mode = knormal;
  58. } else {
  59. thisDevice.countdonwnum_s = (thisDevice.countdonwnum + 1) * kconst_countdown_step_s;
  60. thisDevice.countdonwnum = thisDevice.countdonwnum + 1;
  61. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  62. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  63. }
  64. }
  65. static void set_countdown(int countdownnum) {
  66. thisDevice.countdonwnum = countdownnum;
  67. thisDevice.countdonwnum_s = thisDevice.countdonwnum * kconst_countdown_step_s;
  68. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  69. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  70. }
  71. static void onkey(zkey_t* key, zkey_state_t key_state) {
  72. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  73. printf("on %s \n", key->name);
  74. if (!thisDevice.poweron) {
  75. printf("power on\n");
  76. thisDevice.poweron = true;
  77. thisDevice.level = klevel1;
  78. thisDevice.mode = knormal;
  79. startwork();
  80. } else {
  81. shutdwon();
  82. }
  83. return;
  84. }
  85. // levelkey
  86. if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  87. if (!thisDevice.poweron) return;
  88. printf("on %s \n", key->name);
  89. /**
  90. * @brief
  91. */
  92. if (!hcis_input_is_active(kchange_level_input)) {
  93. hcis_active_input(kchange_level_input);
  94. return;
  95. }
  96. if (thisDevice.level == klevel1) {
  97. printf("changet level to level2\n");
  98. thisDevice.level = klevel2;
  99. //更改臭氧状态
  100. } else if (thisDevice.level == klevel2) {
  101. printf("changet level to level1\n");
  102. thisDevice.level = klevel1;
  103. //更改臭氧状态
  104. }
  105. hcis_active_input(kchange_level_input);
  106. return;
  107. }
  108. // timerkey
  109. if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  110. if (!thisDevice.poweron) return;
  111. printf("on %s \n", key->name);
  112. port_fan_set(true);
  113. // TODO:启动臭氧
  114. if (!hcis_input_is_active(kchange_countdonw_time_input)) {
  115. hcis_active_input(kchange_countdonw_time_input);
  116. if (thisDevice.mode != ktimingMode || thisDevice.countdonwnum_s == 0) {
  117. set_countdown(1);
  118. thisDevice.mode = ktimingMode;
  119. }
  120. return;
  121. }
  122. hcis_active_input(kchange_countdonw_time_input);
  123. increase_and_assign_countdonwnum();
  124. }
  125. if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  126. if (!thisDevice.poweron) return;
  127. printf("on %s \n", key->name);
  128. // TODO:启动臭氧
  129. port_fan_set(true);
  130. if (!hcis_input_is_active(kchange_intermittentmode_time_input)) {
  131. hcis_active_input(kchange_intermittentmode_time_input);
  132. if (thisDevice.mode != kintermittentMode || thisDevice.countdonwnum_s == 0) {
  133. set_countdown(1);
  134. thisDevice.mode = kintermittentMode;
  135. }
  136. return;
  137. }
  138. hcis_active_input(kchange_intermittentmode_time_input);
  139. increase_and_assign_countdonwnum();
  140. }
  141. }
  142. void update_countdown() {
  143. if /* */ (thisDevice.mode == knormal) {
  144. thisDevice.countdonwnum = 0;
  145. thisDevice.countdonwnum_s = 0;
  146. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  147. if (thisDevice.countdonwnum_s != 0) {
  148. thisDevice.countdonwnum_s = //
  149. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  150. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  151. }
  152. }
  153. }
  154. void process_countdwonevent() {
  155. //更新倒计时计数
  156. update_countdown();
  157. /**
  158. * @brief
  159. */
  160. if (thisDevice.mode == ktimingMode) {
  161. //定时自动关机
  162. if (thisDevice.poweron) {
  163. if (thisDevice.countdonwnum_s == 0) {
  164. shutdwon();
  165. }
  166. }
  167. } else if (thisDevice.mode == kintermittentMode) {
  168. /**
  169. * @brief
  170. * ,,,,,
  171. */
  172. if (thisDevice.working) {
  173. //间歇模式下,定时到达定时时间,停止工作
  174. if (thisDevice.countdonwnum_s == 0) stopwork();
  175. } else {
  176. //间歇模式下,再次到达工作时间,再次工作
  177. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > INTERMITTENTMODE_PERIOD_S) {
  178. set_countdown(thisDevice.countdonw_setting_num);
  179. startwork();
  180. }
  181. }
  182. }
  183. };
  184. void ozone_control_start();
  185. void ozone_control_stop();
  186. void ozone_control_schedule();
  187. void do_debug_light_state() {
  188. static uint8_t debug_led_state = 1;
  189. debug_led_state = !debug_led_state;
  190. port_debug_set(debug_led_state);
  191. }
  192. void init_all_subdevice_state() {
  193. port_debug_set(false);
  194. port_fan_set(false);
  195. port_led0_set(false);
  196. port_led1_set(false);
  197. port_led2_set(false);
  198. port_led3_set(false);
  199. port_led_r_set(false);
  200. port_led_g_set(false);
  201. port_led_b_set(false);
  202. }
  203. int main(void) {
  204. SystemInit(); //配置系统时钟
  205. DeviceClockAllEnable(); //打开所有外设时钟
  206. systicket_init();
  207. /*系统初始化*/
  208. zgpio_init_all_gpio(); //
  209. port_init(); //
  210. init_all_subdevice_state(); //
  211. printf("==========OZONE_GENERATOR==========\n"); //
  212. printf("= manufactor: iflytop\n"); //
  213. printf("= version : %s\n", VERSION); //
  214. printf("=\n"); //
  215. /*组件初始化*/
  216. zkey_init(&key_module); //按键初始化
  217. ozone_control_init();
  218. /**
  219. * @brief
  220. * 20k起步50hz100ms计算功率
  221. */
  222. // printf_ozone_freq_and_power_table();
  223. // ozone_control_init();
  224. while (true) {
  225. //按键扫描逻辑
  226. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  227. END();
  228. DO_IT_EACH_MS(150) { do_debug_light_state(); }
  229. END();
  230. ozone_control_schedule();
  231. hcis_shcedule();
  232. process_countdwonevent();
  233. // printf("countddonw %d\n", thisDevice.countdonwnum_s);
  234. }
  235. }