274|0

3866

帖子

1

资源

版主

【Silicon Labs BG22-EK4108A 蓝牙开发评测】四 程序加按键开发步骤 [复制链接]

本帖最后由 damiaa 于 2022-1-7 15:48 编辑

【Silicon Labs BG22-EK4108A 蓝牙开发评测】四 程序加按键开发步骤

1,【Silicon Labs BG22-EK4108A 蓝牙开发评测】三 点灯程序开发步骤之后继续(利用已经生成的程序)

2,加入按键组件

QQ图片20220107111425.png 并生成一个实例按键

QQ图片20220107111529.png

QQ图片20220107112546.png

同步的这里也可以看到信息

QQ图片20220107112359.png 保存一下,代码里面可以看到生成的两个和按键相关的文件  记得配置的按键和板子的一致,这个板子的那个唯一按键是pc7

QQ图片20220107112650.png 3,配置蓝牙的这个按键的 Characteristic

切换到蓝牙配置里面:改一下名字(不改也没关系)

QQ图片20220107113428.png 左边最上面是新建service Characteristic 和描述

在LED and BUTTON 上面选择new Characteristic

QQ图片20220107113827.png 再配置好Characteristic(BTN)的参数

QQ图片20220107114440.png 4,保存一下

5,app.c加代码

QQ图片20220107145016.png QQ图片20220107141112.png

QQ图片20220107140755.png QQ图片20220107141355.png



#include <stdbool.h>      
#include "em_common.h"    //加入
#include "app_assert.h"
#include "sl_bluetooth.h"
#include "gatt_db.h"
#include "app.h"
#include "sl_simple_led_instances.h" //加入
#include "sl_simple_button_instances.h"//加入
// The advertising set handle allocated from Bluetooth stack.
static uint8_t advertising_set_handle = 0xff;//加入
// These variables need to be added and are used for the connection handle
// and the notification parameters to send Button data to the mobile app
uint8_t g_lab4Connection;//加入
uint8_t notification_data[1] = {0};//加入
uint16_t notification_len = 0;//加入
uint8_t notifyEnabled = false;//加入
extern sl_simple_button_context_t simple_btn0_context;//加入
/*
 * Override function for button press that needs to be added.  This function is
 * called in interrupt context and uses the Bluetooth stack external signaling.
 */
void sl_button_on_change(const sl_button_t *handle)//加入 记得加在SL_WEAK void app_init(void)前
{
  sl_simple_button_context_t *ctxt = ((sl_simple_button_context_t *)handle[0].context);
  if (ctxt->state) {
      ctxt->history += 1;
      sl_bt_external_signal(1);
  }
}
/**************************************************************************//**
 * Application Init.
 *****************************************************************************/
SL_WEAK void app_init(void)
{
  /////////////////////////////////////////////////////////////////////////////
  // Put your additional application init code here!                         //
  // This is called once during start-up.                                    //
  /////////////////////////////////////////////////////////////////////////////
}

/**************************************************************************//**
 * Application Process Action.
 *****************************************************************************/
SL_WEAK void app_process_action(void)
{
  /////////////////////////////////////////////////////////////////////////////
  // Put your additional application code here!                              //
  // This is called infinitely.                                              //
  // Do not call blocking functions from here!                               //
  /////////////////////////////////////////////////////////////////////////////
}

/**************************************************************************//**
 * Bluetooth stack event handler.
 * This overrides the dummy weak implementation.
 *
 * @param[in] evt Event coming from the Bluetooth stack.
 *****************************************************************************/
void sl_bt_on_event(sl_bt_msg_t *evt)
{
  sl_status_t sc;
  bd_addr address;
  uint8_t address_type;
  uint8_t system_id[8];

  switch (SL_BT_MSG_ID(evt->header)) {
    // -------------------------------
    // This event indicates the device has started and the radio is ready.
    // Do not call any stack command before receiving this boot event!
    case sl_bt_evt_system_boot_id:

      // Extract unique ID from BT Address.
      sc = sl_bt_system_get_identity_address(&address, &address_type);
      app_assert_status(sc);

      // Pad and reverse unique ID to get System ID.
      system_id[0] = address.addr[5];
      system_id[1] = address.addr[4];
      system_id[2] = address.addr[3];
      system_id[3] = 0xFF;
      system_id[4] = 0xFE;
      system_id[5] = address.addr[2];
      system_id[6] = address.addr[1];
      system_id[7] = address.addr[0];

      sc = sl_bt_gatt_server_write_attribute_value(gattdb_system_id,
                                                   0,
                                                   sizeof(system_id),
                                                   system_id);
      app_assert_status(sc);

      // Create an advertising set.
      sc = sl_bt_advertiser_create_set(&advertising_set_handle);
      app_assert_status(sc);

      // Set advertising interval to 100ms.
      sc = sl_bt_advertiser_set_timing(
        advertising_set_handle,
        160, // min. adv. interval (milliseconds * 1.6)
        160, // max. adv. interval (milliseconds * 1.6)
        0,   // adv. duration
        0);  // max. num. adv. events
      app_assert_status(sc);
      // Start general advertising and enable connections.
      sc = sl_bt_advertiser_start(
        advertising_set_handle,
        sl_bt_advertiser_general_discoverable,
        sl_bt_advertiser_connectable_scannable);
      app_assert_status(sc);
      break;

    // -------------------------------
    // This event indicates that a new connection was opened.
    case sl_bt_evt_connection_opened_id:
      //When sending notifications we need the connection handle.  Capture it here
            g_lab4Connection = evt->data.evt_connection_opened.connection;
      break;

    // -------------------------------
    // This event indicates that a connection was closed.
    case sl_bt_evt_connection_closed_id:
      // Restart advertising after client has disconnected.
      sc = sl_bt_advertiser_start(
        advertising_set_handle,
        sl_bt_advertiser_general_discoverable,
        sl_bt_advertiser_connectable_scannable);
      app_assert_status(sc);
      break;

    ///////////////////////////////////////////////////////////////////////////
    // Add additional event handlers here as your application requires!      //
    ///////////////////////////////////////////////////////////////////////////
       case sl_bt_evt_gatt_server_user_write_request_id://加入
      if (evt->data.evt_gatt_server_user_write_request.characteristic == gattdb_LED) {
              // Write user supplied value to LEDs.
                if (evt->data.evt_gatt_server_attribute_value.value.data[0]) {

                    //This is the use of the Simple LED component
                    sl_led_turn_on(&sl_led_led0);
                }
                else {
                    //This is the use of the Simple LED component
                    sl_led_turn_off(&sl_led_led0);
                }
              sl_bt_gatt_server_send_user_write_response(
                  evt->data.evt_gatt_server_user_write_request.connection,
                  gattdb_LED, SL_STATUS_OK);
            }
            break;
       case sl_bt_evt_system_external_signal_id://加入
             /* Process external signals */
             if (notifyEnabled) {
                 if (evt->data.evt_system_external_signal.extsignals == 1)  // 1 = BTN0
                 {
                     notification_data[0] = (uint8_t)simple_btn0_context.history;
                     simple_btn0_context.history = 0;

                     // send number of button presses
                     sc = sl_bt_gatt_server_send_characteristic_notification(
                             g_lab4Connection, gattdb_BTN, sizeof(notification_data),
                             notification_data, ¬ification_len);
                 }
             }
             break;
       case  sl_bt_evt_gatt_server_characteristic_status_id://加入
             if ((evt->data.evt_gatt_server_characteristic_status.characteristic == gattdb_BTN)
                 && (evt->data.evt_gatt_server_characteristic_status.status_flags == 0x01)) {
          if (evt->data.evt_gatt_server_characteristic_status.client_config_flags == 0x00) {
                notifyEnabled = false;
          }
          else {
             notifyEnabled = true;
          }
       }
       break;
    // -------------------------------
    // Default event handler.
    default:
      break;
  }
}

6,编译,下载,运行。

7,打开EFRConnect - v2.4.0,broswer里找到你改的名称的设备连接

8,进入log里面查看,按一下按键可以看到有按键信息。

     进入Unknown service 里面查看notify(点击一下),然后去看一下按键,可以看到一个1的数据出来。

9,如果觉得数据老是个1不过瘾 case sl_bt_evt_system_external_signal_id:改一下再编译下载运行,按键后log和notify的数据就从1按下一次数据加1

 case sl_bt_evt_system_external_signal_id:
             /* Process external signals */
             if (notifyEnabled) {
                 if (evt->data.evt_system_external_signal.extsignals == 1)  // 1 = BTN0
                 {
                     static uint8_t val=0;
                     val=val+1;
                     notification_data[0] =val; //(uint8_t)simple_btn0_context.history;
                     simple_btn0_context.history = 0;

                     // send number of button presses
                     sc = sl_bt_gatt_server_send_characteristic_notification(
                             g_lab4Connection, gattdb_BTN, sizeof(notification_data),
                             notification_data, ¬ification_len);
                 }
             }
             break;

微信图片_20220107154731.jpg 微信图片_20220107154736.jpg

10,实验完毕,谢谢。


回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

最新文章 更多>>
    关闭
    站长推荐上一条 1/6 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2022 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表