From f838504f229dee6e8384b21d276de2326333fe8f Mon Sep 17 00:00:00 2001
From: Tom Greig <tag2y19@soton.ac.uk>
Date: Fri, 31 Jan 2025 13:04:20 +0000
Subject: [PATCH] Add periodic time message on notify characteristic

Both up and down buttons prompt indicate messages now.
---
 apps/ble-demo/src/main.c | 41 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/apps/ble-demo/src/main.c b/apps/ble-demo/src/main.c
index 6acbbed..c470dd1 100644
--- a/apps/ble-demo/src/main.c
+++ b/apps/ble-demo/src/main.c
@@ -53,6 +53,8 @@ static int ble_in_callback( uint16_t connection_handle,
 static void gpio_init( void );
 static void button_handler( void* data );
 
+static void timeout( struct os_event* event );
+
 /* GLOBALS!!! */
 
 static struct {
@@ -68,6 +70,7 @@ static struct {
 uint8_t frame_buffer[128 / 8 * 64 + 1] = { 0x40, 0, };
 uint8_t* frame = &frame_buffer[1];
 const size_t frame_size = 128 / 8 * 64;
+uint32_t t = 0;
 
 /* DATA!!! */
 
@@ -145,6 +148,12 @@ int main( void ) {
 
 	gpio_init();
 
+	t = 0;
+	static struct os_callout timer;
+	os_callout_init( &timer, os_eventq_dflt_get(),
+		&timeout, &timer );
+	os_callout_reset( &timer, OS_TICKS_PER_SEC );
+
 	while ( 1 ) {
 
 		os_eventq_run( os_eventq_dflt_get() );
@@ -573,13 +582,13 @@ static void button_handler( void* data ) {
 
 		ble_state.count--;
 
-		if ( ble_state.notify_status ) {
+		if ( ble_state.indicate_status ) {
 
 			struct os_mbuf* om =
 				ble_hs_mbuf_from_flat( &( ble_state.count ), 1 );
-			assert( 0 == ble_gatts_notify_custom(
+			assert( 0 == ble_gatts_indicate_custom(
 				ble_state.connection_handle,
-				ble_state.notify_handle, om ) );
+				ble_state.indicate_handle, om ) );
 
 		}
 
@@ -606,3 +615,29 @@ static void button_handler( void* data ) {
 	i2c_write( frame_buffer, sizeof frame_buffer );
 
 }
+
+static void timeout( struct os_event* event ) {
+
+	t++;
+
+	char str[32];
+	snprintf( str, sizeof str,
+		"Time: %lu:%02lu", t / 60, t % 60 );
+	memset( frame + frame_size / 2, 0, frame_size / 8 );
+	text8( str, frame, 4, 0 );
+	i2c_write( frame_buffer, sizeof frame_buffer );
+
+	if ( ble_state.notify_status ) {
+
+		struct os_mbuf* om =
+			ble_hs_mbuf_from_flat( str, strlen( str ) );
+		assert( 0 == ble_gatts_notify_custom(
+			ble_state.connection_handle,
+			ble_state.notify_handle, om ) );
+
+	}
+
+	struct os_callout* timer = (struct os_callout*) event->ev_arg;
+	os_callout_reset( timer, OS_TICKS_PER_SEC );
+
+}
-- 
GitLab