asdf

Dependencies:   BLE_API mbed nRF51822

Committer:
amithy
Date:
Fri Nov 10 02:28:21 2017 +0000
Revision:
0:503f6a4871e6
asdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
amithy 0:503f6a4871e6 1 /* mbed Microcontroller Library
amithy 0:503f6a4871e6 2 * Copyright (c) 2006-2013 ARM Limited
amithy 0:503f6a4871e6 3 *
amithy 0:503f6a4871e6 4 * Licensed under the Apache License, Version 2.0 (the "License");
amithy 0:503f6a4871e6 5 * you may not use this file except in compliance with the License.
amithy 0:503f6a4871e6 6 * You may obtain a copy of the License at
amithy 0:503f6a4871e6 7 *
amithy 0:503f6a4871e6 8 * http://www.apache.org/licenses/LICENSE-2.0
amithy 0:503f6a4871e6 9 *
amithy 0:503f6a4871e6 10 * Unless required by applicable law or agreed to in writing, software
amithy 0:503f6a4871e6 11 * distributed under the License is distributed on an "AS IS" BASIS,
amithy 0:503f6a4871e6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
amithy 0:503f6a4871e6 13 * See the License for the specific language governing permissions and
amithy 0:503f6a4871e6 14 * limitations under the License.
amithy 0:503f6a4871e6 15 */
amithy 0:503f6a4871e6 16
amithy 0:503f6a4871e6 17 #include "mbed.h"
amithy 0:503f6a4871e6 18 #include "BLE.h"
amithy 0:503f6a4871e6 19 #include "LEDService.h"
amithy 0:503f6a4871e6 20
amithy 0:503f6a4871e6 21 BLE ble;
amithy 0:503f6a4871e6 22 DigitalOut alivenessLED(LED1);
amithy 0:503f6a4871e6 23 DigitalOut actuatedLED(LED2);
amithy 0:503f6a4871e6 24
amithy 0:503f6a4871e6 25 const static char DEVICE_NAME[] = "LED";
amithy 0:503f6a4871e6 26 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
amithy 0:503f6a4871e6 27
amithy 0:503f6a4871e6 28 LEDService *ledServicePtr;
amithy 0:503f6a4871e6 29
amithy 0:503f6a4871e6 30 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
amithy 0:503f6a4871e6 31 {
amithy 0:503f6a4871e6 32 ble.gap().startAdvertising();
amithy 0:503f6a4871e6 33 }
amithy 0:503f6a4871e6 34
amithy 0:503f6a4871e6 35 void periodicCallback(void)
amithy 0:503f6a4871e6 36 {
amithy 0:503f6a4871e6 37 //alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
amithy 0:503f6a4871e6 38 }
amithy 0:503f6a4871e6 39
amithy 0:503f6a4871e6 40 /**
amithy 0:503f6a4871e6 41 * This callback allows the LEDService to receive updates to the ledState Characteristic.
amithy 0:503f6a4871e6 42 *
amithy 0:503f6a4871e6 43 * @param[in] params
amithy 0:503f6a4871e6 44 * Information about the characterisitc being updated.
amithy 0:503f6a4871e6 45 */
amithy 0:503f6a4871e6 46 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
amithy 0:503f6a4871e6 47 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
amithy 0:503f6a4871e6 48 actuatedLED = *(params->data);
amithy 0:503f6a4871e6 49 }
amithy 0:503f6a4871e6 50 }
amithy 0:503f6a4871e6 51
amithy 0:503f6a4871e6 52 int main(void)
amithy 0:503f6a4871e6 53 {
amithy 0:503f6a4871e6 54 alivenessLED = 0;
amithy 0:503f6a4871e6 55 actuatedLED = 0;
amithy 0:503f6a4871e6 56
amithy 0:503f6a4871e6 57 Ticker ticker;
amithy 0:503f6a4871e6 58 ticker.attach(periodicCallback, 1);
amithy 0:503f6a4871e6 59
amithy 0:503f6a4871e6 60 ble.init();
amithy 0:503f6a4871e6 61 ble.gap().onDisconnection(disconnectionCallback);
amithy 0:503f6a4871e6 62 ble.gattServer().onDataWritten(onDataWrittenCallback);
amithy 0:503f6a4871e6 63
amithy 0:503f6a4871e6 64 bool initialValueForLEDCharacteristic = false;
amithy 0:503f6a4871e6 65 LEDService ledService(ble, initialValueForLEDCharacteristic);
amithy 0:503f6a4871e6 66 ledServicePtr = &ledService;
amithy 0:503f6a4871e6 67
amithy 0:503f6a4871e6 68 /* setup advertising */
amithy 0:503f6a4871e6 69 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
amithy 0:503f6a4871e6 70 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
amithy 0:503f6a4871e6 71 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
amithy 0:503f6a4871e6 72 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
amithy 0:503f6a4871e6 73 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
amithy 0:503f6a4871e6 74 ble.gap().startAdvertising();
amithy 0:503f6a4871e6 75
amithy 0:503f6a4871e6 76 while (true) {
amithy 0:503f6a4871e6 77 ble.waitForEvent();
amithy 0:503f6a4871e6 78 }
amithy 0:503f6a4871e6 79 }