25 #include "freertos/FreeRTOS.h"
26 #include "freertos/timers.h"
27 #include "freertos/xtensa_api.h"
28 #include "freertos/FreeRTOSConfig.h"
29 #include "freertos/queue.h"
30 #include "freertos/task.h"
32 #include "esp_bt_main.h"
33 #include "esp_bt_device.h"
34 #include "esp_gap_bt_api.h"
35 #include "esp_a2dp_api.h"
36 #include "driver/i2s.h"
37 #include "esp_avrc_api.h"
38 #include "esp_spp_api.h"
40 #include "nvs_flash.h"
41 #include "SoundData.h"
42 #include "VolumeControl.h"
44 #ifdef ARDUINO_ARCH_ESP32
45 #include "esp32-hal-log.h"
46 #include "esp32-hal-bt.h"
50 extern "C" bool btStart();
51 extern "C" void delay(
long millis);
52 extern "C" unsigned long millis();
57 #if !defined(CURRENT_ESP_IDF) && !defined(I2S_COMM_FORMAT_STAND_I2S)
59 #define I2S_COMM_FORMAT_STAND_I2S (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
60 #define I2S_COMM_FORMAT_STAND_MSB (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
66 #define I2S_MODE_DAC_BUILT_IN 0
72 typedef void (* app_callback_t) (uint16_t event,
void *param);
83 #define BT_AV_TAG "BT_AV"
84 #define BT_RC_CT_TAG "RCCT"
85 #define BT_APP_TAG "BT_API"
86 #define APP_RC_CT_TL_GET_CAPS (0)
102 this->auto_reconnect = active;
109 virtual void end(
bool releaseMemory=
false);
128 volume_control_ptr = ptr;
144 virtual void debounce(
void(*cb)(
void),
int ms);
150 const char*
to_str(esp_a2d_connection_state_t state);
153 const char*
to_str(esp_a2d_audio_state_t state);
156 const char*
to_str(esp_bd_addr_t bda);
160 task_priority = priority;
163 #ifdef CURRENT_ESP_IDF
165 virtual void set_discoverability(esp_bt_discovery_mode_t d);
169 bool auto_reconnect=
true;
170 uint32_t debounce_ms = 0;
173 esp_bd_addr_t last_connection = {0,0,0,0,0,0};
174 bool is_start_disabled =
false;
175 void (*connection_state_callback)(esp_a2d_connection_state_t state,
void* obj) =
nullptr;
176 void (*audio_state_callback)(esp_a2d_audio_state_t state,
void* obj) =
nullptr;
177 void *connection_state_obj =
nullptr;
178 void *audio_state_obj =
nullptr;
179 const char *m_a2d_conn_state_str[4] = {
"Disconnected",
"Connecting",
"Connected",
"Disconnecting"};
180 const char *m_a2d_audio_state_str[3] = {
"Suspended",
"Stopped",
"Started"};
181 esp_a2d_audio_state_t audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
182 esp_a2d_connection_state_t connection_state = ESP_A2D_CONNECTION_STATE_DISCONNECTED;
183 UBaseType_t task_priority = configMAX_PRIORITIES - 3;
184 #ifdef CURRENT_ESP_IDF
185 esp_bt_discovery_mode_t discoverability = ESP_BT_GENERAL_DISCOVERABLE;
188 virtual const char* last_bda_nvs_name() = 0;
189 virtual void get_last_connection();
190 virtual void set_last_connection(esp_bd_addr_t bda);
191 virtual void clean_last_connection();
192 virtual void connect_to_last_device();
193 virtual bool has_last_connection();
195 virtual void set_scan_mode_connectable(
bool connectable);
199 return volume_control_ptr !=
nullptr ? volume_control_ptr : &default_volume_control;
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:95
virtual bool is_connected()=0
Checks if A2DP is connected.
virtual VolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:198
virtual void set_volume_control(VolumeControl *ptr)
you can define a custom VolumeControl implementation
Definition: BluetoothA2DPCommon.h:127
const char * to_str(esp_a2d_connection_state_t state)
converts esp_a2d_connection_state_t to a string
Definition: BluetoothA2DPCommon.cpp:211
virtual ~BluetoothA2DPCommon()=default
Destructor.
virtual esp_a2d_connection_state_t get_connection_state()
Determine the connection state.
Definition: BluetoothA2DPCommon.cpp:22
void set_auto_reconnect(bool active)
activate / deactivate the automatic reconnection to the last address (per default this is on)
Definition: BluetoothA2DPCommon.h:101
virtual void set_on_audio_state_changed(void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
Set the callback that is called when the audio state is changed.
Definition: BluetoothA2DPCommon.cpp:191
void set_task_priority(UBaseType_t priority)
defines the task priority (the default value is configMAX_PRIORITIES - 3)
Definition: BluetoothA2DPCommon.h:159
virtual esp_a2d_audio_state_t get_audio_state()
Determine the actual audio state.
Definition: BluetoothA2DPCommon.cpp:18
virtual void end(bool releaseMemory=false)
Closes the connection and stops A2DP.
Definition: BluetoothA2DPCommon.cpp:40
void log_free_heap()
Logs the free heap.
Definition: BluetoothA2DPCommon.cpp:206
virtual void debounce(void(*cb)(void), int ms)
Prevents that the same method is executed multiple times within the indicated time limit.
Definition: BluetoothA2DPCommon.cpp:197
virtual void set_on_connection_state_changed(void(*callBack)(esp_a2d_connection_state_t state, void *), void *obj=nullptr)
Set the callback that is called when the connection state is changed.
Definition: BluetoothA2DPCommon.cpp:185
virtual void disconnect()
Closes the connection.
Definition: BluetoothA2DPCommon.cpp:27
virtual void set_volume(uint8_t volume)=0
Changes the volume (use the range 0-100)
virtual int get_volume()=0
Determines the volume.
Default implementation for handling of the volume of the audio data.
Definition: VolumeControl.h:66
Abstract class for handling of the volume of the audio data.
Definition: VolumeControl.h:27
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:75
app_callback_t cb
Definition: BluetoothA2DPCommon.h:78
uint16_t event
Definition: BluetoothA2DPCommon.h:77
uint16_t sig
Definition: BluetoothA2DPCommon.h:76
void * param
Definition: BluetoothA2DPCommon.h:79