LED and Button for BLE workshop (TinyBLE)

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
rgrover1
Date:
Tue Sep 29 11:50:28 2015 +0000
Revision:
9:35a5a5796286
Parent:
7:c9271599ec5f
Child:
10:af76616e4d75
updating underlying libraries.

Who changed what in which revision?

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