LED.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. };
  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. };
  32. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  33. }
  34. void beep_init(void)
  35. {
  36. // Set the LEDC peripheral configuration
  37. example_ledc_init();
  38. // Set duty to 50%
  39. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  40. // Update duty to apply the new value
  41. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  42. }
  43. static void _74HC595_WriteByte(unsigned char Byte)
  44. {
  45. unsigned char i;
  46. for(i=0;i<8;i++)
  47. {
  48. uint8_t bit = (Byte >> (i)) & 0x01;
  49. gpio_set_level(LED_DATA_PIN,bit);
  50. LED_LCLK_0 ;
  51. LED_LCLK_1 ;
  52. }
  53. LED_SCLK_0;
  54. LED_SCLK_1;
  55. }
  56. void led_set(uint8_t led_index,uint8_t led_status)
  57. {
  58. ESP_LOGI(LOG_TAG,"led_set %s",led_status ? "open":"close");
  59. if(led_status)
  60. {
  61. _74HC595_WriteByte(led[led_index]);
  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. }
  90. }