remove epd callback

This commit is contained in:
Shuanglei Tao
2025-04-26 14:07:20 +08:00
parent da7cbd05e8
commit 436f5eb2f0
3 changed files with 66 additions and 78 deletions

View File

@@ -15,6 +15,7 @@
#include "ble_srv_common.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_pwr_mgmt.h"
#include "app_scheduler.h"
#include "EPD_service.h"
#include "nrf_log.h"
@@ -30,11 +31,10 @@
#define EPD_CFG_DEFAULT {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x03, 0x09, 0x03}
#endif
#define BLE_EPD_BASE_UUID {{0XEC, 0X5A, 0X67, 0X1C, 0XC1, 0XB6, 0X46, 0XFB, \
0X8D, 0X91, 0X28, 0XD8, 0X22, 0X36, 0X75, 0X62}}
#define BLE_UUID_EPD_CHARACTERISTIC 0x0002
extern uint32_t timestamp(void); // defined in main.c
// defined in main.c
extern uint32_t timestamp(void);
extern void set_timestamp(uint32_t timestamp);
extern void sleep_mode_enter(void);
static void epd_gui_update(void * p_event_data, uint16_t event_size)
{
@@ -70,15 +70,11 @@ static void on_disconnect(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
EPD_GPIO_Uninit();
}
static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t length)
static void epd_service_on_write(ble_epd_t * p_epd, uint8_t * p_data, uint16_t length)
{
NRF_LOG_DEBUG("[EPD]: on_write LEN=%d\n", length);
NRF_LOG_HEXDUMP_DEBUG(p_data, length);
if (p_data == NULL || length <= 0) return;
NRF_LOG_DEBUG("[EPD]: CMD=0x%02x, LEN=%d\n", p_data[0], length);
if (p_epd->epd_cmd_cb != NULL) {
if (p_epd->epd_cmd_cb(p_data[0], length > 1 ? &p_data[1] : NULL, length - 1))
return;
}
switch (p_data[0])
{
@@ -133,12 +129,37 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
p_epd->epd->drv->sleep();
break;
case EPD_CMD_SET_TIME: {
if (length < 5) return;
NRF_LOG_DEBUG("time: %02x %02x %02x %02x\n", p_data[1], p_data[2], p_data[3], p_data[4]);
if (length > 5) NRF_LOG_DEBUG("timezone: %d\n", (int8_t)p_data[5]);
uint32_t timestamp = (p_data[1] << 24) | (p_data[2] << 16) | (p_data[3] << 8) | p_data[4];
timestamp += (length > 5 ? (int8_t)p_data[5] : 8) * 60 * 60; // timezone
set_timestamp(timestamp);
p_epd->display_mode = length > 6 ? (display_mode_t)p_data[6] : MODE_CALENDAR;
ble_epd_on_timer(p_epd, timestamp, true);
} break;
case EPD_CMD_SET_CONFIG:
if (length < 2) return;
memcpy(&p_epd->config, &p_data[1], (length - 1 > EPD_CONFIG_SIZE) ? EPD_CONFIG_SIZE : length - 1);
epd_config_write(&p_epd->config);
break;
case EPD_CMD_SYS_SLEEP:
sleep_mode_enter();
break;
case EPD_CMD_SYS_RESET:
#if defined(S112)
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_RESET);
#else
NVIC_SystemReset();
#endif
break;
case EPD_CMD_CFG_ERASE:
epd_config_clear(&p_epd->config);
nrf_delay_ms(100); // required
@@ -182,7 +203,7 @@ static void on_write(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
}
else if (p_evt_write->handle == p_epd->char_handles.value_handle)
{
epd_service_process(p_epd, p_evt_write->data, p_evt_write->len);
epd_service_on_write(p_epd, p_evt_write->data, p_evt_write->len);
}
else
{
@@ -230,31 +251,30 @@ void ble_epd_on_ble_evt(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
static uint32_t epd_service_init(ble_epd_t * p_epd)
{
ble_uuid_t ble_uuid;
ble_uuid128_t base_uuid = BLE_EPD_BASE_UUID;
ble_uuid_t ble_uuid = {0};
ble_uuid128_t base_uuid = BLE_UUID_EPD_SVC_BASE;
ble_add_char_params_t add_char_params;
VERIFY_SUCCESS(sd_ble_uuid_vs_add(&base_uuid, &p_epd->uuid_type));
VERIFY_SUCCESS(sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type));
ble_uuid.type = p_epd->uuid_type;
ble_uuid.uuid = BLE_UUID_EPD_SERVICE;
ble_uuid.type = ble_uuid.type;
ble_uuid.uuid = BLE_UUID_EPD_SVC;
VERIFY_SUCCESS(sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid,
&p_epd->service_handle));
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_EPD_CHARACTERISTIC;
add_char_params.uuid_type = p_epd->uuid_type;
add_char_params.uuid = BLE_UUID_EPD_CHAR;
add_char_params.uuid_type = ble_uuid.type;
add_char_params.max_len = BLE_EPD_MAX_DATA_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1;
add_char_params.char_props.write = 1;
add_char_params.char_props.write_wo_resp = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
return characteristic_add(p_epd->service_handle, &add_char_params, &p_epd->char_handles);
}
@@ -270,10 +290,9 @@ void ble_epd_sleep_prepare(ble_epd_t * p_epd)
}
}
uint32_t ble_epd_init(ble_epd_t * p_epd, epd_callback_t cmd_cb)
uint32_t ble_epd_init(ble_epd_t * p_epd)
{
if (p_epd == NULL) return NRF_ERROR_NULL;
p_epd->epd_cmd_cb = cmd_cb;
// Initialize the service structure.
p_epd->max_data_len = BLE_EPD_MAX_DATA_LEN;

View File

@@ -43,14 +43,18 @@ void ble_epd_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
#define BLE_EPD_DEF(_name) static ble_epd_t _name;
#endif
#define BLE_UUID_EPD_SERVICE 0x0001
#define EPD_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
#define BLE_UUID_EPD_SVC_BASE {{0XEC, 0X5A, 0X67, 0X1C, 0XC1, 0XB6, 0X46, 0XFB, \
0X8D, 0X91, 0X28, 0XD8, 0X22, 0X36, 0X75, 0X62}}
#define BLE_UUID_EPD_SVC 0x0001
#define BLE_UUID_EPD_CHAR 0x0002
#define EPD_SVC_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
#if defined(S112)
#define BLE_EPD_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - 3)
#else
#define BLE_EPD_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer. */
#endif
typedef bool (*epd_callback_t)(uint8_t cmd, uint8_t *data, uint16_t len);
/**< EPD Service command IDs. */
enum EPD_CMDS
@@ -77,7 +81,6 @@ enum EPD_CMDS
*/
typedef struct
{
uint8_t uuid_type; /**< UUID type for EPD Service Base UUID. */
uint16_t service_handle; /**< Handle of EPD Service (as provided by the S110 SoftDevice). */
ble_gatts_char_handles_t char_handles; /**< Handles related to the EPD characteristic (as provided by the SoftDevice). */
uint16_t conn_handle; /**< Handle of the current connection (as provided by the SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */
@@ -85,8 +88,7 @@ typedef struct
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
epd_model_t *epd; /**< current EPD model */
epd_config_t config; /**< EPD config */
epd_callback_t epd_cmd_cb; /**< EPD callback */
display_mode_t display_mode; /**< gui display mode */
display_mode_t display_mode; /**< GUI display mode */
} ble_epd_t;
typedef struct
@@ -108,12 +110,11 @@ void ble_epd_sleep_prepare(ble_epd_t * p_epd);
* @param[out] p_epd EPD Service structure. This structure must be supplied
* by the application. It is initialized by this function and will
* later be used to identify this particular service instance.
* @param[in] cmd_cb Time update callback
*
* @retval NRF_SUCCESS If the service was successfully initialized. Otherwise, an error code is returned.
* @retval NRF_ERROR_NULL If either of the pointers p_epd or p_epd_init is NULL.
*/
uint32_t ble_epd_init(ble_epd_t * p_epd, epd_callback_t cmd_cb);
uint32_t ble_epd_init(ble_epd_t * p_epd);
/**@brief Function for handling the EPD Service's BLE events.
*

54
main.c
View File

@@ -85,8 +85,8 @@ NRF_BLE_GATT_DEF(m_gatt);
BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
#endif
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_EPD_SERVICE, \
EPD_SERVICE_UUID_TYPE}}; /**< Universally unique service identifier. */
static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_EPD_SVC, \
EPD_SVC_UUID_TYPE}}; /**< Universally unique service identifier. */
BLE_EPD_DEF(m_epd); /**< Structure to identify the EPD Service. */
static uint32_t m_timestamp = 1735689600; /**< Current timestamp. */
@@ -154,7 +154,7 @@ static void application_timers_start(void)
*
* @note This function will not return.
*/
static void sleep_mode_enter(void)
void sleep_mode_enter(void)
{
NRF_LOG_DEBUG("Entering deep sleep mode\n");
@@ -162,51 +162,12 @@ static void sleep_mode_enter(void)
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
}
bool epd_cmd_callback(uint8_t cmd, uint8_t *data, uint16_t len)
{
switch (cmd)
{
case EPD_CMD_SET_TIME:
if (len < 4) {
NRF_LOG_DEBUG("invalid time data!\n");
return false;
}
NRF_LOG_DEBUG("time: %02x %02x %02x %02x\n", data[0], data[1], data[2], data[3]);
if (len > 4) NRF_LOG_DEBUG("timezone: %d\n", (int8_t)data[4]);
app_timer_stop(m_clock_timer_id);
m_timestamp = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
m_timestamp += (len > 4 ? (int8_t)data[4] : 8) * 60 * 60; // timezone
app_timer_start(m_clock_timer_id, CLOCK_TIMER_INTERVAL, NULL);
m_epd.display_mode = len > 5 ? (display_mode_t)data[5] : MODE_CALENDAR;
ble_epd_on_timer(&m_epd, m_timestamp, true);
return true;
case EPD_CMD_SYS_SLEEP:
sleep_mode_enter();
return true;
case EPD_CMD_SYS_RESET:
#if defined(S112)
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_RESET);
#else
NVIC_SystemReset();
#endif
return true;
default:
break;
}
return false;
}
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
memset(&m_epd, 0, sizeof(ble_epd_t));
APP_ERROR_CHECK(ble_epd_init(&m_epd, epd_cmd_callback));
APP_ERROR_CHECK(ble_epd_init(&m_epd));
}
/**@brief Function for the GAP initialization.
@@ -604,6 +565,13 @@ uint32_t timestamp(void)
return m_timestamp;
}
void set_timestamp(uint32_t timestamp)
{
app_timer_stop(m_clock_timer_id);
m_timestamp = timestamp;
app_timer_start(m_clock_timer_id, CLOCK_TIMER_INTERVAL, NULL);
}
/**@brief Function for initializing the nrf log module.
*/
static void log_init(void)