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.

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