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.
Fork of pdiot-ble-notify-array by
source/main.cpp@5:b630d2a88c51, 2016-08-02 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Aug 02 09:45:20 2016 +0100
- Revision:
- 5:b630d2a88c51
- Parent:
- 1:cd85f873e10d
- Child:
- 14:014670ad5e18
Merge pull request #14 from ARMmbed/add_button_configuration
Add button configuration for BLE_Button application.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
0:3fe9d5124576 | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
0:3fe9d5124576 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Vincent Coubard |
0:3fe9d5124576 | 3 | * |
Vincent Coubard |
0:3fe9d5124576 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
0:3fe9d5124576 | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
0:3fe9d5124576 | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
0:3fe9d5124576 | 7 | * |
Vincent Coubard |
0:3fe9d5124576 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
0:3fe9d5124576 | 9 | * |
Vincent Coubard |
0:3fe9d5124576 | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
0:3fe9d5124576 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
0:3fe9d5124576 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
0:3fe9d5124576 | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
0:3fe9d5124576 | 14 | * limitations under the License. |
Vincent Coubard |
0:3fe9d5124576 | 15 | */ |
Vincent Coubard |
0:3fe9d5124576 | 16 | #include <mbed-events/events.h> |
Vincent Coubard |
0:3fe9d5124576 | 17 | |
Vincent Coubard |
0:3fe9d5124576 | 18 | #include <mbed.h> |
Vincent Coubard |
0:3fe9d5124576 | 19 | #include "ble/BLE.h" |
Vincent Coubard |
0:3fe9d5124576 | 20 | #include "ble/Gap.h" |
Vincent Coubard |
0:3fe9d5124576 | 21 | #include "ButtonService.h" |
Vincent Coubard |
0:3fe9d5124576 | 22 | |
Vincent Coubard |
0:3fe9d5124576 | 23 | DigitalOut led1(LED1, 1); |
mbed_official | 5:b630d2a88c51 | 24 | InterruptIn button(BLE_BUTTON_PIN_NAME); |
Vincent Coubard |
0:3fe9d5124576 | 25 | |
Vincent Coubard |
0:3fe9d5124576 | 26 | static EventQueue eventQueue( |
Vincent Coubard |
0:3fe9d5124576 | 27 | /* event count */ 10 * /* event size */ 32 |
Vincent Coubard |
0:3fe9d5124576 | 28 | ); |
Vincent Coubard |
0:3fe9d5124576 | 29 | |
Vincent Coubard |
0:3fe9d5124576 | 30 | const static char DEVICE_NAME[] = "Button"; |
Vincent Coubard |
0:3fe9d5124576 | 31 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
Vincent Coubard |
0:3fe9d5124576 | 32 | |
Vincent Coubard |
0:3fe9d5124576 | 33 | ButtonService *buttonServicePtr; |
Vincent Coubard |
0:3fe9d5124576 | 34 | |
Vincent Coubard |
0:3fe9d5124576 | 35 | void buttonPressedCallback(void) |
Vincent Coubard |
0:3fe9d5124576 | 36 | { |
Vincent Coubard |
0:3fe9d5124576 | 37 | eventQueue.post(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), true); |
Vincent Coubard |
0:3fe9d5124576 | 38 | } |
Vincent Coubard |
0:3fe9d5124576 | 39 | |
Vincent Coubard |
0:3fe9d5124576 | 40 | void buttonReleasedCallback(void) |
Vincent Coubard |
0:3fe9d5124576 | 41 | { |
Vincent Coubard |
0:3fe9d5124576 | 42 | eventQueue.post(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), false); |
Vincent Coubard |
0:3fe9d5124576 | 43 | } |
Vincent Coubard |
0:3fe9d5124576 | 44 | |
Vincent Coubard |
0:3fe9d5124576 | 45 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Vincent Coubard |
0:3fe9d5124576 | 46 | { |
Vincent Coubard |
0:3fe9d5124576 | 47 | BLE::Instance().gap().startAdvertising(); // restart advertising |
Vincent Coubard |
0:3fe9d5124576 | 48 | } |
Vincent Coubard |
0:3fe9d5124576 | 49 | |
Vincent Coubard |
0:3fe9d5124576 | 50 | void blinkCallback(void) |
Vincent Coubard |
0:3fe9d5124576 | 51 | { |
Vincent Coubard |
0:3fe9d5124576 | 52 | led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
Vincent Coubard |
0:3fe9d5124576 | 53 | } |
Vincent Coubard |
0:3fe9d5124576 | 54 | |
Vincent Coubard |
0:3fe9d5124576 | 55 | void onBleInitError(BLE &ble, ble_error_t error) |
Vincent Coubard |
0:3fe9d5124576 | 56 | { |
Vincent Coubard |
0:3fe9d5124576 | 57 | /* Initialization error handling should go here */ |
Vincent Coubard |
0:3fe9d5124576 | 58 | } |
Vincent Coubard |
0:3fe9d5124576 | 59 | |
Vincent Coubard |
0:3fe9d5124576 | 60 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Vincent Coubard |
0:3fe9d5124576 | 61 | { |
Vincent Coubard |
0:3fe9d5124576 | 62 | BLE& ble = params->ble; |
Vincent Coubard |
0:3fe9d5124576 | 63 | ble_error_t error = params->error; |
Vincent Coubard |
0:3fe9d5124576 | 64 | |
Vincent Coubard |
0:3fe9d5124576 | 65 | if (error != BLE_ERROR_NONE) { |
Vincent Coubard |
0:3fe9d5124576 | 66 | /* In case of error, forward the error handling to onBleInitError */ |
Vincent Coubard |
0:3fe9d5124576 | 67 | onBleInitError(ble, error); |
Vincent Coubard |
0:3fe9d5124576 | 68 | return; |
Vincent Coubard |
0:3fe9d5124576 | 69 | } |
Vincent Coubard |
0:3fe9d5124576 | 70 | |
Vincent Coubard |
0:3fe9d5124576 | 71 | /* Ensure that it is the default instance of BLE */ |
Vincent Coubard |
0:3fe9d5124576 | 72 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Vincent Coubard |
0:3fe9d5124576 | 73 | return; |
Vincent Coubard |
0:3fe9d5124576 | 74 | } |
Vincent Coubard |
0:3fe9d5124576 | 75 | |
Vincent Coubard |
0:3fe9d5124576 | 76 | ble.gap().onDisconnection(disconnectionCallback); |
Vincent Coubard |
0:3fe9d5124576 | 77 | |
Vincent Coubard |
0:3fe9d5124576 | 78 | button.fall(buttonPressedCallback); |
Vincent Coubard |
0:3fe9d5124576 | 79 | button.rise(buttonReleasedCallback); |
Vincent Coubard |
0:3fe9d5124576 | 80 | |
Vincent Coubard |
0:3fe9d5124576 | 81 | /* Setup primary service. */ |
Vincent Coubard |
0:3fe9d5124576 | 82 | buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); |
Vincent Coubard |
0:3fe9d5124576 | 83 | |
Vincent Coubard |
0:3fe9d5124576 | 84 | /* setup advertising */ |
Vincent Coubard |
0:3fe9d5124576 | 85 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Vincent Coubard |
0:3fe9d5124576 | 86 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Vincent Coubard |
0:3fe9d5124576 | 87 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Vincent Coubard |
0:3fe9d5124576 | 88 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Vincent Coubard |
0:3fe9d5124576 | 89 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
Vincent Coubard |
0:3fe9d5124576 | 90 | ble.gap().startAdvertising(); |
Vincent Coubard |
0:3fe9d5124576 | 91 | } |
Vincent Coubard |
0:3fe9d5124576 | 92 | |
Vincent Coubard |
0:3fe9d5124576 | 93 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Vincent Coubard |
0:3fe9d5124576 | 94 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
0:3fe9d5124576 | 95 | eventQueue.post(Callback<void()>(&ble, &BLE::processEvents)); |
Vincent Coubard |
0:3fe9d5124576 | 96 | } |
Vincent Coubard |
0:3fe9d5124576 | 97 | |
Vincent Coubard |
0:3fe9d5124576 | 98 | int main() |
Vincent Coubard |
0:3fe9d5124576 | 99 | { |
mbed_official | 1:cd85f873e10d | 100 | eventQueue.post_every(500, blinkCallback); |
Vincent Coubard |
0:3fe9d5124576 | 101 | |
Vincent Coubard |
0:3fe9d5124576 | 102 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
0:3fe9d5124576 | 103 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Vincent Coubard |
0:3fe9d5124576 | 104 | ble.init(bleInitComplete); |
Vincent Coubard |
0:3fe9d5124576 | 105 | |
Vincent Coubard |
0:3fe9d5124576 | 106 | while (true) { |
Vincent Coubard |
0:3fe9d5124576 | 107 | eventQueue.dispatch(); |
Vincent Coubard |
0:3fe9d5124576 | 108 | } |
Vincent Coubard |
0:3fe9d5124576 | 109 | |
Vincent Coubard |
0:3fe9d5124576 | 110 | return 0; |
Vincent Coubard |
0:3fe9d5124576 | 111 | } |