pwm_audio.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef _PWM_AUDIO_H_
  2. #define _PWM_AUDIO_H_
  3. #include <rtthread.h>
  4. /**
  5. * @brief Configuration parameters of pwm audio for pwm_audio_init function
  6. */
  7. typedef struct
  8. {
  9. int gpio_num_left; /*!< the LEDC output gpio_num, Left channel */
  10. int gpio_num_right; /*!< the LEDC output gpio_num, Right channel */
  11. // ledc_channel_t ledc_channel_left; /*!< LEDC channel (0 - 7), Corresponding to left channel*/
  12. // ledc_channel_t ledc_channel_right; /*!< LEDC channel (0 - 7), Corresponding to right channel*/
  13. // ledc_timer_t ledc_timer_sel; /*!< Select the timer source of channel (0 - 3) */
  14. uint8_t duty_resolution; /*!< ledc pwm bits */
  15. uint32_t ringbuf_len; /*!< ringbuffer size */
  16. } pwm_audio_config_t;
  17. /**
  18. * @brief pwm audio status
  19. */
  20. typedef enum
  21. {
  22. PWM_AUDIO_STATUS_UN_INIT = 0, /*!< PWM ?????? */
  23. PWM_AUDIO_STATUS_IDLE = 1, /*!< pwm audio idle */
  24. PWM_AUDIO_STATUS_BUSY = 2, /*!< pwm audio busy */
  25. } pwm_audio_status_t;
  26. /**
  27. * @brief Initializes and configure the pwm audio.
  28. * Configure pwm audio with the given source.
  29. *
  30. * @param cfg Pointer of pwm_audio_config_t struct
  31. *
  32. * @return
  33. * - ESP_OK Success
  34. */
  35. rt_err_t pwm_audio_init(const pwm_audio_config_t *cfg);
  36. rt_err_t pwm_audio_wait_complete(rt_tick_t ticks_to_wait);
  37. /**
  38. * @brief Start audio play
  39. *
  40. * @return
  41. * - ESP_OK Success
  42. */
  43. rt_err_t pwm_audio_start(void);
  44. /**
  45. * @brief Write data
  46. *
  47. * @param inbuf
  48. * @param len
  49. * @param bytes_written
  50. * @param ticks_to_wait
  51. *
  52. * @return
  53. * - ESP_OK Success
  54. */
  55. rt_err_t pwm_audio_write(uint8_t *inbuf, size_t len, size_t *bytes_written, rt_tick_t ticks_to_wait);
  56. /**
  57. * @brief stop audio play
  58. *
  59. * @return
  60. * - ESP_OK Success
  61. */
  62. rt_err_t pwm_audio_stop(void);
  63. /**
  64. * @brief Deinit pwm, timer and gpio
  65. *
  66. * @return
  67. * - ESP_OK Success
  68. */
  69. rt_err_t pwm_audio_deinit(void);
  70. /**
  71. * @brief Set parameter for pwm audio.
  72. *
  73. * Similar to pwm_audio_set_sample_rate(), but also sets bit width.
  74. *
  75. * @param rate sample rate (ex: 8000, 44100...)
  76. * @param bits bit width
  77. * @param ch channel number
  78. *
  79. * @return
  80. * - ESP_OK Success
  81. * - ESP_ERR_INVALID_ARG Parameter error
  82. */
  83. rt_err_t pwm_audio_set_param(int rate, uint8_t bits, int ch);
  84. /**
  85. * @brief Set samplerate for pwm audio.
  86. *
  87. * @param rate sample rate (ex: 8000, 44100...)
  88. *
  89. * @return
  90. * - ESP_OK Success
  91. * - ESP_ERR_INVALID_ARG Parameter error
  92. */
  93. rt_err_t pwm_audio_set_sample_rate(int rate);
  94. /**
  95. * @brief Set volume for pwm audio.
  96. * !!!Using volume greater than 0 may cause variable overflow and distortion!!!
  97. * Usually you should enter a volume less than or equal to 0
  98. *
  99. * @param volume Volume to set (-16 ~ 16), see Macro VOLUME_0DB
  100. * Set to 0 for original output;
  101. * Set to less then 0 for attenuation, and -16 is mute;
  102. * Set to more than 0 for enlarge, and 16 is double output
  103. *
  104. * @return
  105. * - ESP_OK Success
  106. * - ESP_ERR_INVALID_ARG Parameter error
  107. */
  108. rt_err_t pwm_audio_set_volume(int8_t volume);
  109. /**
  110. * @brief Get parameter for pwm audio.
  111. *
  112. * @param rate sample rate
  113. * @param bits bit width
  114. * @param ch channel number
  115. *
  116. * @return
  117. * - ESP_OK Success
  118. * - ESP_ERR_INVALID_ARG Parameter error
  119. */
  120. rt_err_t pwm_audio_get_param(int *rate, int *bits, int *ch);
  121. /**
  122. * @brief get pwm audio status
  123. *
  124. * @param status current pwm_audio status
  125. *
  126. * @return
  127. * - ESP_OK Success
  128. */
  129. rt_err_t pwm_audio_get_status(pwm_audio_status_t *status);
  130. #endif