
Service and app to control Relay over BLE.
Dependencies: BLE_API mbed nRF51822
main.cpp@0:ab978b3b579a, 2015-03-23 (annotated)
- Committer:
- abhishekkumardwivedi
- Date:
- Mon Mar 23 19:47:19 2015 +0000
- Revision:
- 0:ab978b3b579a
Service and app to control Relay remotely over BLE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abhishekkumardwivedi | 0:ab978b3b579a | 1 | /* mbed Microcontroller Library |
abhishekkumardwivedi | 0:ab978b3b579a | 2 | * Copyright (c) 2006-2013 ARM Limited |
abhishekkumardwivedi | 0:ab978b3b579a | 3 | * |
abhishekkumardwivedi | 0:ab978b3b579a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
abhishekkumardwivedi | 0:ab978b3b579a | 5 | * you may not use this file except in compliance with the License. |
abhishekkumardwivedi | 0:ab978b3b579a | 6 | * You may obtain a copy of the License at |
abhishekkumardwivedi | 0:ab978b3b579a | 7 | * |
abhishekkumardwivedi | 0:ab978b3b579a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
abhishekkumardwivedi | 0:ab978b3b579a | 9 | * |
abhishekkumardwivedi | 0:ab978b3b579a | 10 | * Unless required by applicable law or agreed to in writing, software |
abhishekkumardwivedi | 0:ab978b3b579a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
abhishekkumardwivedi | 0:ab978b3b579a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
abhishekkumardwivedi | 0:ab978b3b579a | 13 | * See the License for the specific language governing permissions and |
abhishekkumardwivedi | 0:ab978b3b579a | 14 | * limitations under the License. |
abhishekkumardwivedi | 0:ab978b3b579a | 15 | */ |
abhishekkumardwivedi | 0:ab978b3b579a | 16 | |
abhishekkumardwivedi | 0:ab978b3b579a | 17 | #include "mbed.h" |
abhishekkumardwivedi | 0:ab978b3b579a | 18 | #include "BLEDevice.h" |
abhishekkumardwivedi | 0:ab978b3b579a | 19 | #include "RelayService.h" |
abhishekkumardwivedi | 0:ab978b3b579a | 20 | |
abhishekkumardwivedi | 0:ab978b3b579a | 21 | BLEDevice ble; |
abhishekkumardwivedi | 0:ab978b3b579a | 22 | |
abhishekkumardwivedi | 0:ab978b3b579a | 23 | DigitalOut relay(p5); |
abhishekkumardwivedi | 0:ab978b3b579a | 24 | |
abhishekkumardwivedi | 0:ab978b3b579a | 25 | const static char DEVICE_NAME[] = "RELAY"; |
abhishekkumardwivedi | 0:ab978b3b579a | 26 | static const uint16_t uuid16_list[] = {RelayService::RELAY_SERVICE_UUID}; |
abhishekkumardwivedi | 0:ab978b3b579a | 27 | |
abhishekkumardwivedi | 0:ab978b3b579a | 28 | RelayService *relayServicePtr; |
abhishekkumardwivedi | 0:ab978b3b579a | 29 | |
abhishekkumardwivedi | 0:ab978b3b579a | 30 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
abhishekkumardwivedi | 0:ab978b3b579a | 31 | { |
abhishekkumardwivedi | 0:ab978b3b579a | 32 | ble.startAdvertising(); |
abhishekkumardwivedi | 0:ab978b3b579a | 33 | } |
abhishekkumardwivedi | 0:ab978b3b579a | 34 | |
abhishekkumardwivedi | 0:ab978b3b579a | 35 | void onDataWrittenCallback(const GattCharacteristicWriteCBParams *params) |
abhishekkumardwivedi | 0:ab978b3b579a | 36 | { |
abhishekkumardwivedi | 0:ab978b3b579a | 37 | if ((params->charHandle == relayServicePtr->getValueHandle()) && (params->len == 1)) { |
abhishekkumardwivedi | 0:ab978b3b579a | 38 | relay = *(params->data); |
abhishekkumardwivedi | 0:ab978b3b579a | 39 | } |
abhishekkumardwivedi | 0:ab978b3b579a | 40 | } |
abhishekkumardwivedi | 0:ab978b3b579a | 41 | |
abhishekkumardwivedi | 0:ab978b3b579a | 42 | int main(void) |
abhishekkumardwivedi | 0:ab978b3b579a | 43 | { |
abhishekkumardwivedi | 0:ab978b3b579a | 44 | relay = 0; |
abhishekkumardwivedi | 0:ab978b3b579a | 45 | |
abhishekkumardwivedi | 0:ab978b3b579a | 46 | ble.init(); |
abhishekkumardwivedi | 0:ab978b3b579a | 47 | ble.onDisconnection(disconnectionCallback); |
abhishekkumardwivedi | 0:ab978b3b579a | 48 | ble.onDataWritten(onDataWrittenCallback); |
abhishekkumardwivedi | 0:ab978b3b579a | 49 | |
abhishekkumardwivedi | 0:ab978b3b579a | 50 | bool initialValueForRelayCharacteristic = false; |
abhishekkumardwivedi | 0:ab978b3b579a | 51 | RelayService relayService(ble, initialValueForRelayCharacteristic); |
abhishekkumardwivedi | 0:ab978b3b579a | 52 | relayServicePtr = &relayService; |
abhishekkumardwivedi | 0:ab978b3b579a | 53 | |
abhishekkumardwivedi | 0:ab978b3b579a | 54 | /* setup advertising */ |
abhishekkumardwivedi | 0:ab978b3b579a | 55 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
abhishekkumardwivedi | 0:ab978b3b579a | 56 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
abhishekkumardwivedi | 0:ab978b3b579a | 57 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
abhishekkumardwivedi | 0:ab978b3b579a | 58 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
abhishekkumardwivedi | 0:ab978b3b579a | 59 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); |
abhishekkumardwivedi | 0:ab978b3b579a | 60 | ble.startAdvertising(); |
abhishekkumardwivedi | 0:ab978b3b579a | 61 | |
abhishekkumardwivedi | 0:ab978b3b579a | 62 | while (true) { |
abhishekkumardwivedi | 0:ab978b3b579a | 63 | ble.waitForEvent(); |
abhishekkumardwivedi | 0:ab978b3b579a | 64 | } |
abhishekkumardwivedi | 0:ab978b3b579a | 65 | } |