BTLE example for a CJMCU-8223 (NRF51822+Lis3dh). Also includes an OLED display. Further details on ioprog.com
Dependencies: mbed BLE_API nRF51822
main.cpp@0:8fa1c7356f71, 2020-06-24 (annotated)
- Committer:
- f3d
- Date:
- Wed Jun 24 11:37:11 2020 +0000
- Revision:
- 0:8fa1c7356f71
Initial commit for CJMCU-8223 offering an LED service and an accelerometer service. Also includes an Oled display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:8fa1c7356f71 | 1 | /* mbed Microcontroller Library |
f3d | 0:8fa1c7356f71 | 2 | * Copyright (c) 2006-2013 ARM Limited |
f3d | 0:8fa1c7356f71 | 3 | * |
f3d | 0:8fa1c7356f71 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
f3d | 0:8fa1c7356f71 | 5 | * you may not use this file except in compliance with the License. |
f3d | 0:8fa1c7356f71 | 6 | * You may obtain a copy of the License at |
f3d | 0:8fa1c7356f71 | 7 | * |
f3d | 0:8fa1c7356f71 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
f3d | 0:8fa1c7356f71 | 9 | * |
f3d | 0:8fa1c7356f71 | 10 | * Unless required by applicable law or agreed to in writing, software |
f3d | 0:8fa1c7356f71 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
f3d | 0:8fa1c7356f71 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f3d | 0:8fa1c7356f71 | 13 | * See the License for the specific language governing permissions and |
f3d | 0:8fa1c7356f71 | 14 | * limitations under the License. |
f3d | 0:8fa1c7356f71 | 15 | */ |
f3d | 0:8fa1c7356f71 | 16 | |
f3d | 0:8fa1c7356f71 | 17 | #include "mbed.h" |
f3d | 0:8fa1c7356f71 | 18 | #include "ble/BLE.h" |
f3d | 0:8fa1c7356f71 | 19 | #include "LEDService.h" |
f3d | 0:8fa1c7356f71 | 20 | #include "accelService.h" |
f3d | 0:8fa1c7356f71 | 21 | #include "oled.h" |
f3d | 0:8fa1c7356f71 | 22 | #define enable_interrupts() asm(" cpsie i ") |
f3d | 0:8fa1c7356f71 | 23 | #define disable_interrupts() asm(" cpsid i ") |
f3d | 0:8fa1c7356f71 | 24 | |
f3d | 0:8fa1c7356f71 | 25 | |
f3d | 0:8fa1c7356f71 | 26 | DigitalOut myled(P0_29); |
f3d | 0:8fa1c7356f71 | 27 | |
f3d | 0:8fa1c7356f71 | 28 | const static char DEVICE_NAME[] = "CJMCU-8223"; |
f3d | 0:8fa1c7356f71 | 29 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID,accelService::ACCEL_SERVICE_UUID}; |
f3d | 0:8fa1c7356f71 | 30 | accelService *AccelServicePtr; |
f3d | 0:8fa1c7356f71 | 31 | LEDService *ledServicePtr; |
f3d | 0:8fa1c7356f71 | 32 | |
f3d | 0:8fa1c7356f71 | 33 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
f3d | 0:8fa1c7356f71 | 34 | { |
f3d | 0:8fa1c7356f71 | 35 | BLE::Instance().gap().startAdvertising(); |
f3d | 0:8fa1c7356f71 | 36 | } |
f3d | 0:8fa1c7356f71 | 37 | |
f3d | 0:8fa1c7356f71 | 38 | /** |
f3d | 0:8fa1c7356f71 | 39 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
f3d | 0:8fa1c7356f71 | 40 | * |
f3d | 0:8fa1c7356f71 | 41 | * @param[in] params |
f3d | 0:8fa1c7356f71 | 42 | * Information about the characterisitc being updated. |
f3d | 0:8fa1c7356f71 | 43 | */ |
f3d | 0:8fa1c7356f71 | 44 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
f3d | 0:8fa1c7356f71 | 45 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
f3d | 0:8fa1c7356f71 | 46 | myled = *(params->data); |
f3d | 0:8fa1c7356f71 | 47 | } |
f3d | 0:8fa1c7356f71 | 48 | } |
f3d | 0:8fa1c7356f71 | 49 | void onDataReadCallback(const GattReadCallbackParams *params) { |
f3d | 0:8fa1c7356f71 | 50 | |
f3d | 0:8fa1c7356f71 | 51 | } |
f3d | 0:8fa1c7356f71 | 52 | /** |
f3d | 0:8fa1c7356f71 | 53 | * This function is called when the ble initialization process has failed |
f3d | 0:8fa1c7356f71 | 54 | */ |
f3d | 0:8fa1c7356f71 | 55 | void onBleInitError(BLE &ble, ble_error_t error) |
f3d | 0:8fa1c7356f71 | 56 | { |
f3d | 0:8fa1c7356f71 | 57 | /* Initialization error handling should go here */ |
f3d | 0:8fa1c7356f71 | 58 | } |
f3d | 0:8fa1c7356f71 | 59 | |
f3d | 0:8fa1c7356f71 | 60 | /** |
f3d | 0:8fa1c7356f71 | 61 | * Callback triggered when the ble initialization process has finished |
f3d | 0:8fa1c7356f71 | 62 | */ |
f3d | 0:8fa1c7356f71 | 63 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
f3d | 0:8fa1c7356f71 | 64 | { |
f3d | 0:8fa1c7356f71 | 65 | BLE& ble = params->ble; |
f3d | 0:8fa1c7356f71 | 66 | ble_error_t error = params->error; |
f3d | 0:8fa1c7356f71 | 67 | |
f3d | 0:8fa1c7356f71 | 68 | if (error != BLE_ERROR_NONE) { |
f3d | 0:8fa1c7356f71 | 69 | /* In case of error, forward the error handling to onBleInitError */ |
f3d | 0:8fa1c7356f71 | 70 | onBleInitError(ble, error); |
f3d | 0:8fa1c7356f71 | 71 | return; |
f3d | 0:8fa1c7356f71 | 72 | } |
f3d | 0:8fa1c7356f71 | 73 | |
f3d | 0:8fa1c7356f71 | 74 | /* Ensure that it is the default instance of BLE */ |
f3d | 0:8fa1c7356f71 | 75 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
f3d | 0:8fa1c7356f71 | 76 | return; |
f3d | 0:8fa1c7356f71 | 77 | } |
f3d | 0:8fa1c7356f71 | 78 | |
f3d | 0:8fa1c7356f71 | 79 | ble.gap().onDisconnection(disconnectionCallback); |
f3d | 0:8fa1c7356f71 | 80 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
f3d | 0:8fa1c7356f71 | 81 | ble.gattServer().onDataRead(onDataReadCallback); |
f3d | 0:8fa1c7356f71 | 82 | bool initialValueForLEDCharacteristic = false; |
f3d | 0:8fa1c7356f71 | 83 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
f3d | 0:8fa1c7356f71 | 84 | int16_t initialValue = 0; |
f3d | 0:8fa1c7356f71 | 85 | AccelServicePtr = new accelService(ble,initialValue); |
f3d | 0:8fa1c7356f71 | 86 | |
f3d | 0:8fa1c7356f71 | 87 | /* setup advertising */ |
f3d | 0:8fa1c7356f71 | 88 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
f3d | 0:8fa1c7356f71 | 89 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
f3d | 0:8fa1c7356f71 | 90 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
f3d | 0:8fa1c7356f71 | 91 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
f3d | 0:8fa1c7356f71 | 92 | ble.gap().setAdvertisingInterval(100); /* 100ms. */ |
f3d | 0:8fa1c7356f71 | 93 | ble.gap().startAdvertising(); |
f3d | 0:8fa1c7356f71 | 94 | } |
f3d | 0:8fa1c7356f71 | 95 | oled Oled; |
f3d | 0:8fa1c7356f71 | 96 | int main(void) |
f3d | 0:8fa1c7356f71 | 97 | { |
f3d | 0:8fa1c7356f71 | 98 | |
f3d | 0:8fa1c7356f71 | 99 | int Count=0; |
f3d | 0:8fa1c7356f71 | 100 | Oled.begin(); |
f3d | 0:8fa1c7356f71 | 101 | Lis3dh.begin(); |
f3d | 0:8fa1c7356f71 | 102 | Oled.print(0,0 ,"Hello"); |
f3d | 0:8fa1c7356f71 | 103 | |
f3d | 0:8fa1c7356f71 | 104 | |
f3d | 0:8fa1c7356f71 | 105 | BLE &ble = BLE::Instance(); |
f3d | 0:8fa1c7356f71 | 106 | ble.init(bleInitComplete); |
f3d | 0:8fa1c7356f71 | 107 | |
f3d | 0:8fa1c7356f71 | 108 | while (ble.hasInitialized() == false) { |
f3d | 0:8fa1c7356f71 | 109 | Oled.print(0,0,"BLE init fail"); |
f3d | 0:8fa1c7356f71 | 110 | myled = 1; |
f3d | 0:8fa1c7356f71 | 111 | wait(0.2); |
f3d | 0:8fa1c7356f71 | 112 | myled = 0; |
f3d | 0:8fa1c7356f71 | 113 | wait(0.1); |
f3d | 0:8fa1c7356f71 | 114 | |
f3d | 0:8fa1c7356f71 | 115 | /* spin loop */ } |
f3d | 0:8fa1c7356f71 | 116 | Oled.print(0,0,"BLE init done"); |
f3d | 0:8fa1c7356f71 | 117 | while (true) { |
f3d | 0:8fa1c7356f71 | 118 | // update the values for the BLE interface |
f3d | 0:8fa1c7356f71 | 119 | AccelServicePtr->poll(); |
f3d | 0:8fa1c7356f71 | 120 | int x,y,z; |
f3d | 0:8fa1c7356f71 | 121 | x = 1; |
f3d | 0:8fa1c7356f71 | 122 | y = 2; |
f3d | 0:8fa1c7356f71 | 123 | z = 3; |
f3d | 0:8fa1c7356f71 | 124 | // show the values on the display |
f3d | 0:8fa1c7356f71 | 125 | if (Lis3dh.dataReady()) |
f3d | 0:8fa1c7356f71 | 126 | { |
f3d | 0:8fa1c7356f71 | 127 | Lis3dh.read(x,y,z); |
f3d | 0:8fa1c7356f71 | 128 | Oled.print(0,0,x/16); |
f3d | 0:8fa1c7356f71 | 129 | Oled.print(0,1,y/16); |
f3d | 0:8fa1c7356f71 | 130 | Oled.print(0,2,z/16); |
f3d | 0:8fa1c7356f71 | 131 | } |
f3d | 0:8fa1c7356f71 | 132 | ble.waitForEvent(); |
f3d | 0:8fa1c7356f71 | 133 | |
f3d | 0:8fa1c7356f71 | 134 | } |
f3d | 0:8fa1c7356f71 | 135 | } |