mamont mamont / Mbed OS nrf52840_mdk_ble_batteryservice
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 <events/mbed_events.h>
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "ble/Gap.h"
00021 #include "ble/services/BatteryService.h"
00022 
00023 DigitalOut led1(P0_22, 1);
00024 
00025 const static char     DEVICE_NAME[] = "BATTERY";
00026 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
00027 
00028 static uint8_t batteryLevel = 50;
00029 static BatteryService* batteryServicePtr;
00030 
00031 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00032 
00033 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00034 {
00035     BLE::Instance().gap().startAdvertising();
00036 }
00037 
00038 void updateSensorValue() {
00039     batteryLevel++;
00040     if (batteryLevel > 100) {
00041         batteryLevel = 20;
00042     }
00043 
00044     batteryServicePtr->updateBatteryLevel(batteryLevel);
00045 }
00046 
00047 void blinkCallback(void)
00048 {
00049     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00050 
00051     BLE &ble = BLE::Instance();
00052     if (ble.gap().getState().connected) {
00053         eventQueue.call(updateSensorValue);
00054     }
00055 }
00056 
00057 /**
00058  * This function is called when the ble initialization process has failled
00059  */
00060 void onBleInitError(BLE &ble, ble_error_t error)
00061 {
00062     /* Initialization error handling should go here */
00063 }
00064 
00065 void printMacAddress()
00066 {
00067     /* ////print out device MAC address to the console*/
00068     Gap::AddressType_t addr_type;
00069     Gap::Address_t address;
00070     BLE::Instance().gap().getAddress(&addr_type, address);
00071     //printf("DEVICE MAC ADDRESS: ");
00072     for (int i = 5; i >= 1; i--){
00073         //////////printf("%02x:", address[i]);
00074     }
00075     //printf("%02x\r\n", address[0]);
00076 }
00077 
00078 /**
00079  * Callback triggered when the ble initialization process has finished
00080  */
00081 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00082 {
00083     BLE&        ble   = params->ble;
00084     ble_error_t error = params->error;
00085 
00086     if (error != BLE_ERROR_NONE) {
00087         /* In case of error, forward the error handling to onBleInitError */
00088         onBleInitError(ble, error);
00089         return;
00090     }
00091 
00092     /* Ensure that it is the default instance of BLE */
00093     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00094         return;
00095     }
00096 
00097     ble.gap().onDisconnection(disconnectionCallback);
00098 
00099     /* Setup primary service */
00100     batteryServicePtr = new BatteryService(ble, batteryLevel);
00101 
00102     /* Setup advertising */
00103     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00104     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
00105     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
00106     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00107     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00108     ble.gap().startAdvertising();
00109 
00110     //printMacAddress();
00111 }
00112 
00113 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00114     BLE &ble = BLE::Instance();
00115     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00116 }
00117 
00118 int main()
00119 {
00120     eventQueue.call_every(500, blinkCallback);
00121 
00122     BLE &ble = BLE::Instance();
00123     ble.onEventsToProcess(scheduleBleEventsProcessing);
00124     ble.init(bleInitComplete);
00125 
00126     eventQueue.dispatch_forever();
00127 
00128     return 0;
00129 }