app critics will say it's money, cash, toes

Committer:
vazbyte
Date:
Sun Oct 28 12:36:21 2018 +0000
Revision:
0:07212de2fec1
Child:
1:df8884d38960
started from BLE now the whole team -- here

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vazbyte 0:07212de2fec1 1 /* mbed Microcontroller Library
vazbyte 0:07212de2fec1 2 * Copyright (c) 2006-2014 ARM Limited
vazbyte 0:07212de2fec1 3 *
vazbyte 0:07212de2fec1 4 * Licensed under the Apache License, Version 2.0 (the "License");
vazbyte 0:07212de2fec1 5 * you may not use this file except in compliance with the License.
vazbyte 0:07212de2fec1 6 * You may obtain a copy of the License at
vazbyte 0:07212de2fec1 7 *
vazbyte 0:07212de2fec1 8 * http://www.apache.org/licenses/LICENSE-2.0
vazbyte 0:07212de2fec1 9 *
vazbyte 0:07212de2fec1 10 * Unless required by applicable law or agreed to in writing, software
vazbyte 0:07212de2fec1 11 * distributed under the License is distributed on an "AS IS" BASIS,
vazbyte 0:07212de2fec1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vazbyte 0:07212de2fec1 13 * See the License for the specific language governing permissions and
vazbyte 0:07212de2fec1 14 * limitations under the License.
vazbyte 0:07212de2fec1 15 */
vazbyte 0:07212de2fec1 16
vazbyte 0:07212de2fec1 17 #include <events/mbed_events.h>
vazbyte 0:07212de2fec1 18 #include <mbed.h>
vazbyte 0:07212de2fec1 19 #include "ble/BLE.h"
vazbyte 0:07212de2fec1 20 #include "ble/Gap.h"
vazbyte 0:07212de2fec1 21 #include "ble/services/BatteryService.h"
vazbyte 0:07212de2fec1 22
vazbyte 0:07212de2fec1 23 DigitalOut led1(LED1, 1);
vazbyte 0:07212de2fec1 24
vazbyte 0:07212de2fec1 25 const static char DEVICE_NAME[] = "BATTERY";
vazbyte 0:07212de2fec1 26 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
vazbyte 0:07212de2fec1 27
vazbyte 0:07212de2fec1 28 static uint8_t batteryLevel = 50;
vazbyte 0:07212de2fec1 29 static BatteryService* batteryServicePtr;
vazbyte 0:07212de2fec1 30
vazbyte 0:07212de2fec1 31 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
vazbyte 0:07212de2fec1 32
vazbyte 0:07212de2fec1 33 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
vazbyte 0:07212de2fec1 34 {
vazbyte 0:07212de2fec1 35 BLE::Instance().gap().startAdvertising();
vazbyte 0:07212de2fec1 36 }
vazbyte 0:07212de2fec1 37
vazbyte 0:07212de2fec1 38 void updateSensorValue() {
vazbyte 0:07212de2fec1 39 batteryLevel++;
vazbyte 0:07212de2fec1 40 if (batteryLevel > 100) {
vazbyte 0:07212de2fec1 41 batteryLevel = 20;
vazbyte 0:07212de2fec1 42 }
vazbyte 0:07212de2fec1 43
vazbyte 0:07212de2fec1 44 batteryServicePtr->updateBatteryLevel(batteryLevel);
vazbyte 0:07212de2fec1 45 }
vazbyte 0:07212de2fec1 46
vazbyte 0:07212de2fec1 47 void blinkCallback(void)
vazbyte 0:07212de2fec1 48 {
vazbyte 0:07212de2fec1 49 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
vazbyte 0:07212de2fec1 50
vazbyte 0:07212de2fec1 51 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 52 if (ble.gap().getState().connected) {
vazbyte 0:07212de2fec1 53 eventQueue.call(updateSensorValue);
vazbyte 0:07212de2fec1 54 }
vazbyte 0:07212de2fec1 55 }
vazbyte 0:07212de2fec1 56
vazbyte 0:07212de2fec1 57 /**
vazbyte 0:07212de2fec1 58 * This function is called when the ble initialization process has failled
vazbyte 0:07212de2fec1 59 */
vazbyte 0:07212de2fec1 60 void onBleInitError(BLE &ble, ble_error_t error)
vazbyte 0:07212de2fec1 61 {
vazbyte 0:07212de2fec1 62 /* Initialization error handling should go here */
vazbyte 0:07212de2fec1 63 }
vazbyte 0:07212de2fec1 64
vazbyte 0:07212de2fec1 65 void printMacAddress()
vazbyte 0:07212de2fec1 66 {
vazbyte 0:07212de2fec1 67 /* Print out device MAC address to the console*/
vazbyte 0:07212de2fec1 68 Gap::AddressType_t addr_type;
vazbyte 0:07212de2fec1 69 Gap::Address_t address;
vazbyte 0:07212de2fec1 70 BLE::Instance().gap().getAddress(&addr_type, address);
vazbyte 0:07212de2fec1 71 printf("DEVICE MAC ADDRESS: ");
vazbyte 0:07212de2fec1 72 for (int i = 5; i >= 1; i--){
vazbyte 0:07212de2fec1 73 printf("%02x:", address[i]);
vazbyte 0:07212de2fec1 74 }
vazbyte 0:07212de2fec1 75 printf("%02x\r\n", address[0]);
vazbyte 0:07212de2fec1 76 }
vazbyte 0:07212de2fec1 77
vazbyte 0:07212de2fec1 78 /**
vazbyte 0:07212de2fec1 79 * Callback triggered when the ble initialization process has finished
vazbyte 0:07212de2fec1 80 */
vazbyte 0:07212de2fec1 81 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
vazbyte 0:07212de2fec1 82 {
vazbyte 0:07212de2fec1 83 BLE& ble = params->ble;
vazbyte 0:07212de2fec1 84 ble_error_t error = params->error;
vazbyte 0:07212de2fec1 85
vazbyte 0:07212de2fec1 86 if (error != BLE_ERROR_NONE) {
vazbyte 0:07212de2fec1 87 /* In case of error, forward the error handling to onBleInitError */
vazbyte 0:07212de2fec1 88 onBleInitError(ble, error);
vazbyte 0:07212de2fec1 89 return;
vazbyte 0:07212de2fec1 90 }
vazbyte 0:07212de2fec1 91
vazbyte 0:07212de2fec1 92 /* Ensure that it is the default instance of BLE */
vazbyte 0:07212de2fec1 93 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
vazbyte 0:07212de2fec1 94 return;
vazbyte 0:07212de2fec1 95 }
vazbyte 0:07212de2fec1 96
vazbyte 0:07212de2fec1 97 ble.gap().onDisconnection(disconnectionCallback);
vazbyte 0:07212de2fec1 98
vazbyte 0:07212de2fec1 99 /* Setup primary service */
vazbyte 0:07212de2fec1 100 batteryServicePtr = new BatteryService(ble, batteryLevel);
vazbyte 0:07212de2fec1 101
vazbyte 0:07212de2fec1 102 /* Setup advertising */
vazbyte 0:07212de2fec1 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vazbyte 0:07212de2fec1 104 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
vazbyte 0:07212de2fec1 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
vazbyte 0:07212de2fec1 106 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vazbyte 0:07212de2fec1 107 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
vazbyte 0:07212de2fec1 108 ble.gap().startAdvertising();
vazbyte 0:07212de2fec1 109
vazbyte 0:07212de2fec1 110 printMacAddress();
vazbyte 0:07212de2fec1 111 }
vazbyte 0:07212de2fec1 112
vazbyte 0:07212de2fec1 113 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
vazbyte 0:07212de2fec1 114 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 115 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
vazbyte 0:07212de2fec1 116 }
vazbyte 0:07212de2fec1 117
vazbyte 0:07212de2fec1 118 int main()
vazbyte 0:07212de2fec1 119 {
vazbyte 0:07212de2fec1 120 eventQueue.call_every(500, blinkCallback);
vazbyte 0:07212de2fec1 121
vazbyte 0:07212de2fec1 122 BLE &ble = BLE::Instance();
vazbyte 0:07212de2fec1 123 ble.onEventsToProcess(scheduleBleEventsProcessing);
vazbyte 0:07212de2fec1 124 ble.init(bleInitComplete);
vazbyte 0:07212de2fec1 125
vazbyte 0:07212de2fec1 126 eventQueue.dispatch_forever();
vazbyte 0:07212de2fec1 127
vazbyte 0:07212de2fec1 128 return 0;
vazbyte 0:07212de2fec1 129 }