mbed First Project Code

Dependencies:   BLE_API mbed nRF51822

Committer:
alanlan
Date:
Sun May 29 02:58:59 2016 +0000
Revision:
0:05616fd393ff
Initial Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alanlan 0:05616fd393ff 1 /* mbed Microcontroller Library
alanlan 0:05616fd393ff 2 * Copyright (c) 2006-2013 ARM Limited
alanlan 0:05616fd393ff 3 *
alanlan 0:05616fd393ff 4 * Licensed under the Apache License, Version 2.0 (the "License");
alanlan 0:05616fd393ff 5 * you may not use this file except in compliance with the License.
alanlan 0:05616fd393ff 6 * You may obtain a copy of the License at
alanlan 0:05616fd393ff 7 *
alanlan 0:05616fd393ff 8 * http://www.apache.org/licenses/LICENSE-2.0
alanlan 0:05616fd393ff 9 *
alanlan 0:05616fd393ff 10 * Unless required by applicable law or agreed to in writing, software
alanlan 0:05616fd393ff 11 * distributed under the License is distributed on an "AS IS" BASIS,
alanlan 0:05616fd393ff 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
alanlan 0:05616fd393ff 13 * See the License for the specific language governing permissions and
alanlan 0:05616fd393ff 14 * limitations under the License.
alanlan 0:05616fd393ff 15 */
alanlan 0:05616fd393ff 16
alanlan 0:05616fd393ff 17 #include "mbed.h"
alanlan 0:05616fd393ff 18 #include "ble/BLE.h"
alanlan 0:05616fd393ff 19
alanlan 0:05616fd393ff 20 #include "ble/services/UARTService.h"
alanlan 0:05616fd393ff 21
alanlan 0:05616fd393ff 22 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
alanlan 0:05616fd393ff 23 * it will have an impact on code-size and power consumption. */
alanlan 0:05616fd393ff 24
alanlan 0:05616fd393ff 25 #if NEED_CONSOLE_OUTPUT
alanlan 0:05616fd393ff 26 #define DEBUG(...) { printf(__VA_ARGS__); }
alanlan 0:05616fd393ff 27 #else
alanlan 0:05616fd393ff 28 #define DEBUG(...) /* nothing */
alanlan 0:05616fd393ff 29 #endif /* #if NEED_CONSOLE_OUTPUT */
alanlan 0:05616fd393ff 30
alanlan 0:05616fd393ff 31 BLEDevice ble;
alanlan 0:05616fd393ff 32 DigitalOut led1(LED1);
alanlan 0:05616fd393ff 33 AnalogIn bat(p20);
alanlan 0:05616fd393ff 34
alanlan 0:05616fd393ff 35 UARTService *uartServicePtr;
alanlan 0:05616fd393ff 36
alanlan 0:05616fd393ff 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
alanlan 0:05616fd393ff 38 {
alanlan 0:05616fd393ff 39 DEBUG("Disconnected!\n\r");
alanlan 0:05616fd393ff 40 DEBUG("Restarting the advertising process\n\r");
alanlan 0:05616fd393ff 41 ble.startAdvertising();
alanlan 0:05616fd393ff 42 }
alanlan 0:05616fd393ff 43
alanlan 0:05616fd393ff 44 void onDataWritten(const GattWriteCallbackParams *params)
alanlan 0:05616fd393ff 45 {
alanlan 0:05616fd393ff 46 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
alanlan 0:05616fd393ff 47 uint16_t bytesRead = params->len;
alanlan 0:05616fd393ff 48 DEBUG("received %u bytes\n\r", bytesRead);
alanlan 0:05616fd393ff 49 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
alanlan 0:05616fd393ff 50 }
alanlan 0:05616fd393ff 51 }
alanlan 0:05616fd393ff 52
alanlan 0:05616fd393ff 53 void periodicCallback(void)
alanlan 0:05616fd393ff 54 {
alanlan 0:05616fd393ff 55 led1 = !led1;
alanlan 0:05616fd393ff 56 }
alanlan 0:05616fd393ff 57
alanlan 0:05616fd393ff 58 int main(void)
alanlan 0:05616fd393ff 59 {
alanlan 0:05616fd393ff 60 led1 = 1;
alanlan 0:05616fd393ff 61 Ticker ticker;
alanlan 0:05616fd393ff 62 ticker.attach(periodicCallback, 1);
alanlan 0:05616fd393ff 63
alanlan 0:05616fd393ff 64 DEBUG("Initialising the nRF51822\n\r");
alanlan 0:05616fd393ff 65 ble.init();
alanlan 0:05616fd393ff 66 ble.onDisconnection(disconnectionCallback);
alanlan 0:05616fd393ff 67 ble.onDataWritten(onDataWritten);
alanlan 0:05616fd393ff 68
alanlan 0:05616fd393ff 69 /* setup advertising */
alanlan 0:05616fd393ff 70 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
alanlan 0:05616fd393ff 71 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
alanlan 0:05616fd393ff 72 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
alanlan 0:05616fd393ff 73 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
alanlan 0:05616fd393ff 74 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
alanlan 0:05616fd393ff 75 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
alanlan 0:05616fd393ff 76
alanlan 0:05616fd393ff 77 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
alanlan 0:05616fd393ff 78 ble.startAdvertising();
alanlan 0:05616fd393ff 79
alanlan 0:05616fd393ff 80 UARTService uartService(ble);
alanlan 0:05616fd393ff 81 uartServicePtr = &uartService;
alanlan 0:05616fd393ff 82
alanlan 0:05616fd393ff 83 if (bat.read() != 0)
alanlan 0:05616fd393ff 84 {
alanlan 0:05616fd393ff 85 printf("%f", bat.read());
alanlan 0:05616fd393ff 86 }
alanlan 0:05616fd393ff 87
alanlan 0:05616fd393ff 88 while (true) {
alanlan 0:05616fd393ff 89 ble.waitForEvent();
alanlan 0:05616fd393ff 90 }
alanlan 0:05616fd393ff 91 }