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.

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