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.

89 lines
2.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
  1. /**
  2. * @file heating_plate.c
  3. * @author Finny (tianjialong0106@163.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-09-26
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "heating_plate.h"
  12. #include "driver/ledc.h"
  13. #include "esp_err.h"
  14. #include "light.h"
  15. #define LEDC_TIMER LEDC_TIMER_0 //
  16. #define LEDC_MODE LEDC_LOW_SPEED_MODE //
  17. #define LEDC_OUTPUT_IO (20) // Define the output GPIO
  18. #define LEDC_CHANNEL LEDC_CHANNEL_0 //
  19. #define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
  20. #define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
  21. #define LEDC_FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
  22. #define heating_plate_target_temp 48.0
  23. #define heating_plate_variable_temperature_range 0.5
  24. static heating_plate_structer_t *heating_plate_structer_s;
  25. static get_temp_callback_t get_temp_cb_s;
  26. void T_heating_plate_init(heating_plate_structer_t *heating_plate_structer)
  27. {
  28. heating_plate_structer_s = heating_plate_structer;
  29. // Prepare and then apply the LEDC PWM timer configuration
  30. ledc_timer_config_t ledc_timer = {
  31. .speed_mode = LEDC_MODE,
  32. .timer_num = LEDC_TIMER,
  33. .duty_resolution = LEDC_DUTY_RES,
  34. .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
  35. .clk_cfg = LEDC_AUTO_CLK};
  36. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  37. // Prepare and then apply the LEDC PWM channel configuration
  38. ledc_channel_config_t ledc_channel = {
  39. .speed_mode = LEDC_MODE,
  40. .channel = LEDC_CHANNEL,
  41. .timer_sel = LEDC_TIMER,
  42. .intr_type = LEDC_INTR_DISABLE,
  43. .gpio_num = LEDC_OUTPUT_IO,
  44. .duty = 0, // Set duty to 0%
  45. .hpoint = 0};
  46. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  47. }
  48. void T_heating_plate_registered_cb(get_temp_callback_t cb) { get_temp_cb_s = cb; }
  49. void T_heating_plate_start(void)
  50. {
  51. T_heating_plate_set_and_update_duty(LEDC_DUTY);
  52. }
  53. void T_heating_plate_stop(void)
  54. {
  55. T_heating_plate_set_and_update_duty(0);
  56. }
  57. void T_heating_plate_schedule(void)
  58. {
  59. if (heating_plate_structer_s->heating_plate_preheat_start_flag)
  60. {
  61. if (get_temp_cb_s() >= heating_plate_target_temp + heating_plate_variable_temperature_range)
  62. {
  63. T_heating_plate_stop();
  64. T_heating_plate_light_set_rgb_by_state(HeatComplete);
  65. }
  66. else if (get_temp_cb_s() <= heating_plate_target_temp - heating_plate_variable_temperature_range)
  67. {
  68. T_heating_plate_start();
  69. T_heating_plate_light_set_rgb_by_state(Heating);
  70. }
  71. }
  72. }
  73. void T_heating_plate_set_and_update_duty(uint32_t duty)
  74. {
  75. // Set duty
  76. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty));
  77. // Update duty to apply the new value
  78. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  79. }