Task #9 - callbacks return a struct with Arduino strings so beginners can avoid using std:: strings.

This commit is contained in:
James Hudson 2020-05-01 10:01:33 +02:00
parent 344ef344ca
commit 50e83a14be
4 changed files with 53 additions and 14 deletions

View File

@ -62,8 +62,8 @@ void onBLEStateChanged(BLENotifications::State state) {
}
// Setup a callback for when a notification arrives
void onNotificationArrived(const Notification * notification) {
Serial.println(notification->title.c_str());
void onNotificationArrived(const ArduinoNotification * notification, Notification * rawData) {
Serial.println(notification->title);
}
// Register the callback to be informed when a notification arrives

View File

@ -51,11 +51,16 @@ void onBLEStateChanged(BLENotifications::State state) {
// A notification arrived from the mobile device, ie a social media notification or incoming call.
void onNotificationArrived(const Notification * notification) {
// parameters:
// - notification: an Arduino-friendly structure containing notification information. Do not keep a
// pointer to this data - it will be destroyed after this function.
// - rawNotificationData: a pointer to the underlying data. It contains the same information, but is
// not beginner-friendly. For advanced use-cases.
void onNotificationArrived(const ArduinoNotification * notification, const Notification * rawNotificationData) {
Serial.print("Got notification: ");
Serial.println(notification->title.c_str()); // The title, ie name of who sent the message
Serial.println(notification->message.c_str()); // The detail, ie "be home for dinner at 7".
Serial.println(notification->type.c_str()); // Which app sent it
Serial.println(notification->title); // The title, ie name of who sent the message
Serial.println(notification->message); // The detail, ie "be home for dinner at 7".
Serial.println(notification->type); // Which app sent it
Serial.println(notifications.getNotificationCategoryDescription(notification->category)); // ie "social media"
Serial.println(notification->categoryCount); // How may other notifications are there from this app (ie badge number)
if (notification->category == CategoryIDIncomingCall) {
@ -70,11 +75,11 @@ void onNotificationArrived(const Notification * notification) {
// A notification was cleared
void onNotificationRemoved(const Notification * notification) {
void onNotificationRemoved(const ArduinoNotification * notification, const Notification * rawNotificationData) {
Serial.print("Removed notification: ");
Serial.println(notification->title.c_str());
Serial.println(notification->message.c_str());
Serial.println(notification->type.c_str());
Serial.println(notification->title);
Serial.println(notification->message);
Serial.println(notification->type);
}

View File

@ -204,7 +204,8 @@ void ANCSBLEClient::onDataSourceNotify(
if (!notification->title.empty() && !notification->message.empty()) {
if (notificationCB && notification->isComplete == false) {
ESP_LOGI(LOG_TAG, "got a full notification: %s - %s ", notification->title.c_str(), notification->message.c_str());
notificationCB(notification);
const ArduinoNotification arduinoNotification = ArduinoNotification(*notification);
notificationCB(&arduinoNotification, notification);
}
notification->isComplete = true;
}
@ -239,7 +240,8 @@ void ANCSBLEClient::onNotificationSourceNotify(
}
if (removedCB) {
removedCB(notification);
const ArduinoNotification arduinoNotification = ArduinoNotification(*notification);
removedCB(&arduinoNotification, notification);
}
}
else if (pData[0] == ANCS::EventIDNotificationAdded) {

View File

@ -7,6 +7,7 @@
#define BLE_NOTIFICATION_H_
#include <string>
#include <WString.h> // Arduino string
/**
* Notification category, based on ANCS values, but could also be used for Android.
@ -91,17 +92,48 @@ struct Notification {
uint8_t categoryCount; /**< Number of other notifications in this category (ie badge number count). */
};
/**
* C++ strings might be confusing for beginners, so this is the same struct as Notification, but using
* Arduino strings. We still use Notification for the underlying logic, because of the heap fragmentation issues
* with Arduino strings.
*/
struct ArduinoNotification {
String title;
String message;
String type;
uint32_t eventFlags; /**< Bitfield of ANCS::EventFlags flags. */
time_t time;
uint32_t uuid = 0;
bool showed = false;
bool isComplete = false;
NotificationCategory category; /**< If it is a call, social media, email, etc. */
uint8_t categoryCount; /**< Number of other notifications in this category (ie badge number count). */
ArduinoNotification(const Notification & src) {
title = String(src.title.c_str());
message = String(src.message.c_str());
type = String(src.type.c_str());
eventFlags = src.eventFlags;
time = src.time;
uuid = src.uuid;
showed = src.showed;
isComplete = src.isComplete;
category = src.category;
categoryCount = src.categoryCount;
}
};
/**
* Callback for when a notification arrives.
* @param notification The notification that just arrived.
*/
typedef void (*ble_notification_arrived_t)(const Notification * notification);
typedef void (*ble_notification_arrived_t)(const ArduinoNotification * notification, const Notification * rawNotificationData);
/**
* Callback for when a notification was removed.
* @param notification The notification that was removed.
*/
typedef void (*ble_notification_removed_t)(const Notification * notification);
typedef void (*ble_notification_removed_t)(const ArduinoNotification * notification, const Notification * rawNotificationData);