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