ECG data acquisition with Analog device frontend and Redbear nano BLE

Dependencies:   BLE_API mbed nRF51822

Fork of BLENano_SimpleControls by RedBearLab

Reference Design

2 channel EKG with Redbear BLE reference and Analog Device amplifier to generate RAW EKG data fed into Medtrics MaaS service . Medtrics API can consumer raw input with given parameters of ADC sample frequency and scaling factor

Specification

  • Application Processor: nRF51822/BLE , Analog frontend: AD8232 /
  • Input Analog Voltage = 3.3V
  • 10 bit ADC input range = (0-1023) or scaling factor= 3.22mV/unit (this is ADC resolution)
  • ADC sample frequency (BLE pull rate) = 250Hz (4ms per sample)

/media/uploads/pkweitai/ble_-3-.jpg Reference IOS and Android app will be online soon!

Committer:
pkweitai
Date:
Sat Jan 07 06:20:36 2017 +0000
Revision:
4:b95d3432a495
Parent:
3:f530ca03e014
medtricsIO ECG data collection via BLE Redbear reference design

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:81a97eb70d3d 1 /*
RedBearLab 1:81a97eb70d3d 2
RedBearLab 1:81a97eb70d3d 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:81a97eb70d3d 4
RedBearLab 1:81a97eb70d3d 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:81a97eb70d3d 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:81a97eb70d3d 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:81a97eb70d3d 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:81a97eb70d3d 9 subject to the following conditions:
RedBearLab 1:81a97eb70d3d 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:81a97eb70d3d 11
RedBearLab 1:81a97eb70d3d 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:81a97eb70d3d 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:81a97eb70d3d 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:81a97eb70d3d 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:81a97eb70d3d 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:81a97eb70d3d 17
RedBearLab 1:81a97eb70d3d 18 */
RedBearLab 0:be2e4095513a 19
RedBearLab 0:be2e4095513a 20 #include "mbed.h"
RedBearLab 2:3cd654f42efa 21 #include "ble/BLE.h"
RedBearLab 0:be2e4095513a 22 #include "Servo.h"
RedBearLab 0:be2e4095513a 23
RedBearLab 0:be2e4095513a 24
RedBearLab 0:be2e4095513a 25 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:be2e4095513a 26 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:be2e4095513a 27 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:be2e4095513a 28
RedBearLab 0:be2e4095513a 29 #define TXRX_BUF_LEN 20
RedBearLab 0:be2e4095513a 30
RedBearLab 0:be2e4095513a 31 #define DIGITAL_OUT_PIN P0_9 //TXD
pkweitai 4:b95d3432a495 32 #define DIGITAL_IN_PIN10 P0_10 //CTS
pkweitai 4:b95d3432a495 33 #define DIGITAL_IN_PIN11 P0_11 //RXD
pkweitai 4:b95d3432a495 34
pkweitai 4:b95d3432a495 35 //#define PWM_PIN P0_11 //RXD
RedBearLab 0:be2e4095513a 36 #define SERVO_PIN P0_8 //RTS
RedBearLab 0:be2e4095513a 37 #define ANALOG_IN_PIN P0_4 //P04
RedBearLab 0:be2e4095513a 38
RedBearLab 2:3cd654f42efa 39 BLE ble;
RedBearLab 0:be2e4095513a 40
RedBearLab 0:be2e4095513a 41 DigitalOut LED_SET(DIGITAL_OUT_PIN);
pkweitai 4:b95d3432a495 42 DigitalIn P10(DIGITAL_IN_PIN10);
pkweitai 4:b95d3432a495 43 DigitalIn P11(DIGITAL_IN_PIN11);
pkweitai 4:b95d3432a495 44
pkweitai 4:b95d3432a495 45 //PwmOut PWM(PWM_PIN);
RedBearLab 0:be2e4095513a 46 AnalogIn ANALOG(ANALOG_IN_PIN);
RedBearLab 0:be2e4095513a 47 Servo MYSERVO(SERVO_PIN);
RedBearLab 0:be2e4095513a 48
RedBearLab 0:be2e4095513a 49 //Serial pc(USBTX, USBRX);
RedBearLab 0:be2e4095513a 50
RedBearLab 0:be2e4095513a 51 static uint8_t analog_enabled = 0;
RedBearLab 0:be2e4095513a 52 static uint8_t old_state = 0;
RedBearLab 0:be2e4095513a 53
RedBearLab 0:be2e4095513a 54 // The Nordic UART Service
RedBearLab 0:be2e4095513a 55 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 56 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 57 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 58 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:be2e4095513a 59
RedBearLab 0:be2e4095513a 60
RedBearLab 0:be2e4095513a 61 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 62 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 63
RedBearLab 0:be2e4095513a 64 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 65 //static uint8_t rx_len=0;
RedBearLab 0:be2e4095513a 66
RedBearLab 0:be2e4095513a 67
RedBearLab 0:be2e4095513a 68 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:be2e4095513a 69
RedBearLab 0:be2e4095513a 70 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:be2e4095513a 71
RedBearLab 0:be2e4095513a 72 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 73
RedBearLab 0:be2e4095513a 74 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 75
RedBearLab 0:be2e4095513a 76
RedBearLab 0:be2e4095513a 77
RedBearLab 3:f530ca03e014 78 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:be2e4095513a 79 {
RedBearLab 0:be2e4095513a 80 //pc.printf("Disconnected \r\n");
RedBearLab 0:be2e4095513a 81 //pc.printf("Restart advertising \r\n");
RedBearLab 3:f530ca03e014 82 ble.gap().startAdvertising();
RedBearLab 0:be2e4095513a 83 }
RedBearLab 0:be2e4095513a 84
RedBearLab 2:3cd654f42efa 85 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:be2e4095513a 86 {
RedBearLab 0:be2e4095513a 87 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 88 uint16_t bytesRead;
RedBearLab 0:be2e4095513a 89
RedBearLab 2:3cd654f42efa 90 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:be2e4095513a 91 {
RedBearLab 0:be2e4095513a 92 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 93 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 94 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:be2e4095513a 95
RedBearLab 0:be2e4095513a 96 //for(index=0; index<bytesRead; index++)
RedBearLab 0:be2e4095513a 97 //pc.putc(buf[index]);
RedBearLab 0:be2e4095513a 98
RedBearLab 0:be2e4095513a 99 if(buf[0] == 0x01)
RedBearLab 0:be2e4095513a 100 {
RedBearLab 0:be2e4095513a 101 if(buf[1] == 0x01)
RedBearLab 0:be2e4095513a 102 LED_SET = 1;
RedBearLab 0:be2e4095513a 103 else
RedBearLab 0:be2e4095513a 104 LED_SET = 0;
RedBearLab 0:be2e4095513a 105 }
RedBearLab 0:be2e4095513a 106 else if(buf[0] == 0xA0)
RedBearLab 0:be2e4095513a 107 {
RedBearLab 0:be2e4095513a 108 if(buf[1] == 0x01)
RedBearLab 0:be2e4095513a 109 analog_enabled = 1;
RedBearLab 0:be2e4095513a 110 else
RedBearLab 0:be2e4095513a 111 analog_enabled = 0;
RedBearLab 0:be2e4095513a 112 }
RedBearLab 0:be2e4095513a 113 else if(buf[0] == 0x02)
RedBearLab 0:be2e4095513a 114 {
pkweitai 4:b95d3432a495 115 //float value = (float)buf[1]/255;
pkweitai 4:b95d3432a495 116 //PWM = value;
RedBearLab 0:be2e4095513a 117 }
RedBearLab 0:be2e4095513a 118 else if(buf[0] == 0x03)
RedBearLab 0:be2e4095513a 119 {
RedBearLab 0:be2e4095513a 120 MYSERVO.write(buf[1]);
RedBearLab 0:be2e4095513a 121 }
RedBearLab 0:be2e4095513a 122 else if(buf[0] == 0x04)
RedBearLab 0:be2e4095513a 123 {
RedBearLab 0:be2e4095513a 124 analog_enabled = 0;
pkweitai 4:b95d3432a495 125 //PWM = 0;
pkweitai 4:b95d3432a495 126 //MYSERVO.write(0);
RedBearLab 0:be2e4095513a 127 LED_SET = 0;
RedBearLab 0:be2e4095513a 128 old_state = 0;
RedBearLab 0:be2e4095513a 129 }
RedBearLab 0:be2e4095513a 130
RedBearLab 0:be2e4095513a 131 }
RedBearLab 0:be2e4095513a 132 }
RedBearLab 0:be2e4095513a 133 /*
RedBearLab 0:be2e4095513a 134 void uartCB(void)
RedBearLab 0:be2e4095513a 135 {
RedBearLab 0:be2e4095513a 136 while(pc.readable())
RedBearLab 0:be2e4095513a 137 {
RedBearLab 0:be2e4095513a 138 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:be2e4095513a 139 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:be2e4095513a 140 {
RedBearLab 0:be2e4095513a 141 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:be2e4095513a 142 pc.printf("RecHandler \r\n");
RedBearLab 0:be2e4095513a 143 pc.printf("Length: ");
RedBearLab 0:be2e4095513a 144 pc.putc(rx_len);
RedBearLab 0:be2e4095513a 145 pc.printf("\r\n");
RedBearLab 0:be2e4095513a 146 rx_len = 0;
RedBearLab 0:be2e4095513a 147 break;
RedBearLab 0:be2e4095513a 148 }
RedBearLab 0:be2e4095513a 149 }
RedBearLab 0:be2e4095513a 150 }
RedBearLab 0:be2e4095513a 151 */
RedBearLab 0:be2e4095513a 152 void m_status_check_handle(void)
RedBearLab 0:be2e4095513a 153 {
RedBearLab 0:be2e4095513a 154 uint8_t buf[3];
pkweitai 4:b95d3432a495 155 //if (analog_enabled) // if analog reading enabled
pkweitai 4:b95d3432a495 156 if ( 1 && ((P10 ==1) || (P11==1)))
pkweitai 4:b95d3432a495 157 {
pkweitai 4:b95d3432a495 158 //idle
pkweitai 4:b95d3432a495 159 }
pkweitai 4:b95d3432a495 160 else
RedBearLab 0:be2e4095513a 161 {
RedBearLab 0:be2e4095513a 162 // Read and send out
RedBearLab 0:be2e4095513a 163 float s = ANALOG;
RedBearLab 0:be2e4095513a 164 uint16_t value = s*1024;
RedBearLab 0:be2e4095513a 165 buf[0] = (0x0B);
RedBearLab 0:be2e4095513a 166 buf[1] = (value >> 8);
RedBearLab 0:be2e4095513a 167 buf[2] = (value);
RedBearLab 0:be2e4095513a 168 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 169 }
RedBearLab 0:be2e4095513a 170
RedBearLab 0:be2e4095513a 171 // If digital in changes, report the state
pkweitai 4:b95d3432a495 172 //if (BUTTON != old_state)
pkweitai 4:b95d3432a495 173 // {
pkweitai 4:b95d3432a495 174 // old_state = BUTTON;
RedBearLab 0:be2e4095513a 175
pkweitai 4:b95d3432a495 176 if (P10 == 1)
RedBearLab 0:be2e4095513a 177 {
RedBearLab 0:be2e4095513a 178 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 179 buf[1] = (0x01);
RedBearLab 0:be2e4095513a 180 buf[2] = (0x00);
RedBearLab 0:be2e4095513a 181 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 182 }
RedBearLab 0:be2e4095513a 183 else
RedBearLab 0:be2e4095513a 184 {
RedBearLab 0:be2e4095513a 185 buf[0] = (0x0A);
RedBearLab 0:be2e4095513a 186 buf[1] = (0x00);
RedBearLab 0:be2e4095513a 187 buf[2] = (0x00);
RedBearLab 0:be2e4095513a 188 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
RedBearLab 0:be2e4095513a 189 }
pkweitai 4:b95d3432a495 190 //}
RedBearLab 0:be2e4095513a 191 }
RedBearLab 0:be2e4095513a 192
RedBearLab 0:be2e4095513a 193
RedBearLab 0:be2e4095513a 194 int main(void)
RedBearLab 0:be2e4095513a 195 {
RedBearLab 0:be2e4095513a 196 Ticker ticker;
pkweitai 4:b95d3432a495 197 ticker.attach_us(m_status_check_handle, 4000);
RedBearLab 0:be2e4095513a 198
RedBearLab 0:be2e4095513a 199 ble.init();
RedBearLab 0:be2e4095513a 200 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:be2e4095513a 201 ble.onDataWritten(WrittenHandler);
RedBearLab 0:be2e4095513a 202
RedBearLab 0:be2e4095513a 203 //pc.baud(9600);
RedBearLab 0:be2e4095513a 204 //pc.printf("SimpleChat Init \r\n");
RedBearLab 0:be2e4095513a 205
RedBearLab 0:be2e4095513a 206 //pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:be2e4095513a 207
RedBearLab 0:be2e4095513a 208 // setup advertising
RedBearLab 0:be2e4095513a 209 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:be2e4095513a 210 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:be2e4095513a 211 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:be2e4095513a 212 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:be2e4095513a 213 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:be2e4095513a 214 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:be2e4095513a 215 // 100ms; in multiples of 0.625ms.
RedBearLab 0:be2e4095513a 216 ble.setAdvertisingInterval(160);
RedBearLab 0:be2e4095513a 217
RedBearLab 0:be2e4095513a 218 ble.addService(uartService);
RedBearLab 0:be2e4095513a 219
RedBearLab 0:be2e4095513a 220 ble.startAdvertising();
RedBearLab 0:be2e4095513a 221
RedBearLab 0:be2e4095513a 222 //pc.printf("Advertising Start \r\n");
RedBearLab 0:be2e4095513a 223
RedBearLab 0:be2e4095513a 224 while(1)
RedBearLab 0:be2e4095513a 225 {
RedBearLab 0:be2e4095513a 226 ble.waitForEvent();
RedBearLab 0:be2e4095513a 227 }
RedBearLab 0:be2e4095513a 228 }
RedBearLab 0:be2e4095513a 229
RedBearLab 0:be2e4095513a 230
RedBearLab 0:be2e4095513a 231
RedBearLab 0:be2e4095513a 232
RedBearLab 0:be2e4095513a 233
RedBearLab 0:be2e4095513a 234
RedBearLab 0:be2e4095513a 235
RedBearLab 0:be2e4095513a 236
RedBearLab 0:be2e4095513a 237
RedBearLab 0:be2e4095513a 238
RedBearLab 0:be2e4095513a 239
RedBearLab 0:be2e4095513a 240
RedBearLab 0:be2e4095513a 241
RedBearLab 0:be2e4095513a 242
RedBearLab 0:be2e4095513a 243
RedBearLab 0:be2e4095513a 244
RedBearLab 0:be2e4095513a 245
RedBearLab 0:be2e4095513a 246
RedBearLab 0:be2e4095513a 247
RedBearLab 0:be2e4095513a 248
RedBearLab 0:be2e4095513a 249
RedBearLab 0:be2e4095513a 250
RedBearLab 0:be2e4095513a 251
RedBearLab 0:be2e4095513a 252
RedBearLab 0:be2e4095513a 253
RedBearLab 0:be2e4095513a 254
RedBearLab 0:be2e4095513a 255
RedBearLab 0:be2e4095513a 256
RedBearLab 0:be2e4095513a 257
RedBearLab 0:be2e4095513a 258
RedBearLab 0:be2e4095513a 259