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: mbed BLE_API 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 DigitalOut col0(P0_7, 0); 00022 DigitalOut alivenessLED(P0_13, 0); 00023 DigitalOut actuatedLED(P0_14, 1); 00024 00025 //Serial pc(p5, p4); 00026 00027 const static char DEVICE_NAME[] = "LED"; 00028 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; 00029 00030 uint8_t ts = 1; 00031 00032 LEDService *ledServicePtr; 00033 00034 Ticker ticker; 00035 bool onConnection = false; 00036 00037 void periodicCallback(void) 00038 { 00039 if(!onConnection){ 00040 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ 00041 // actuatedLED = 1; 00042 } 00043 } 00044 00045 /** 00046 * This callback allows the LEDService to receive updates to the ledState Characteristic. 00047 * 00048 * @param[in] params 00049 * Information about the characterisitc being updated. 00050 */ 00051 void onDataWrittenCallback(const GattWriteCallbackParams *params) 00052 { 00053 if ((params->handle == ledServicePtr->getValueHandle())) { 00054 actuatedLED = *(params->data); 00055 onConnection = true; 00056 alivenessLED = 1; 00057 } 00058 } 00059 //Выполняется при событии "onConnect" 00060 void connectionCallback(const Gap::ConnectionCallbackParams_t *params){ 00061 BLE::Instance().gap().stopAdvertising(); 00062 onConnection = true; 00063 } 00064 //Выполняется при событии "onDisconnect" 00065 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00066 { 00067 BLE::Instance().gap().startAdvertising(); 00068 onConnection = false; 00069 } 00070 00071 00072 /** 00073 * This function is called when the ble initialization process has failed 00074 */ 00075 void onBleInitError(BLE &ble, ble_error_t error) 00076 { 00077 /* Initialization error handling should go here */ 00078 } 00079 00080 /** 00081 * Callback triggered when the ble initialization process has finished 00082 */ 00083 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00084 { 00085 BLE& ble = params->ble; 00086 ble_error_t error = params->error; 00087 00088 if (error != BLE_ERROR_NONE) { 00089 /* In case of error, forward the error handling to onBleInitError */ 00090 onBleInitError(ble, error); 00091 return; 00092 } 00093 00094 /* Ensure that it is the default instance of BLE */ 00095 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00096 return; 00097 } 00098 00099 ble.gap().onDisconnection(disconnectionCallback); 00100 //Нужно дописать обработку события в случае Connect 00101 ble.gap().onConnection(connectionCallback); 00102 ble.gattServer().onDataWritten(onDataWrittenCallback); 00103 00104 bool initialValueForLEDCharacteristic = false; 00105 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); 00106 00107 /* setup advertising */ 00108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00110 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00111 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00112 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ 00113 ble.gap().startAdvertising(); 00114 } 00115 00116 00117 int main(void) 00118 { 00119 // pc.baud(115200); 00120 // pc.printf("Initialization starts... \n"); 00121 ticker.attach(periodicCallback, ts); /* Blink LED every second */ 00122 actuatedLED = 1; 00123 00124 BLE &ble = BLE::Instance(); 00125 ble.init(bleInitComplete); 00126 00127 /* SpinWait for initialization to complete. This is necessary because the 00128 * BLE object is used in the main loop below. */ 00129 //while (ble.hasInitialized() == false) { /* spin loop */ } 00130 // pc.printf("Initialization finished \n"); 00131 00132 while (true) { 00133 ble.waitForEvent(); 00134 } 00135 }
Generated on Tue Sep 6 2022 13:36:01 by
1.7.2