2019年1月31日 星期四

DIY - ESP32:ESP32 藍牙(Bluetooth)COEX 程式(八十三)

DIY - ESP32:ESP32 藍牙(Bluetooth)COEX 程式(八十三):

這個程式是用作連接 BLE 和 Classic Bluetooth 的設備,它是使用 GATT(Generic Attribute Profile)和 A2DP(Advanced Audio Distribution Profile)規範,GATT 開始會廣播(Advertising),然後等待設備的連接 ,成功連接 後,GATT 設備便可以讀寫設備資料,這個範例程式是放在 esp_idf/examples/Bluetooth/ a2dp_gatts_coex。

ESP32 藍牙(Bluetooth)COEX 程式
操作系統:Windows 7 64-bit 版本
開發環境:ESP32 Windows Toolchain 20181001 版本
燒寫程式:FLASH_DOWNLOAD_TOOLS_v3.6.4
原程式版本:ESP32 ESP-IDF V3.3-beat1-136
原程式:esp_idf/examples/Bluetooth/ a2dp_gatts_coex
程式:DIY-ESP32-100_11_BT_gatts_SSD1306-20181231

ESP32 藍牙 a2dp_gatts_coex 程式的定義:
#define BT_DEVICE_NAME                  "ESP_COEX_A2DP_DEMO"
#define BLE_ADV_NAME                     "ESP_COEX_BLE_DEMO"
#define GATTS_SERVICE_UUID_A        0x00FF
#define GATTS_CHAR_UUID_A              0xFF01
#define GATTS_DESCR_UUID_A            0x3333
#define GATTS_NUM_HANDLE_A          4

#define GATTS_SERVICE_UUID_B         0x00EE
#define GATTS_CHAR_UUID_B              0xEE01
#define GATTS_DESCR_UUID_B             0x2222
#define GATTS_NUM_HANDLE_B          4

#define GATTS_DEMO_CHAR_VAL_LEN_MAX 0x40
#define PREPARE_BUF_MAX_SIZE        1024
#define PROFILE_NUM                            2
#define PROFILE_A_APP_ID                    0
#define PROFILE_B_APP_ID                    1

mingw32.exe
make menuconfig
Web Radio / Bluetooth Speaker  -> Audio Output -> Select Audio Output Mode (Generic I2S) ->  Built-In DAC

mingw32.exe:
Change Makefile PROJECT_NAME := user
make manuconfig
make –j4 all

由於筆者沒有 PCM 模組,所以將 ESP32 的 A2DP 設定為 Internal DAC,然後直接將電子線路連接到 GPIO26 的輸出。

程式成功編譯,便將 bin 檔案燒錄到 ESP32 模組,完成後重置 ESP32 模組,開啓 ESPlorer ESPlorer v0.2.0-rc6 作串口輸出顯示。

開啓手機 Bluetooth 設備裝置,掃描周邊設備,應該可見 ESP_COEX_A2DP_DEMO 的名稱,然後配對 ESP_COEX_A2DP_DEMO,成功配對後,便可以開啓手機媒體裝置,並且播放音響,ESP32 的喇叭便會播放音響,但音響質素很差,重點是已經通過藍牙(Bluetooth)裝置傳輸音樂訊號到另外的藍牙裝置。

ESP32 GATT 開始會廣播(Advertising)
HUAWEI G9 PLUS 手機藍牙連接
連接成功,HUAWEI G9 PLUS 手機開始播放音樂
ESP32 Bluetooth 設備名稱:ESP_COEX_A2DP_DEMO
ESP32 Bluetooth 設備名稱:ESP_COEX_BLE_DEMO

2019年 1月 31日 天氣報告
氣溫:21.0@ 19:40
相對濕度:百分之 76%
天氣:天色大致良好

2019年1月30日 星期三

DIY - ESP32:ESP32 藍牙(Bluetooth)COEX 電路(八十二)

DIY - ESP32:ESP32 藍牙(Bluetooth)COEX 電路(八十二):

這個程式是用作連接 BLE 和 Classic Bluetooth 的設備,它是使用 GATT(Generic Attribute Profile)和 A2DP(Advanced Audio Distribution Profile)規範,GATT 開始會廣播(Advertising),然後等待設備的連接 ,成功連接後,GATT 設備便可以讀寫設備資料。而電子電路也是使用了 ESP32 內置的 DAC(Digital to Analog Converter)作輸出和 A2DP Sink 電路一樣。

ESP32 藍牙(Bluetooth)Coex 電路製作
ESP32 藍牙(Bluetooth)Coex DAC電路圖
ESP32 I2S 輸出連接:
I2S
Signal
Description
ESP32 Board
Board Signal
I/O Signal
1
LRCK
Left Right Clock
36
G22
GPIO22 (Output)
2
DATA
Data
9
G25
GPIO25 (Output)
3
BCK
Bit Clock
10
G26
GPIO26 (Output)

但 筆者手上並沒有 I2S 編解碼器,如 PCM5102、PCM5102A 或 MAX98357A,但可以更改設定為使用內置 DAC (Digital-to-Analog Converter / 低質量 / 8-bit)或 PDM (Pulse Density Modulation / 高質量),便可以測試程式是否成功。

ESP32 內置 DAC 輸出連接:
DAC
Signal
Description
ESP32 Board
Board Signal
I/O Signal
1
DAC
Channel 1 (Left)
9
G25
GPIO25 (Output)
2
DAC
Channel 2 (Right)
10
G26
GPIO26 (Output)

ESP32 藍牙(Bluetooth)Coex 電路製作
2019年 1月 30日 天氣報告
氣溫:20.5@ 20:00
相對濕度:百分之 61%
天氣:大致多雲

2019年1月26日 星期六

DIY - ESP32:ESP32 藍牙(Bluetooth)GAP API 函數結構件及其它(八十一)

DIY - ESP32:ESP32 藍牙(Bluetooth)GAP API 函數結構件及其它(八十一):

除了 SP32 藍牙(Bluetooth)的 GAP(Generic Access Profile)的 API 函數模塊外,還提供了結構件(Structures)、巨集(Marco)、 泛型型別定義(Type Definitions)和枚舉(Enumerations)。

1. Structure
1.1. struct esp_bt_cod_t
Class of device.
Public Members
Description
uint32_t reserved_2
undefined
uint32_t minor
minor class
uint32_t major
major class
uint32_t service
service class
uint32_t reserved_8
undefined

1.2. struct esp_bt_gap_dev_prop_t
Bluetooth Device Property Descriptor.
Public Members
Description
esp_bt_gap_dev_prop_type_t type
device property type
int len
device property value length
void ﹡val
device property value

2. Macros
Macros
Description
ESP_BT_GAP_RSSI_HIGH_THRLD
RSSI threshold.
High RSSI threshold
ESP_BT_GAP_RSSI_LOW_THRLD
Low RSSI threshold
ESP_BT_GAP_MAX_BDNAME_LEN
Maximum bytes of Bluetooth device name.
ESP_BT_GAP_EIR_DATA_LEN
Maximum size of EIR Significant part.
ESP_BT_PIN_CODE_LEN
Max pin code length
ESP_BT_IO_CAP_OUT
ESP_BT_IO_CAP_IO
ESP_BT_IO_CAP_IN
ESP_BT_IO_CAP_NONE
ESP_BT_COD_SRVC_BIT_MASK
Bits of major service class field.
Major service bit mask
ESP_BT_COD_SRVC_BIT_OFFSET
Major service bit offset
ESP_BT_COD_MAJOR_DEV_BIT_MASK
Bits of major device class field.
Major device bit mask
ESP_BT_COD_MAJOR_DEV_BIT_OFFSET
Major device bit offset
ESP_BT_COD_MINOR_DEV_BIT_MASK
Bits of minor device class field.
Minor device bit mask
ESP_BT_COD_MINOR_DEV_BIT_OFFSET
Minor device bit offset
ESP_BT_COD_FORMAT_TYPE_BIT_MASK
Bits of format type.
Format type bit mask
ESP_BT_COD_FORMAT_TYPE_BIT_OFFSET
Format type bit offset
ESP_BT_COD_FORMAT_TYPE_1
Class of device format type 1
ESP_BT_GAP_MIN_INQ_LEN
Minimum and Maximum inquiry length Minimum inquiry duration, unit is 1.28s
ESP_BT_GAP_MAX_INQ_LEN
Maximum inquiry duration, unit is 1.28s

3. Type Definitions
Type Definitions
Description
typedef uint8_t esp_bt_pin_code_t[ESP_BT_PIN_CODE_LEN]
Pin Code (upto 128 bits) MSB is 0
typedef uint8_t esp_bt_io_cap_t
combination of the io capability
typedef void (esp_bt_gap_cb_t)(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t param)
bluetooth GAP callback function type
Parameters
event: : Event type
param: : Pointer to callback parameter

4. Enumerations
4.1. enum esp_bt_cod_mode_t
Bluetooth A2DP connection states.
Enumeration
Description
ESP_BT_SET_COD_MAJOR_MINOR = 0x01
overwrite major, minor class
ESP_BT_SET_COD_SERVICE_CLASS = 0x02
set the bits in the input, the current bit will remain
ESP_BT_CLR_COD_SERVICE_CLASS = 0x04
clear the bits in the input, others will remain
ESP_BT_SET_COD_ALL = 0x08
overwrite major, minor, set the bits in service class
ESP_BT_INIT_COD = 0x0a
overwrite major, minor, and service class

4.2. enum esp_bt_scan_mode_t
Bluetooth A2DP disconnection reason.
Enumeration
Description
ESP_BT_SCAN_MODE_NONE = 0
Neither discoverable nor connectable
ESP_BT_SCAN_MODE_CONNECTABLE
Connectable but not discoverable
ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE
both discoverable and connectable

4.3. enum esp_bt_gap_dev_prop_type_t
Bluetooth Device Property type.
Enumeration
Description
ESP_BT_GAP_DEV_PROP_BDNAME = 1
Bluetooth device name, value type is int8_t []
ESP_BT_GAP_DEV_PROP_COD
Class of Device, value type is uint32_t
ESP_BT_GAP_DEV_PROP_RSSI
Received Signal strength Indication, value type is int8_t, ranging from -128 to 127
ESP_BT_GAP_DEV_PROP_EIR
Extended Inquiry Response, value type is uint8_t []

4.4. enum esp_bt_eir_type_t
Extended Inquiry Response data type.
Enumeration
Description
ESP_BT_EIR_TYPE_FLAGS = 0x01
Flag with information such as BR/EDR and LE support
ESP_BT_EIR_TYPE_INCMPL_16BITS_UUID = 0x02
Incomplete list of 16-bit service UUIDs
ESP_BT_EIR_TYPE_CMPL_16BITS_UUID = 0x03
Complete list of 16-bit service UUIDs
ESP_BT_EIR_TYPE_INCMPL_32BITS_UUID = 0x04
Incomplete list of 32-bit service UUIDs
ESP_BT_EIR_TYPE_CMPL_32BITS_UUID = 0x05
Complete list of 32-bit service UUIDs
ESP_BT_EIR_TYPE_INCMPL_128BITS_UUID = 0x06
Incomplete list of 128-bit service UUIDs
ESP_BT_EIR_TYPE_CMPL_128BITS_UUID = 0x07
Complete list of 128-bit service UUIDs
ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME = 0x08
Shortened Local Name
ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME = 0x09
Complete Local Name
ESP_BT_EIR_TYPE_TX_POWER_LEVEL = 0x0a
Tx power level, value is 1 octet ranging from -127 to 127, unit is dBm
ESP_BT_EIR_TYPE_MANU_SPECIFIC = 0xff
Manufacturer specific data

4.5. enum esp_bt_cod_srvc_t
Major service class field of Class of Device, mutiple bits can be set.
Enumeration
Description
ESP_BT_COD_SRVC_NONE = 0
None indicates an invalid value
ESP_BT_COD_SRVC_LMTD_DISCOVER = 0x1
Limited Discoverable Mode
ESP_BT_COD_SRVC_POSITIONING = 0x8
Positioning (Location identification)
ESP_BT_COD_SRVC_NETWORKING = 0x10
Networking, e.g. LAN, Ad hoc
ESP_BT_COD_SRVC_RENDERING = 0x20
Rendering, e.g. Printing, Speakers
ESP_BT_COD_SRVC_CAPTURING = 0x40
Capturing, e.g. Scanner, Microphone
ESP_BT_COD_SRVC_OBJ_TRANSFER = 0x80
Object Transfer, e.g. v-Inbox, v-Folder
ESP_BT_COD_SRVC_AUDIO = 0x100
Audio, e.g. Speaker, Microphone, Headset service
ESP_BT_COD_SRVC_TELEPHONY = 0x200
Telephony, e.g. Cordless telephony, Modem, Headset service
ESP_BT_COD_SRVC_INFORMATION = 0x400
Information, e.g., WEB-server, WAP-server

4.6. enum esp_bt_pin_type_t
Enumeration
Description
ESP_BT_PIN_TYPE_VARIABLE = 0
Refer to BTM_PIN_TYPE_VARIABLE
ESP_BT_PIN_TYPE_FIXED = 1
Refer to BTM_PIN_TYPE_FIXED

4.7. enum esp_bt_sp_param_t
Enumeration
Description
ESP_BT_SP_IOCAP_MODE = 0
Set IO mode

4.8. enum esp_bt_cod_major_dev_t
Major device class field of Class of Device.
Enumeration
Description
ESP_BT_COD_MAJOR_DEV_MISC = 0
Miscellaneous
ESP_BT_COD_MAJOR_DEV_COMPUTER = 1
Computer
ESP_BT_COD_MAJOR_DEV_PHONE = 2
Phone(cellular, cordless, pay phone, modem
ESP_BT_COD_MAJOR_DEV_LAN_NAP = 3
LAN, Network Access Point
ESP_BT_COD_MAJOR_DEV_AV = 4
Audio/Video(headset, speaker, stereo, video display, VCR
ESP_BT_COD_MAJOR_DEV_PERIPHERAL = 5
Peripheral(mouse, joystick, keyboard)
ESP_BT_COD_MAJOR_DEV_IMAGING = 6
Imaging(printer, scanner, camera, display
ESP_BT_COD_MAJOR_DEV_WEARABLE = 7
Wearable
ESP_BT_COD_MAJOR_DEV_TOY = 8
Toy
ESP_BT_COD_MAJOR_DEV_HEALTH = 9
Health
ESP_BT_COD_MAJOR_DEV_UNCATEGORIZED = 31
Uncategorized: device not specified

4.9. enum esp_bt_gap_discovery_state_t
Bluetooth Device Discovery state
Enumeration
Description
ESP_BT_GAP_DISCOVERY_STOPPED
device discovery stopped
ESP_BT_GAP_DISCOVERY_STARTED
device discovery started

4.10. enum esp_bt_gap_cb_event_t
BT GAP callback events.
Enumeration
Description
ESP_BT_GAP_DISC_RES_EVT = 0
device discovery result event
ESP_BT_GAP_DISC_STATE_CHANGED_EVT
discovery state changed event
ESP_BT_GAP_RMT_SRVCS_EVT
get remote services event
ESP_BT_GAP_RMT_SRVC_REC_EVT
get remote service record event
ESP_BT_GAP_AUTH_CMPL_EVT
AUTH complete event
ESP_BT_GAP_PIN_REQ_EVT
Legacy Pairing Pin code request
ESP_BT_GAP_CFM_REQ_EVT
Simple Pairing User Confirmation request.
ESP_BT_GAP_KEY_NOTIF_EVT
Simple Pairing Passkey Notification
ESP_BT_GAP_KEY_REQ_EVT
Simple Pairing Passkey request
ESP_BT_GAP_READ_RSSI_DELTA_EVT
read rssi event
ESP_BT_GAP_EVT_MAX

4.11. enum esp_bt_inq_mode_t
Inquiry Mode
Enumeration
Description
ESP_BT_INQ_MODE_GENERAL_INQUIRY
General inquiry mode
ESP_BT_INQ_MODE_LIMITED_INQUIRY
Limited inquiry mode

5. Unions
union esp_bt_gap_cb_param_t.
#include
A2DP state callback parameters.
Unions
Description
struct esp_bt_gap_cb_param_t::disc_res_param disc_res
discovery result parameter struct
struct esp_bt_gap_cb_param_t::disc_state_changed_param disc_st_chgt
discovery state changed parameter struct
struct esp_bt_gap_cb_param_t::rmt_srvcs_param rmt_srvcs
services of remote device parameter struct
struct esp_bt_gap_cb_param_t::rmt_srvc_rec_param rmt_srvc_rec
specific service record from remote device parameter struct
struct esp_bt_gap_cb_param_t::read_rssi_delta_param read_rssi_delta
read rssi parameter struct
struct esp_bt_gap_cb_param_t::auth_cmpl_param auth_cmpl
authentication complete parameter struct
struct esp_bt_gap_cb_param_t::pin_req_param pin_req
pin request parameter struct
struct esp_bt_gap_cb_param_t::cfm_req_param cfm_req
confirm request parameter struct
struct esp_bt_gap_cb_param_t::key_notif_param key_notif
passkey notif parameter struct
struct esp_bt_gap_cb_param_t::key_req_param key_req
passkey request parameter struct
struct auth_cmpl_param
#include
ESP_BT_GAP_AUTH_CMPL_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
esp_bt_status_t stat - authentication  complete status
uint8_t  device_name[ESP_BT_GAP_MAX_BDNAME_LEN+1] - device name
struct cfm_req_param
#include
ESP_BT_GAP_CFM_REQ_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
uint32_t num_val - the numeric value for comparison.
struct disc_res_param
#include
ESP_BT_GAP_DISC_RES_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
int num_prop - number of properties got
esp_bt_gap_dev_prop_t ﹡prop - properties discovered from the new device
struct disc_state_changed_param
#include
ESP_BT_GAP_DISC_STATE_CHANGED_EVT.
Public Members
esp_bt_gap_discovery_state_t state - discovery state
struct key_notif_param
#include
ESP_BT_GAP_KEY_NOTIF_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
uint32_t passkey - the numeric value for passkey entry.
struct key_req_param
#include
ESP_BT_GAP_KEY_REQ_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
struct pin_req_param
#include
ESP_BT_GAP_PIN_REQ_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
bool min_16_digit - TRUE if the pin returned must be at least 16 digits
struct read_rssi_delta_param
#include
ESP_BT_GAP_READ_RSSI_DELTA_EVT ﹡.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
esp_bt_status_t stat - read rssi status
int8_t rssi_delta - rssi delta value range -128 ~127, The value zero indicates that the RSSI is inside the Golden Receive Power Range, the Golden Receive Power Range is from ESP_BT_GAP_RSSI_LOW_THRLD to ESP_BT_GAP_RSSI_HIGH_THRLD
struct rmt_srvc_rec_param
#include
ESP_BT_GAP_RMT_SRVC_REC_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
esp_bt_status_t stat - service search status
struct rmt_srvcs_param
#include
ESP_BT_GAP_RMT_SRVCS_EVT.
Public Members
esp_bd_addr_t bda - remote bluetooth device address
esp_bt_status_t stat - service search status
int num_uuids - number of UUID in uuid_list
esp_bt_uuid_t ﹡uuid_list - list of service UUIDs of remote device

Header File:bt/bluedroid/api/include/api/esp_gap_bt_api.h
參考網址:https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/bluetooth/esp_gap_bt.html

2019年 1月 26日 天氣報告
氣溫:16.9@ 22:00
相對濕度:百分之 69%
天氣:天色大致良好