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 N06_NAND by
Diff: main.cpp
- Revision:
- 22:d467526abc4a
- Parent:
- 21:0e7c08f5386f
- Child:
- 23:2e73c391bb12
diff -r 0e7c08f5386f -r d467526abc4a main.cpp
--- a/main.cpp Wed Oct 05 09:16:58 2016 +0000
+++ b/main.cpp Sun Dec 18 05:33:58 2016 +0000
@@ -1,108 +1,111 @@
-/* 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.
- */
+#include <mbed.h>
+#include "ble/BLE.h"
+#include "bricks.h"
-#include "mbed.h"
-#include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
-
-DigitalOut led1(LED1, 1);
-
-const static char DEVICE_NAME[] = "HRM1";
-static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+//==============================================================================
+// Custom Service Definition
+//==============================================================================
-static volatile bool triggerSensorPolling = false;
+ typedef union Buffer
+ {
+ uint8_t ubuf[10];
+ char cbuf[10];
+ uint8_t byte;
+ uint16_t word16;
+ uint32_t word32;
+ int i;
+ double d;
+ float f;
+ } Buffer;
+
+// Service svc(0xA000); // setup GATT service, UUID 0xA000
+// Characteristic a(svc,0xA001,"r"); // characteristic A with UUID 0xA001
+// Characteristic x(svc,0xA002,"w"); // characteristic X with UUID 0xA002
+// Characteristic y(svc,0xA003,"w"); // characteristic Y with UUID 0xA003
+
+ static Buffer adata[1];
+ ReadOnlyArrayGattCharacteristic<Buffer,1> a(0xA001, adata);
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
- (void)params;
- BLE::Instance().gap().startAdvertising(); // restart advertising
-}
+ static Buffer xdata[1];
+ WriteOnlyArrayGattCharacteristic<Buffer,1> x(0xA002, xdata);
-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;
-}
+ static Buffer ydata[1];
+ WriteOnlyArrayGattCharacteristic<Buffer,1> y(0xA002, ydata);
+
+ GattCharacteristic *characteristics[] = {&a,&x,&y};
+ GattService service(0xA000, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+
+//==============================================================================
+// Some Callbacks
+//==============================================================================
-void onBleInitError(BLE &ble, ble_error_t error)
-{
- (void)ble;
- (void)error;
- /* Initialization error handling should go here */
-}
+ void onWritten(Blob &blue) // Handle writes to writeCharacteristic
+ {
+ const GattWriteCallbackParams *p = blue.pWritten;
+
+ // Check to see what characteristic was written, by handle
+
+ if(p->handle == x.getValueHandle())
+ blink("x x x xxx x ",CONNECTED);
+ else if(p->handle == y.getValueHandle())
+ blink("x x x xxx xxx x ",CONNECTED);
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
-{
- BLE& ble = params->ble;
- ble_error_t error = params->error;
+ // Update the readChar with the value of writeChar
- if (error != BLE_ERROR_NONE) {
- onBleInitError(ble, error);
- return;
- }
+ //blue.pble->gattServer().write(a.getValueHandle(), p->data, p->len);
+ blue.gatt().write(a.getValueHandle(), p->data, p->len);
+ }
+
- if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
- return;
- }
+ void onConnect(Blob &blue) // Connection Callback
+ {
+ blink("x x x ",CONNECTED); // indicate advertising
+ }
- ble.gap().onDisconnection(disconnectionCallback);
- /* Setup primary service. */
- uint8_t hrmCounter = 60; // init HRM to 60bps
- HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+ void onDisconnect(Blob &blue) // Disconnection Callback
+ {
+ blue.start(); // start advertising on client disconnect
+ blink(ADVERTISE); // indicate advertising
+ }
+
- /* Setup advertising. */
- 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);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
- ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.gap().setAdvertisingInterval(1000); /* 1000ms */
- ble.gap().startAdvertising();
+ void onError(Blob &blue) // Error Reporting Callback
+ {
+ (void) blue; // Avoid compiler warnings
+ blink(FAULT); // indicate advertising
+ }
+
- // infinite loop
- while (true) {
- // check for trigger from periodicCallback()
- if (triggerSensorPolling && ble.getGapState().connected) {
- triggerSensorPolling = false;
-
- // Do blocking calls or whatever is necessary for sensor polling.
- // In our case, we simply update the HRM measurement.
- hrmCounter++;
-
- // 60 <= HRM bps <= 100
- if (hrmCounter == 100) {
- hrmCounter = 60;
- }
+ void onSetup(Blob &blue) // Immediately After Initializing BLE
+ {
+ blue.device("nRF51-DK"); // setup device name
+ blue.data("ABCDEF"); // setup advertising data
+ blue.name("T06.0 Custom GATT"); // setup advertising name
- // update bps
- hrService.updateHeartRate(hrmCounter);
- } else {
- ble.waitForEvent(); // low power wait for event
- }
- }
-}
+ blue.onConnect(onConnect); // setup connection callback
+ blue.onDisconnect(onDisconnect); // setup disconnection callback
+ blue.onWritten(onWritten); // setup data written callback
+
+ //blue.service(svc); // setup custom service
+ blue.service(service); // setup custom service
+
+ blue.advertise("C:ng",100); // start advertising @ 100 msec interval
+ blink(ADVERTISE); // show that board is advertising
+ }
-int main(void)
-{
- Ticker ticker;
- ticker.attach(periodicCallback, 1); // blink LED every second
+//==============================================================================
+// Main Program
+//==============================================================================
+
+ int main(void)
+ {
+ blink(IDLE); // idle blinking - just started!
- BLE::Instance().init(bleInitComplete);
-}
+ Blob blue; // declare a blob (BLe OBject)
+ blue.init(onSetup,onError); // init BLE base layer, always do first
+
+ while (true) // Infinite loop waiting for BLE events
+ blue.sleep(); // low power waiting for BLE events
+ }
