LED.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdio.h>
  2. #include "LED.h"
  3. #include "esp_log.h"
  4. static const char *LOG_TAG = "LED";
  5. // uint8_t led[6]={0x04,0x20,0x08,0x10,0x02,0x40 };
  6. // uint8_t led[6]={0x20,0x02,0x04,0x08,0x10,0x40 };
  7. // dailiao baoyang tunxing fengcun guzhang tingji
  8. uint8_t led[6] = {0x04, 0x40, 0x20, 0x10, 0x08, 0x02};
  9. #include <stdio.h>
  10. #include "esp_err.h"
  11. static void example_ledc_init(void)
  12. {
  13. // Prepare and then apply the LEDC PWM timer configuration
  14. ledc_timer_config_t ledc_timer = {
  15. .speed_mode = LEDC_MODE,
  16. .timer_num = LEDC_TIMER,
  17. .duty_resolution = LEDC_DUTY_RES,
  18. .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
  19. .clk_cfg = LEDC_AUTO_CLK};
  20. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  21. // Prepare and then apply the LEDC PWM channel configuration
  22. ledc_channel_config_t ledc_channel = {
  23. .speed_mode = LEDC_MODE,
  24. .channel = LEDC_CHANNEL,
  25. .timer_sel = LEDC_TIMER,
  26. .intr_type = LEDC_INTR_DISABLE,
  27. .gpio_num = LEDC_OUTPUT_IO,
  28. .duty = 0, // Set duty to 0%
  29. .hpoint = 0};
  30. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  31. }
  32. void beep_init(void)
  33. {
  34. // Set the LEDC peripheral configuration
  35. example_ledc_init();
  36. // Set duty to 50%
  37. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  38. // Update duty to apply the new value
  39. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  40. }
  41. static void _74HC595_WriteByte(unsigned char Byte)
  42. {
  43. unsigned char i;
  44. for (i = 0; i < 8; i++)
  45. {
  46. uint8_t bit = (Byte >> (i)) & 0x01;
  47. gpio_set_level(LED_DATA_PIN, bit);
  48. LED_LCLK_0;
  49. LED_LCLK_1;
  50. }
  51. LED_SCLK_0;
  52. LED_SCLK_1;
  53. }
  54. void led_set(uint8_t led_index, uint8_t led_status)
  55. {
  56. ESP_LOGI(LOG_TAG, "led_set %s", led_status ? "open" : "close");
  57. if (led_status)
  58. {
  59. _74HC595_WriteByte(led[led_index]);
  60. }
  61. else
  62. {
  63. _74HC595_WriteByte(0x00);
  64. }
  65. }
  66. void beep_open()
  67. {
  68. // Set duty to 50%
  69. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
  70. // Update duty to apply the new value
  71. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  72. }
  73. void beep_close()
  74. {
  75. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  76. // Update duty to apply the new value
  77. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  78. }
  79. void beep_blink(uint16_t ms, uint16_t count)
  80. {
  81. for (int i = 0; i < count; i++)
  82. {
  83. // printf("beep\r\n");
  84. beep_open();
  85. vTaskDelay(ms / portTICK_PERIOD_MS);
  86. beep_close();
  87. vTaskDelay(ms / portTICK_PERIOD_MS);
  88. }
  89. }