![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Demo program for LSM303 based boards
Dependencies: mbed AES BLE_API nRF51822 smallAES
main.cpp@7:46123a4652f2, 2020-10-04 (annotated)
- Committer:
- f3d
- Date:
- Sun Oct 04 16:06:05 2020 +0000
- Revision:
- 7:46123a4652f2
- Parent:
- 5:fccb0823ceff
Simplified config
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:21352e62003f | 1 | /* mbed Microcontroller Library |
f3d | 0:21352e62003f | 2 | * Copyright (c) 2006-2013 ARM Limited |
f3d | 0:21352e62003f | 3 | * |
f3d | 0:21352e62003f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
f3d | 0:21352e62003f | 5 | * you may not use this file except in compliance with the License. |
f3d | 0:21352e62003f | 6 | * You may obtain a copy of the License at |
f3d | 0:21352e62003f | 7 | * |
f3d | 0:21352e62003f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
f3d | 0:21352e62003f | 9 | * |
f3d | 0:21352e62003f | 10 | * Unless required by applicable law or agreed to in writing, software |
f3d | 0:21352e62003f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
f3d | 0:21352e62003f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f3d | 0:21352e62003f | 13 | * See the License for the specific language governing permissions and |
f3d | 0:21352e62003f | 14 | * limitations under the License. |
f3d | 0:21352e62003f | 15 | */ |
f3d | 0:21352e62003f | 16 | |
f3d | 0:21352e62003f | 17 | #include "mbed.h" |
f3d | 0:21352e62003f | 18 | #include "ble/BLE.h" |
f3d | 7:46123a4652f2 | 19 | #include "BLEservice.h" |
f3d | 7:46123a4652f2 | 20 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); |
f3d | 7:46123a4652f2 | 21 | void onBleInitError(BLE &ble, ble_error_t error); |
f3d | 7:46123a4652f2 | 22 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); |
f3d | 7:46123a4652f2 | 23 | Ticker ticker; |
f3d | 7:46123a4652f2 | 24 | Serial pc(P0_24, P0_25); |
f3d | 0:21352e62003f | 25 | |
f3d | 7:46123a4652f2 | 26 | |
f3d | 7:46123a4652f2 | 27 | const static char DEVICE_NAME[] = "Test2020"; |
f3d | 7:46123a4652f2 | 28 | static const uint16_t uuid16_list[] = {0x1234,0x1235}; |
f3d | 7:46123a4652f2 | 29 | BLEservice * LED; |
f3d | 7:46123a4652f2 | 30 | BLEservice * Button; |
f3d | 7:46123a4652f2 | 31 | |
f3d | 7:46123a4652f2 | 32 | DigitalOut col1(P0_4, 0); |
f3d | 7:46123a4652f2 | 33 | DigitalOut alivenessLED(P0_13, 0); |
f3d | 7:46123a4652f2 | 34 | DigitalOut actuatedLED(P0_14, 0); |
f3d | 7:46123a4652f2 | 35 | void initLED() |
f3d | 7:46123a4652f2 | 36 | { |
f3d | 7:46123a4652f2 | 37 | actuatedLED = 0; |
f3d | 7:46123a4652f2 | 38 | } |
f3d | 7:46123a4652f2 | 39 | void pollLED() |
f3d | 7:46123a4652f2 | 40 | { |
f3d | 7:46123a4652f2 | 41 | uint16_t Value; |
f3d | 7:46123a4652f2 | 42 | Value = LED->readCharacteristic(0); |
f3d | 7:46123a4652f2 | 43 | actuatedLED = Value; |
f3d | 7:46123a4652f2 | 44 | } |
f3d | 7:46123a4652f2 | 45 | DigitalIn ButtonA(P0_17); |
f3d | 7:46123a4652f2 | 46 | void initButton() |
f3d | 7:46123a4652f2 | 47 | { |
f3d | 7:46123a4652f2 | 48 | |
f3d | 7:46123a4652f2 | 49 | } |
f3d | 7:46123a4652f2 | 50 | void pollButton() |
f3d | 0:21352e62003f | 51 | { |
f3d | 7:46123a4652f2 | 52 | uint16_t Value = ButtonA; |
f3d | 7:46123a4652f2 | 53 | pc.printf("Button = %d\r\n",Value); |
f3d | 7:46123a4652f2 | 54 | Button->writeCharacteristic(0,Value); |
f3d | 7:46123a4652f2 | 55 | } |
f3d | 7:46123a4652f2 | 56 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
f3d | 7:46123a4652f2 | 57 | { |
f3d | 7:46123a4652f2 | 58 | pc.printf("Got writeback for %d\r\n",params->handle); |
f3d | 7:46123a4652f2 | 59 | } |
f3d | 7:46123a4652f2 | 60 | void pollIO() |
f3d | 7:46123a4652f2 | 61 | { |
f3d | 7:46123a4652f2 | 62 | pc.printf("Poll\r\n"); |
f3d | 7:46123a4652f2 | 63 | LED->poll(); |
f3d | 7:46123a4652f2 | 64 | Button->poll(); |
f3d | 7:46123a4652f2 | 65 | } |
f3d | 7:46123a4652f2 | 66 | int main(void) |
f3d | 7:46123a4652f2 | 67 | { |
f3d | 7:46123a4652f2 | 68 | BLE &ble = BLE::Instance(); |
f3d | 7:46123a4652f2 | 69 | ble.init(bleInitComplete); |
f3d | 7:46123a4652f2 | 70 | /* SpinWait for initialization to complete. This is necessary because the |
f3d | 7:46123a4652f2 | 71 | * BLE object is used in the main loop below. */ |
f3d | 7:46123a4652f2 | 72 | while (ble.hasInitialized() == false) { /* spin loop */ } |
f3d | 0:21352e62003f | 73 | |
f3d | 7:46123a4652f2 | 74 | pc.printf("BLE init complete\r\n"); |
f3d | 7:46123a4652f2 | 75 | |
f3d | 7:46123a4652f2 | 76 | uint16_t LED_char_array[]={0xabcd}; |
f3d | 7:46123a4652f2 | 77 | pc.printf("Creating LED Service\r\n"); |
f3d | 7:46123a4652f2 | 78 | LED = new BLEservice(ble,0x1234,1,LED_char_array,initLED,pollLED); |
f3d | 7:46123a4652f2 | 79 | LED->init(); |
f3d | 7:46123a4652f2 | 80 | pc.printf("LED service ready\r\n"); |
f3d | 7:46123a4652f2 | 81 | |
f3d | 7:46123a4652f2 | 82 | uint16_t Button_char_array[]={0xa012}; |
f3d | 7:46123a4652f2 | 83 | pc.printf("Creating Button Service\r\n"); |
f3d | 7:46123a4652f2 | 84 | Button = new BLEservice(ble,0x1235,1,Button_char_array,initButton,pollButton); |
f3d | 7:46123a4652f2 | 85 | Button->init(); |
f3d | 7:46123a4652f2 | 86 | pc.printf("Button service ready\r\n"); |
f3d | 7:46123a4652f2 | 87 | |
f3d | 7:46123a4652f2 | 88 | |
f3d | 7:46123a4652f2 | 89 | ticker.attach(pollIO, 0.1); /* Poll devices every 100ms */ |
f3d | 7:46123a4652f2 | 90 | while (1) |
f3d | 7:46123a4652f2 | 91 | { |
f3d | 7:46123a4652f2 | 92 | ble.waitForEvent(); |
f3d | 0:21352e62003f | 93 | } |
f3d | 0:21352e62003f | 94 | } |
f3d | 7:46123a4652f2 | 95 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
f3d | 7:46123a4652f2 | 96 | { |
f3d | 7:46123a4652f2 | 97 | BLE& ble = params->ble; |
f3d | 7:46123a4652f2 | 98 | ble_error_t error = params->error; |
f3d | 7:46123a4652f2 | 99 | |
f3d | 7:46123a4652f2 | 100 | if (error != BLE_ERROR_NONE) { |
f3d | 7:46123a4652f2 | 101 | /* In case of error, forward the error handling to onBleInitError */ |
f3d | 7:46123a4652f2 | 102 | onBleInitError(ble, error); |
f3d | 7:46123a4652f2 | 103 | return; |
f3d | 7:46123a4652f2 | 104 | } |
f3d | 7:46123a4652f2 | 105 | |
f3d | 7:46123a4652f2 | 106 | /* Ensure that it is the default instance of BLE */ |
f3d | 7:46123a4652f2 | 107 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
f3d | 7:46123a4652f2 | 108 | return; |
f3d | 7:46123a4652f2 | 109 | } |
f3d | 7:46123a4652f2 | 110 | |
f3d | 7:46123a4652f2 | 111 | ble.gap().onDisconnection(disconnectionCallback); |
f3d | 7:46123a4652f2 | 112 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
f3d | 7:46123a4652f2 | 113 | // ble.gattServer().onDataRead(onDataReadCallback); // Nordic Soft device will not call this so have to poll instead |
f3d | 7:46123a4652f2 | 114 | |
f3d | 7:46123a4652f2 | 115 | |
f3d | 7:46123a4652f2 | 116 | /* setup advertising */ |
f3d | 7:46123a4652f2 | 117 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
f3d | 7:46123a4652f2 | 118 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
f3d | 7:46123a4652f2 | 119 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
f3d | 7:46123a4652f2 | 120 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
f3d | 7:46123a4652f2 | 121 | ble.gap().setAdvertisingInterval(500); /* 500ms. */ |
f3d | 7:46123a4652f2 | 122 | pc.printf("Starting advertising\r\n"); |
f3d | 7:46123a4652f2 | 123 | ble.gap().startAdvertising(); |
f3d | 7:46123a4652f2 | 124 | } |
f3d | 7:46123a4652f2 | 125 | void onBleInitError(BLE &ble, ble_error_t error) |
f3d | 7:46123a4652f2 | 126 | { |
f3d | 7:46123a4652f2 | 127 | /* Initialization error handling should go here */ |
f3d | 7:46123a4652f2 | 128 | pc.printf("BLE init error\r\n"); |
f3d | 7:46123a4652f2 | 129 | } |
f3d | 7:46123a4652f2 | 130 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
f3d | 7:46123a4652f2 | 131 | { |
f3d | 7:46123a4652f2 | 132 | pc.printf("Disconnected, restarting advertising\r\n"); |
f3d | 7:46123a4652f2 | 133 | BLE::Instance().gap().startAdvertising(); |
f3d | 7:46123a4652f2 | 134 | } |