From f3a8065213c772c34bbfc44f7d68f332d9d23219 Mon Sep 17 00:00:00 2001 From: Alexander Bobkov Date: Wed, 25 Dec 2024 11:46:34 -0500 Subject: [PATCH] ESP-IDF Robot Controls --- ESP-IDF_Robot/main/blink_example_main.c | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ESP-IDF_Robot/main/blink_example_main.c b/ESP-IDF_Robot/main/blink_example_main.c index 2b7ad6ad3..b6a335012 100644 --- a/ESP-IDF_Robot/main/blink_example_main.c +++ b/ESP-IDF_Robot/main/blink_example_main.c @@ -802,6 +802,35 @@ void app_main(void) /* Toggle the LED state */ gpio_set_level(BLINK_GPIO, s_led_state); s_led_state = !s_led_state; + + + + // ADC + adc_ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0); + if (ret == ESP_OK) { + ESP_LOGI("TASK", "ret is %x, ret_num is %"PRIu32" bytes", ret, ret_num); + for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) { + adc_digi_output_data_t *p = (adc_digi_output_data_t*)&result[i]; + uint32_t chan_num = EXAMPLE_ADC_GET_CHANNEL(p); + uint32_t data = EXAMPLE_ADC_GET_DATA(p); + /* Check the channel number validation, the data is invalid if the channel num exceed the maximum channel */ + if (chan_num < SOC_ADC_CHANNEL_NUM(EXAMPLE_ADC_UNIT)) { + ESP_LOGI(TAG, "Unit: %s, Channel: %"PRIu32", Value: %"PRIx32, unit, chan_num, data); + } else { + ESP_LOGW(TAG, "Invalid data [%s_%"PRIu32"_%"PRIx32"]", unit, chan_num, data); + } + } + /** + * Because printing is slow, so every time you call `ulTaskNotifyTake`, it will immediately return. + * To avoid a task watchdog timeout, add a delay here. When you replace the way you process the data, + * usually you don't need this delay (as this task will block for a while). + */ + vTaskDelay(1); + } else if (ret == ESP_ERR_TIMEOUT) { + //We try to read `EXAMPLE_READ_LEN` until API returns timeout, which means there's no available data + break; + } + vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); } }