esp32-c3 蓝牙 BLE 键盘 串口 低延时 ESP-IDF开发环境

esp32-c3 主控作为 BLE keyboard 的使用介绍

  • 本文默认你已经了解了ESP-IDF开发环境的基本操作,本文只描述思路,不讲操作细节
  • 本文实现了使用esp32c3将来自串口的键码使用BLE蓝牙发送到Win电脑的功能。在windows下进行了测试,使用PC自带的蓝牙和esp32c3配对之后,PC可以收到来自其键盘输入

1 准备材料

  • esp32c3: 这里使用的是合宙的开发板,淘宝9.9包邮,便宜好用。
  • 开发环境:ESP-IDF v4.4.1-dirty
  • 其他:我这里使用的是Ubuntu读取了键盘输入,将数据从串口转发到esp32c3

2 步骤

  1. ~/esp/esp-idf/examples/bluetooth/bluedroid/ble/ble_hid_device_demo 复制到工作目录
  2. 用下面的代码将工程里的 ble_hidd_demo_main.c 全部替换。
  3. 编译下载
  4. 在windows里可以搜索名为test-keyboard的设备,并将其连接。连接成功之后,LED5 会有呼吸灯效果。esp32c3若从uart0接收到特定的数据包,则会将其通过蓝牙转发到PC。

3 其他

  1. CYPRESS BLE_HID协议文档CYPRESS BLE代码示例。esp32c3 串口数据包协议为HID键盘协议包,帧尾部添加0xff作为分割符。
  2. 本设计不在意功耗,调整了蓝牙的上报率 min_interval=0x0002,降低蓝牙链路上面的延时,减少使用键盘时的不跟手的感觉。

4 代码 ble_hidd_demo_main.c

  • 呼吸灯里的 #include "breath_led.h" led_duty 没有提供,请自行实现
/* This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this software is
   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"

#include "esp_hidd_prf_api.h"
#include "esp_bt_defs.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "driver/gpio.h"
#include "hid_dev.h"


#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"

#include "breath_led.h"

#include "driver/ledc.h"
#include "esp_err.h"

void led4_trg (void);
void led4_off (void);
void led5_on();
void led5_off();
#define BUTTON_PIN         9
/**
 * Brief:
 * This example Implemented BLE HID device profile related functions, in which the HID device
 * has 4 Reports (1 is mouse, 2 is keyboard and LED, 3 is Consumer Devices, 4 is Vendor devices).
 * Users can choose different reports according to their own application scenarios.
 * BLE HID profile inheritance and USB HID class.
 */

/**
 * Note:
 * 1. Win10 does not support vendor report , So SUPPORT_REPORT_VENDOR is always set to FALSE, it defines in hidd_le_prf_int.h
 * 2. Update connection parameters are not allowed during iPhone HID encryption, slave turns
 * off the ability to automatically update connection parameters during encryption.
 * 3. After our HID device is connected, the iPhones write 1 to the Report Characteristic Configuration Descriptor,
 * even if the HID encryption is not completed. This should actually be written 1 after the HID encryption is completed.
 * we modify the permissions of the Report Characteristic Configuration Descriptor to `ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED`.
 * if you got `GATT_INSUF_ENCRYPTION` error, please ignore.
 */

#define HID_DEMO_TAG "HID_DEMO"


static uint16_t hid_conn_id = 0;
static bool sec_conn = false;
static bool send_volum_up = false;
#define CHAR_DECLARATION_SIZE   (sizeof(uint8_t))

static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param);

#define HIDD_DEVICE_NAME            "test-Keyboard"
static uint8_t hidd_service_uuid128[] = {
   
    /* LSB <--------------------------------------------------------------------------------> MSB */
    //first uuid, 16bit, [12],[13] is the value
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x12, 0x18, 0x00, 0x00,
};

static esp_ble_adv_data_t hidd_adv_data = {
   
    .set_scan_rsp = false,
    .include_name = true,
    .include_txpower = true,
    .min_interval = 0x0002, //slave connection min interval, Time = min_interval * 1.25 msec
    .max_interval = 0x0006, //slave connection max interval, Time = max_interval * 1.25 msec
    .appearance = 0x03c0,       //HID Generic,
    .manufacturer_len
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值