1.create battery task

2.define le_edc_bat_volt_update(). sending notification with the battery volt characteristic , if notification is enable,

1. define on_connect()
2. define on_disconnect()
This commit is contained in:
Roy
2023-07-25 17:27:10 +08:00
parent d0706fa05c
commit 2c4e6a921f
2 changed files with 75 additions and 7 deletions
+28 -4
View File
@@ -14,17 +14,41 @@ extern "C"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_freertos.h"
#include "ble_srv_common.h"
#include "FreeRTOS.h"
#include "task.h"
#ifdef __cplusplus
}
#endif
uint16_t bas_volt_read(void) {
return 3100;
}
static uint16_t volt = 3100;
uint16_t bas_volt_read(void)
{
return volt;
}
void battery_task(void *pArg)
{
for (;;)
{
// battery psuedo code
vTaskDelay(pdMS_TO_TICKS(1000));
volt = volt ? volt - 1 : 3100;
// update battery volt
extern void le_edc_bat_volt_update(uint16_t bat_volt);
le_edc_bat_volt_update(volt);
}
}
void bas_volt_init(void)
{
xTaskCreate(battery_task, "bat_task", 160, NULL, 2, NULL);
}
+47 -3
View File
@@ -2,6 +2,7 @@
#if NRF_MODULE_ENABLED(BLE_EDC)
#include "app_error.h"
#include "ble_conn_state.h"
#include "ble_gatts.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
@@ -37,16 +38,18 @@ typedef struct
uint16_t battery_volt;
} ble_edc_init_t;
static ble_edc_t ble_edc;
static ble_edc_t ble_edc = {
.conn_handle = BLE_CONN_HANDLE_INVALID,
};
static void on_connect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
{
// TODO...
p_edc->conn_handle = p_ble_evt->evt.common_evt.conn_handle;
}
static void on_disconnect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
{
// TODO...
p_edc->conn_handle = BLE_CONN_HANDLE_INVALID;
}
static void edc_ccc_update(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
@@ -149,10 +152,51 @@ void le_edc_init(void)
ble_edc_init_t edc_init;
memset(&edc_init, 0x00, sizeof(edc_init));
extern void bas_volt_init(void);
bas_volt_init();
extern uint16_t bas_volt_read(void);
edc_init.battery_volt = bas_volt_read();
ret_code_t err_code = ble_edc_init(&edc_init);
APP_ERROR_CHECK(err_code);
}
void le_edc_bat_volt_update(uint16_t bat_volt)
{
ret_code_t err_code = NRF_SUCCESS;
if (bat_volt == ble_edc.bat_volt)
{
return;
}
// update database.
ble_gatts_value_t gatts_value = {
.p_value = (void *)&bat_volt,
.offset = 0,
.len = sizeof(bat_volt),
};
err_code = sd_ble_gatts_value_set(BLE_CONN_HANDLE_INVALID, ble_edc.bat_volt_handles.value_handle, &gatts_value);
APP_ERROR_CHECK(err_code);
ble_edc.bat_volt = bat_volt;
// send value if connected and notifying.
if (ble_edc.bat_volt_notify_en)
{
ble_gatts_hvx_params_t hvx_params = {
.handle = ble_edc.bat_volt_handles.value_handle,
.type = BLE_GATT_HVX_NOTIFICATION,
.offset = gatts_value.offset,
.p_len = &gatts_value.len,
.p_data = gatts_value.p_value,
};
if (ble_conn_state_status(ble_edc.conn_handle) == BLE_CONN_STATUS_CONNECTED)
{
err_code = sd_ble_gatts_hvx(ble_edc.conn_handle, &hvx_params);
APP_ERROR_CHECK(err_code);
}
}
}
#endif // NRF_MODULE_ENABLED(BLE_EDC)