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.

160 lines
3.6 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
  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_init_all_subdevice_state() {
  15. port_debug_set(false);
  16. port_fan_set(false);
  17. port_led0_set(false);
  18. port_led1_set(false);
  19. port_led2_set(false);
  20. port_led3_set(false);
  21. port_led_r_set(false);
  22. port_led_g_set(false);
  23. port_led_b_set(false);
  24. }
  25. //开始工作
  26. static void startwork() { port_fan_set(true); }
  27. //停止工作
  28. static void stopwork() { port_fan_set(false); }
  29. static void poweron() {
  30. this_device.power = true;
  31. this_device.ozone_level = level_low;
  32. this_device.ozone_module = normal;
  33. startwork();
  34. return;
  35. }
  36. static void powerdown() {
  37. this_device.power = false;
  38. stopwork();
  39. return;
  40. }
  41. // 各个按键处理逻辑
  42. static void mf_process_poweron_key(key_t *key) {
  43. printf("key name:%s\n", key->name);
  44. if (!this_device.power) {
  45. mf_init_all_subdevice_state();
  46. poweron();
  47. } else {
  48. powerdown();
  49. }
  50. return;
  51. }
  52. static void mf_process_level_key(key_t *key) {
  53. if (!this_device.power) {
  54. mf_init_all_subdevice_state();
  55. return;
  56. }
  57. printf("key name:%s\n", key->name);
  58. return;
  59. }
  60. static void mf_process_timer_key(key_t *key) {
  61. if (!this_device.power) {
  62. mf_init_all_subdevice_state();
  63. return;
  64. }
  65. printf("key name:%s\n", key->name);
  66. return;
  67. }
  68. static void mf_process_interval_key(key_t *key) {
  69. if (!this_device.power) {
  70. mf_init_all_subdevice_state();
  71. return;
  72. }
  73. printf("key name:%s\n", key->name);
  74. return;
  75. }
  76. static void onkey(key_t *key, key_state_t key_state) {
  77. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  78. mf_process_poweron_key(key);
  79. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  80. mf_process_level_key(key);
  81. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  82. mf_process_timer_key(key);
  83. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  84. mf_process_interval_key(key);
  85. }
  86. }
  87. key_module_t key_module = key_module_init(s_keys, onkey);
  88. //等级处理
  89. void device_level_process() {}
  90. //定时处理
  91. void device_time_process() {}
  92. void hcis_shcedule_process() {
  93. if (!this_device.power) {
  94. mf_init_all_subdevice_state();
  95. return;
  96. }
  97. device_level_process();
  98. device_time_process();
  99. }
  100. void hcis_shcedule() {
  101. static uint32_t ticket = 0;
  102. if (systicket_haspassedms(ticket) > 30) {
  103. ticket = systicket_get_now_ms();
  104. hcis_shcedule_process();
  105. }
  106. }
  107. void hardware_init() {
  108. SystemInit(); //配置系统时钟
  109. DeviceClockAllEnable(); //打开所有外设时钟
  110. systicket_init();
  111. port_init();
  112. }
  113. void hardware_default_settings() { mf_init_all_subdevice_state(); }
  114. void flip_debuf_light_level() {
  115. static uint8_t debug_led_state = 1;
  116. debug_led_state = !debug_led_state;
  117. port_debug_set(debug_led_state);
  118. }
  119. int main() {
  120. hardware_init();
  121. hardware_default_settings();
  122. key_init(&key_module);
  123. while (true) {
  124. // scan key
  125. DO_IT_EACH_MS(20) { key_do_loop_in_each_period(); };
  126. END();
  127. // debug light
  128. DO_IT_EACH_MS(150) { flip_debuf_light_level(); }
  129. END();
  130. hcis_shcedule();
  131. }
  132. return 0;
  133. }