Dragos STOICA / Mbed 2 deprecated BLE_BatteryLevel

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_BatteryLevel by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 BLE  ble;
00022 
00023 DigitalOut led1(LED1, 1);
00024 Ticker t;
00025 
00026 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00027 {
00028     printf("Disconnected handle %u!\n\r", handle);
00029     printf("Restarting the advertising process\n\r");
00030     ble.startAdvertising();
00031 }
00032 
00033 void blink(void)
00034 {
00035     led1 = !led1;
00036 }
00037 
00038 int main(void)
00039 {
00040     uint8_t batteryLevel = 50;
00041     t.attach(blink, 1.0f);
00042 
00043     printf("Initialising the nRF51822\n\r");
00044 
00045     ble.init();
00046     ble.onDisconnection(disconnectionCallback);
00047 
00048     BatteryService batteryService(ble, batteryLevel);
00049 
00050     /* setup advertising */
00051     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00052     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00053     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
00054     ble.startAdvertising();
00055 
00056     while (true) {
00057         ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
00058 
00059         // the magic battery processing
00060         batteryLevel++;
00061         if (batteryLevel > 100) {
00062             batteryLevel = 20;
00063         }
00064 
00065         batteryService.updateBatteryLevel(batteryLevel);
00066     }
00067 }