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