Skip to content

Commit cdb2651

Browse files
Work on adc_mic
1 parent 1b82fb2 commit cdb2651

3 files changed

Lines changed: 60 additions & 29 deletions

File tree

c_mpos/micropython.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ add_library(usermod_c_mpos INTERFACE)
66

77
set(MPOS_C_INCLUDES)
88

9-
#set(MPOS_C_INCLUDES
10-
# ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/include/
11-
# ${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/interface/
12-
#)
9+
set(MPOS_C_INCLUDES
10+
${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/include/
11+
${CMAKE_CURRENT_LIST_DIR}/../lvgl_micropython/lib/micropython/ports/esp32/managed_components/espressif__esp_codec_dev/interface/
12+
)
1313

1414
set(MPOS_C_SOURCES
15-
# ${CMAKE_CURRENT_LIST_DIR}/src/adc_mic.c
15+
${CMAKE_CURRENT_LIST_DIR}/src/adc_mic.c
1616
${CMAKE_CURRENT_LIST_DIR}/src/quirc_decode.c
1717
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/identify.c
1818
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/version_db.c

c_mpos/src/adc_mic.c

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,54 @@
1212

1313
#define ADC_MIC_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__)
1414

15-
static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
16-
// Extract chunk_samples from argument
17-
size_t chunk_samples = mp_obj_get_int(chunk_samples_obj);
15+
static mp_obj_t adc_mic_read(size_t n_args, const mp_obj_t *args) {
16+
// Extract arguments
17+
// args[0]: chunk_samples
18+
// args[1]: unit_id
19+
// args[2]: adc_channel_list
20+
// args[3]: adc_channel_num
21+
// args[4]: sample_rate_hz
22+
// args[5]: atten
23+
24+
size_t chunk_samples = mp_obj_get_int(args[0]);
25+
int unit_id = mp_obj_get_int(args[1]);
1826

27+
mp_obj_t channel_list_obj = args[2];
28+
size_t channel_list_len;
29+
mp_obj_t *channel_list_items;
30+
mp_obj_get_array(channel_list_obj, &channel_list_len, &channel_list_items);
31+
32+
int adc_channel_num = mp_obj_get_int(args[3]);
33+
int sample_rate_hz = mp_obj_get_int(args[4]);
34+
int atten = mp_obj_get_int(args[5]);
35+
1936
ADC_MIC_DEBUG_PRINT("Starting adc_mic_read...\n");
2037
ADC_MIC_DEBUG_PRINT("CONFIG_ADC_MIC_TASK_CORE: %d\n", CONFIG_ADC_MIC_TASK_CORE);
2138

22-
// Configuration (your current manual setup with 2.5 dB atten)
39+
if (adc_channel_num > 10) {
40+
mp_raise_ValueError("Too many channels (max 10)");
41+
}
42+
if (channel_list_len < adc_channel_num) {
43+
mp_raise_ValueError("adc_channel_list shorter than adc_channel_num");
44+
}
45+
46+
uint8_t channels[10];
47+
for (size_t i = 0; i < adc_channel_num; i++) {
48+
channels[i] = (uint8_t)mp_obj_get_int(channel_list_items[i]);
49+
}
50+
51+
// Configuration
2352
audio_codec_adc_cfg_t cfg = {
2453
.handle = NULL,
2554
.max_store_buf_size = 1024 * 2,
2655
.conv_frame_size = 1024,
27-
.unit_id = ADC_UNIT_1,
28-
.adc_channel_list = ((uint8_t[]){ADC_CHANNEL_0}),
29-
.adc_channel_num = 1,
30-
.sample_rate_hz = 16000,
31-
//.atten = ADC_ATTEN_DB_0, // values always 16380
32-
//.atten = ADC_ATTEN_DB_2_5, // values always 16380
33-
.atten = ADC_ATTEN_DB_6, // values around 12500 +/- 320 (silence) or 4000 (loud talk)
34-
//.atten = ADC_ATTEN_DB_11, // values around -1130 +/- 160 (silence)
56+
.unit_id = (adc_unit_t)unit_id,
57+
.adc_channel_list = channels,
58+
.adc_channel_num = adc_channel_num, // can probably just count adc_channel_list
59+
.sample_rate_hz = sample_rate_hz,
60+
.atten = (adc_atten_t)atten,
3561
};
36-
ADC_MIC_DEBUG_PRINT("Config created for channel %d, sample rate %d, atten %d\n", ADC_CHANNEL_0, 16000, cfg.atten);
62+
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);
3763

3864
// ────────────────────────────────────────────────
3965
// Initialization (same as before)
@@ -55,8 +81,8 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
5581
}
5682

5783
esp_codec_dev_sample_info_t fs = {
58-
.sample_rate = 16000,
59-
.channel = 1,
84+
.sample_rate = sample_rate_hz,
85+
.channel = adc_channel_num,
6086
.bits_per_sample = 16,
6187
};
6288
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) {
6995
// ────────────────────────────────────────────────
7096
// Small reusable buffer + tracking variables
7197
// ────────────────────────────────────────────────
72-
const size_t buf_size = chunk_samples * sizeof(int16_t);
98+
const size_t buf_size = chunk_samples * sizeof(int16_t) * adc_channel_num;
7399
int16_t *audio_buffer = heap_caps_malloc(buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
74-
//int16_t *audio_buffer = heap_caps_thread.start_new_thread(testit, ())_malloc_prefer(buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT);
100+
//int16_t *audio_buffer = heap_caps_malloc_prefer(buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT);
75101
if (audio_buffer == NULL) {
76102
esp_codec_dev_close(dev);
77103
esp_codec_dev_delete(dev);
@@ -99,10 +125,6 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
99125
break;
100126
}
101127
vTaskDelay(pdMS_TO_TICKS(1)); // 1 ms yield
102-
//if (ret != (int)buf_size) {
103-
// ADC_MIC_DEBUG_PRINT("Partial read at chunk %d: got %d bytes (expected %zu)\n",
104-
// chunk, ret, buf_size);
105-
//}
106128

107129
// Update global min/max
108130
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) {
146168

147169
return last_buf_obj ? last_buf_obj : mp_obj_new_bytes(NULL, 0);
148170
}
149-
MP_DEFINE_CONST_FUN_OBJ_1(adc_mic_read_obj, adc_mic_read);
171+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adc_mic_read_obj, 6, 6, adc_mic_read);
150172

151173
static const mp_rom_map_elem_t adc_mic_module_globals_table[] = {
152174
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adc_mic) },

scripts/build_mpos.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@ popd
2929
idfile="$codebasedir"/lvgl_micropython/lib/micropython/ports/esp32/main/idf_component.yml
3030
echo "Patching $idfile"...
3131

32-
echo "Check need to add esp32-camera..."
32+
echo "Check need to add esp32-camera to $idfile"
3333
if ! grep esp32-camera "$idfile"; then
3434
echo "Adding esp32-camera to $idfile"
3535
echo " mpos/esp32-camera:
3636
git: https://github.com/MicroPythonOS/esp32-camera" >> "$idfile"
3737
else
3838
echo "No need to add esp32-camera to $idfile"
3939
fi
40-
echo "Resulting file:"
40+
41+
echo "Check need to add adc_mic to $idfile"
42+
if ! grep esp32-camera "$idfile"; then
43+
echo "Adding esp32-camera to $idfile"
44+
echo ' espressif/adc_mic: "*"' >> "$idfile"
45+
else
46+
echo "No need to add adc_mic to $idfile"
47+
fi
48+
49+
echo "Resulting $idfile file:"
4150
cat "$idfile"
4251

4352
echo "Check need to add lvgl_micropython manifest to micropython-camera-API's manifest..."

0 commit comments

Comments
 (0)