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.

211 lines
6.8 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
  1. /**
  2. * @file light.c
  3. * @author Finny (tianjialong0106@163.com)
  4. * @brief Project micro shape photometer light control
  5. * @version 0.1
  6. * @date 2022-09-25
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "light.h"
  12. #include "driver/gpio.h"
  13. #include "esp_log.h"
  14. #include "port.h"
  15. #define DEBUG_LIGHT_TOGGLE_TIME 300
  16. #define OTHER_LIGHT_TOGGLE_TIME 500
  17. #define GPIO_DEBUG_LIGHT 12
  18. #define GPIO_POWER_LIGHT 12
  19. #define GPIO_HEATING_PLATE_STATE_LIGHT 12
  20. #define GPIO_REACTION_STATE_LIGHT 12
  21. #define GPIO_WIFI_STATE_LIGHT 12
  22. static power_state_light_structer_t *power_state_light_structer_s;
  23. static heating_plate_state_light_structer_t *heating_plate_state_light_structer_s;
  24. static reaction_state_light_structer_t *reaction_state_light_structer_s;
  25. static wifi_state_light_structer_t *wifi_state_light_structer_s;
  26. // Debug light
  27. void T_debug_light_init(void)
  28. {
  29. gpio_config_t gpio_debug_light_init_structer;
  30. gpio_debug_light_init_structer.intr_type = GPIO_INTR_DISABLE;
  31. gpio_debug_light_init_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  32. gpio_debug_light_init_structer.pin_bit_mask = (1ULL << GPIO_DEBUG_LIGHT);
  33. gpio_debug_light_init_structer.pull_down_en = 0;
  34. gpio_debug_light_init_structer.pull_up_en = 0;
  35. gpio_config(&gpio_debug_light_init_structer);
  36. }
  37. void T_light_toggle_level(uint8_t io_num)
  38. {
  39. gpio_set_level(io_num, !gpio_get_level(GPIO_DEBUG_LIGHT));
  40. }
  41. // Power light
  42. void T_power_light_init(power_state_light_structer_t *power_state_light_structer)
  43. {
  44. power_state_light_structer_s = power_state_light_structer;
  45. gpio_config_t gpio_debug_light_init_structer;
  46. gpio_debug_light_init_structer.intr_type = GPIO_INTR_DISABLE;
  47. gpio_debug_light_init_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  48. gpio_debug_light_init_structer.pin_bit_mask = (1ULL << GPIO_POWER_LIGHT);
  49. gpio_debug_light_init_structer.pull_down_en = 0;
  50. gpio_debug_light_init_structer.pull_up_en = 0;
  51. gpio_config(&gpio_debug_light_init_structer);
  52. }
  53. // Heating state light
  54. void T_heating_plate_state_light_init(heating_plate_state_light_structer_t *heating_plate_state_light_structer)
  55. {
  56. heating_plate_state_light_structer_s = heating_plate_state_light_structer;
  57. gpio_config_t gpio_heating_plate_state_light_init_structer;
  58. gpio_heating_plate_state_light_init_structer.intr_type = GPIO_INTR_DISABLE;
  59. gpio_heating_plate_state_light_init_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  60. gpio_heating_plate_state_light_init_structer.pin_bit_mask = (1ULL << GPIO_HEATING_PLATE_STATE_LIGHT);
  61. gpio_heating_plate_state_light_init_structer.pull_down_en = 0;
  62. gpio_heating_plate_state_light_init_structer.pull_up_en = 0;
  63. gpio_config(&gpio_heating_plate_state_light_init_structer);
  64. }
  65. void T_heating_plate_light_change_state(heating_plate_state_t change_state)
  66. {
  67. heating_plate_state_light_structer_s->state = change_state;
  68. heating_plate_state_light_structer_s->change_flag = true;
  69. }
  70. void T_heating_plate_light_set_rgb_by_state(heating_plate_state_t state)
  71. {
  72. if (state == NoHeat)
  73. {
  74. gpio_set_level(GPIO_HEATING_PLATE_STATE_LIGHT, 0);
  75. }
  76. else if (state == HeatComplete)
  77. {
  78. gpio_set_level(GPIO_HEATING_PLATE_STATE_LIGHT, 1);
  79. }
  80. }
  81. // Reaction state light
  82. void T_reaction_state_light_init(reaction_state_light_structer_t *reaction_state_light_structer)
  83. {
  84. reaction_state_light_structer_s = reaction_state_light_structer;
  85. gpio_config_t gpio_reaction_state_light_init_structer;
  86. gpio_reaction_state_light_init_structer.intr_type = GPIO_INTR_DISABLE;
  87. gpio_reaction_state_light_init_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  88. gpio_reaction_state_light_init_structer.pin_bit_mask = (1ULL << GPIO_REACTION_STATE_LIGHT);
  89. gpio_reaction_state_light_init_structer.pull_down_en = 0;
  90. gpio_reaction_state_light_init_structer.pull_up_en = 0;
  91. gpio_config(&gpio_reaction_state_light_init_structer);
  92. }
  93. void T_reaction_light_change_state(reaction_state_t change_state)
  94. {
  95. reaction_state_light_structer_s->state = change_state;
  96. reaction_state_light_structer_s->change_flag = true;
  97. }
  98. void T_reaction_light_set_rgb_by_state(reaction_state_t state)
  99. {
  100. if (state == NoReaction)
  101. {
  102. gpio_set_level(GPIO_REACTION_STATE_LIGHT, 0);
  103. }
  104. else if (state == ReactionComplete)
  105. {
  106. gpio_set_level(GPIO_REACTION_STATE_LIGHT, 1);
  107. }
  108. }
  109. // WIFI state light
  110. void T_wifi_state_light_init(wifi_state_light_structer_t *wifi_state_light_structer)
  111. {
  112. wifi_state_light_structer_s = wifi_state_light_structer;
  113. gpio_config_t gpio_wifi_state_light_init_structer;
  114. gpio_wifi_state_light_init_structer.intr_type = GPIO_INTR_DISABLE;
  115. gpio_wifi_state_light_init_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  116. gpio_wifi_state_light_init_structer.pin_bit_mask = (1ULL << GPIO_WIFI_STATE_LIGHT);
  117. gpio_wifi_state_light_init_structer.pull_down_en = 0;
  118. gpio_wifi_state_light_init_structer.pull_up_en = 0;
  119. gpio_config(&gpio_wifi_state_light_init_structer);
  120. }
  121. void T_wifi_light_change_state(wifi_state_t change_state)
  122. {
  123. wifi_state_light_structer_s->state = change_state;
  124. wifi_state_light_structer_s->change_flag = true;
  125. }
  126. void T_wifi_light_set_rgb_by_state(wifi_state_t state)
  127. {
  128. if (state == NotConnected)
  129. {
  130. gpio_set_level(GPIO_WIFI_STATE_LIGHT, 0);
  131. }
  132. else if (state == ConnectionComplete)
  133. {
  134. gpio_set_level(GPIO_WIFI_STATE_LIGHT, 1);
  135. }
  136. }
  137. void T_debug_light_schedule(void)
  138. {
  139. static uint32_t debug_light_time;
  140. if (port_haspassedms(debug_light_time) > DEBUG_LIGHT_TOGGLE_TIME)
  141. {
  142. T_light_toggle_level(GPIO_DEBUG_LIGHT);
  143. debug_light_time = port_get_ticket();
  144. }
  145. }
  146. void T_light_schedule(void)
  147. {
  148. static uint32_t light_ticket;
  149. if (heating_plate_state_light_structer_s->change_flag)
  150. {
  151. T_heating_plate_light_set_rgb_by_state(heating_plate_state_light_structer_s->state);
  152. heating_plate_state_light_structer_s->change_flag = false;
  153. }
  154. if (reaction_state_light_structer_s->change_flag)
  155. {
  156. T_reaction_light_set_rgb_by_state(reaction_state_light_structer_s->state);
  157. reaction_state_light_structer_s->change_flag = false;
  158. }
  159. if (wifi_state_light_structer_s->change_flag)
  160. {
  161. T_reaction_light_set_rgb_by_state(wifi_state_light_structer_s->state);
  162. wifi_state_light_structer_s->change_flag = false;
  163. }
  164. if (port_haspassedms(light_ticket) > OTHER_LIGHT_TOGGLE_TIME)
  165. {
  166. if (heating_plate_state_light_structer_s->state == Heating)
  167. {
  168. T_light_toggle_level(GPIO_HEATING_PLATE_STATE_LIGHT);
  169. }
  170. if (reaction_state_light_structer_s->state == Reacting)
  171. {
  172. T_light_toggle_level(GPIO_REACTION_STATE_LIGHT);
  173. }
  174. if (wifi_state_light_structer_s->state == Connecting)
  175. {
  176. T_light_toggle_level(GPIO_WIFI_STATE_LIGHT);
  177. }
  178. light_ticket = port_get_ticket();
  179. }
  180. }