BLE Heart Rate Sample Program for HRM1017 which is using Nordic nRF51822 confirmed the connection of nRFToolbox on Android.
Dependencies: BLE_API mbed nRF51822 color_pixels
Fork of BLE_HTM_HRM1017 by
Diff: main.cpp
- Revision:
- 11:d32f4f43161d
- Parent:
- 10:8a67578c3ef0
--- a/main.cpp Mon Aug 22 06:12:27 2016 +0000 +++ b/main.cpp Sun Oct 16 14:25:59 2016 +0000 @@ -1,10 +1,12 @@ #include "mbed.h" #include "BLE.h" +#include "color_pixels.h" #include <math.h> +#include "PulseSensor.h" -#define NEED_DEBUG 1 -#if NEED_DEBUG +#define NEED_DEBUG +#ifdef NEED_DEBUG #define DEBUG(...) { printf(__VA_ARGS__); } #else #define DEBUG(...) /* nothing */ @@ -87,11 +89,16 @@ triggerSensorPolling = true; } +ColorPixels pixels(6,8); +int ledfade = 0; +void pixelPattern(int n, uint8_t r, uint8_t g, uint8_t b); + + int main(void) -{ +{ Ticker ticker; ticker.attach(periodicCallback, 1); - + DEBUG("Initialising the nRF51822\r\n"); ble.init(); DEBUG("Init done\r\n"); @@ -114,6 +121,14 @@ ble.gattServer().addService(battService); ble.gattServer().addService(deviceInformationService); DEBUG("Add Service\r\n"); + + initPulseSensor(); + + Ticker hrm_ticker; + hrm_ticker.attach(calcHeartRate, 0.02); + + int counter = 0; + uint8_t cl = 193; while (true) { if (triggerSensorPolling) { @@ -122,9 +137,20 @@ } else { ble.waitForEvent(); } + if (counter % 3 == 0) { + if (ledfade) { + --ledfade; + pixelPattern(ledfade, cl, cl, cl); + counter = 0; + } + } + ++counter; } + } + + void updateServiceValues(void) { /* Decrement the battery level. */ @@ -132,7 +158,77 @@ ble.gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt)); /* Randomize the heart rate. */ - hrmCounter = (rand() % 150) + 30; + // hrmCounter = (rand() % 150) + 30; + if (isQS()) { + hrmCounter = getBPM(); + DEBUG("BPM: %d\r\n", hrmCounter); + ledfade = 5; + } + bpm[1] = hrmCounter; ble.gattServer().write(hrmChar.getValueAttribute().getHandle(), bpm, sizeof(bpm)); } + + +void pixelPattern(int n, uint8_t r, uint8_t g, uint8_t b) { + + if (n < 0 || n > 4) return; + + switch (n) { + case 4: + pixels.set_color(7, r, g, b); + pixels.set_color(6, r, g, b); + pixels.set_color(5, r, g, b); + pixels.set_color(4, r, g, b); + pixels.set_color(3, r, g, b); + pixels.set_color(2, r, g, b); + pixels.set_color(1, r, g, b); + pixels.set_color(0, r, g, b); + pixels.update(); + break; + case 3: + pixels.set_color(7, 0, 0, 0); + pixels.set_color(6, r/2, g/2, b/2); + pixels.set_color(5, r*3/4, g*3/4, b*3/4); + pixels.set_color(4, r, g, b); + pixels.set_color(3, r, g, b); + pixels.set_color(2, r*3/4, g*3/4, b*3/4); + pixels.set_color(1, r/2, g/2, b/2); + pixels.set_color(0, 0, 0, 0); + pixels.update(); + break; + case 2: + pixels.set_color(7, 0, 0, 0); + pixels.set_color(6, 0, 0, 0); + pixels.set_color(5, r/2, g/2, b/2); + pixels.set_color(4, r*3/4, g*3/4, b*3/4); + pixels.set_color(3, r*3/4, g*3/4, b*3/4); + pixels.set_color(2, r/2, g/2, b/2); + pixels.set_color(1, 0, 0, 0); + pixels.set_color(0, 0, 0, 0); + pixels.update(); + break; + case 1: + pixels.set_color(7, 0, 0, 0); + pixels.set_color(6, 0, 0, 0); + pixels.set_color(5, 0, 0, 0); + pixels.set_color(4, r/2, g/2, b/4); + pixels.set_color(3, r/2, g/2, b/4); + pixels.set_color(2, 0, 0, 0); + pixels.set_color(1, 0, 0, 0); + pixels.set_color(0, 0, 0, 0); + pixels.update(); + break; + case 0: + default: + pixels.set_color(7, 0, 0, 0); + pixels.set_color(6, 0, 0, 0); + pixels.set_color(5, 0, 0, 0); + pixels.set_color(4, 0, 0, 0); + pixels.set_color(3, 0, 0, 0); + pixels.set_color(2, 0, 0, 0); + pixels.set_color(1, 0, 0, 0); + pixels.set_color(0, 0, 0, 0); + pixels.update(); + } +} \ No newline at end of file