diff --git a/software/main/CMakeLists.txt b/software/main/CMakeLists.txt index e8911ec..e8b8f71 100644 --- a/software/main/CMakeLists.txt +++ b/software/main/CMakeLists.txt @@ -1,3 +1,19 @@ -idf_component_register(SRCS "src/main.cpp" "src/helper.cpp" "src/wav.cpp" "src/storage.cpp" "src/i2s.cpp" "src/bluetooth.cpp" "src/avrcp.cpp" "src/a2dp.cpp" "src/twai.cpp" "src/volume.cpp" - INCLUDE_DIRS "include" - EMBED_FILES "assets/connect.wav" "assets/disconnect.wav") +idf_component_register( + SRCS + "src/main.cpp" + "src/helper.cpp" + "src/wav.cpp" + "src/storage.cpp" + "src/i2s.cpp" + "src/bluetooth.cpp" + "src/avrcp.cpp" + "src/a2dp.cpp" + "src/twai.cpp" + "src/volume.cpp" + "src/leds.cpp" + INCLUDE_DIRS + "include" + EMBED_FILES + "assets/connect.wav" + "assets/disconnect.wav" +) diff --git a/software/main/include/config.h b/software/main/include/config.h index 20adb8a..2bd226d 100644 --- a/software/main/include/config.h +++ b/software/main/include/config.h @@ -10,6 +10,7 @@ #define I2S_PIN_DATA GPIO_NUM_33 #define TWAI_PIN_CTX GPIO_NUM_5 #define TWAI_PIN_CRX GPIO_NUM_19 + #define LED_PIN_BLUETOOTH GPIO_NUM_2 #else #pragma message ( "Building for the production" ) #define I2S_PIN_BCK GPIO_NUM_5 @@ -17,5 +18,6 @@ #define I2S_PIN_DATA GPIO_NUM_17 #define TWAI_PIN_CTX GPIO_NUM_32 #define TWAI_PIN_CRX GPIO_NUM_35 + #define LED_PIN_BLUETOOTH GPIO_NUM_26 #endif diff --git a/software/main/include/leds.h b/software/main/include/leds.h new file mode 100644 index 0000000..959a3dd --- /dev/null +++ b/software/main/include/leds.h @@ -0,0 +1,12 @@ +#pragma once + +namespace leds { + enum Bluetooth { + DISCOVERABLE, + CONNECTED, + DISCONNECTED, + }; + + void init(); + void set_bluetooth(Bluetooth state); +} diff --git a/software/main/src/a2dp.cpp b/software/main/src/a2dp.cpp index c56edef..c476be6 100644 --- a/software/main/src/a2dp.cpp +++ b/software/main/src/a2dp.cpp @@ -9,6 +9,7 @@ #include "storage.h" #include "i2s.h" #include "bluetooth.h" +#include "leds.h" #define A2DP_TAG "APP_A2DP" @@ -21,6 +22,7 @@ static void handle_connection_state(uint16_t event, esp_a2d_cb_param_t* a2d) { static bool was_connected = false; if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { ESP_LOGI(A2DP_TAG, "ESP_A2D_CONNECTION_STATE_DISCONNECTED"); + leds::set_bluetooth(leds::Bluetooth::DISCONNECTED); if (a2d->conn_stat.disc_rsn == ESP_A2D_DISC_RSN_ABNORMAL && retry_count < 3) { ESP_LOGI(A2DP_TAG,"Connection try number: %d", retry_count); @@ -30,12 +32,12 @@ static void handle_connection_state(uint16_t event, esp_a2d_cb_param_t* a2d) { bluetooth::set_scan_mode(true); if (was_connected) { - vTaskDelay(300 / portTICK_PERIOD_MS); WAV_PLAY(disconnect); } } } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){ ESP_LOGI(A2DP_TAG, "ESP_A2D_CONNECTION_STATE_CONNECTED"); + leds::set_bluetooth(leds::Bluetooth::CONNECTED); bluetooth::set_scan_mode(false); retry_count = 0; diff --git a/software/main/src/bluetooth.cpp b/software/main/src/bluetooth.cpp index afa3b8e..9bb4906 100644 --- a/software/main/src/bluetooth.cpp +++ b/software/main/src/bluetooth.cpp @@ -9,6 +9,7 @@ #include "esp_spp_api.h" #include "bluetooth.h" +#include "leds.h" #include "helper.h" #include @@ -123,7 +124,7 @@ void bluetooth::init() { while (bt_stack_status != ESP_BLUEDROID_STATUS_ENABLED) { if (esp_bluedroid_enable() != ESP_OK) { ESP_LOGE(BT_TAG, "Failed to enable bluedroid"); - vTaskDelay(100 / portTICK_PERIOD_MS); + vTaskDelay(pdMS_TO_TICKS(100)); } else { ESP_LOGI(BT_TAG, "Bluedroid enabled"); } @@ -149,6 +150,11 @@ void bluetooth::init() { void bluetooth::set_scan_mode(bool connectable) { if (esp_bt_gap_set_scan_mode(connectable ? ESP_BT_CONNECTABLE : ESP_BT_NON_CONNECTABLE, connectable ? ESP_BT_GENERAL_DISCOVERABLE : ESP_BT_NON_DISCOVERABLE)) { ESP_LOGE(BT_TAG,"esp_bt_gap_set_scan_mode failed"); + return; + } + + if (connectable) { + leds::set_bluetooth(leds::Bluetooth::DISCOVERABLE); } } diff --git a/software/main/src/leds.cpp b/software/main/src/leds.cpp new file mode 100644 index 0000000..f8702ba --- /dev/null +++ b/software/main/src/leds.cpp @@ -0,0 +1,47 @@ +#include "driver/gpio.h" +#include "sys/lock.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "config.h" +#include "leds.h" + +static leds::Bluetooth state = leds::Bluetooth::DISCONNECTED; +static _lock_t lock; +static void update_bluetooth_led(void*) { + static uint8_t current_level = 0; + for (;;) { + _lock_acquire(&lock); + switch (state) { + case leds::Bluetooth::DISCOVERABLE: + current_level = !current_level; + break; + case leds::Bluetooth::CONNECTED: + current_level = 1; + break; + case leds::Bluetooth::DISCONNECTED: + current_level = 0; + break; + } + _lock_release(&lock); + + gpio_set_level(LED_PIN_BLUETOOTH, current_level); + vTaskDelay(pdMS_TO_TICKS(500)); + } +} + +void leds::init() { + // Setup the gpio pin + gpio_reset_pin(LED_PIN_BLUETOOTH); + gpio_set_direction(LED_PIN_BLUETOOTH, GPIO_MODE_OUTPUT); + + // Start the task + xTaskCreate(update_bluetooth_led, "Bluetooth LED", 1024, nullptr, 0, nullptr); +} + +void leds::set_bluetooth(leds::Bluetooth s) { + _lock_acquire(&lock); + state = s; + _lock_release(&lock); +} diff --git a/software/main/src/main.cpp b/software/main/src/main.cpp index 8e8e805..0c05f0e 100644 --- a/software/main/src/main.cpp +++ b/software/main/src/main.cpp @@ -1,6 +1,5 @@ #include -#include "esp_avrc_api.h" #include "esp_log.h" #include "esp_system.h" @@ -11,6 +10,7 @@ #include "a2dp.h" #include "twai.h" #include "volume.h" +#include "leds.h" #define APP_TAG "APP" @@ -18,6 +18,8 @@ extern "C" void app_main() { ESP_LOGI(APP_TAG, "Starting Car Stereo"); ESP_LOGI(APP_TAG, "Available Heap: %u", esp_get_free_heap_size()); + leds::init(); + nvs::init(); i2s::init(); bluetooth::init();