From cdb2651e9104cc81c5a9a546cb468060afdd6057 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 16 Feb 2026 16:12:06 +0100 Subject: [PATCH] Work on adc_mic --- c_mpos/micropython.cmake | 10 +++--- c_mpos/src/adc_mic.c | 66 ++++++++++++++++++++++++++-------------- scripts/build_mpos.sh | 13 ++++++-- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/c_mpos/micropython.cmake b/c_mpos/micropython.cmake index d442f0c3..9669288a 100644 --- a/c_mpos/micropython.cmake +++ b/c_mpos/micropython.cmake @@ -6,13 +6,13 @@ add_library(usermod_c_mpos INTERFACE) set(MPOS_C_INCLUDES) -#set(MPOS_C_INCLUDES -# ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/include/ -# ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/interface/ -#) +set(MPOS_C_INCLUDES + ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/include/ + ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/interface/ +) set(MPOS_C_SOURCES -# ${CMAKE_CURRENT_LIST_DIR}/src/adc_mic.c + ${CMAKE_CURRENT_LIST_DIR}/src/adc_mic.c ${CMAKE_CURRENT_LIST_DIR}/src/quirc_decode.c ${CMAKE_CURRENT_LIST_DIR}/quirc/lib/identify.c ${CMAKE_CURRENT_LIST_DIR}/quirc/lib/version_db.c diff --git a/c_mpos/src/adc_mic.c b/c_mpos/src/adc_mic.c index fecf91c2..22882efc 100644 --- a/c_mpos/src/adc_mic.c +++ b/c_mpos/src/adc_mic.c @@ -12,28 +12,54 @@ #define ADC_MIC_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__) -static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) { - // Extract chunk_samples from argument - size_t chunk_samples = mp_obj_get_int(chunk_samples_obj); +static mp_obj_t adc_mic_read(size_t n_args, const mp_obj_t *args) { + // Extract arguments + // args[0]: chunk_samples + // args[1]: unit_id + // args[2]: adc_channel_list + // args[3]: adc_channel_num + // args[4]: sample_rate_hz + // args[5]: atten + + size_t chunk_samples = mp_obj_get_int(args[0]); + int unit_id = mp_obj_get_int(args[1]); + mp_obj_t channel_list_obj = args[2]; + size_t channel_list_len; + mp_obj_t *channel_list_items; + mp_obj_get_array(channel_list_obj, &channel_list_len, &channel_list_items); + + int adc_channel_num = mp_obj_get_int(args[3]); + int sample_rate_hz = mp_obj_get_int(args[4]); + int atten = mp_obj_get_int(args[5]); + ADC_MIC_DEBUG_PRINT("Starting adc_mic_read...\n"); ADC_MIC_DEBUG_PRINT("CONFIG_ADC_MIC_TASK_CORE: %d\n", CONFIG_ADC_MIC_TASK_CORE); - // Configuration (your current manual setup with 2.5 dB atten) + if (adc_channel_num > 10) { + mp_raise_ValueError("Too many channels (max 10)"); + } + if (channel_list_len < adc_channel_num) { + mp_raise_ValueError("adc_channel_list shorter than adc_channel_num"); + } + + uint8_t channels[10]; + for (size_t i = 0; i < adc_channel_num; i++) { + channels[i] = (uint8_t)mp_obj_get_int(channel_list_items[i]); + } + + // Configuration audio_codec_adc_cfg_t cfg = { .handle = NULL, .max_store_buf_size = 1024 * 2, .conv_frame_size = 1024, - .unit_id = ADC_UNIT_1, - .adc_channel_list = ((uint8_t[]){ADC_CHANNEL_0}), - .adc_channel_num = 1, - .sample_rate_hz = 16000, - //.atten = ADC_ATTEN_DB_0, // values always 16380 - //.atten = ADC_ATTEN_DB_2_5, // values always 16380 - .atten = ADC_ATTEN_DB_6, // values around 12500 +/- 320 (silence) or 4000 (loud talk) - //.atten = ADC_ATTEN_DB_11, // values around -1130 +/- 160 (silence) + .unit_id = (adc_unit_t)unit_id, + .adc_channel_list = channels, + .adc_channel_num = adc_channel_num, // can probably just count adc_channel_list + .sample_rate_hz = sample_rate_hz, + .atten = (adc_atten_t)atten, }; - ADC_MIC_DEBUG_PRINT("Config created for channel %d, sample rate %d, atten %d\n", ADC_CHANNEL_0, 16000, cfg.atten); + ADC_MIC_DEBUG_PRINT("Config created for unit %d, channels %d, sample rate %d, atten %d\n", unit_id, adc_channel_num, sample_rate_hz, atten); // ──────────────────────────────────────────────── // Initialization (same as before) @@ -55,8 +81,8 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) { } esp_codec_dev_sample_info_t fs = { - .sample_rate = 16000, - .channel = 1, + .sample_rate = sample_rate_hz, + .channel = adc_channel_num, .bits_per_sample = 16, }; esp_err_t open_ret = esp_codec_dev_open(dev, &fs); @@ -69,9 +95,9 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) { // ──────────────────────────────────────────────── // Small reusable buffer + tracking variables // ──────────────────────────────────────────────── - const size_t buf_size = chunk_samples * sizeof(int16_t); + const size_t buf_size = chunk_samples * sizeof(int16_t) * adc_channel_num; int16_t *audio_buffer = heap_caps_malloc(buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - //int16_t *audio_buffer = heap_caps_thread.start_new_thread(testit, ())_malloc_prefer(buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT); + //int16_t *audio_buffer = heap_caps_malloc_prefer(buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT); if (audio_buffer == NULL) { esp_codec_dev_close(dev); esp_codec_dev_delete(dev); @@ -99,10 +125,6 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) { break; } vTaskDelay(pdMS_TO_TICKS(1)); // 1 ms yield - //if (ret != (int)buf_size) { - // ADC_MIC_DEBUG_PRINT("Partial read at chunk %d: got %d bytes (expected %zu)\n", - // chunk, ret, buf_size); - //} // Update global min/max for (size_t i = 0; i < chunk_samples; i++) { @@ -146,7 +168,7 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) { return last_buf_obj ? last_buf_obj : mp_obj_new_bytes(NULL, 0); } -MP_DEFINE_CONST_FUN_OBJ_1(adc_mic_read_obj, adc_mic_read); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adc_mic_read_obj, 6, 6, adc_mic_read); static const mp_rom_map_elem_t adc_mic_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adc_mic) }, diff --git a/scripts/build_mpos.sh b/scripts/build_mpos.sh index 13b8a770..60aaba0c 100755 --- a/scripts/build_mpos.sh +++ b/scripts/build_mpos.sh @@ -29,7 +29,7 @@ popd idfile="$codebasedir"/lvgl_micropython/lib/micropython/ports/esp32/main/idf_component.yml echo "Patching $idfile"... -echo "Check need to add esp32-camera..." +echo "Check need to add esp32-camera to $idfile" if ! grep esp32-camera "$idfile"; then echo "Adding esp32-camera to $idfile" echo " mpos/esp32-camera: @@ -37,7 +37,16 @@ if ! grep esp32-camera "$idfile"; then else echo "No need to add esp32-camera to $idfile" fi -echo "Resulting file:" + +echo "Check need to add adc_mic to $idfile" +if ! grep esp32-camera "$idfile"; then + echo "Adding esp32-camera to $idfile" + echo ' espressif/adc_mic: "*"' >> "$idfile" +else + echo "No need to add adc_mic to $idfile" +fi + +echo "Resulting $idfile file:" cat "$idfile" echo "Check need to add lvgl_micropython manifest to micropython-camera-API's manifest..."