Added led to indicate bluetooth status

This commit is contained in:
Dreaded_X 2023-04-01 04:25:58 +02:00
parent 8505ac6049
commit e9df974c9e
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
7 changed files with 93 additions and 6 deletions

View File

@ -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"
)

View File

@ -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

View File

@ -0,0 +1,12 @@
#pragma once
namespace leds {
enum Bluetooth {
DISCOVERABLE,
CONNECTED,
DISCONNECTED,
};
void init();
void set_bluetooth(Bluetooth state);
}

View File

@ -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;

View File

@ -9,6 +9,7 @@
#include "esp_spp_api.h"
#include "bluetooth.h"
#include "leds.h"
#include "helper.h"
#include <cstring>
@ -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);
}
}

View File

@ -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);
}

View File

@ -1,6 +1,5 @@
#include <cmath>
#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();