Arduino A2DP
Loading...
Searching...
No Matches
BluetoothA2DPCommon.h
Go to the documentation of this file.
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// Copyright 2020 Phil Schatzmann
14
25#pragma once
26
27#include "config.h"
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <stdbool.h>
33#include <string.h>
34#include <math.h>
35#include "freertos/FreeRTOS.h" // needed for ESP Arduino < 2.0
36#include "freertos/timers.h"
37#include "freertos/xtensa_api.h"
38#include "freertos/FreeRTOSConfig.h"
39#include "freertos/queue.h"
40#include "freertos/task.h"
41#include "esp_bt.h"
42#include "esp_bt_main.h"
43#include "esp_bt_device.h"
44#include "esp_gap_bt_api.h"
45#include "esp_a2dp_api.h"
46#include "driver/i2s.h"
47#include "esp_avrc_api.h"
48#include "esp_spp_api.h"
49#include "nvs.h"
50#include "nvs_flash.h"
51#include "SoundData.h"
52#include "A2DPVolumeControl.h"
53#include "esp_task_wdt.h"
54
55#ifdef ARDUINO_ARCH_ESP32
56#include "esp32-hal-log.h"
57#include "esp32-hal-bt.h"
58#else
59#include "esp_log.h"
60
61extern "C" bool btStart();
62extern "C" void delay(long millis);
63extern "C" unsigned long millis();
64
65#endif
66
67// Support for old and new IDF version
68#if !defined(ESP_IDF_4) && !defined(I2S_COMM_FORMAT_STAND_I2S)
69// support for old idf releases
70# define I2S_COMM_FORMAT_STAND_I2S (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
71# define I2S_COMM_FORMAT_STAND_MSB (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
72# define I2S_COMM_FORMAT_STAND_PCM_LONG (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
73# define I2S_COMM_FORMAT_STAND_PCM_SHORT (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_SHORT)
74
75#endif
76
80typedef void (* app_callback_t) (uint16_t event, void *param);
81
83typedef struct {
84 uint16_t sig;
85 uint16_t event;
87 void *param;
88} app_msg_t;
89
90
91#define BT_AV_TAG "BT_AV"
92#define BT_RC_CT_TAG "RCCT"
93#define BT_APP_TAG "BT_API"
94#define APP_RC_CT_TL_GET_CAPS (0)
95
96enum ReconnectStatus { NoReconnect, AutoReconnect, IsReconnecting};
97
98
105 public:
107 virtual ~BluetoothA2DPCommon() = default;
108
110 void set_auto_reconnect(bool active);
112 virtual void disconnect();
113
115 virtual bool reconnect();
116
117 virtual bool connect_to(esp_bd_addr_t peer);
119 virtual void set_connected(bool active);
120
122 virtual void end(bool releaseMemory=false);
123
125 virtual bool is_connected() = 0;
126
128 virtual void set_volume(uint8_t volume){
129 ESP_LOGI(BT_AV_TAG, "set_volume: %d", volume);
130 volume_value = volume;
131 volume_control()->set_volume(volume);
132 volume_control()->set_enabled(true);
133 is_volume_used = true;
134 }
135
137 virtual int get_volume(){
138 return is_volume_used ? volume_value : 0;
139 }
140
143 volume_control_ptr = ptr;
144 }
145
147 virtual esp_a2d_audio_state_t get_audio_state();
148
150 virtual esp_a2d_connection_state_t get_connection_state();
151
154 virtual void set_on_connection_state_changed(void (*callBack)(esp_a2d_connection_state_t state, void *), void *obj=nullptr);
155
158 virtual void set_on_audio_state_changed_post(void (*callBack)(esp_a2d_audio_state_t state, void*), void* obj=nullptr);
159
161 virtual void set_on_audio_state_changed(void (*callBack)(esp_a2d_audio_state_t state, void*), void* obj=nullptr);
162
164 virtual void debounce(void(*cb)(void),int ms);
165
167 void log_free_heap();
168
170 const char* to_str(esp_a2d_connection_state_t state);
171
173 const char* to_str(esp_a2d_audio_state_t state);
174
176 const char* to_str(esp_bd_addr_t bda);
177
179 void set_task_priority(UBaseType_t priority){
180 task_priority = priority;
181 }
182
183
185 void set_task_core(BaseType_t core){
186 task_core = core;
187 }
188
190 void set_event_queue_size(int size){
191 event_queue_size = size;
192 }
193
195 void set_event_stack_size(int size){
196 event_stack_size = size;
197 }
198
200 virtual esp_bd_addr_t* get_last_peer_address() {
201 return &last_connection;
202 }
203
204#ifdef ESP_IDF_4
206 virtual void set_discoverability(esp_bt_discovery_mode_t d);
207#endif
208
209 protected:
210 esp_bd_addr_t peer_bd_addr;
211 ReconnectStatus reconnect_status = AutoReconnect;
212 unsigned long reconnect_timout=0;
213 unsigned int default_reconnect_timout=10000;
214 bool is_autoreconnect_allowed = false;
215 uint32_t debounce_ms = 0;
216 A2DPDefaultVolumeControl default_volume_control;
217 A2DPVolumeControl *volume_control_ptr = nullptr;
218 esp_bd_addr_t last_connection = {0,0,0,0,0,0};
219 bool is_start_disabled = false;
220 void (*connection_state_callback)(esp_a2d_connection_state_t state, void* obj) = nullptr;
221 void (*audio_state_callback)(esp_a2d_audio_state_t state, void* obj) = nullptr;
222 void (*audio_state_callback_post)(esp_a2d_audio_state_t state, void* obj) = nullptr;
223 void *connection_state_obj = nullptr;
224 void *audio_state_obj = nullptr;
225 void *audio_state_obj_post = nullptr;
226 const char *m_a2d_conn_state_str[4] = {"Disconnected", "Connecting", "Connected", "Disconnecting"};
227 const char *m_a2d_audio_state_str[3] = {"Suspended", "Stopped", "Started"};
228 esp_a2d_audio_state_t audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
229 esp_a2d_connection_state_t connection_state = ESP_A2D_CONNECTION_STATE_DISCONNECTED;
230 UBaseType_t task_priority = configMAX_PRIORITIES - 10;
231 // volume
232 uint8_t volume_value = 0;
233 bool is_volume_used = false;
234 BaseType_t task_core = 1;
235
236 int event_queue_size = 20;
237 int event_stack_size = 3072;
238
239
240#ifdef ESP_IDF_4
241 esp_bt_discovery_mode_t discoverability = ESP_BT_GENERAL_DISCOVERABLE;
242#endif
243
244 virtual esp_err_t esp_a2d_connect(esp_bd_addr_t peer) = 0;
245 virtual const char* last_bda_nvs_name() = 0;
246 virtual void get_last_connection();
247 virtual void set_last_connection(esp_bd_addr_t bda);
248 virtual void clean_last_connection();
249 virtual bool has_last_connection();
250 // change the scan mode
251 virtual void set_scan_mode_connectable(bool connectable);
252
255 return volume_control_ptr !=nullptr ? volume_control_ptr : &default_volume_control;
256 }
257};
258
void delay(long millis)
call vTaskDelay to deley for the indicated number of milliseconds
Definition: BluetoothA2DPCommon.cpp:342
bool btStart()
Startup logic as implemented by Arduino - This is not available if we use this library outside of Ard...
Definition: BluetoothA2DPCommon.cpp:316
void(* app_callback_t)(uint16_t event, void *param)
handler for the dispatched work
Definition: BluetoothA2DPCommon.h:80
Default implementation for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:88
Abstract class for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:28
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:104
void set_event_stack_size(int size)
Defines the stack size of the event task (in bytes)
Definition: BluetoothA2DPCommon.h:195
virtual void set_connected(bool active)
Calls disconnect or reconnect.
Definition: BluetoothA2DPCommon.cpp:52
virtual void set_volume(uint8_t volume)
Sets the volume (range 0 - 255)
Definition: BluetoothA2DPCommon.h:128
virtual int get_volume()
Determines the actual volume.
Definition: BluetoothA2DPCommon.h:137
virtual bool is_connected()=0
Checks if A2DP is connected.
virtual void set_on_audio_state_changed_post(void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
Definition: BluetoothA2DPCommon.cpp:226
const char * to_str(esp_a2d_connection_state_t state)
converts esp_a2d_connection_state_t to a string
Definition: BluetoothA2DPCommon.cpp:247
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.cpp:28
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:219
void set_task_priority(UBaseType_t priority)
defines the task priority (the default value is configMAX_PRIORITIES - 10)
Definition: BluetoothA2DPCommon.h:179
void set_task_core(BaseType_t core)
Defines the core which is used to start the tasks (to process the events and audio queue)
Definition: BluetoothA2DPCommon.h:185
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:76
virtual void set_volume_control(A2DPVolumeControl *ptr)
you can define a custom VolumeControl implementation
Definition: BluetoothA2DPCommon.h:142
void log_free_heap()
Logs the free heap.
Definition: BluetoothA2DPCommon.cpp:242
virtual A2DPVolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:254
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:233
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:212
virtual void disconnect()
Closes the connection.
Definition: BluetoothA2DPCommon.cpp:62
virtual bool reconnect()
Reconnects to the last device.
Definition: BluetoothA2DPCommon.cpp:33
virtual esp_bd_addr_t * get_last_peer_address()
Provides the address of the last device.
Definition: BluetoothA2DPCommon.h:200
void set_event_queue_size(int size)
Defines the queue size of the event task.
Definition: BluetoothA2DPCommon.h:190
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:83
app_callback_t cb
Definition: BluetoothA2DPCommon.h:86
uint16_t event
Definition: BluetoothA2DPCommon.h:85
uint16_t sig
Definition: BluetoothA2DPCommon.h:84
void * param
Definition: BluetoothA2DPCommon.h:87