Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions examples/voice_agent/main/board.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include "codec_init.h"
#include "codec_board.h"
#include "driver/temperature_sensor.h"
Expand All @@ -13,17 +14,25 @@ static temperature_sensor_handle_t temp_sensor = NULL;
void board_init()
{
ESP_LOGI(TAG, "Initializing board");

// Initialize board support package and LEDs

// Initialize NVS first - required by many components
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "NVS initialized");

// Initialize board support package I2C
bsp_i2c_init();
bsp_leds_init();
bsp_led_set(BSP_LED_RED, true);
bsp_led_set(BSP_LED_BLUE, true);
ESP_LOGI(TAG, "I2C initialized");

// Initialize temperature sensor
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
ESP_LOGI(TAG, "Temperature sensor initialized");

// Initialize codec board
set_codec_board_type(CONFIG_CODEC_BOARD_TYPE);
Expand All @@ -33,6 +42,9 @@ void board_init()
.reuse_dev = false
};
init_codec(&cfg);
ESP_LOGI(TAG, "Codec initialized");

ESP_LOGI(TAG, "Board initialization complete");
}

float board_get_temp(void)
Expand Down
95 changes: 48 additions & 47 deletions examples/voice_agent/main/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,53 +40,54 @@ static void on_participant_info(const livekit_participant_info_t* info, void* ct

/// Invoked by a remote participant to set the state of an on-board LED.
static void set_led_state(const livekit_rpc_invocation_t* invocation, void* ctx)
{
if (invocation->payload == NULL) {
livekit_rpc_return_error("Missing payload");
return;
}
cJSON *root = cJSON_Parse(invocation->payload);
if (!root) {
livekit_rpc_return_error("Invalid JSON");
return;
}

char* error = NULL;
do {
const cJSON *color_entry = cJSON_GetObjectItemCaseSensitive(root, "color");
const cJSON *state_entry = cJSON_GetObjectItemCaseSensitive(root, "state");
if (!cJSON_IsString(color_entry) || !cJSON_IsBool(state_entry)) {
error = "Unexpected JSON format";
break;
}

const char *color = color_entry->valuestring;
bool state = cJSON_IsTrue(state_entry);

bsp_led_t led;
if (strncmp(color, "red", 3) == 0) {
led = BSP_LED_RED;
} else if (strncmp(color, "blue", 4) == 0) {
led = BSP_LED_BLUE;
} else {
error = "Unsupported color";
break;
}
// There is a known bug in the BSP component, so we need to invert the state for now.
// See https://github.com/espressif/esp-bsp/pull/610.
if (bsp_led_set(led, !state) != ESP_OK) {
error = "Failed to set LED state";
break;
}
} while (0);

if (!error) {
livekit_rpc_return_ok(NULL);
} else {
livekit_rpc_return_error(error);
}
// Perform necessary cleanup after returning an RPC result.
cJSON_Delete(root);
{
livekit_rpc_return_ok(NULL);
// if (invocation->payload == NULL) {
// livekit_rpc_return_error("Missing payload");
// return;
// }
// cJSON *root = cJSON_Parse(invocation->payload);
// if (!root) {
// livekit_rpc_return_error("Invalid JSON");
// return;
// }

// char* error = NULL;
// do {
// const cJSON *color_entry = cJSON_GetObjectItemCaseSensitive(root, "color");
// const cJSON *state_entry = cJSON_GetObjectItemCaseSensitive(root, "state");
// if (!cJSON_IsString(color_entry) || !cJSON_IsBool(state_entry)) {
// error = "Unexpected JSON format";
// break;
// }

// const char *color = color_entry->valuestring;
// bool state = cJSON_IsTrue(state_entry);

// bsp_led_t led;
// if (strncmp(color, "red", 3) == 0) {
// led = BSP_LED_RED;
// } else if (strncmp(color, "blue", 4) == 0) {
// led = BSP_LED_BLUE;
// } else {
// error = "Unsupported color";
// break;
// }
// // There is a known bug in the BSP component, so we need to invert the state for now.
// // See https://github.com/espressif/esp-bsp/pull/610.
// if (bsp_led_set(led, !state) != ESP_OK) {
// error = "Failed to set LED state";
// break;
// }
// } while (0);

// if (!error) {
// livekit_rpc_return_ok(NULL);
// } else {
// livekit_rpc_return_error(error);
// }
// // Perform necessary cleanup after returning an RPC result.
// cJSON_Delete(root);
}

/// Invoked by a remote participant to get the current CPU temperature.
Expand Down
4 changes: 2 additions & 2 deletions examples/voice_agent/main/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ dependencies:
path: ../../../components/third_party/esp-webrtc-solution/components/codec_board
# Eventually, the BSP will perform all the functions of the codec_board component.
# It currently is used because codec_board is required to support AEC.
esp32_s3_korvo_2:
version: ">=0.1"
esp-box-3:
version: ">=3.0.1"
render_impl:
path: ../../../components/third_party/esp-webrtc-solution/components/av_render/render_impl
livekit_sandbox:
Expand Down
20 changes: 20 additions & 0 deletions examples/voice_agent/main/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "esp_log.h"
#include "sdkconfig.h"
#include "media_lib_adapter.h"
#include "media_lib_os.h"
#include "livekit.h"
Expand All @@ -7,6 +8,8 @@
#include "board.h"
#include "example.h"

static const char *TAG = "main";

static void run_async_join_room(void *arg)
{
join_room(); // See example.c
Expand All @@ -23,9 +26,26 @@ static int network_event_handler(bool connected)

void app_main(void)
{
ESP_LOGI(TAG, "=== ESP32-S3 Box-3 Voice Agent Starting ===");

ESP_LOGI(TAG, "Setting log level...");
esp_log_level_set("*", ESP_LOG_INFO);

ESP_LOGI(TAG, "Initializing LiveKit system...");
livekit_system_init();
ESP_LOGI(TAG, "LiveKit system initialized");

ESP_LOGI(TAG, "Initializing board...");
board_init();
ESP_LOGI(TAG, "Board initialized");

ESP_LOGI(TAG, "Initializing media...");
media_init();
ESP_LOGI(TAG, "Media initialized");

ESP_LOGI(TAG, "Initializing network...");
network_init(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD, network_event_handler);
ESP_LOGI(TAG, "Network initialized");

ESP_LOGI(TAG, "=== Voice Agent initialization complete ===");
}
8 changes: 6 additions & 2 deletions examples/voice_agent/partitions.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
nvs, data, nvs, 0x9000, 0x6000,
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 3M,
factory, app, factory, 0x10000, 6M,
ota_0, app, ota_0, 0x700000, 2M,
storage, data, spiffs, 0x900000, 2M,
model, data, spiffs, 0xb00000, 4000K
Loading