From 66a27534909ec93d1d63f240bed0cad28edf3717 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 14 Nov 2021 01:14:38 +0100
Subject: [PATCH] ecg: Add 16 bit sample counter to BLE stream

---
 Documentation/bluetooth/ecg.rst | 2 ++
 preload/apps/ecg/__init__.py    | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/Documentation/bluetooth/ecg.rst b/Documentation/bluetooth/ecg.rst
index 3af7cf77..42d39a33 100644
--- a/Documentation/bluetooth/ecg.rst
+++ b/Documentation/bluetooth/ecg.rst
@@ -25,3 +25,5 @@ ECG samples characteristic
 
 List of 16 bit samples (big endian). Enable notifications to
 receive a stream of samples while the ECG app is open.
+
+The first 16 bit are a sample counter (big endian).
diff --git a/preload/apps/ecg/__init__.py b/preload/apps/ecg/__init__.py
index 3b9b5fa7..971b59e1 100644
--- a/preload/apps/ecg/__init__.py
+++ b/preload/apps/ecg/__init__.py
@@ -85,6 +85,7 @@ class ECG:
                         self.disp.backlight(20)
                 else:
                     self.ble_streaming = True
+                    self.ble_sample_count = 0
                     if not config.get_option("BLE Disp"):
                         self.disp.backlight(0)
 
@@ -173,10 +174,12 @@ class ECG:
 
     def callback_ecg(self, datasets):
         if self.ble_streaming:
+            self.ble_sample_count += len(datasets)
             self.b.gatts_notify(
                 1,
                 self.ecg_data_handle,
-                struct.pack(">" + ("h" * len(datasets)), *datasets),
+                struct.pack(">h", self.ble_sample_count & 0xFFFF)
+                + struct.pack(">" + ("h" * len(datasets)), *datasets),
             )
 
             # Don't update the screen if it should be off during a connection
-- 
GitLab