22#include "py/runtime.h"
33#include "py/mphal.h"
44#include "esp_heap_caps.h"
5- #include "esp_codec_dev.h"
5+ #include "esp_codec_dev.h" // Include for esp_codec_dev_*
66#include "adc_mic.h" // Include for audio_codec_adc_cfg_t, audio_codec_new_adc_data, etc.
7+ #include <errno.h> // For ENOMEM
78
89static mp_obj_t adc_mic_read (void ) {
910 // Configure for mono ADC on GPIO1 (ADC1_CHANNEL_0) at 16kHz
@@ -20,7 +21,7 @@ static mp_obj_t adc_mic_read(void) {
2021 };
2122 esp_codec_dev_handle_t dev = esp_codec_dev_new (& codec_dev_cfg );
2223 if (dev == NULL ) {
23- audio_codec_delete_adc_data (adc_if );
24+ audio_codec_delete_data_if (adc_if );
2425 mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Failed to create codec device" ));
2526 }
2627
@@ -31,39 +32,39 @@ static mp_obj_t adc_mic_read(void) {
3132 .bits_per_sample = 16 ,
3233 };
3334 if (esp_codec_dev_open (dev , & fs ) != ESP_OK ) {
34- esp_codec_dev_del (dev );
35- audio_codec_delete_adc_data (adc_if );
35+ esp_codec_dev_delete (dev );
36+ audio_codec_delete_data_if (adc_if );
3637 mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Failed to open codec device" ));
3738 }
3839
3940 // Allocate buffer for 16000 samples (16-bit, so 32000 bytes)
40- const size_t buf_size = 16000 * sizeof (uint16_t );
41- uint8_t * audio_buffer = (uint8_t * )heap_caps_malloc (buf_size , MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT );
41+ const size_t buf_size = 16000 * sizeof (int16_t );
42+ int16_t * audio_buffer = (int16_t * )heap_caps_malloc (buf_size , MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT );
4243 if (audio_buffer == NULL ) {
4344 esp_codec_dev_close (dev );
44- esp_codec_dev_del (dev );
45- audio_codec_delete_adc_data (adc_if );
46- mp_raise_OSError (MP_ENOMEM );
45+ esp_codec_dev_delete (dev );
46+ audio_codec_delete_data_if (adc_if );
47+ mp_raise_OSError (ENOMEM );
4748 }
4849
4950 // Read the data (blocking until buffer is filled)
5051 int ret = esp_codec_dev_read (dev , audio_buffer , buf_size );
5152 if (ret < 0 ) {
5253 heap_caps_free (audio_buffer );
5354 esp_codec_dev_close (dev );
54- esp_codec_dev_del (dev );
55- audio_codec_delete_adc_data (adc_if );
55+ esp_codec_dev_delete (dev );
56+ audio_codec_delete_data_if (adc_if );
5657 mp_raise_msg_varg (& mp_type_RuntimeError , MP_ERROR_TEXT ("Failed to read audio data: %d" ), ret );
5758 }
5859
5960 // Create MicroPython bytes object from the buffer
60- mp_obj_t buf_obj = mp_obj_new_bytes (audio_buffer , ret );
61+ mp_obj_t buf_obj = mp_obj_new_bytes (( const byte * ) audio_buffer , ret );
6162
6263 // Cleanup
6364 heap_caps_free (audio_buffer );
6465 esp_codec_dev_close (dev );
65- esp_codec_dev_del (dev );
66- audio_codec_delete_adc_data (adc_if );
66+ esp_codec_dev_delete (dev );
67+ audio_codec_delete_data_if (adc_if );
6768
6869 return buf_obj ;
6970}
@@ -80,4 +81,4 @@ const mp_obj_module_t adc_mic_user_cmodule = {
8081 .globals = (mp_obj_dict_t * )& adc_mic_module_globals ,
8182};
8283
83- MP_REGISTER_MODULE (MP_QSTR_adc_mic , adc_mic_user_cmodule );
84+ MP_REGISTER_MODULE (MP_QSTR_adc_mic , adc_mic_user_cmodule );
0 commit comments