Arduino A2DP
BluetoothA2DPCommon.h
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 
15 #pragma once
16 
17 #include "config.h"
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <math.h>
25 #include "freertos/FreeRTOS.h" // needed for ESP Arduino < 2.0
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"
31 #include "esp_bt.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"
39 #include "nvs.h"
40 #include "nvs_flash.h"
41 #include "SoundData.h"
42 #include "VolumeControl.h"
43 
44 #ifdef ARDUINO_ARCH_ESP32
45 #include "esp32-hal-log.h"
46 #include "esp32-hal-bt.h"
47 #else
48 #include "esp_log.h"
49 
50 extern "C" bool btStart();
51 extern "C" void delay(long millis);
52 extern "C" unsigned long millis();
53 
54 #endif
55 
56 // Support for old and new IDF version
57 #if !defined(CURRENT_ESP_IDF) && !defined(I2S_COMM_FORMAT_STAND_I2S)
58 // support for old idf releases
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)
61 #endif
62 
63 // prevent compile errors for ESP32C3
64 #ifdef ESP32C3
65 // DAC mode not supported!
66 #define I2S_MODE_DAC_BUILT_IN 0
67 #endif
68 
72 typedef void (* app_callback_t) (uint16_t event, void *param);
73 
75 typedef struct {
76  uint16_t sig;
77  uint16_t event;
78  app_callback_t cb;
79  void *param;
80 } app_msg_t;
81 
82 
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)
87 
88 
89 
96  public:
98  virtual ~BluetoothA2DPCommon() = default;
99 
101  void set_auto_reconnect(bool active){
102  this->auto_reconnect = active;
103  }
104 
106  virtual void disconnect();
107 
109  virtual void end(bool releaseMemory=false);
110 
112  virtual bool is_connected() = 0;
113 
114  // /// obsolete: please use is_connected
115  // DEPRECATED
116  // virtual bool isConnected(){
117  // return is_connected();
118  // }
119 
121  virtual void set_volume(uint8_t volume) = 0;
122 
124  virtual int get_volume() = 0;
125 
127  virtual void set_volume_control(VolumeControl *ptr){
128  volume_control_ptr = ptr;
129  }
130 
132  virtual esp_a2d_audio_state_t get_audio_state();
133 
135  virtual esp_a2d_connection_state_t get_connection_state();
136 
138  virtual void set_on_connection_state_changed(void (*callBack)(esp_a2d_connection_state_t state, void *), void *obj=nullptr);
139 
141  virtual void set_on_audio_state_changed(void (*callBack)(esp_a2d_audio_state_t state, void*), void* obj=nullptr);
142 
144  virtual void debounce(void(*cb)(void),int ms);
145 
147  void log_free_heap();
148 
150  const char* to_str(esp_a2d_connection_state_t state);
151 
153  const char* to_str(esp_a2d_audio_state_t state);
154 
156  const char* to_str(esp_bd_addr_t bda);
157 
159  void set_task_priority(UBaseType_t priority){
160  task_priority = priority;
161  }
162 
163 #ifdef CURRENT_ESP_IDF
165  virtual void set_discoverability(esp_bt_discovery_mode_t d);
166 #endif
167 
168  protected:
169  bool auto_reconnect=true;
170  uint32_t debounce_ms = 0;
171  DefaultVolumeControl default_volume_control;
172  VolumeControl *volume_control_ptr = nullptr;
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;
186 #endif
187 
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();
194  // change the scan mode
195  virtual void set_scan_mode_connectable(bool connectable);
196 
199  return volume_control_ptr !=nullptr ? volume_control_ptr : &default_volume_control;
200  }
201 
202 
203 
204 };
205 
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