Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 #include "LEDService.h" 00020 00021 00022 DigitalOut alivenessLED(LED1, 0); 00023 DigitalOut actuatedLED(LED2, 0); 00024 Serial pc(USBTX, USBRX); 00025 00026 const static char DEVICE_NAME[] = "LEDWTFFTW"; 00027 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; 00028 const static char AdvData[]="Hello World!"; 00029 LEDService *ledServicePtr; 00030 00031 Ticker ticker; 00032 00033 void timeoutCallback(const Gap::TimeoutSource_t source){ 00034 BLE::Instance().gap().stopAdvertising(); 00035 pc.printf("got a timeout\r\n"); 00036 BLE::Instance().gap().startAdvertising(); 00037 } 00038 00039 00040 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00041 { 00042 pc.printf("disconnected\r\n"); 00043 BLE::Instance().gap().startAdvertising(); 00044 uint8_t deviceName[30] = {0}; 00045 unsigned lengthP = 0; 00046 BLE::Instance().gap().getDeviceName(deviceName, &lengthP); 00047 pc.printf("new device name is %s", deviceName); 00048 00049 } 00050 00051 void connectionCallback(const Gap::ConnectionCallbackParams_t *params){ 00052 00053 pc.printf("connected\r\n"); 00054 00055 } 00056 void periodicCallback(void) 00057 { 00058 // pc.printf("got here\r\n"); 00059 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ 00060 } 00061 00062 /** 00063 * This callback allows the LEDService to receive updates to the ledState Characteristic. 00064 * 00065 * @param[in] params 00066 * Information about the characterisitc being updated. 00067 */ 00068 void onDataWrittenCallback(const GattWriteCallbackParams *params) { 00069 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { 00070 actuatedLED = *(params->data); 00071 } 00072 } 00073 00074 00075 /** 00076 * This function is called when the ble initialization process has failed 00077 */ 00078 void onBleInitError(BLE &ble, ble_error_t error) 00079 { 00080 /* Initialization error handling should go here */ 00081 } 00082 00083 /** 00084 * Callback triggered when the ble initialization process has finished 00085 */ 00086 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00087 { 00088 BLE& ble = params->ble; 00089 ble_error_t error = params->error; 00090 00091 if (error != BLE_ERROR_NONE) { 00092 /* In case of error, forward the error handling to onBleInitError */ 00093 onBleInitError(ble, error); 00094 return; 00095 } 00096 00097 /* Ensure that it is the default instance of BLE */ 00098 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00099 return; 00100 } 00101 00102 ble.gap().onDisconnection(disconnectionCallback); 00103 ble.gattServer().onDataWritten(onDataWrittenCallback); 00104 ble.gap().onTimeout(timeoutCallback); 00105 00106 bool initialValueForLEDCharacteristic = false; 00107 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); 00108 00109 /* setup advertising */ 00110 ble.gap().setDeviceName((uint8_t*)DEVICE_NAME); 00111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00112 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00114 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)AdvData, sizeof(AdvData)); 00115 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00116 ble.gap().setAdvertisingInterval(60000); /* you shall not pass */ 00117 00118 ble.gap().setAdvertisingTimeout(900); 00119 ble.gap().startAdvertising(); 00120 } 00121 00122 int main(void) 00123 { 00124 ticker.attach(periodicCallback, 1); /* Blink LED every second */ 00125 00126 BLE &ble = BLE::Instance(); 00127 ble.init(bleInitComplete); 00128 00129 pc .baud(19200); 00130 /* SpinWait for initialization to complete. This is necessary because the 00131 * BLE object is used in the main loop below. */ 00132 while (ble.hasInitialized() == false) { /* spin loop */ } 00133 00134 while (true) { 00135 // pc.printf("got here\r\n"); 00136 ble.waitForEvent(); 00137 } 00138 }
Generated on Fri Jul 29 2022 00:59:23 by
1.7.2