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.

252 lines
9.0 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
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
  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/thisdevice.h"
  13. #include "test.h"
  14. #include "zsimple_timer/zsimple_timer.h"
  15. /***********************************************************************************************************************
  16. * =========================================================================================================== *
  17. ***********************************************************************************************************************/
  18. void onkey(zkey_t* key, zkey_state_t key_state);
  19. /***********************************************************************************************************************
  20. * =========================================================================================================== *
  21. ***********************************************************************************************************************/
  22. static zkey_t s_keys[] = {
  23. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  24. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  25. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  26. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  27. };
  28. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  29. static zsimple_timer_t zsimple_timer_mem[20]; //最多同时存在20个定时器
  30. zsimple_timer_t* debuglighttimer;
  31. /***********************************************************************************************************************
  32. * =============================================================================================================== *
  33. ***********************************************************************************************************************/
  34. static uint32_t compute_countdown_num(int countdowns) {
  35. if (countdowns) return countdowns / kconst_countdown_step_s + !!(countdowns % kconst_countdown_step_s);
  36. }
  37. static void shutdwon() {
  38. printf("power off\n");
  39. thisDevice.poweron = false;
  40. port_fan_set(false);
  41. //@TODO:关闭臭氧
  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. } else {
  50. thisDevice.countdonwnum_s = (thisDevice.countdonwnum + 1) * kconst_countdown_step_s;
  51. thisDevice.countdonwnum = thisDevice.countdonwnum + 1;
  52. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  53. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  54. }
  55. }
  56. static void set_countdown(int countdownnum) {
  57. thisDevice.countdonwnum = countdownnum;
  58. thisDevice.countdonwnum_s = thisDevice.countdonwnum * kconst_countdown_step_s;
  59. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  60. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  61. }
  62. static void onkey(zkey_t* key, zkey_state_t key_state) {
  63. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  64. printf("on %s \n", key->name);
  65. if (!thisDevice.poweron) {
  66. printf("power on\n");
  67. thisDevice.poweron = true;
  68. thisDevice.level = klevel1;
  69. thisDevice.mode = knormal;
  70. //设置灯光效果
  71. port_fan_set(true);
  72. //@TODO:启动臭氧
  73. } else {
  74. shutdwon();
  75. }
  76. return;
  77. }
  78. // levelkey
  79. if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  80. if (!thisDevice.poweron) return;
  81. printf("on %s \n", key->name);
  82. /**
  83. * @brief
  84. */
  85. if (!lcs_input_is_active(kchange_level_input)) {
  86. lcs_active_input(kchange_level_input);
  87. return;
  88. }
  89. if (thisDevice.level == klevel1) {
  90. printf("changet level to level2\n");
  91. thisDevice.level = klevel2;
  92. //更改臭氧状态
  93. } else if (thisDevice.level == klevel2) {
  94. printf("changet level to level1\n");
  95. thisDevice.level = klevel1;
  96. //更改臭氧状态
  97. }
  98. lcs_active_input(kchange_level_input);
  99. return;
  100. }
  101. // timerkey
  102. if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  103. if (!thisDevice.poweron) return;
  104. printf("on %s \n", key->name);
  105. if (!lcs_input_is_active(kchange_countdonw_time_input)) {
  106. lcs_active_input(kchange_countdonw_time_input);
  107. if (thisDevice.mode != ktimingMode || thisDevice.countdonwnum_s == 0) {
  108. set_countdown(1);
  109. thisDevice.mode = ktimingMode;
  110. }
  111. return;
  112. }
  113. lcs_active_input(kchange_countdonw_time_input);
  114. increase_and_assign_countdonwnum();
  115. }
  116. if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  117. if (!thisDevice.poweron) return;
  118. printf("on %s \n", key->name);
  119. if (!lcs_input_is_active(kchange_intermittentmode_time_input)) {
  120. lcs_active_input(kchange_intermittentmode_time_input);
  121. if (thisDevice.mode != kintermittentMode || thisDevice.countdonwnum_s == 0) {
  122. set_countdown(1);
  123. thisDevice.mode = kintermittentMode;
  124. }
  125. return;
  126. }
  127. lcs_active_input(kchange_intermittentmode_time_input);
  128. increase_and_assign_countdonwnum();
  129. }
  130. }
  131. void compute_countdown() {
  132. if /* */ (thisDevice.mode == knormal) {
  133. thisDevice.countdonwnum = 0;
  134. thisDevice.countdonwnum_s = 0;
  135. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  136. if (thisDevice.countdonwnum_s != 0) {
  137. thisDevice.countdonwnum_s = //
  138. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  139. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  140. }
  141. }
  142. }
  143. void process_countdwonevent() {
  144. //计算countdown
  145. compute_countdown();
  146. /**
  147. * @brief
  148. */
  149. if (thisDevice.mode == ktimingMode) {
  150. //定时自动关机
  151. if (thisDevice.poweron) {
  152. if (thisDevice.countdonwnum_s == 0) {
  153. shutdwon();
  154. }
  155. }
  156. } else if (thisDevice.mode == kintermittentMode) {
  157. /**
  158. * @brief
  159. * ,,,,,
  160. */
  161. if (!thisDevice.intermittentMode_idle) {
  162. if (thisDevice.countdonwnum_s == 0) {
  163. //切换状态到待机状态
  164. // TODO:关闭臭氧
  165. thisDevice.intermittentMode_idle = true;
  166. port_fan_set(false);
  167. }
  168. } else {
  169. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > INTERMITTENTMODE_PERIOD_S) {
  170. thisDevice.intermittentMode_idle = false;
  171. set_countdown(thisDevice.countdonw_setting_num);
  172. //启动设备
  173. port_fan_set(true);
  174. }
  175. }
  176. }
  177. };
  178. void do_debug_light_state(zsimple_timer_t* handler) {
  179. static uint8_t debug_led_state = 1;
  180. debug_led_state = !debug_led_state;
  181. port_debug_set(debug_led_state);
  182. }
  183. void init_all_subdevice_state() {
  184. port_debug_set(false);
  185. port_fan_set(false);
  186. port_led0_set(false);
  187. port_led1_set(false);
  188. port_led2_set(false);
  189. port_led3_set(false);
  190. port_led_r_set(false);
  191. port_led_g_set(false);
  192. port_led_b_set(false);
  193. }
  194. int main(void) {
  195. SystemInit(); //配置系统时钟
  196. DeviceClockAllEnable(); //打开所有外设时钟
  197. systicket_init();
  198. /*系统初始化*/
  199. zgpio_init_all_gpio(); //
  200. port_init(); //
  201. init_all_subdevice_state(); //
  202. printf("==========OZONE_GENERATOR==========\n"); //
  203. printf("= manufactor: iflytop\n"); //
  204. printf("= version : %s\n", VERSION); //
  205. printf("=\n"); //
  206. /*组件初始化*/
  207. zsimple_timer_module_init(zsimple_timer_mem, ZARR_SIZE(zsimple_timer_mem), systicket_get_now_ms); //定时器模块初始化
  208. zkey_init(&key_module); //按键初始化
  209. // port_fan_set(true);
  210. // port_ozone_pwm_set_duty(30000, 10 * 1000);
  211. // port_ozone_pwm_start();
  212. debuglighttimer = zsimple_timer_alloc();
  213. zsimple_timer_trigger_static(debuglighttimer, 300, INFINITE_TIMES /*触发次数:*/, true, do_debug_light_state);
  214. // lcs_status_light__set_color(/*r:*/ true, /*g:*/ false, /*b:*/ false);
  215. while (true) {
  216. //按键扫描逻辑
  217. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  218. END();
  219. zsimple_timer_schedule();
  220. lcs_shcedule();
  221. process_countdwonevent();
  222. printf("countddonw %d\n", thisDevice.countdonwnum_s);
  223. // test_all_light();
  224. }
  225. }