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 X_NUCLEO_IDB0XA1
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2014 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.h" 00019 #include "BatteryService.h" 00020 00021 DigitalOut led1(LED1, 1); 00022 Ticker t; 00023 BatteryService *batteryService = NULL; 00024 uint8_t batteryLevel = 50; 00025 00026 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams) 00027 { 00028 printf("Disconnected handle %u!\n\r", disconnectionParams->handle); 00029 printf("Restarting the advertising process\n\r"); 00030 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising 00031 } 00032 00033 void blink(void) 00034 { 00035 led1 = !led1; 00036 } 00037 00038 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00039 { 00040 BLE &ble = params->ble; 00041 ble_error_t error = params->error; 00042 Gap& gap = ble.gap(); 00043 00044 if (error != BLE_ERROR_NONE) { 00045 return; 00046 } 00047 00048 gap.onDisconnection(disconnectionCallback); 00049 00050 batteryService = new BatteryService(ble, batteryLevel); 00051 00052 /* setup advertising */ 00053 gap.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00054 gap.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00055 gap.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ 00056 gap.startAdvertising(); 00057 } 00058 00059 int main(void) 00060 { 00061 t.attach(blink, 1.0f); 00062 00063 printf("Initialising the nRF51822\n\r"); 00064 00065 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00066 ble.init(bleInitComplete); 00067 00068 /* SpinWait for initialization to complete. This is necessary because the 00069 * BLE object is used in the main loop below. */ 00070 while (ble.hasInitialized() == false) { /* spin loop */ } 00071 00072 while (true) { 00073 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) 00074 00075 // the magic battery processing 00076 batteryLevel++; 00077 if (batteryLevel > 100) { 00078 batteryLevel = 20; 00079 } 00080 00081 batteryService->updateBatteryLevel(batteryLevel); 00082 } 00083 }
Generated on Sun Jul 17 2022 15:57:55 by
1.7.2