![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program to fill up the heap
Fork of BLE_BatteryLevel by
main.cpp@4:5b64235d1b85, 2014-06-12 (annotated)
- Committer:
- Rohit Grover
- Date:
- Thu Jun 12 13:56:06 2014 +0100
- Revision:
- 4:5b64235d1b85
- Parent:
- 3:288b2baffd14
- Child:
- 5:77ad8d8dc9c5
updating the batteryLevel example with new API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
4:5b64235d1b85 | 1 | /* mbed Microcontroller Library |
Rohit Grover |
4:5b64235d1b85 | 2 | * Copyright (c) 2006-2013 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" |
Rohit Grover |
4:5b64235d1b85 | 19 | |
Rohit Grover |
4:5b64235d1b85 | 20 | BLEDevice ble; |
Rohit Grover |
4:5b64235d1b85 | 21 | |
Rohit Grover |
4:5b64235d1b85 | 22 | DigitalOut led1(LED1); |
Rohit Grover |
4:5b64235d1b85 | 23 | |
Rohit Grover |
4:5b64235d1b85 | 24 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Rohit Grover |
4:5b64235d1b85 | 25 | * it will have an impact on code-size and power |
Rohit Grover |
4:5b64235d1b85 | 26 | * consumption. */ |
Rohit Grover |
4:5b64235d1b85 | 27 | |
Rohit Grover |
4:5b64235d1b85 | 28 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
4:5b64235d1b85 | 29 | Serial pc(USBTX, USBRX); |
Rohit Grover |
4:5b64235d1b85 | 30 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Rohit Grover |
4:5b64235d1b85 | 31 | #else |
Rohit Grover |
4:5b64235d1b85 | 32 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
4:5b64235d1b85 | 33 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Rohit Grover |
4:5b64235d1b85 | 34 | |
Rohit Grover |
4:5b64235d1b85 | 35 | /* Battery Level Service */ |
Rohit Grover |
4:5b64235d1b85 | 36 | uint8_t batt = 72; /* Battery level */ |
Rohit Grover |
4:5b64235d1b85 | 37 | GattCharacteristic battLevel (GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batt, sizeof(batt), sizeof(batt), |
Rohit Grover |
4:5b64235d1b85 | 38 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
Rohit Grover |
4:5b64235d1b85 | 39 | GattCharacteristic *battChars[] = { |
Rohit Grover |
4:5b64235d1b85 | 40 | &battLevel, |
Rohit Grover |
4:5b64235d1b85 | 41 | }; |
Rohit Grover |
4:5b64235d1b85 | 42 | GattService battService (GattService::UUID_BATTERY_SERVICE, battChars, sizeof(battChars) / sizeof(GattCharacteristic *)); |
Rohit Grover |
4:5b64235d1b85 | 43 | |
Rohit Grover |
4:5b64235d1b85 | 44 | |
Rohit Grover |
4:5b64235d1b85 | 45 | void disconnectionCallback(void) |
Rohit Grover |
4:5b64235d1b85 | 46 | { |
Rohit Grover |
4:5b64235d1b85 | 47 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
4:5b64235d1b85 | 48 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
4:5b64235d1b85 | 49 | ble.startAdvertising(); |
Rohit Grover |
4:5b64235d1b85 | 50 | } |
Rohit Grover |
4:5b64235d1b85 | 51 | |
Rohit Grover |
4:5b64235d1b85 | 52 | /** |
Rohit Grover |
4:5b64235d1b85 | 53 | * Runs once a second in interrupt context triggered by the 'ticker'; updates |
Rohit Grover |
4:5b64235d1b85 | 54 | * battery level if there is a connection. |
Rohit Grover |
4:5b64235d1b85 | 55 | */ |
Rohit Grover |
4:5b64235d1b85 | 56 | void periodicCallback(void) |
Rohit Grover |
4:5b64235d1b85 | 57 | { |
Rohit Grover |
4:5b64235d1b85 | 58 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Rohit Grover |
4:5b64235d1b85 | 59 | |
Rohit Grover |
4:5b64235d1b85 | 60 | if (ble.getGapState().connected) { |
Rohit Grover |
4:5b64235d1b85 | 61 | /* Update battery level */ |
Rohit Grover |
4:5b64235d1b85 | 62 | batt++; |
Rohit Grover |
4:5b64235d1b85 | 63 | if (batt > 100) { |
Rohit Grover |
4:5b64235d1b85 | 64 | batt = 72; |
Rohit Grover |
4:5b64235d1b85 | 65 | } |
Rohit Grover |
4:5b64235d1b85 | 66 | ble.updateCharacteristicValue(battLevel.getHandle(), (uint8_t *)&batt, sizeof(batt)); |
Rohit Grover |
4:5b64235d1b85 | 67 | } |
Rohit Grover |
4:5b64235d1b85 | 68 | } |
Rohit Grover |
4:5b64235d1b85 | 69 | |
Rohit Grover |
4:5b64235d1b85 | 70 | int main(void) |
Rohit Grover |
4:5b64235d1b85 | 71 | { |
Rohit Grover |
4:5b64235d1b85 | 72 | led1 = 1; |
Rohit Grover |
4:5b64235d1b85 | 73 | Ticker ticker; |
Rohit Grover |
4:5b64235d1b85 | 74 | ticker.attach(periodicCallback, 1); |
Rohit Grover |
4:5b64235d1b85 | 75 | |
Rohit Grover |
4:5b64235d1b85 | 76 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
4:5b64235d1b85 | 77 | ble.init(); |
Rohit Grover |
4:5b64235d1b85 | 78 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
4:5b64235d1b85 | 79 | |
Rohit Grover |
4:5b64235d1b85 | 80 | /* setup advertising */ |
Rohit Grover |
4:5b64235d1b85 | 81 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
4:5b64235d1b85 | 82 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
4:5b64235d1b85 | 83 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
4:5b64235d1b85 | 84 | ble.startAdvertising(); |
Rohit Grover |
4:5b64235d1b85 | 85 | |
Rohit Grover |
4:5b64235d1b85 | 86 | ble.addService(battService); |
Rohit Grover |
4:5b64235d1b85 | 87 | |
Rohit Grover |
4:5b64235d1b85 | 88 | while (true) { |
Rohit Grover |
4:5b64235d1b85 | 89 | ble.waitForEvent(); |
Rohit Grover |
4:5b64235d1b85 | 90 | } |
Rohit Grover |
4:5b64235d1b85 | 91 | } |