|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 based Bluefruit LE modules |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | +
|
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + All text above, and the splash screen below must be included in |
| 12 | + any redistribution |
| 13 | +*********************************************************************/ |
| 14 | +#include <bluefruit.h> |
| 15 | + |
| 16 | +#define STATUS_LED (17) |
| 17 | +#define BLINKY_MS (2000) |
| 18 | + |
| 19 | +/* HRM Service Definitions |
| 20 | + * Heart Rate Monitor Service: 0x180D |
| 21 | + * Heart Rate Measurement Char: 0x2A37 |
| 22 | + * Body Sensor Location Char: 0x2A38 |
| 23 | + */ |
| 24 | +BLEService hrms = BLEService(UUID16_SVC_HEART_RATE); |
| 25 | +BLECharacteristic hrmc = BLECharacteristic(UUID16_CHR_HEART_RATE_MEASUREMENT); |
| 26 | +BLECharacteristic bslc = BLECharacteristic(UUID16_CHR_BODY_SENSOR_LOCATION); |
| 27 | + |
| 28 | +BLEDis bledis; // DIS (Device Information Service) helper class instance |
| 29 | +BLEBas blebas; // BAS (Battery Service) helper class instance |
| 30 | + |
| 31 | +uint32_t blinkyms; |
| 32 | + |
| 33 | +// Advance function prototypes |
| 34 | +void setupAdv(void); |
| 35 | +void setupHRM(void); |
| 36 | +void connect_callback(void); |
| 37 | +void disconnect_callback(uint8_t reason); |
| 38 | + |
| 39 | +void setup() |
| 40 | +{ |
| 41 | + Serial.begin(115200); |
| 42 | + Serial.println("Bluefruit52 HRM Example"); |
| 43 | + Serial.println("-----------------------"); |
| 44 | + |
| 45 | + // Setup LED pins and reset blinky counter |
| 46 | + pinMode(STATUS_LED, OUTPUT); |
| 47 | + blinkyms = millis(); |
| 48 | + |
| 49 | + // Initialise the Bluefruit module |
| 50 | + Serial.println("Initialise the Bluefruit nRF52 module"); |
| 51 | + Bluefruit.begin(); |
| 52 | + |
| 53 | + // Set the advertised device name (keep it short!) |
| 54 | + Serial.println("Setting Device Name to 'Feather52 HRM'"); |
| 55 | + Bluefruit.setName("Feather52 HRM"); |
| 56 | + |
| 57 | + // Set the connect/disconnect callback handlers |
| 58 | + Bluefruit.setConnectCallback(connect_callback); |
| 59 | + Bluefruit.setDisconnectCallback(disconnect_callback); |
| 60 | + |
| 61 | + // Configure and Start the Device Information Service |
| 62 | + Serial.println("Configuring the Device Information Service"); |
| 63 | + bledis.setManufacturer("Adafruit Industries"); |
| 64 | + bledis.setModel("nRF52 Feather"); |
| 65 | + bledis.setFirmwareRev("1.0.0"); |
| 66 | + bledis.start(); |
| 67 | + |
| 68 | + // Start the BLE Battery Service and set it to 100% |
| 69 | + Serial.println("Configuring the Battery Service"); |
| 70 | + blebas.start(); |
| 71 | + blebas.update(100); |
| 72 | + |
| 73 | + // Setup the Heart Rate Monitor service using |
| 74 | + // BLEService and BLECharacteristic classes |
| 75 | + Serial.println("Configuring the Heart Rate Monitor Service"); |
| 76 | + setupHRM(); |
| 77 | + |
| 78 | + // Setup the advertising packet(s) |
| 79 | + Serial.println("Setting up the advertising payload(s)"); |
| 80 | + setupAdv(); |
| 81 | + |
| 82 | + // Start Advertising |
| 83 | + Serial.println("Ready Player One! (Advertising)"); |
| 84 | + Bluefruit.startAdvertising(); |
| 85 | +} |
| 86 | + |
| 87 | +void setupAdv(void) |
| 88 | +{ |
| 89 | + Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 90 | + Bluefruit.addAdvTxPower(); |
| 91 | + |
| 92 | + // Include HRM Service UUID |
| 93 | + Bluefruit.addAdvService(hrms); |
| 94 | + |
| 95 | + // There isn't enough room in the advertising packet for the |
| 96 | + // name so we'll place it on the secondary Scan Response packet |
| 97 | + Bluefruit.addScanRespName(); |
| 98 | +} |
| 99 | + |
| 100 | +void setupHRM(void) |
| 101 | +{ |
| 102 | + // Configure the Heart Rate Monitor service |
| 103 | + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml |
| 104 | + // Supported Characteristics: |
| 105 | + // Name UUID Requirement Properties |
| 106 | + // ---------------------------- ------ ----------- ---------- |
| 107 | + // Heart Rate Measurement 0x2137 Mandatory Notify |
| 108 | + // Body Sensor Location 0x2138 Optional Read |
| 109 | + // Heart Rate Control Point 0x2A39 Conditional Write <-- Not used here |
| 110 | + hrms.start(); |
| 111 | + |
| 112 | + // Note: You must start the service before calling any characteristics |
| 113 | + // Calling .start() on a BLECharacteristic will cause it to be added to |
| 114 | + // the last BLEService that was 'start()'ed! |
| 115 | + |
| 116 | + // Configure the Heart Rate Measurement characteristic |
| 117 | + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.heart_rate_measurement.xml |
| 118 | + // Permission = Notify |
| 119 | + // Min Len = 1 |
| 120 | + // Max Len = 8 |
| 121 | + // B0 = UINT8 - Flag (MANDATORY) |
| 122 | + // b5:7 = Reserved |
| 123 | + // b4 = RR-Internal (0 = Not present, 1 = Present) |
| 124 | + // b3 = Energy expended status (0 = Not present, 1 = Present) |
| 125 | + // b1:2 = Sensor contact status (0+1 = Not supported, 2 = Supported but contact not detected, 3 = Supported and detected) |
| 126 | + // b0 = Value format (0 = UINT8, 1 = UINT16) |
| 127 | + // B1 = UINT8 - 8-bit heart rate measurement value in BPM |
| 128 | + // B2:3 = UINT16 - 16-bit heart rate measurement value in BPM |
| 129 | + // B4:5 = UINT16 - Energy expended in joules |
| 130 | + // B6:7 = UINT16 - RR Internal (1/1024 second resolution) |
| 131 | + hrmc.setProperties(CHR_PROPS_NOTIFY); |
| 132 | + hrmc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 133 | + hrmc.setMaxLen(8); |
| 134 | + hrmc.start(); |
| 135 | + |
| 136 | + // Set the characteristic to use 8-bit values, with the sensor connected and detected |
| 137 | + uint8_t hrmdata[2] = { 0b00000110, 0x40 }; |
| 138 | + hrmc.write(hrmdata, 2); |
| 139 | + |
| 140 | + // Configure the Body Sensor Location characteristic |
| 141 | + // See: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml |
| 142 | + // Permission = Read |
| 143 | + // Min Len = 1 |
| 144 | + // Max Len = 1 |
| 145 | + // B0 = UINT8 - Body Sensor Location |
| 146 | + // 0 = Other |
| 147 | + // 1 = Chest |
| 148 | + // 2 = Wrist |
| 149 | + // 3 = Finger |
| 150 | + // 4 = Hand |
| 151 | + // 5 = Ear Lobe |
| 152 | + // 6 = Foot |
| 153 | + // 7:255 = Reserved |
| 154 | + bslc.setProperties(CHR_PROPS_READ); |
| 155 | + bslc.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 156 | + bslc.setFixedLen(1); |
| 157 | + bslc.start(); |
| 158 | + |
| 159 | + // Set the characteristic to 'Wrist' (2) |
| 160 | + bslc.write(2); |
| 161 | +} |
| 162 | + |
| 163 | +uint8_t bps = 0; |
| 164 | +void loop() |
| 165 | +{ |
| 166 | + // Blinky! |
| 167 | + if (blinkyms+BLINKY_MS < millis()) { |
| 168 | + blinkyms = millis(); |
| 169 | + digitalToggle(STATUS_LED); |
| 170 | + |
| 171 | + // Set the characteristic to use 8-bit values, with the sensor connected and detected |
| 172 | + uint8_t hrmdata[2] = { 0b00000110, bps++ }; |
| 173 | + hrmc.write(hrmdata, sizeof(hrmdata)); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +void connect_callback(void) |
| 178 | +{ |
| 179 | + Serial.println("Connected"); |
| 180 | +} |
| 181 | + |
| 182 | +void disconnect_callback(uint8_t reason) |
| 183 | +{ |
| 184 | + (void) reason; |
| 185 | + |
| 186 | + Serial.println("Disconnected"); |
| 187 | + Serial.println("Bluefruit will auto start advertising (default)"); |
| 188 | +} |
0 commit comments