LED and Button for BLE workshop (TinyBLE)

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
rgrover1
Date:
Tue Mar 24 08:10:57 2015 +0000
Revision:
1:a84c3717aa5e
Parent:
0:ed5c53845de7
Child:
4:16e8c289c99e
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 0:ed5c53845de7 18 #include "BLEDevice.h"
rgrover1 0:ed5c53845de7 19 #include "LEDService.h"
rgrover1 0:ed5c53845de7 20
rgrover1 0:ed5c53845de7 21 BLEDevice 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 0:ed5c53845de7 30 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:ed5c53845de7 31 {
rgrover1 0:ed5c53845de7 32 ble.startAdvertising();
rgrover1 0:ed5c53845de7 33 }
rgrover1 0:ed5c53845de7 34
rgrover1 0:ed5c53845de7 35 void periodicCallback(void)
rgrover1 0:ed5c53845de7 36 {
rgrover1 0:ed5c53845de7 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 0:ed5c53845de7 46 void onDataWrittenCallback(const GattCharacteristicWriteCBParams *params) {
rgrover1 0:ed5c53845de7 47 if ((params->charHandle == 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 0:ed5c53845de7 61 ble.onDisconnection(disconnectionCallback);
rgrover1 0:ed5c53845de7 62 ble.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 0:ed5c53845de7 69 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:ed5c53845de7 70 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 0:ed5c53845de7 71 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 0:ed5c53845de7 72 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 1:a84c3717aa5e 73 ble.setAdvertisingInterval(1000); /* 1000ms. */
rgrover1 0:ed5c53845de7 74 ble.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 }