Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
Diff: main.cpp
- Revision:
- 4:821e9af5e750
- Parent:
- 3:a51ca6313ad2
diff -r a51ca6313ad2 -r 821e9af5e750 main.cpp
--- a/main.cpp Tue Sep 29 15:06:46 2015 +0000
+++ b/main.cpp Tue Nov 03 23:10:37 2015 +0000
@@ -1,17 +1,8 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+/* BLE Example Heart rate tutorial, just pay attention that the
+ * X-Nucleo-BLE board driver is not compatible to the new mbed BLE_API
+ * so don't update the newest version of BLE_API, also on this example
+ * we added a custom service (LED-ON/OFF) with 2 characteristics (Read and Write)
+ * to turn on/off the led
*/
#include "mbed.h"
@@ -21,43 +12,86 @@
#include "ble/services/DeviceInformationService.h"
BLE ble;
+
+// If you apply the D13 pin patch you cannot use this led anymore
DigitalOut led1(LED1);
-const static char DEVICE_NAME[] = "HRM1";
+const static char DEVICE_NAME[] = "LeoBoard";
+// Has the heart service, device information,
+// and a custom service (for controllign the leds)
static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
- GattService::UUID_DEVICE_INFORMATION_SERVICE};
+ GattService::UUID_DEVICE_INFORMATION_SERVICE, 0xFFFF
+ }; //Custom UUID, FFFF is reserved for development
static volatile bool triggerSensorPolling = false;
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
+// Custom service and characteristics UUIDS
+uint16_t customServiceUUID = 0xA000;
+uint16_t readCharUUID = 0xA001;
+uint16_t writeCharUUID = 0xA002;
+
+// Set Up custom Characteristics (Package max size is 20bytes)
+static uint8_t readValue[20] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
+static uint8_t writeValue[20] = {0};
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+// Set up custom service
+GattCharacteristic *characteristics[] = {&readChar, &writeChar};
+GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
+ printf("Start advertising\r\n");
ble.gap().startAdvertising(); // restart advertising
}
+// Handle writes to writeCharacteristic
+void writeCharCallback(const GattWriteCallbackParams *params) {
+ // check to see what characteristic was written, by handle
+ if(params->handle == writeChar.getValueHandle()) {
+ // toggle LED if only 1 byte is written
+ if(params->len == 1) {
+ led1 = params->data[0];
+ (params->data[0] == 0x00) ? printf("led off\n\r") : printf("led on\n\r"); // print led toggle
+ }
+ else {
+ printf("Data received: length = %d, data = 0x",params->len);
+ for(int x=0; x < params->len; x++) {
+ printf("%x", params->data[x]);
+ }
+ printf("\n\r");
+ }
+ // update the readChar with the value of writeChar
+ ble.updateCharacteristicValue(readChar.getValueHandle(), params->data,params->len);
+ }
+}
+
void periodicCallback(void)
{
- led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
/* Note that the periodicCallback() executes in interrupt context, so it is safer to do
* heavy-weight sensor polling from the main thread. */
triggerSensorPolling = true;
}
-int main(void)
-{
- led1 = 1;
+int main(void){
Ticker ticker;
ticker.attach(periodicCallback, 1); // blink LED every second
+ printf("Initialize BLE\r\n");
ble.init();
ble.gap().onDisconnection(disconnectionCallback);
+ ble.gattServer().onDataWritten(writeCharCallback);
+
/* Setup primary service. */
uint8_t hrmCounter = 100; // init HRM to 100bps
HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
- /* Setup auxiliary service. */
- DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+ // Device Information
+ DeviceInformationService deviceInfo(ble, "StarkIndustires", "Quadcopter", "SN1", "hw-rev1", "fw-rev1", "BetaVer");
- /* Setup advertising. */
+ // add our custom service
+ ble.addService(customService);
+
+ // Setup advertising. Indicate that we only support bluetooth low energy
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
@@ -65,8 +99,7 @@
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(1000); /* 1000ms */
ble.gap().startAdvertising();
-
- // infinite loop
+
while (1) {
// check for trigger from periodicCallback()
if (triggerSensorPolling && ble.getGapState().connected) {
