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.

247 lines
5.9 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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 "key.h"
  4. #include "port.h"
  5. #include "systicket.h"
  6. #include "this_device.h"
  7. static key_t s_keys[] = {
  8. s_key_init("powerkey", port_gpio_get_power_key_state),
  9. s_key_init("levelkey", port_gpio_get_level_key_state),
  10. s_key_init("timerkey", port_gpio_get_timer_key_state),
  11. s_key_init("intervalkey", port_gpio_get_interval_key_state),
  12. };
  13. static ThisDevice_t this_device;
  14. static void mf_led_rgb_set(bool red, bool green, bool blue) {
  15. port_led_r_set(red);
  16. port_led_g_set(green);
  17. port_led_b_set(blue);
  18. }
  19. static void mf_init_all_subdevice_state() {
  20. port_debug_set(false);
  21. port_fan_set(false);
  22. port_led0_set(false);
  23. port_led1_set(false);
  24. port_led2_set(false);
  25. port_led3_set(false);
  26. port_led_r_set(false);
  27. port_led_g_set(false);
  28. port_led_b_set(false);
  29. }
  30. static void time_set_led_state(uint8_t led_num, bool state) {
  31. switch (led_num) {
  32. case 0:
  33. port_led0_set(false);
  34. port_led1_set(false);
  35. port_led2_set(false);
  36. port_led3_set(false);
  37. break;
  38. case 1:
  39. port_led0_set(state);
  40. port_led1_set(false);
  41. port_led2_set(false);
  42. port_led3_set(false);
  43. break;
  44. case 2:
  45. port_led0_set(state);
  46. port_led1_set(state);
  47. port_led2_set(false);
  48. port_led3_set(false);
  49. break;
  50. case 3:
  51. port_led0_set(state);
  52. port_led1_set(state);
  53. port_led2_set(state);
  54. port_led3_set(false);
  55. break;
  56. case 4:
  57. port_led0_set(state);
  58. port_led1_set(state);
  59. port_led2_set(state);
  60. port_led3_set(state);
  61. break;
  62. default:
  63. printf("no more led\n");
  64. break;
  65. }
  66. }
  67. //开始工作
  68. static void startwork() { port_fan_set(true); }
  69. //停止工作
  70. static void stopwork() { port_fan_set(false); }
  71. static void poweron() {
  72. this_device.power = true;
  73. this_device.ozone_level = level_low;
  74. this_device.ozone_module = normal;
  75. startwork();
  76. return;
  77. }
  78. static void powerdown() {
  79. this_device.power = false;
  80. stopwork();
  81. return;
  82. }
  83. // 各个按键处理逻辑
  84. static void mf_process_poweron_key(key_t *key) {
  85. printf("key name:%s\n", key->name);
  86. if (!this_device.power) {
  87. mf_init_all_subdevice_state();
  88. poweron();
  89. } else {
  90. powerdown();
  91. }
  92. return;
  93. }
  94. static void mf_process_level_key(key_t *key) {
  95. if (!this_device.power) {
  96. mf_init_all_subdevice_state();
  97. return;
  98. }
  99. printf("key name:%s\n", key->name);
  100. if (this_device.ozone_level == level_high) {
  101. this_device.ozone_level = level_low;
  102. } else if (this_device.ozone_level == level_low) {
  103. this_device.ozone_level = level_high;
  104. }
  105. return;
  106. }
  107. static void mf_process_timer_key(key_t *key) {
  108. if (!this_device.power) {
  109. mf_init_all_subdevice_state();
  110. return;
  111. }
  112. printf("key name:%s\n", key->name);
  113. if (this_device.configuration_mode == false) {
  114. this_device.configuration_start_time = systicket_get_now_ms();
  115. this_device.configuration_mode = true;
  116. } else {
  117. this_device.time_led_num = this_device.time_led_num + 1;
  118. if (this_device.time_led_num > 4) {
  119. this_device.time_led_num = this_device.time_led_num - 4;
  120. }
  121. }
  122. return;
  123. }
  124. static void mf_process_interval_key(key_t *key) {
  125. if (!this_device.power) {
  126. mf_init_all_subdevice_state();
  127. return;
  128. }
  129. printf("key name:%s\n", key->name);
  130. return;
  131. }
  132. static void onkey(key_t *key, key_state_t key_state) {
  133. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  134. mf_process_poweron_key(key);
  135. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  136. mf_process_level_key(key);
  137. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  138. mf_process_timer_key(key);
  139. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  140. mf_process_interval_key(key);
  141. }
  142. }
  143. key_module_t key_module = key_module_init(s_keys, onkey);
  144. //等级处理
  145. void device_level_process() {
  146. if (this_device.ozone_level == level_low) {
  147. mf_led_rgb_set(true, false, false);
  148. } else if (this_device.ozone_level == level_high) {
  149. mf_led_rgb_set(false, false, true);
  150. }
  151. }
  152. //定时处理
  153. void device_time_process() {}
  154. void hcis_shcedule_process() {
  155. if (!this_device.power) {
  156. mf_init_all_subdevice_state();
  157. return;
  158. }
  159. device_level_process();
  160. device_time_process();
  161. }
  162. void hcis_shcedule() {
  163. static uint32_t ticket = 0;
  164. if (systicket_haspassedms(ticket) > 30) {
  165. ticket = systicket_get_now_ms();
  166. hcis_shcedule_process();
  167. }
  168. }
  169. void configuration_shcedule() {
  170. static bool led_state;
  171. static uint32_t led_ticket = 0;
  172. if (this_device.configuration_mode == true) {
  173. if (systicket_haspassedms(this_device.configuration_start_time) > 2000) {
  174. this_device.configuration_mode = false;
  175. time_set_led_state(this_device.time_led_num, true);
  176. }
  177. if (systicket_haspassedms(led_ticket) > 100) {
  178. led_ticket = systicket_get_now_ms();
  179. time_set_led_state(this_device.time_led_num, led_state);
  180. led_state = !led_state;
  181. }
  182. }
  183. }
  184. void hardware_init() {
  185. SystemInit(); //配置系统时钟
  186. DeviceClockAllEnable(); //打开所有外设时钟
  187. systicket_init();
  188. port_init();
  189. }
  190. void hardware_default_settings() { mf_init_all_subdevice_state(); }
  191. void flip_debuf_light_level() {
  192. static uint8_t debug_led_state = 1;
  193. debug_led_state = !debug_led_state;
  194. port_debug_set(debug_led_state);
  195. }
  196. int main() {
  197. hardware_init();
  198. hardware_default_settings();
  199. key_init(&key_module);
  200. while (true) {
  201. // scan key
  202. DO_IT_EACH_MS(20) { key_do_loop_in_each_period(); };
  203. END();
  204. // debug light
  205. DO_IT_EACH_MS(200) { flip_debuf_light_level(); }
  206. END();
  207. hcis_shcedule();
  208. configuration_shcedule();
  209. }
  210. return 0;
  211. }