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