LED.c 3.0 KB

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