-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathBLE_Heart_Service.cpp
More file actions
48 lines (43 loc) · 2.26 KB
/
BLE_Heart_Service.cpp
File metadata and controls
48 lines (43 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include "BLE_Heart_Service.h"
#include "DirConManager.h"
#include <Constants.h>
BLE_Heart_Service::BLE_Heart_Service() : pHeartService(nullptr), heartRateMeasurementCharacteristic(nullptr) {}
void BLE_Heart_Service::setupService(NimBLEServer *pServer, MyCharacteristicCallbacks *chrCallbacks) {
// HEART RATE MONITOR SERVICE SETUP
pHeartService = spinBLEServer.pServer->createService(HEARTSERVICE_UUID);
heartRateMeasurementCharacteristic = pHeartService->createCharacteristic(HEARTCHARACTERISTIC_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
byte heartRateMeasurement[2] = {0x00, 0x00};
heartRateMeasurementCharacteristic->setValue(heartRateMeasurement, 2);
heartRateMeasurementCharacteristic->setCallbacks(chrCallbacks);
pHeartService->start();
// Add service UUID to DirCon MDNS
DirConManager::addBleServiceUuid(pHeartService->getUUID());
}
void BLE_Heart_Service::deinit() {
if (pHeartService != nullptr) {
if (heartRateMeasurementCharacteristic != nullptr) {
pHeartService->removeCharacteristic(heartRateMeasurementCharacteristic);
heartRateMeasurementCharacteristic = nullptr;
}
spinBLEServer.pServer->removeService(pHeartService);
pHeartService = nullptr;
spinBLEServer.pServer->getAdvertising()->stop();
SS2K_LOG(BLE_COMMON_LOG_TAG, "Heart Rate Service De-initialized");
}
}
void BLE_Heart_Service::update() {
byte heartRateMeasurement[2] = {0x00, (byte)rtConfig->hr.getValue()};
// Notify the cycling power measurement characteristic
spinBLEServer.notifyBleAndDircon(heartRateMeasurementCharacteristic, heartRateMeasurement, 2);
const int kLogBufCapacity = 125; // Data(10), Sep(data/2), Arrow(3), CharId(37), Sep(3), CharId(37), Sep(3), Name(8), Prefix(2), HR(7), Suffix(2), Nul(1), rounded up
char logBuf[kLogBufCapacity];
const size_t heartRateMeasurementLength = sizeof(heartRateMeasurement) / sizeof(heartRateMeasurement[0]);
logCharacteristic(logBuf, kLogBufCapacity, heartRateMeasurement, heartRateMeasurementLength, HEARTSERVICE_UUID, heartRateMeasurementCharacteristic->getUUID(),
"HRS(HRM)[ HR(%d) ]", rtConfig->hr.getValue() % 1000);
}