mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Extend i2s config options (#6056)
This commit is contained in:
		| @@ -20,7 +20,9 @@ DEPENDENCIES = ["i2s_audio"] | |||||||
| CONF_ADC_PIN = "adc_pin" | CONF_ADC_PIN = "adc_pin" | ||||||
| CONF_ADC_TYPE = "adc_type" | CONF_ADC_TYPE = "adc_type" | ||||||
| CONF_PDM = "pdm" | CONF_PDM = "pdm" | ||||||
|  | CONF_SAMPLE_RATE = "sample_rate" | ||||||
| CONF_BITS_PER_SAMPLE = "bits_per_sample" | CONF_BITS_PER_SAMPLE = "bits_per_sample" | ||||||
|  | CONF_USE_APLL = "use_apll" | ||||||
|  |  | ||||||
| I2SAudioMicrophone = i2s_audio_ns.class_( | I2SAudioMicrophone = i2s_audio_ns.class_( | ||||||
|     "I2SAudioMicrophone", I2SAudioIn, microphone.Microphone, cg.Component |     "I2SAudioMicrophone", I2SAudioIn, microphone.Microphone, cg.Component | ||||||
| @@ -62,9 +64,11 @@ BASE_SCHEMA = microphone.MICROPHONE_SCHEMA.extend( | |||||||
|         cv.GenerateID(): cv.declare_id(I2SAudioMicrophone), |         cv.GenerateID(): cv.declare_id(I2SAudioMicrophone), | ||||||
|         cv.GenerateID(CONF_I2S_AUDIO_ID): cv.use_id(I2SAudioComponent), |         cv.GenerateID(CONF_I2S_AUDIO_ID): cv.use_id(I2SAudioComponent), | ||||||
|         cv.Optional(CONF_CHANNEL, default="right"): cv.enum(CHANNELS), |         cv.Optional(CONF_CHANNEL, default="right"): cv.enum(CHANNELS), | ||||||
|  |         cv.Optional(CONF_SAMPLE_RATE, default=16000): cv.int_range(min=1), | ||||||
|         cv.Optional(CONF_BITS_PER_SAMPLE, default="32bit"): cv.All( |         cv.Optional(CONF_BITS_PER_SAMPLE, default="32bit"): cv.All( | ||||||
|             _validate_bits, cv.enum(BITS_PER_SAMPLE) |             _validate_bits, cv.enum(BITS_PER_SAMPLE) | ||||||
|         ), |         ), | ||||||
|  |         cv.Optional(CONF_USE_APLL, default=False): cv.boolean, | ||||||
|     } |     } | ||||||
| ).extend(cv.COMPONENT_SCHEMA) | ).extend(cv.COMPONENT_SCHEMA) | ||||||
|  |  | ||||||
| @@ -105,6 +109,8 @@ async def to_code(config): | |||||||
|         cg.add(var.set_pdm(config[CONF_PDM])) |         cg.add(var.set_pdm(config[CONF_PDM])) | ||||||
|  |  | ||||||
|     cg.add(var.set_channel(config[CONF_CHANNEL])) |     cg.add(var.set_channel(config[CONF_CHANNEL])) | ||||||
|  |     cg.add(var.set_sample_rate(config[CONF_SAMPLE_RATE])) | ||||||
|     cg.add(var.set_bits_per_sample(config[CONF_BITS_PER_SAMPLE])) |     cg.add(var.set_bits_per_sample(config[CONF_BITS_PER_SAMPLE])) | ||||||
|  |     cg.add(var.set_use_apll(config[CONF_USE_APLL])) | ||||||
|  |  | ||||||
|     await microphone.register_microphone(var, config) |     await microphone.register_microphone(var, config) | ||||||
|   | |||||||
| @@ -47,14 +47,14 @@ void I2SAudioMicrophone::start_() { | |||||||
|   } |   } | ||||||
|   i2s_driver_config_t config = { |   i2s_driver_config_t config = { | ||||||
|       .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX), |       .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX), | ||||||
|       .sample_rate = 16000, |       .sample_rate = this->sample_rate_, | ||||||
|       .bits_per_sample = this->bits_per_sample_, |       .bits_per_sample = this->bits_per_sample_, | ||||||
|       .channel_format = this->channel_, |       .channel_format = this->channel_, | ||||||
|       .communication_format = I2S_COMM_FORMAT_STAND_I2S, |       .communication_format = I2S_COMM_FORMAT_STAND_I2S, | ||||||
|       .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, |       .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, | ||||||
|       .dma_buf_count = 4, |       .dma_buf_count = 4, | ||||||
|       .dma_buf_len = 256, |       .dma_buf_len = 256, | ||||||
|       .use_apll = false, |       .use_apll = this->use_apll_, | ||||||
|       .tx_desc_auto_clear = false, |       .tx_desc_auto_clear = false, | ||||||
|       .fixed_mclk = 0, |       .fixed_mclk = 0, | ||||||
|       .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, |       .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, | ||||||
|   | |||||||
| @@ -31,7 +31,9 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   void set_channel(i2s_channel_fmt_t channel) { this->channel_ = channel; } |   void set_channel(i2s_channel_fmt_t channel) { this->channel_ = channel; } | ||||||
|  |   void set_sample_rate(uint32_t sample_rate) { this->sample_rate_ = sample_rate; } | ||||||
|   void set_bits_per_sample(i2s_bits_per_sample_t bits_per_sample) { this->bits_per_sample_ = bits_per_sample; } |   void set_bits_per_sample(i2s_bits_per_sample_t bits_per_sample) { this->bits_per_sample_ = bits_per_sample; } | ||||||
|  |   void set_use_apll(uint32_t use_apll) { this->use_apll_ = use_apll; } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   void start_(); |   void start_(); | ||||||
| @@ -45,7 +47,9 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub | |||||||
| #endif | #endif | ||||||
|   bool pdm_{false}; |   bool pdm_{false}; | ||||||
|   i2s_channel_fmt_t channel_; |   i2s_channel_fmt_t channel_; | ||||||
|  |   uint32_t sample_rate_; | ||||||
|   i2s_bits_per_sample_t bits_per_sample_; |   i2s_bits_per_sample_t bits_per_sample_; | ||||||
|  |   bool use_apll_; | ||||||
|  |  | ||||||
|   HighFrequencyLoopRequester high_freq_; |   HighFrequencyLoopRequester high_freq_; | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user