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.

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