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 X_NUCLEO_IDB0XA1 mbed
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 00023 const static char DEVICE_NAME[] = "BATTERY"; 00024 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; 00025 00026 static volatile bool triggerSensorPolling = false; 00027 00028 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00029 { 00030 (void)params; 00031 BLE::Instance().gap().startAdvertising(); // restart advertising 00032 } 00033 00034 void blink(void) 00035 { 00036 led1 = !led1; 00037 triggerSensorPolling = true; 00038 } 00039 00040 void onBleInitError(BLE &ble, ble_error_t error) 00041 { 00042 (void)ble; 00043 (void)error; 00044 /* Initialization error handling should go here */ 00045 } 00046 00047 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00048 { 00049 BLE& ble = params->ble; 00050 ble_error_t error = params->error; 00051 00052 if (error != BLE_ERROR_NONE) { 00053 onBleInitError(ble, error); 00054 return; 00055 } 00056 00057 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00058 return; 00059 } 00060 00061 ble.gap().onDisconnection(disconnectionCallback); 00062 00063 uint8_t batteryLevel = 50; 00064 BatteryService batteryService(ble, batteryLevel); 00065 00066 /* setup advertising */ 00067 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00068 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); 00069 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); 00070 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00071 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ 00072 ble.startAdvertising(); 00073 00074 while (true) { 00075 // check for trigger from periodicCallback() 00076 if (triggerSensorPolling && ble.getGapState().connected) { 00077 triggerSensorPolling = false; 00078 00079 // the magic battery processing 00080 batteryLevel++; 00081 if (batteryLevel > 100) { 00082 batteryLevel = 20; 00083 } 00084 00085 batteryService.updateBatteryLevel(batteryLevel); 00086 } else { 00087 ble.waitForEvent(); // low power wait for event 00088 } 00089 } 00090 } 00091 00092 int main(void) 00093 { 00094 Ticker t; 00095 t.attach(blink, 1.0f); 00096 00097 BLE::Instance().init(bleInitComplete); 00098 } 00099 00100
Generated on Thu Jul 14 2022 08:25:17 by
1.7.2