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.

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