![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program to fill up the heap
Fork of BLE_BatteryLevel by
main.cpp@14:4024aa3f72b0, 2014-12-15 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Dec 15 08:10:46 2014 +0000
- Revision:
- 14:4024aa3f72b0
- Parent:
- 8:45beed07b093
- Child:
- 15:60e095fe4b45
minor improvement to the demo;; also updated underlying libraries to latest revision.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
4:5b64235d1b85 | 1 | /* mbed Microcontroller Library |
rgrover1 | 14:4024aa3f72b0 | 2 | * Copyright (c) 2006-2014 ARM Limited |
Rohit Grover |
4:5b64235d1b85 | 3 | * |
Rohit Grover |
4:5b64235d1b85 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
4:5b64235d1b85 | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
4:5b64235d1b85 | 6 | * You may obtain a copy of the License at |
Rohit Grover |
4:5b64235d1b85 | 7 | * |
Rohit Grover |
4:5b64235d1b85 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
4:5b64235d1b85 | 9 | * |
Rohit Grover |
4:5b64235d1b85 | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
4:5b64235d1b85 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
4:5b64235d1b85 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
4:5b64235d1b85 | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
4:5b64235d1b85 | 14 | * limitations under the License. |
Rohit Grover |
4:5b64235d1b85 | 15 | */ |
Rohit Grover |
4:5b64235d1b85 | 16 | |
Rohit Grover |
4:5b64235d1b85 | 17 | #include "mbed.h" |
Rohit Grover |
4:5b64235d1b85 | 18 | #include "BLEDevice.h" |
rgrover1 | 8:45beed07b093 | 19 | #include "BatteryService.h" |
Rohit Grover |
4:5b64235d1b85 | 20 | |
Rohit Grover |
4:5b64235d1b85 | 21 | BLEDevice ble; |
Rohit Grover |
4:5b64235d1b85 | 22 | |
rgrover1 | 14:4024aa3f72b0 | 23 | DigitalOut led1(LED1, 1); |
rgrover1 | 14:4024aa3f72b0 | 24 | Ticker t; |
Rohit Grover |
4:5b64235d1b85 | 25 | |
rgrover1 | 8:45beed07b093 | 26 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
Rohit Grover |
4:5b64235d1b85 | 27 | { |
rgrover1 | 14:4024aa3f72b0 | 28 | printf("Disconnected handle %u!\n\r", handle); |
rgrover1 | 14:4024aa3f72b0 | 29 | printf("Restarting the advertising process\n\r"); |
Rohit Grover |
4:5b64235d1b85 | 30 | ble.startAdvertising(); |
Rohit Grover |
4:5b64235d1b85 | 31 | } |
Rohit Grover |
4:5b64235d1b85 | 32 | |
rgrover1 | 14:4024aa3f72b0 | 33 | void blink(void) |
Rohit Grover |
4:5b64235d1b85 | 34 | { |
rgrover1 | 14:4024aa3f72b0 | 35 | led1 = !led1; |
Rohit Grover |
4:5b64235d1b85 | 36 | } |
Rohit Grover |
4:5b64235d1b85 | 37 | |
Rohit Grover |
4:5b64235d1b85 | 38 | int main(void) |
Rohit Grover |
4:5b64235d1b85 | 39 | { |
rgrover1 | 14:4024aa3f72b0 | 40 | uint8_t batteryLevel = 50; |
rgrover1 | 14:4024aa3f72b0 | 41 | t.attach(blink, 1.0f); |
Rohit Grover |
4:5b64235d1b85 | 42 | |
rgrover1 | 14:4024aa3f72b0 | 43 | printf("Initialising the nRF51822\n\r"); |
rgrover1 | 14:4024aa3f72b0 | 44 | |
Rohit Grover |
4:5b64235d1b85 | 45 | ble.init(); |
Rohit Grover |
4:5b64235d1b85 | 46 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
4:5b64235d1b85 | 47 | |
Rohit Grover |
4:5b64235d1b85 | 48 | /* setup advertising */ |
Rohit Grover |
4:5b64235d1b85 | 49 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
4:5b64235d1b85 | 50 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 14:4024aa3f72b0 | 51 | ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); /* 1000ms; in multiples of 0.625ms. */ |
Rohit Grover |
4:5b64235d1b85 | 52 | ble.startAdvertising(); |
Rohit Grover |
4:5b64235d1b85 | 53 | |
rgrover1 | 14:4024aa3f72b0 | 54 | BatteryService batteryService(ble, batteryLevel); |
Rohit Grover |
4:5b64235d1b85 | 55 | |
Rohit Grover |
4:5b64235d1b85 | 56 | while (true) { |
rgrover1 | 14:4024aa3f72b0 | 57 | ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) |
rgrover1 | 14:4024aa3f72b0 | 58 | |
rgrover1 | 14:4024aa3f72b0 | 59 | // the magic battery processing |
rgrover1 | 14:4024aa3f72b0 | 60 | batteryLevel++; |
rgrover1 | 14:4024aa3f72b0 | 61 | if (batteryLevel > 100) { |
rgrover1 | 14:4024aa3f72b0 | 62 | batteryLevel = 20; |
rgrover1 | 14:4024aa3f72b0 | 63 | } |
rgrover1 | 14:4024aa3f72b0 | 64 | |
rgrover1 | 14:4024aa3f72b0 | 65 | batteryService.updateBatteryLevel(batteryLevel); |
Rohit Grover |
4:5b64235d1b85 | 66 | } |
Rohit Grover |
4:5b64235d1b85 | 67 | } |