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 Amaldi_6_BLE_UART_IDB0XA1 by
Revision 8:60033323cbcb, committed 2017-04-25
- Comitter:
- waby38
- Date:
- Tue Apr 25 15:45:05 2017 +0000
- Parent:
- 7:8490fe113598
- Commit message:
- Back to original X_NUCLEO_IDB0XA1 lib
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDService.h Tue Apr 25 15:45:05 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 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.
+ */
+
+#ifndef __BLE_LED_SERVICE_H__
+#define __BLE_LED_SERVICE_H__
+
+class LEDService {
+public:
+ const static uint16_t LED_SERVICE_UUID = 0xA000;
+ const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
+
+ LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
+ ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
+ {
+ GattCharacteristic *charTable[] = {&ledState};
+ GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.addService(ledService);
+ }
+
+ GattAttribute::Handle_t getValueHandle() const {
+ return ledState.getValueHandle();
+ }
+
+private:
+ BLEDevice &ble;
+ ReadWriteGattCharacteristic<bool> ledState;
+};
+
+#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- a/X_NUCLEO_IDB0XA1.lib Tue Apr 25 15:26:50 2017 +0000 +++ b/X_NUCLEO_IDB0XA1.lib Tue Apr 25 15:45:05 2017 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#4606b082c3e6 +http://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#fa98703ece8e
--- a/main.cpp Tue Apr 25 15:26:50 2017 +0000
+++ b/main.cpp Tue Apr 25 15:45:05 2017 +0000
@@ -16,77 +16,44 @@
#include "mbed.h"
#include "ble/BLE.h"
-#include "ble/services/UARTService.h"
-#include "Serial.h"
-
-#define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN*10)
+#include "LEDService.h"
-const static char DEVICE_NAME[] = "UART";
-UARTService *uartServicePtr;
-static uint8_t uartBuff[UART_BUFFER];
-static uint8_t uartBuffPos=0;
+DigitalOut actuatedLED(LED1, 0);
+
+const static char DEVICE_NAME[] = "LED";
+static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
-void onBleError(ble_error_t error);
-
-DigitalOut led1(LED1);
-Serial uart1(USBTX,USBRX);
-Ticker ticker;
+LEDService *ledServicePtr;
-#define DEBUG uart1.printf
-
-/* Ticker */
-void periodicCallback(void)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
- led1 = !led1;
+ (void)params;
+ BLE::Instance().gap().startAdvertising(); // restart advertising
}
-/* UART */
-void uartRx(void)
-{
- if(uart1.readable()){
- uartBuff[uartBuffPos] = uart1.getc();
- if((uartBuff[uartBuffPos] == '\r') || (uartBuff[uartBuffPos] == '\n') || (uartBuffPos >= UART_BUFFER)) {
- uartBuff[uartBuffPos] = '\0';
- /* We are sending the whole string even if less than BLE_UART_SERVICE_MAX_DATA_LEN otherwise we need to wait */
- uartServicePtr->write(uartBuff, (uartBuffPos/UARTService::BLE_UART_SERVICE_MAX_DATA_LEN +1) * UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
- DEBUG("TX : %s\r\n", uartBuff);
- memset(uartBuff, 0, UART_BUFFER);
- uartBuffPos = 0;
- }
- else
- uartBuffPos++;
+/**
+ * This callback allows the LEDService to receive updates to the ledState Characteristic.
+ *
+ * @param[in] params
+ * Information about the characterisitc being updated.
+ */
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+ if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
+ actuatedLED = *(params->data);
}
}
-
-/* BLE */
-void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params) {
- DEBUG("BLE Client Connected!\n\r");
- DEBUG("Please type a string and press return\r\n");
-
- ticker.attach(periodicCallback, 1);
-}
-
-void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
- (void)params;
- DEBUG("BLE Client Disconnected!\r\n");
- ticker.detach();
- BLE::Instance().gap().startAdvertising(); // restart advertising
- led1=0;
-}
-
-void BleOnDataWrittenCallback(const GattWriteCallbackParams *params) {
- if (params->handle == uartServicePtr->getTXCharacteristicHandle()){
- DEBUG("RX: %s\r\n", params->data);
- }
-}
-
-void onBleError(ble_error_t error) {
- DEBUG("BLE Error: %d\r\n");
- /* Handle error now */
+/**
+ * This function is called when the ble initialization process has failled
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+ /* Initialization error handling should go here */
}
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
@@ -94,7 +61,7 @@
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
- onBleError(error);
+ onBleInitError(ble, error);
return;
}
@@ -103,22 +70,18 @@
return;
}
- ble.gap().onConnection(BleConnectionCallback);
- ble.gap().onDisconnection(BleDisconnectionCallback);
- ble.gattServer().onDataWritten(BleOnDataWrittenCallback);
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gattServer().onDataWritten(onDataWrittenCallback);
- DEBUG("BLE UARTService: ");
- /* Setup primary service. */
- UARTService uartService(ble);
- uartServicePtr = &uartService;
- DEBUG("Started\r\n");
+ bool initialValueForLEDCharacteristic = true;
+ ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
/* setup advertising */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.gap().setAdvertisingInterval(500); /* 500ms. */
+ ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ble.gap().startAdvertising();
while (true) {
@@ -128,11 +91,8 @@
int main(void)
{
- led1=0;
-
- uart1.baud(9600);
- uart1.attach(uartRx,Serial::RxIrq);
BLE &ble = BLE::Instance();
+
ble.init(bleInitComplete);
}
