[Silicon Labs BG22-EK4108A Bluetooth Development Review] Four Programs Plus Button Development Steps
[Copy link]
This post was last edited by damiaa on 2022-1-7 15:48
[Silicon Labs BG22-EK4108A Bluetooth Development Review] Four Programs Plus Button Development Steps
1. [Silicon Labs BG22-EK4108A Bluetooth Development Evaluation] Continue after the three-point light program development steps (using the already generated program)
2. Add button components
and generate an instance button
You can also see the information here.
Save it, you can see two files related to the buttons generated in the code. Remember to configure the buttons consistent with the board. The only button on this board is pc7
3. Configure the Bluetooth button's Characteristic
Switch to Bluetooth configuration: change the name (it doesn’t matter if you don’t change it)
The top left is the newly created service Characteristics and description
Select new Characteristic on LED and BUTTON
Then configure the parameters of Characteristic (BTN)
4, save it
5. Add code to app.c
#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. Compile, download, and run.
7. Open EFRConnect - v2.4.0 and find the device connection you changed the name to in the broswer.
8. Enter the log and press a button to see the key information.
Enter the Unknown service and check notify (click it), then look at the button and you can see a data of 1.
9. If you feel that the data is always 1, case sl_bt_evt_system_external_signal_id: change it and compile, download and run. After pressing the button, the data of log and notify will increase by 1 from 1 each time you press the button.
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;
10. The experiment is completed. Thank you.
|