Modified sample program of BLE_BatteryLevel. The program add analog input pin setting and batteryLevel data is changed by analog input.
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_BatteryLevel by
main.cpp@21:d3784939bbc0, 2017-06-28 (annotated)
- Committer:
- Jun_adi
- Date:
- Wed Jun 28 13:03:00 2017 +0000
- Revision:
- 21:d3784939bbc0
- Parent:
- 20:e7bc34a4a3db
Update typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
4:5b64235d1b85 | 1 | /* mbed Microcontroller Library |
rgrover1 | 12: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" |
rgrover1 | 16:5cdd04cf1ed4 | 18 | #include "BLE.h" |
rgrover1 | 8:45beed07b093 | 19 | #include "BatteryService.h" |
Rohit Grover |
4:5b64235d1b85 | 20 | |
rgrover1 | 12:4024aa3f72b0 | 21 | DigitalOut led1(LED1, 1); |
rgrover1 | 12:4024aa3f72b0 | 22 | Ticker t; |
vcoubard | 17:5afb0e5a48fc | 23 | BatteryService *batteryService = NULL; |
Jun_adi | 20:e7bc34a4a3db | 24 | |
Jun_adi | 20:e7bc34a4a3db | 25 | //uint8_t batteryLevel = 50; |
Jun_adi | 21:d3784939bbc0 | 26 | uint8_t batteryLevel = 0; // Change initial value of batteryLevel |
Jun_adi | 20:e7bc34a4a3db | 27 | |
Jun_adi | 20:e7bc34a4a3db | 28 | //Pin setting for Analog input |
Jun_adi | 20:e7bc34a4a3db | 29 | AnalogIn analogin(P0_4); |
Rohit Grover |
4:5b64235d1b85 | 30 | |
vcoubard | 17:5afb0e5a48fc | 31 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams) |
Rohit Grover |
4:5b64235d1b85 | 32 | { |
vcoubard | 17:5afb0e5a48fc | 33 | printf("Disconnected handle %u!\n\r", disconnectionParams->handle); |
rgrover1 | 12:4024aa3f72b0 | 34 | printf("Restarting the advertising process\n\r"); |
vcoubard | 17:5afb0e5a48fc | 35 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
Rohit Grover |
4:5b64235d1b85 | 36 | } |
Rohit Grover |
4:5b64235d1b85 | 37 | |
rgrover1 | 12:4024aa3f72b0 | 38 | void blink(void) |
Rohit Grover |
4:5b64235d1b85 | 39 | { |
rgrover1 | 12:4024aa3f72b0 | 40 | led1 = !led1; |
Rohit Grover |
4:5b64235d1b85 | 41 | } |
Rohit Grover |
4:5b64235d1b85 | 42 | |
vcoubard | 17:5afb0e5a48fc | 43 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
vcoubard | 17:5afb0e5a48fc | 44 | { |
vcoubard | 17:5afb0e5a48fc | 45 | BLE &ble = params->ble; |
vcoubard | 17:5afb0e5a48fc | 46 | ble_error_t error = params->error; |
vcoubard | 17:5afb0e5a48fc | 47 | Gap& gap = ble.gap(); |
vcoubard | 17:5afb0e5a48fc | 48 | |
vcoubard | 17:5afb0e5a48fc | 49 | if (error != BLE_ERROR_NONE) { |
vcoubard | 17:5afb0e5a48fc | 50 | return; |
vcoubard | 17:5afb0e5a48fc | 51 | } |
vcoubard | 17:5afb0e5a48fc | 52 | |
vcoubard | 17:5afb0e5a48fc | 53 | gap.onDisconnection(disconnectionCallback); |
vcoubard | 17:5afb0e5a48fc | 54 | |
vcoubard | 17:5afb0e5a48fc | 55 | batteryService = new BatteryService(ble, batteryLevel); |
vcoubard | 17:5afb0e5a48fc | 56 | |
vcoubard | 17:5afb0e5a48fc | 57 | /* setup advertising */ |
vcoubard | 17:5afb0e5a48fc | 58 | gap.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
vcoubard | 17:5afb0e5a48fc | 59 | gap.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
vcoubard | 17:5afb0e5a48fc | 60 | gap.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
vcoubard | 17:5afb0e5a48fc | 61 | gap.startAdvertising(); |
vcoubard | 17:5afb0e5a48fc | 62 | } |
vcoubard | 17:5afb0e5a48fc | 63 | |
Rohit Grover |
4:5b64235d1b85 | 64 | int main(void) |
Rohit Grover |
4:5b64235d1b85 | 65 | { |
rgrover1 | 12:4024aa3f72b0 | 66 | t.attach(blink, 1.0f); |
Rohit Grover |
4:5b64235d1b85 | 67 | |
rgrover1 | 12:4024aa3f72b0 | 68 | printf("Initialising the nRF51822\n\r"); |
rgrover1 | 12:4024aa3f72b0 | 69 | |
vcoubard | 17:5afb0e5a48fc | 70 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
vcoubard | 17:5afb0e5a48fc | 71 | ble.init(bleInitComplete); |
rgrover1 | 13:60e095fe4b45 | 72 | |
vcoubard | 17:5afb0e5a48fc | 73 | /* SpinWait for initialization to complete. This is necessary because the |
vcoubard | 17:5afb0e5a48fc | 74 | * BLE object is used in the main loop below. */ |
vcoubard | 17:5afb0e5a48fc | 75 | while (ble.hasInitialized() == false) { /* spin loop */ } |
Rohit Grover |
4:5b64235d1b85 | 76 | |
Rohit Grover |
4:5b64235d1b85 | 77 | while (true) { |
rgrover1 | 12:4024aa3f72b0 | 78 | ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) |
Jun_adi | 20:e7bc34a4a3db | 79 | |
Jun_adi | 20:e7bc34a4a3db | 80 | //Read Anlog input voltage |
Jun_adi | 20:e7bc34a4a3db | 81 | batteryLevel = analogin.read()*100; |
Jun_adi | 20:e7bc34a4a3db | 82 | batteryService->updateBatteryLevel(batteryLevel); |
Jun_adi | 20:e7bc34a4a3db | 83 | } |
Jun_adi | 20:e7bc34a4a3db | 84 | /* |
rgrover1 | 12:4024aa3f72b0 | 85 | // the magic battery processing |
rgrover1 | 12:4024aa3f72b0 | 86 | batteryLevel++; |
rgrover1 | 12:4024aa3f72b0 | 87 | if (batteryLevel > 100) { |
rgrover1 | 12:4024aa3f72b0 | 88 | batteryLevel = 20; |
rgrover1 | 12:4024aa3f72b0 | 89 | } |
rgrover1 | 12:4024aa3f72b0 | 90 | |
vcoubard | 17:5afb0e5a48fc | 91 | batteryService->updateBatteryLevel(batteryLevel); |
Rohit Grover |
4:5b64235d1b85 | 92 | } |
Jun_adi | 20:e7bc34a4a3db | 93 | */ |
Rohit Grover |
4:5b64235d1b85 | 94 | } |