repo

Dependencies:   mbed BLE_API nRF51822 X_NUCLEO_IDB0XA1

Committer:
orshefi
Date:
Sat Feb 16 15:01:30 2019 +0000
Revision:
80:4fbfa09ac26c
Parent:
75:8128a06c0a21
Child:
81:b26ba1ec7625
proper ble input virtual inheritance;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 #include "mbed.h"
rgrover1 67:b2d2dee347c0 2 #include "ble/BLE.h"
orshefi 80:4fbfa09ac26c 3 #include "ModifiedHeartRateService.h"
orshefi 80:4fbfa09ac26c 4 //#include "ble/services/BatteryService.h"
rgrover1 67:b2d2dee347c0 5 #include "ble/services/DeviceInformationService.h"
orshefi 80:4fbfa09ac26c 6 #include <stdbool.h>
orshefi 80:4fbfa09ac26c 7 #include <stdint.h>
orshefi 80:4fbfa09ac26c 8 #include "oled.h"
orshefi 80:4fbfa09ac26c 9 #include "nrf.h"
orshefi 80:4fbfa09ac26c 10 #include "nrf51.h"
orshefi 80:4fbfa09ac26c 11 #include "nrf_gpio.h"
orshefi 80:4fbfa09ac26c 12 #include "Refrect.h"
ktownsend 0:87a7fc231fae 13
orshefi 80:4fbfa09ac26c 14
orshefi 80:4fbfa09ac26c 15 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
orshefi 80:4fbfa09ac26c 16 // Pin Definition
orshefi 80:4fbfa09ac26c 17 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ktownsend 0:87a7fc231fae 18
orshefi 80:4fbfa09ac26c 19 //Serial pc(P0_11,P0_9);
orshefi 80:4fbfa09ac26c 20
orshefi 80:4fbfa09ac26c 21 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
orshefi 80:4fbfa09ac26c 22
orshefi 80:4fbfa09ac26c 23 const static char DEVICE_NAME[] = "Valiber-V1";
rgrover1 42:06ebef2e0e44 24 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
rgrover1 42:06ebef2e0e44 25 GattService::UUID_DEVICE_INFORMATION_SERVICE};
rgrover1 39:6390604f904c 26 static volatile bool triggerSensorPolling = false;
orshefi 80:4fbfa09ac26c 27 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
orshefi 80:4fbfa09ac26c 28 // Variable Definition
orshefi 80:4fbfa09ac26c 29 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
rgrover1 72:99c283dfe28d 30
orshefi 80:4fbfa09ac26c 31 uint8_t hrmCounter = 0; // init ble Counter
orshefi 80:4fbfa09ac26c 32 uint8_t oledCounter = 0; // init oled Counter
orshefi 80:4fbfa09ac26c 33 float avg1 = 0;
orshefi 80:4fbfa09ac26c 34 uint64_t avg2 = 0;
orshefi 80:4fbfa09ac26c 35 float calibration = 0;
orshefi 80:4fbfa09ac26c 36 uint16_t result = 0;
orshefi 80:4fbfa09ac26c 37 int avgcnt1 = 0;
orshefi 80:4fbfa09ac26c 38 int avgcnt2 = 0;
orshefi 80:4fbfa09ac26c 39 int swbg = 0;
orshefi 80:4fbfa09ac26c 40 int swen = 0;
orshefi 80:4fbfa09ac26c 41 int shut_down = 0;
orshefi 80:4fbfa09ac26c 42 ModifiedHeartRateService *hrService;
rgrover1 72:99c283dfe28d 43 DeviceInformationService *deviceInfo;
orshefi 80:4fbfa09ac26c 44 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
rgrover1 72:99c283dfe28d 45
orshefi 80:4fbfa09ac26c 46 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params);
orshefi 80:4fbfa09ac26c 47
orshefi 80:4fbfa09ac26c 48 uint8_t trig1[2];
orshefi 80:4fbfa09ac26c 49 uint16_t bytesRead;
orshefi 80:4fbfa09ac26c 50
orshefi 80:4fbfa09ac26c 51 void blink()
ktownsend 0:87a7fc231fae 52 {
orshefi 80:4fbfa09ac26c 53 Set_Display_On_Off(0xAE);
orshefi 80:4fbfa09ac26c 54 wait_ms(50);
orshefi 80:4fbfa09ac26c 55 Set_Display_On_Off(0xAF);
Rohit Grover 11:1d9aafee4984 56 }
Rohit Grover 11:1d9aafee4984 57
rgrover1 74:c9d58e7847c4 58 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ktownsend 0:87a7fc231fae 59 {
rgrover1 74:c9d58e7847c4 60 BLE &ble = params->ble;
rgrover1 74:c9d58e7847c4 61 ble_error_t error = params->error;
rgrover1 74:c9d58e7847c4 62
rgrover1 72:99c283dfe28d 63 if (error != BLE_ERROR_NONE) {
rgrover1 72:99c283dfe28d 64 return;
rgrover1 72:99c283dfe28d 65 }
ktownsend 0:87a7fc231fae 66
rgrover1 65:cb76569f74f6 67 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 45:98c5a34b07a4 68 /* Setup primary service. */
orshefi 80:4fbfa09ac26c 69 hrService = new ModifiedHeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbedAustin 55:3a7d497a3e03 70 /* Setup auxiliary service. */
rgrover1 72:99c283dfe28d 71 deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
rgrover1 45:98c5a34b07a4 72 /* Setup advertising. */
rgrover1 65:cb76569f74f6 73 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 65:cb76569f74f6 74 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 65:cb76569f74f6 75 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rgrover1 65:cb76569f74f6 76 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 65:cb76569f74f6 77 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 67:b2d2dee347c0 78 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
rgrover1 65:cb76569f74f6 79 ble.gap().startAdvertising();
rgrover1 72:99c283dfe28d 80 }
rgrover1 72:99c283dfe28d 81
orshefi 80:4fbfa09ac26c 82 void cal()
rgrover1 72:99c283dfe28d 83 {
orshefi 80:4fbfa09ac26c 84 measure_led = 0;
orshefi 80:4fbfa09ac26c 85 calibration = 0;
orshefi 80:4fbfa09ac26c 86 wait(0.5);
orshefi 80:4fbfa09ac26c 87 analog_read = (1-a0.read())*1000;
orshefi 80:4fbfa09ac26c 88 // analog_read = ref.read()*1000;
orshefi 80:4fbfa09ac26c 89 calibration = analog_read;
orshefi 80:4fbfa09ac26c 90 part_num(analog_read);
orshefi 80:4fbfa09ac26c 91 // pc.printf("Subtruction : %f\n",analog_read);
orshefi 80:4fbfa09ac26c 92 // wait(0.1);
orshefi 80:4fbfa09ac26c 93 hrmCounter = 55;
orshefi 80:4fbfa09ac26c 94 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 95 wait_ms(70);
orshefi 80:4fbfa09ac26c 96 hrmCounter = num1;
orshefi 80:4fbfa09ac26c 97 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 98 wait_ms(70);
orshefi 80:4fbfa09ac26c 99 hrmCounter = num2;
orshefi 80:4fbfa09ac26c 100 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 101 wait_ms(70);
orshefi 80:4fbfa09ac26c 102 hrmCounter = num3;
orshefi 80:4fbfa09ac26c 103 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 104 wait_ms(70);
orshefi 80:4fbfa09ac26c 105 unsigned char Name1[12]={35,65,76,73,66,82,65,84,69,68,96,0};
orshefi 80:4fbfa09ac26c 106 Show_String(1,Name1,14,55);
orshefi 80:4fbfa09ac26c 107 measure_led = 1;
orshefi 80:4fbfa09ac26c 108 }
rgrover1 73:49b6090478e2 109
orshefi 80:4fbfa09ac26c 110 void meas()
orshefi 80:4fbfa09ac26c 111 {
orshefi 80:4fbfa09ac26c 112 avg1 = 0;
orshefi 80:4fbfa09ac26c 113 measure_led = 0;
orshefi 80:4fbfa09ac26c 114 wait(0.5);
orshefi 80:4fbfa09ac26c 115 analog_read = (1-a0.read())*1000;
orshefi 80:4fbfa09ac26c 116 // analog_read = ref.read()*1000;
orshefi 80:4fbfa09ac26c 117 analog_read = analog_read - calibration;
orshefi 80:4fbfa09ac26c 118 analog_read = analog_read*1.1154416 -0.004;
orshefi 80:4fbfa09ac26c 119 if(analog_read < 0)
orshefi 80:4fbfa09ac26c 120 {
orshefi 80:4fbfa09ac26c 121 analog_read = analog_read* -1;
orshefi 80:4fbfa09ac26c 122 }
orshefi 80:4fbfa09ac26c 123 part_num(analog_read);
orshefi 80:4fbfa09ac26c 124 // pc.printf("%f\n",analog_read);
orshefi 80:4fbfa09ac26c 125 // wait(0.1);
orshefi 80:4fbfa09ac26c 126 hrmCounter = 55;
orshefi 80:4fbfa09ac26c 127 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 128 wait_ms(70);
orshefi 80:4fbfa09ac26c 129 hrmCounter = num1;
orshefi 80:4fbfa09ac26c 130 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 131 wait_ms(70);
orshefi 80:4fbfa09ac26c 132 hrmCounter = num2;
orshefi 80:4fbfa09ac26c 133 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 134 wait_ms(70);
orshefi 80:4fbfa09ac26c 135 hrmCounter = num3;
orshefi 80:4fbfa09ac26c 136 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 137 wait_ms(70);
orshefi 80:4fbfa09ac26c 138 unsigned char Name1[12]={96,96,96,54,33,44,96,96,96,96,96,0};
orshefi 80:4fbfa09ac26c 139 Show_String(1,Name1,14,55);
orshefi 80:4fbfa09ac26c 140 measure_led = 1;
orshefi 80:4fbfa09ac26c 141 }
orshefi 80:4fbfa09ac26c 142
orshefi 80:4fbfa09ac26c 143 void start_up()
orshefi 80:4fbfa09ac26c 144 {
orshefi 80:4fbfa09ac26c 145 Set_Display_On_Off(0xAF);
orshefi 80:4fbfa09ac26c 146 NRF_RADIO->POWER = 1;
orshefi 80:4fbfa09ac26c 147 }
Rohit Grover 36:ea2a1b4f51c1 148
orshefi 80:4fbfa09ac26c 149 void btnrise()
orshefi 80:4fbfa09ac26c 150 {
orshefi 80:4fbfa09ac26c 151 alive_led = 1;
orshefi 80:4fbfa09ac26c 152 if((swbg<500)&&(swbg>10))
orshefi 80:4fbfa09ac26c 153 {
orshefi 80:4fbfa09ac26c 154 meas();
orshefi 80:4fbfa09ac26c 155 }
orshefi 80:4fbfa09ac26c 156 if((swbg > 500)&&(swbg <1000))
orshefi 80:4fbfa09ac26c 157 {
orshefi 80:4fbfa09ac26c 158 alive_led = 0;
orshefi 80:4fbfa09ac26c 159 cal();
orshefi 80:4fbfa09ac26c 160 }
orshefi 80:4fbfa09ac26c 161 if(swbg > 1000)
orshefi 80:4fbfa09ac26c 162 {
orshefi 80:4fbfa09ac26c 163 shut_down = 1;
orshefi 80:4fbfa09ac26c 164 }
orshefi 80:4fbfa09ac26c 165 swbg = 0;
orshefi 80:4fbfa09ac26c 166 }
rgrover1 67:b2d2dee347c0 167
orshefi 80:4fbfa09ac26c 168 void btnfall()
orshefi 80:4fbfa09ac26c 169 {
orshefi 80:4fbfa09ac26c 170 NRF_POWER->SYSTEMOFF = 0;
orshefi 80:4fbfa09ac26c 171 start_up();
orshefi 80:4fbfa09ac26c 172 alive_led = 0;
orshefi 80:4fbfa09ac26c 173 while(wake_up == 0)
orshefi 80:4fbfa09ac26c 174 {
orshefi 80:4fbfa09ac26c 175 swbg++;
orshefi 80:4fbfa09ac26c 176 wait_ms(2);
orshefi 80:4fbfa09ac26c 177 if((swbg > 500)&&(swbg <1000))
orshefi 80:4fbfa09ac26c 178 {
orshefi 80:4fbfa09ac26c 179 unsigned char Name1[12]={35,65,76,73,66,82,65,84,69,31,96,0};
orshefi 80:4fbfa09ac26c 180 Show_String(1,Name1,14,55);
orshefi 80:4fbfa09ac26c 181 // alive_led = 1;
orshefi 80:4fbfa09ac26c 182 // wait_ms(50);
orshefi 80:4fbfa09ac26c 183 // alive_led = 0;
orshefi 80:4fbfa09ac26c 184 }
orshefi 80:4fbfa09ac26c 185 if(swbg > 1000)
orshefi 80:4fbfa09ac26c 186 {
orshefi 80:4fbfa09ac26c 187 unsigned char Name1[12]={51,72,85,84,96,36,79,87,78,31,96,0};
orshefi 80:4fbfa09ac26c 188 Show_String(1,Name1,14,55);
orshefi 80:4fbfa09ac26c 189 alive_led = 1;
Rohit Grover 36:ea2a1b4f51c1 190 }
ktownsend 0:87a7fc231fae 191 }
ktownsend 0:87a7fc231fae 192 }
orshefi 80:4fbfa09ac26c 193
orshefi 80:4fbfa09ac26c 194
orshefi 80:4fbfa09ac26c 195
orshefi 80:4fbfa09ac26c 196 int main(void)
orshefi 80:4fbfa09ac26c 197 {
orshefi 80:4fbfa09ac26c 198 main_power = 1;
orshefi 80:4fbfa09ac26c 199 V33ON = 1;
orshefi 80:4fbfa09ac26c 200 LCDON = 1;
orshefi 80:4fbfa09ac26c 201 measure_led = 1;
orshefi 80:4fbfa09ac26c 202 wdt_init();
orshefi 80:4fbfa09ac26c 203 wake_up.mode(PullUp);
orshefi 80:4fbfa09ac26c 204 wake_up.rise(&btnrise);
orshefi 80:4fbfa09ac26c 205 wake_up.fall(&btnfall);
orshefi 80:4fbfa09ac26c 206 OLED_initial();
orshefi 80:4fbfa09ac26c 207 oledwrite(0,0,0,0);//oled structure initialization
orshefi 80:4fbfa09ac26c 208 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
orshefi 80:4fbfa09ac26c 209 ble.init(bleInitComplete);
orshefi 80:4fbfa09ac26c 210 while (ble.hasInitialized() == false) { /* spin loop */ }
orshefi 80:4fbfa09ac26c 211 hrmCounter = 0;
orshefi 80:4fbfa09ac26c 212 hrService->updateHeartRate(hrmCounter);//hrs write command
orshefi 80:4fbfa09ac26c 213 part_num(hrmCounter);
orshefi 80:4fbfa09ac26c 214 while (1) {//Main loop
orshefi 80:4fbfa09ac26c 215 if (ble.getGapState().connected)//Checks ble connection
orshefi 80:4fbfa09ac26c 216 {
orshefi 80:4fbfa09ac26c 217 NRF_WDT->RR[0] = WDT_RR_RR_Reload;
orshefi 80:4fbfa09ac26c 218 if(hrService->trig1[0] == 5) //"button" conditioning - trig1 is the hrs read buffer (furture info in heartrateservice.h, control point onDataWritten)
orshefi 80:4fbfa09ac26c 219 {
orshefi 80:4fbfa09ac26c 220 cal();
orshefi 80:4fbfa09ac26c 221 hrService->trig1[0] = 0;//reset the value to zero, simulating a click button
orshefi 80:4fbfa09ac26c 222 }
orshefi 80:4fbfa09ac26c 223 if(hrService->trig1[0] == 4) //"button" conditioning - trig1 is the hrs read buffer (furture info in heartrateservice.h, control point onDataWritten)
orshefi 80:4fbfa09ac26c 224 {
orshefi 80:4fbfa09ac26c 225 meas();
orshefi 80:4fbfa09ac26c 226 hrService->trig1[0] = 0;//reset the value to zero, simulating a click button
orshefi 80:4fbfa09ac26c 227 }
orshefi 80:4fbfa09ac26c 228 if(hrService->trig1[0] == 3) //repeatedly measure "button" conditioning - trig1 is the hrs read buffer (furture info in heartrateservice.h, control point onDataWritten)
orshefi 80:4fbfa09ac26c 229 {
orshefi 80:4fbfa09ac26c 230 meas();
orshefi 80:4fbfa09ac26c 231 // hrService->trig1[0] = 0;//reset the value to zero, simulating a click button
orshefi 80:4fbfa09ac26c 232 }
orshefi 80:4fbfa09ac26c 233 if(shut_down == 1)
orshefi 80:4fbfa09ac26c 234 {
orshefi 80:4fbfa09ac26c 235 LCDON = 0;
orshefi 80:4fbfa09ac26c 236 V33ON = 0;
orshefi 80:4fbfa09ac26c 237 NRF_RADIO->POWER = 0;
orshefi 80:4fbfa09ac26c 238 NRF_POWER->SYSTEMOFF =POWER_SYSTEMOFF_SYSTEMOFF_Enter;
orshefi 80:4fbfa09ac26c 239 }
orshefi 80:4fbfa09ac26c 240 }
orshefi 80:4fbfa09ac26c 241 else
orshefi 80:4fbfa09ac26c 242 {
orshefi 80:4fbfa09ac26c 243 NRF_WDT->RR[0] = WDT_RR_RR_Reload;
orshefi 80:4fbfa09ac26c 244 if(shut_down == 1)
orshefi 80:4fbfa09ac26c 245 {
orshefi 80:4fbfa09ac26c 246 LCDON = 0;
orshefi 80:4fbfa09ac26c 247 V33ON = 0;
orshefi 80:4fbfa09ac26c 248 measure_led = 0;
orshefi 80:4fbfa09ac26c 249 alive_led = 1;
orshefi 80:4fbfa09ac26c 250 clk = 0;
orshefi 80:4fbfa09ac26c 251 SCLK = 0;
orshefi 80:4fbfa09ac26c 252 SDIN = 0;
orshefi 80:4fbfa09ac26c 253 RES = 0;
orshefi 80:4fbfa09ac26c 254 CS = 0;
orshefi 80:4fbfa09ac26c 255 DC = 0;
orshefi 80:4fbfa09ac26c 256 scl = 1;
orshefi 80:4fbfa09ac26c 257 sda = 1;
orshefi 80:4fbfa09ac26c 258 wait_ms(100);
orshefi 80:4fbfa09ac26c 259 NRF_RADIO->POWER = 0;
orshefi 80:4fbfa09ac26c 260 NRF_POWER->SYSTEMOFF =POWER_SYSTEMOFF_SYSTEMOFF_Enter;
orshefi 80:4fbfa09ac26c 261 }
orshefi 80:4fbfa09ac26c 262 }
orshefi 80:4fbfa09ac26c 263 }
orshefi 80:4fbfa09ac26c 264 }
orshefi 80:4fbfa09ac26c 265
orshefi 80:4fbfa09ac26c 266 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
orshefi 80:4fbfa09ac26c 267 {
orshefi 80:4fbfa09ac26c 268 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertisin
orshefi 80:4fbfa09ac26c 269 blink();
orshefi 80:4fbfa09ac26c 270 blink();
orshefi 80:4fbfa09ac26c 271 blink();
orshefi 80:4fbfa09ac26c 272 blink();
orshefi 80:4fbfa09ac26c 273 blink();
orshefi 80:4fbfa09ac26c 274 blink();
orshefi 80:4fbfa09ac26c 275 }