diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-03-26 03:49:12 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-03-26 03:49:12 (GMT) |
| commit | 6ef420d2407a4685b56a233f6b0f849e90c6cf49 (patch) | |
| tree | 917f4304cace71ca0d7e472546a1538d8ac991a4 /src/components | |
| parent | 8b7bbf4b84a008e42a0ebba8af316690af696d5b (diff) | |
sans heart
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/ble/HeartRateService.cpp | 82 | ||||
| -rw-r--r-- | src/components/ble/HeartRateService.h | 42 | ||||
| -rw-r--r-- | src/components/ble/NimbleController.cpp | 13 | ||||
| -rw-r--r-- | src/components/ble/NimbleController.h | 5 | ||||
| -rw-r--r-- | src/components/heartrate/Biquad.cpp | 26 | ||||
| -rw-r--r-- | src/components/heartrate/Biquad.h | 22 | ||||
| -rw-r--r-- | src/components/heartrate/HeartRateController.cpp | 35 | ||||
| -rw-r--r-- | src/components/heartrate/HeartRateController.h | 40 | ||||
| -rw-r--r-- | src/components/heartrate/Ppg.cpp | 99 | ||||
| -rw-r--r-- | src/components/heartrate/Ppg.h | 31 | ||||
| -rw-r--r-- | src/components/heartrate/Ptagc.cpp | 27 | ||||
| -rw-r--r-- | src/components/heartrate/Ptagc.h | 17 |
12 files changed, 2 insertions, 437 deletions
diff --git a/src/components/ble/HeartRateService.cpp b/src/components/ble/HeartRateService.cpp deleted file mode 100644 index 4824a6b..0000000 --- a/src/components/ble/HeartRateService.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "components/ble/HeartRateService.h" -#include "components/heartrate/HeartRateController.h" -#include "systemtask/SystemTask.h" -#include <nrf_log.h> - -using namespace Pinetime::Controllers; - -constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid; -constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid; - -namespace { - int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { - auto* heartRateService = static_cast<HeartRateService*>(arg); - return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt); - } -} - -// TODO Refactoring - remove dependency to SystemTask -HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController) - : system {system}, - heartRateController {heartRateController}, - characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u, - .access_cb = HeartRateServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = &heartRateMeasurementHandle}, - {0}}, - serviceDefinition { - {/* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = &heartRateServiceUuid.u, - .characteristics = characteristicDefinition}, - {0}, - } { - // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) - heartRateController.SetService(this); -} - -void HeartRateService::Init() { - int res = 0; - res = ble_gatts_count_cfg(serviceDefinition); - ASSERT(res == 0); - - res = ble_gatts_add_svcs(serviceDefinition); - ASSERT(res == 0); -} - -int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { - if (attributeHandle == heartRateMeasurementHandle) { - NRF_LOG_INFO("HEARTRATE : handle = %d", heartRateMeasurementHandle); - uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value - - int res = os_mbuf_append(context->om, buffer, 2); - return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; - } - return 0; -} - -void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) { - if(!heartRateMeasurementNotificationEnable) return; - - uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value - auto* om = ble_hs_mbuf_from_flat(buffer, 2); - - uint16_t connectionHandle = system.nimble().connHandle(); - - if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { - return; - } - - ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om); -} - -void HeartRateService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { - if(attributeHandle == heartRateMeasurementHandle) - heartRateMeasurementNotificationEnable = true; -} - -void HeartRateService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { - if(attributeHandle == heartRateMeasurementHandle) - heartRateMeasurementNotificationEnable = false; -}
\ No newline at end of file diff --git a/src/components/ble/HeartRateService.h b/src/components/ble/HeartRateService.h deleted file mode 100644 index 4e4a5a4..0000000 --- a/src/components/ble/HeartRateService.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#define min // workaround: nimble's min/max macros conflict with libstdc++ -#define max -#include <host/ble_gap.h> -#include <atomic> -#undef max -#undef min - -namespace Pinetime { - namespace System { - class SystemTask; - } - namespace Controllers { - class HeartRateController; - class HeartRateService { - public: - HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController); - void Init(); - int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - void OnNewHeartRateValue(uint8_t hearRateValue); - - void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle); - void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle); - - private: - Pinetime::System::SystemTask& system; - Controllers::HeartRateController& heartRateController; - static constexpr uint16_t heartRateServiceId {0x180D}; - static constexpr uint16_t heartRateMeasurementId {0x2A37}; - - static constexpr ble_uuid16_t heartRateServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateServiceId}; - - static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId}; - - struct ble_gatt_chr_def characteristicDefinition[2]; - struct ble_gatt_svc_def serviceDefinition[2]; - - uint16_t heartRateMeasurementHandle; - std::atomic_bool heartRateMeasurementNotificationEnable {false}; - }; - } -} diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index bc712f4..f63d98c 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -27,8 +27,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, NotificationManager& notificationManager, Battery& batteryController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash, - HeartRateController& heartRateController + Pinetime::Drivers::SpiNorFlash& spiNorFlash ) : systemTask {systemTask}, bleController {bleController}, @@ -43,7 +42,6 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeService {dateTimeController}, batteryInformationService {batteryController}, immediateAlertService {systemTask, notificationManager}, - heartRateService {systemTask, heartRateController}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } @@ -86,7 +84,6 @@ void NimbleController::Init() { dfuService.Init(); batteryInformationService.Init(); immediateAlertService.Init(); - heartRateService.Init(); int rc; rc = ble_hs_util_ensure_addr(0); @@ -243,14 +240,6 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { event->subscribe.prev_notify, event->subscribe.cur_notify, event->subscribe.prev_indicate); - - if (event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) { - heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); - } else if (event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) { - heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); - } else if (event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) { - heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); - } break; case BLE_GAP_EVENT_MTU: diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 0facca6..c9d6420 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -14,7 +14,6 @@ #include "components/ble/CurrentTimeService.h" #include "components/ble/DeviceInformationService.h" #include "components/ble/DfuService.h" -#include "components/ble/HeartRateService.h" #include "components/ble/ImmediateAlertService.h" #include "components/ble/ServiceDiscovery.h" @@ -40,8 +39,7 @@ namespace Pinetime { DateTime& dateTimeController, NotificationManager& notificationManager, Battery& batteryController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash, - HeartRateController& heartRateController); + Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event* event); @@ -74,7 +72,6 @@ namespace Pinetime { CurrentTimeService currentTimeService; BatteryInformationService batteryInformationService; ImmediateAlertService immediateAlertService; - HeartRateService heartRateService; ServiceDiscovery serviceDiscovery; uint8_t addrType; diff --git a/src/components/heartrate/Biquad.cpp b/src/components/heartrate/Biquad.cpp deleted file mode 100644 index b7edd40..0000000 --- a/src/components/heartrate/Biquad.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* - SPDX-License-Identifier: LGPL-3.0-or-later - Original work Copyright (C) 2020 Daniel Thompson - C++ port Copyright (C) 2021 Jean-François Milants -*/ - -#include "components/heartrate/Biquad.h" - -using namespace Pinetime::Controllers; - -/** Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/ppg.py */ -Biquad::Biquad(float b0, float b1, float b2, float a1, float a2) : b0 {b0}, b1 {b1}, b2 {b2}, a1 {a1}, a2 {a2} { -} - -float Biquad::Step(float x) { - auto v1 = this->v1; - auto v2 = this->v2; - - auto v = x - (a1 * v1) - (a2 * v2); - auto y = (b0 * v) + (b1 * v1) + (b2 * v2); - - this->v2 = v1; - this->v1 = v; - - return y; -} diff --git a/src/components/heartrate/Biquad.h b/src/components/heartrate/Biquad.h deleted file mode 100644 index 7c8ca58..0000000 --- a/src/components/heartrate/Biquad.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -namespace Pinetime { - namespace Controllers { - /// Direct Form II Biquad Filter - class Biquad { - public: - Biquad(float b0, float b1, float b2, float a1, float a2); - float Step(float x); - - private: - float b0; - float b1; - float b2; - float a1; - float a2; - - float v1 = 0.0f; - float v2 = 0.0f; - }; - } -} diff --git a/src/components/heartrate/HeartRateController.cpp b/src/components/heartrate/HeartRateController.cpp deleted file mode 100644 index e0d6927..0000000 --- a/src/components/heartrate/HeartRateController.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "components/heartrate/HeartRateController.h" -#include <heartratetask/HeartRateTask.h> -#include <systemtask/SystemTask.h> - -using namespace Pinetime::Controllers; - -void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) { - this->state = newState; - if (this->heartRate != heartRate) { - this->heartRate = heartRate; - service->OnNewHeartRateValue(heartRate); - } -} - -void HeartRateController::Start() { - if (task != nullptr) { - state = States::NotEnoughData; - task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement); - } -} - -void HeartRateController::Stop() { - if (task != nullptr) { - state = States::Stopped; - task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement); - } -} - -void HeartRateController::SetHeartRateTask(Pinetime::Applications::HeartRateTask* task) { - this->task = task; -} - -void HeartRateController::SetService(Pinetime::Controllers::HeartRateService* service) { - this->service = service; -} diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h deleted file mode 100644 index a63f1a7..0000000 --- a/src/components/heartrate/HeartRateController.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include <cstdint> -#include <components/ble/HeartRateService.h> - -namespace Pinetime { - namespace Applications { - class HeartRateTask; - } - namespace System { - class SystemTask; - } - namespace Controllers { - class HeartRateController { - public: - enum class States { Stopped, NotEnoughData, NoTouch, Running }; - - HeartRateController() = default; - void Start(); - void Stop(); - void Update(States newState, uint8_t heartRate); - - void SetHeartRateTask(Applications::HeartRateTask* task); - States State() const { - return state; - } - uint8_t HeartRate() const { - return heartRate; - } - - void SetService(Pinetime::Controllers::HeartRateService* service); - - private: - Applications::HeartRateTask* task = nullptr; - States state = States::Stopped; - uint8_t heartRate = 0; - Pinetime::Controllers::HeartRateService* service = nullptr; - }; - } -}
\ No newline at end of file diff --git a/src/components/heartrate/Ppg.cpp b/src/components/heartrate/Ppg.cpp deleted file mode 100644 index a5d8369..0000000 --- a/src/components/heartrate/Ppg.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - SPDX-License-Identifier: LGPL-3.0-or-later - Original work Copyright (C) 2020 Daniel Thompson - C++ port Copyright (C) 2021 Jean-François Milants -*/ - -#include "components/heartrate/Ppg.h" -#include <vector> -#include <nrf_log.h> -using namespace Pinetime::Controllers; - -/** Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/ppg.py */ -namespace { - int Compare(int8_t* d1, int8_t* d2, size_t count) { - int e = 0; - for (size_t i = 0; i < count; i++) { - auto d = d1[i] - d2[i]; - e += d * d; - } - return e; - } - - int CompareShift(int8_t* d, int shift, size_t count) { - return Compare(d + shift, d, count - shift); - } - - int Trough(int8_t* d, size_t size, uint8_t mn, uint8_t mx) { - auto z2 = CompareShift(d, mn - 2, size); - auto z1 = CompareShift(d, mn - 1, size); - for (int i = mn; i < mx + 1; i++) { - auto z = CompareShift(d, i, size); - if (z2 > z1 && z1 < z) - return i; - z2 = z1; - z1 = z; - } - return -1; - } -} - -Ppg::Ppg() - : hpf {0.87033078, -1.74066156, 0.87033078, -1.72377617, 0.75754694}, - agc {20, 0.971, 2}, - lpf {0.11595249, 0.23190498, 0.11595249, -0.72168143, 0.18549138} { -} - -int8_t Ppg::Preprocess(float spl) { - spl -= offset; - spl = hpf.Step(spl); - spl = agc.Step(spl); - spl = lpf.Step(spl); - - auto spl_int = static_cast<int8_t>(spl); - - if (dataIndex < 200) - data[dataIndex++] = spl_int; - return spl_int; -} - -float Ppg::HeartRate() { - if (dataIndex < 200) - return 0; - - NRF_LOG_INFO("PREPROCESS, offset = %d", offset); - auto hr = ProcessHeartRate(); - dataIndex = 0; - return hr; -} -float Ppg::ProcessHeartRate() { - auto t0 = Trough(data.data(), dataIndex, 7, 48); - if (t0 < 0) - return 0; - - float t1 = t0 * 2; - t1 = Trough(data.data(), dataIndex, t1 - 5, t1 + 5); - if (t1 < 0) - return 0; - - float t2 = static_cast<int>(t1 * 3) / 2; - t2 = Trough(data.data(), dataIndex, t2 - 5, t2 + 5); - if (t2 < 0) - return 0; - - float t3 = static_cast<int>(t2 * 4) / 3; - t3 = Trough(data.data(), dataIndex, t3 - 4, t3 + 4); - if (t3 < 0) - return static_cast<int>(60 * 24 * 3) / static_cast<int>(t2); - - return static_cast<int>(60 * 24 * 4) / static_cast<int>(t3); -} - -void Ppg::SetOffset(uint16_t offset) { - this->offset = offset; - dataIndex = 0; -} - -void Ppg::Reset() { - dataIndex = 0; -} diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h deleted file mode 100644 index 7000c87..0000000 --- a/src/components/heartrate/Ppg.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include <array> -#include <cstddef> -#include <cstdint> -#include "components/heartrate/Biquad.h" -#include "components/heartrate/Ptagc.h" - -namespace Pinetime { - namespace Controllers { - class Ppg { - public: - Ppg(); - int8_t Preprocess(float spl); - float HeartRate(); - - void SetOffset(uint16_t i); - void Reset(); - - private: - std::array<int8_t, 200> data; - size_t dataIndex = 0; - float offset; - Biquad hpf; - Ptagc agc; - Biquad lpf; - - float ProcessHeartRate(); - }; - } -} diff --git a/src/components/heartrate/Ptagc.cpp b/src/components/heartrate/Ptagc.cpp deleted file mode 100644 index 1c60bc2..0000000 --- a/src/components/heartrate/Ptagc.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - SPDX-License-Identifier: LGPL-3.0-or-later - Original work Copyright (C) 2020 Daniel Thompson - C++ port Copyright (C) 2021 Jean-François Milants -*/ - -#include "components/heartrate/Ptagc.h" -#include <cmath> - -using namespace Pinetime::Controllers; - -/** Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/ppg.py */ -Ptagc::Ptagc(float start, float decay, float threshold) : peak {start}, decay {decay}, boost {1.0f / decay}, threshold {threshold} { -} - -float Ptagc::Step(float spl) { - if (std::abs(spl) > peak) - peak *= boost; - else - peak *= decay; - - if ((spl > (peak * threshold)) || (spl < (peak * -threshold))) - return 0.0f; - - spl = 100.0f * spl / (2.0f * peak); - return spl; -} diff --git a/src/components/heartrate/Ptagc.h b/src/components/heartrate/Ptagc.h deleted file mode 100644 index 3476636..0000000 --- a/src/components/heartrate/Ptagc.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -namespace Pinetime { - namespace Controllers { - class Ptagc { - public: - Ptagc(float start, float decay, float threshold); - float Step(float spl); - - private: - float peak; - float decay; - float boost; - float threshold; - }; - } -} |
