![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Smartphone app control stepper motors via bluetooth low energy(BLE) on stm32F401RE x IHM02A1 x IDB05A1.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IHM02A1 mbed
Fork of BLE_LED_IDB0XA1_demo by
main.cpp@3:e0efdb741bd4, 2016-02-25 (annotated)
- Committer:
- apalmieri
- Date:
- Thu Feb 25 10:07:11 2016 +0000
- Revision:
- 3:e0efdb741bd4
- Parent:
- 0:aae2d6c2a9eb
- Child:
- 7:126b141a8c86
Code cleaning
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apalmieri | 0:aae2d6c2a9eb | 1 | /* mbed Microcontroller Library |
apalmieri | 0:aae2d6c2a9eb | 2 | * Copyright (c) 2006-2013 ARM Limited |
apalmieri | 0:aae2d6c2a9eb | 3 | * |
apalmieri | 0:aae2d6c2a9eb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
apalmieri | 0:aae2d6c2a9eb | 5 | * you may not use this file except in compliance with the License. |
apalmieri | 0:aae2d6c2a9eb | 6 | * You may obtain a copy of the License at |
apalmieri | 0:aae2d6c2a9eb | 7 | * |
apalmieri | 0:aae2d6c2a9eb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
apalmieri | 0:aae2d6c2a9eb | 9 | * |
apalmieri | 0:aae2d6c2a9eb | 10 | * Unless required by applicable law or agreed to in writing, software |
apalmieri | 0:aae2d6c2a9eb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
apalmieri | 0:aae2d6c2a9eb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
apalmieri | 0:aae2d6c2a9eb | 13 | * See the License for the specific language governing permissions and |
apalmieri | 0:aae2d6c2a9eb | 14 | * limitations under the License. |
apalmieri | 0:aae2d6c2a9eb | 15 | */ |
apalmieri | 0:aae2d6c2a9eb | 16 | |
apalmieri | 0:aae2d6c2a9eb | 17 | #include "mbed.h" |
apalmieri | 0:aae2d6c2a9eb | 18 | #include "ble/BLE.h" |
apalmieri | 0:aae2d6c2a9eb | 19 | #include "LEDService.h" |
apalmieri | 0:aae2d6c2a9eb | 20 | |
apalmieri | 0:aae2d6c2a9eb | 21 | DigitalOut actuatedLED(LED1, 0); |
apalmieri | 0:aae2d6c2a9eb | 22 | |
apalmieri | 0:aae2d6c2a9eb | 23 | const static char DEVICE_NAME[] = "LED"; |
apalmieri | 0:aae2d6c2a9eb | 24 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
apalmieri | 0:aae2d6c2a9eb | 25 | |
apalmieri | 0:aae2d6c2a9eb | 26 | LEDService *ledServicePtr; |
apalmieri | 0:aae2d6c2a9eb | 27 | |
apalmieri | 0:aae2d6c2a9eb | 28 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
apalmieri | 0:aae2d6c2a9eb | 29 | { |
apalmieri | 0:aae2d6c2a9eb | 30 | (void)params; |
apalmieri | 3:e0efdb741bd4 | 31 | BLE::Instance().gap().startAdvertising(); // restart advertising |
apalmieri | 0:aae2d6c2a9eb | 32 | } |
apalmieri | 0:aae2d6c2a9eb | 33 | |
apalmieri | 0:aae2d6c2a9eb | 34 | /** |
apalmieri | 0:aae2d6c2a9eb | 35 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
apalmieri | 0:aae2d6c2a9eb | 36 | * |
apalmieri | 0:aae2d6c2a9eb | 37 | * @param[in] params |
apalmieri | 0:aae2d6c2a9eb | 38 | * Information about the characterisitc being updated. |
apalmieri | 0:aae2d6c2a9eb | 39 | */ |
apalmieri | 0:aae2d6c2a9eb | 40 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
apalmieri | 0:aae2d6c2a9eb | 41 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
apalmieri | 0:aae2d6c2a9eb | 42 | actuatedLED = *(params->data); |
apalmieri | 0:aae2d6c2a9eb | 43 | } |
apalmieri | 0:aae2d6c2a9eb | 44 | } |
apalmieri | 0:aae2d6c2a9eb | 45 | |
apalmieri | 0:aae2d6c2a9eb | 46 | /** |
apalmieri | 0:aae2d6c2a9eb | 47 | * This function is called when the ble initialization process has failled |
apalmieri | 0:aae2d6c2a9eb | 48 | */ |
apalmieri | 0:aae2d6c2a9eb | 49 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 0:aae2d6c2a9eb | 50 | { |
apalmieri | 0:aae2d6c2a9eb | 51 | /* Initialization error handling should go here */ |
apalmieri | 0:aae2d6c2a9eb | 52 | } |
apalmieri | 0:aae2d6c2a9eb | 53 | |
apalmieri | 0:aae2d6c2a9eb | 54 | /** |
apalmieri | 0:aae2d6c2a9eb | 55 | * Callback triggered when the ble initialization process has finished |
apalmieri | 0:aae2d6c2a9eb | 56 | */ |
apalmieri | 0:aae2d6c2a9eb | 57 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
apalmieri | 0:aae2d6c2a9eb | 58 | { |
apalmieri | 0:aae2d6c2a9eb | 59 | BLE& ble = params->ble; |
apalmieri | 0:aae2d6c2a9eb | 60 | ble_error_t error = params->error; |
apalmieri | 0:aae2d6c2a9eb | 61 | |
apalmieri | 0:aae2d6c2a9eb | 62 | if (error != BLE_ERROR_NONE) { |
apalmieri | 0:aae2d6c2a9eb | 63 | /* In case of error, forward the error handling to onBleInitError */ |
apalmieri | 0:aae2d6c2a9eb | 64 | onBleInitError(ble, error); |
apalmieri | 0:aae2d6c2a9eb | 65 | return; |
apalmieri | 0:aae2d6c2a9eb | 66 | } |
apalmieri | 0:aae2d6c2a9eb | 67 | |
apalmieri | 0:aae2d6c2a9eb | 68 | /* Ensure that it is the default instance of BLE */ |
apalmieri | 0:aae2d6c2a9eb | 69 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
apalmieri | 0:aae2d6c2a9eb | 70 | return; |
apalmieri | 0:aae2d6c2a9eb | 71 | } |
apalmieri | 0:aae2d6c2a9eb | 72 | |
apalmieri | 0:aae2d6c2a9eb | 73 | ble.gap().onDisconnection(disconnectionCallback); |
apalmieri | 0:aae2d6c2a9eb | 74 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
apalmieri | 0:aae2d6c2a9eb | 75 | |
apalmieri | 0:aae2d6c2a9eb | 76 | bool initialValueForLEDCharacteristic = true; |
apalmieri | 3:e0efdb741bd4 | 77 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
apalmieri | 0:aae2d6c2a9eb | 78 | |
apalmieri | 0:aae2d6c2a9eb | 79 | /* setup advertising */ |
apalmieri | 0:aae2d6c2a9eb | 80 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
apalmieri | 0:aae2d6c2a9eb | 81 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
apalmieri | 0:aae2d6c2a9eb | 82 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
apalmieri | 0:aae2d6c2a9eb | 83 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
apalmieri | 0:aae2d6c2a9eb | 84 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
apalmieri | 0:aae2d6c2a9eb | 85 | ble.gap().startAdvertising(); |
apalmieri | 0:aae2d6c2a9eb | 86 | |
apalmieri | 0:aae2d6c2a9eb | 87 | while (true) { |
apalmieri | 0:aae2d6c2a9eb | 88 | ble.waitForEvent(); |
apalmieri | 0:aae2d6c2a9eb | 89 | } |
apalmieri | 0:aae2d6c2a9eb | 90 | } |
apalmieri | 0:aae2d6c2a9eb | 91 | |
apalmieri | 0:aae2d6c2a9eb | 92 | int main(void) |
apalmieri | 0:aae2d6c2a9eb | 93 | { |
apalmieri | 0:aae2d6c2a9eb | 94 | BLE &ble = BLE::Instance(); |
apalmieri | 0:aae2d6c2a9eb | 95 | |
apalmieri | 0:aae2d6c2a9eb | 96 | ble.init(bleInitComplete); |
apalmieri | 0:aae2d6c2a9eb | 97 | } |
apalmieri | 0:aae2d6c2a9eb | 98 |