Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:e8b2929feaea, 2020-06-05 (annotated)
- Committer:
- mamont090671
- Date:
- Fri Jun 05 14:20:12 2020 +0000
- Revision:
- 0:e8b2929feaea
nrf52840-mdk
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mamont090671 | 0:e8b2929feaea | 1 | /* mbed Microcontroller Library |
| mamont090671 | 0:e8b2929feaea | 2 | * Copyright (c) 2006-2013 ARM Limited |
| mamont090671 | 0:e8b2929feaea | 3 | * |
| mamont090671 | 0:e8b2929feaea | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| mamont090671 | 0:e8b2929feaea | 5 | * you may not use this file except in compliance with the License. |
| mamont090671 | 0:e8b2929feaea | 6 | * You may obtain a copy of the License at |
| mamont090671 | 0:e8b2929feaea | 7 | * |
| mamont090671 | 0:e8b2929feaea | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| mamont090671 | 0:e8b2929feaea | 9 | * |
| mamont090671 | 0:e8b2929feaea | 10 | * Unless required by applicable law or agreed to in writing, software |
| mamont090671 | 0:e8b2929feaea | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| mamont090671 | 0:e8b2929feaea | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| mamont090671 | 0:e8b2929feaea | 13 | * See the License for the specific language governing permissions and |
| mamont090671 | 0:e8b2929feaea | 14 | * limitations under the License. |
| mamont090671 | 0:e8b2929feaea | 15 | */ |
| mamont090671 | 0:e8b2929feaea | 16 | #include <events/mbed_events.h> |
| mamont090671 | 0:e8b2929feaea | 17 | |
| mamont090671 | 0:e8b2929feaea | 18 | #include <mbed.h> |
| mamont090671 | 0:e8b2929feaea | 19 | #include "ble/BLE.h" |
| mamont090671 | 0:e8b2929feaea | 20 | #include "ble/Gap.h" |
| mamont090671 | 0:e8b2929feaea | 21 | #include "ButtonService.h" |
| mamont090671 | 0:e8b2929feaea | 22 | |
| mamont090671 | 0:e8b2929feaea | 23 | DigitalOut led1(P0_24, 1); |
| mamont090671 | 0:e8b2929feaea | 24 | InterruptIn button(P0_18); |
| mamont090671 | 0:e8b2929feaea | 25 | |
| mamont090671 | 0:e8b2929feaea | 26 | static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE); |
| mamont090671 | 0:e8b2929feaea | 27 | |
| mamont090671 | 0:e8b2929feaea | 28 | const static char DEVICE_NAME[] = "Button"; |
| mamont090671 | 0:e8b2929feaea | 29 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
| mamont090671 | 0:e8b2929feaea | 30 | |
| mamont090671 | 0:e8b2929feaea | 31 | ButtonService *buttonServicePtr; |
| mamont090671 | 0:e8b2929feaea | 32 | |
| mamont090671 | 0:e8b2929feaea | 33 | void buttonPressedCallback(void) |
| mamont090671 | 0:e8b2929feaea | 34 | { |
| mamont090671 | 0:e8b2929feaea | 35 | eventQueue.call(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), true); |
| mamont090671 | 0:e8b2929feaea | 36 | } |
| mamont090671 | 0:e8b2929feaea | 37 | |
| mamont090671 | 0:e8b2929feaea | 38 | void buttonReleasedCallback(void) |
| mamont090671 | 0:e8b2929feaea | 39 | { |
| mamont090671 | 0:e8b2929feaea | 40 | eventQueue.call(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), false); |
| mamont090671 | 0:e8b2929feaea | 41 | } |
| mamont090671 | 0:e8b2929feaea | 42 | |
| mamont090671 | 0:e8b2929feaea | 43 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| mamont090671 | 0:e8b2929feaea | 44 | { |
| mamont090671 | 0:e8b2929feaea | 45 | BLE::Instance().gap().startAdvertising(); // restart advertising |
| mamont090671 | 0:e8b2929feaea | 46 | led1 = 1; |
| mamont090671 | 0:e8b2929feaea | 47 | } |
| mamont090671 | 0:e8b2929feaea | 48 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
| mamont090671 | 0:e8b2929feaea | 49 | { |
| mamont090671 | 0:e8b2929feaea | 50 | led1 = 0; |
| mamont090671 | 0:e8b2929feaea | 51 | } |
| mamont090671 | 0:e8b2929feaea | 52 | |
| mamont090671 | 0:e8b2929feaea | 53 | void blinkCallback(void) |
| mamont090671 | 0:e8b2929feaea | 54 | { |
| mamont090671 | 0:e8b2929feaea | 55 | // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
| mamont090671 | 0:e8b2929feaea | 56 | } |
| mamont090671 | 0:e8b2929feaea | 57 | |
| mamont090671 | 0:e8b2929feaea | 58 | void onBleInitError(BLE &ble, ble_error_t error) |
| mamont090671 | 0:e8b2929feaea | 59 | { |
| mamont090671 | 0:e8b2929feaea | 60 | /* Initialization error handling should go here */ |
| mamont090671 | 0:e8b2929feaea | 61 | } |
| mamont090671 | 0:e8b2929feaea | 62 | |
| mamont090671 | 0:e8b2929feaea | 63 | void printMacAddress() |
| mamont090671 | 0:e8b2929feaea | 64 | { |
| mamont090671 | 0:e8b2929feaea | 65 | /* Print out device MAC address to the console*/ |
| mamont090671 | 0:e8b2929feaea | 66 | Gap::AddressType_t addr_type; |
| mamont090671 | 0:e8b2929feaea | 67 | Gap::Address_t address; |
| mamont090671 | 0:e8b2929feaea | 68 | BLE::Instance().gap().getAddress(&addr_type, address); |
| mamont090671 | 0:e8b2929feaea | 69 | printf("DEVICE MAC ADDRESS: "); |
| mamont090671 | 0:e8b2929feaea | 70 | for (int i = 5; i >= 1; i--){ |
| mamont090671 | 0:e8b2929feaea | 71 | printf("%02x:", address[i]); |
| mamont090671 | 0:e8b2929feaea | 72 | } |
| mamont090671 | 0:e8b2929feaea | 73 | printf("%02x\r\n", address[0]); |
| mamont090671 | 0:e8b2929feaea | 74 | } |
| mamont090671 | 0:e8b2929feaea | 75 | |
| mamont090671 | 0:e8b2929feaea | 76 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| mamont090671 | 0:e8b2929feaea | 77 | { |
| mamont090671 | 0:e8b2929feaea | 78 | BLE& ble = params->ble; |
| mamont090671 | 0:e8b2929feaea | 79 | ble_error_t error = params->error; |
| mamont090671 | 0:e8b2929feaea | 80 | |
| mamont090671 | 0:e8b2929feaea | 81 | if (error != BLE_ERROR_NONE) { |
| mamont090671 | 0:e8b2929feaea | 82 | /* In case of error, forward the error handling to onBleInitError */ |
| mamont090671 | 0:e8b2929feaea | 83 | onBleInitError(ble, error); |
| mamont090671 | 0:e8b2929feaea | 84 | return; |
| mamont090671 | 0:e8b2929feaea | 85 | } |
| mamont090671 | 0:e8b2929feaea | 86 | |
| mamont090671 | 0:e8b2929feaea | 87 | /* Ensure that it is the default instance of BLE */ |
| mamont090671 | 0:e8b2929feaea | 88 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| mamont090671 | 0:e8b2929feaea | 89 | return; |
| mamont090671 | 0:e8b2929feaea | 90 | } |
| mamont090671 | 0:e8b2929feaea | 91 | |
| mamont090671 | 0:e8b2929feaea | 92 | ble.gap().onDisconnection(disconnectionCallback); |
| mamont090671 | 0:e8b2929feaea | 93 | ble.gap().onConnection(connectionCallback); |
| mamont090671 | 0:e8b2929feaea | 94 | |
| mamont090671 | 0:e8b2929feaea | 95 | button.fall(buttonPressedCallback); |
| mamont090671 | 0:e8b2929feaea | 96 | button.rise(buttonReleasedCallback); |
| mamont090671 | 0:e8b2929feaea | 97 | |
| mamont090671 | 0:e8b2929feaea | 98 | /* Setup primary service. */ |
| mamont090671 | 0:e8b2929feaea | 99 | buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); |
| mamont090671 | 0:e8b2929feaea | 100 | |
| mamont090671 | 0:e8b2929feaea | 101 | /* setup advertising */ |
| mamont090671 | 0:e8b2929feaea | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| mamont090671 | 0:e8b2929feaea | 103 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| mamont090671 | 0:e8b2929feaea | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| mamont090671 | 0:e8b2929feaea | 105 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| mamont090671 | 0:e8b2929feaea | 106 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
| mamont090671 | 0:e8b2929feaea | 107 | ble.gap().startAdvertising(); |
| mamont090671 | 0:e8b2929feaea | 108 | |
| mamont090671 | 0:e8b2929feaea | 109 | // printMacAddress(); |
| mamont090671 | 0:e8b2929feaea | 110 | } |
| mamont090671 | 0:e8b2929feaea | 111 | |
| mamont090671 | 0:e8b2929feaea | 112 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
| mamont090671 | 0:e8b2929feaea | 113 | BLE &ble = BLE::Instance(); |
| mamont090671 | 0:e8b2929feaea | 114 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
| mamont090671 | 0:e8b2929feaea | 115 | } |
| mamont090671 | 0:e8b2929feaea | 116 | |
| mamont090671 | 0:e8b2929feaea | 117 | int main() |
| mamont090671 | 0:e8b2929feaea | 118 | { |
| mamont090671 | 0:e8b2929feaea | 119 | eventQueue.call_every(500, blinkCallback); |
| mamont090671 | 0:e8b2929feaea | 120 | |
| mamont090671 | 0:e8b2929feaea | 121 | BLE &ble = BLE::Instance(); |
| mamont090671 | 0:e8b2929feaea | 122 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
| mamont090671 | 0:e8b2929feaea | 123 | ble.init(bleInitComplete); |
| mamont090671 | 0:e8b2929feaea | 124 | |
| mamont090671 | 0:e8b2929feaea | 125 | eventQueue.dispatch_forever(); |
| mamont090671 | 0:e8b2929feaea | 126 | |
| mamont090671 | 0:e8b2929feaea | 127 | return 0; |
| mamont090671 | 0:e8b2929feaea | 128 | } |