работает в паре с micro:bit мигалкой

Dependencies:   mbed BLE_API nRF51822

Программа для управляющего устройства на nRF51822. Используется одна Button1 для включения\выключения светодиода на устройстве приемнике.

Committer:
mamont090671
Date:
Sat Dec 14 07:01:45 2019 +0000
Revision:
12:1cd9fd69a4f4
Parent:
11:023d96b0e427
Child:
13:7b6d69a11fb5
nRF51822 master

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:415d7f24cb91 1 /* mbed Microcontroller Library
rgrover1 0:415d7f24cb91 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:415d7f24cb91 3 *
rgrover1 0:415d7f24cb91 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:415d7f24cb91 5 * you may not use this file except in compliance with the License.
rgrover1 0:415d7f24cb91 6 * You may obtain a copy of the License at
rgrover1 0:415d7f24cb91 7 *
rgrover1 0:415d7f24cb91 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:415d7f24cb91 9 *
rgrover1 0:415d7f24cb91 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:415d7f24cb91 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:415d7f24cb91 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:415d7f24cb91 13 * See the License for the specific language governing permissions and
rgrover1 0:415d7f24cb91 14 * limitations under the License.
rgrover1 0:415d7f24cb91 15 */
rgrover1 0:415d7f24cb91 16
rgrover1 0:415d7f24cb91 17 #include "mbed.h"
rgrover1 6:1730f66fb14d 18 #include "ble/BLE.h"
rgrover1 6:1730f66fb14d 19 #include "ble/DiscoveredCharacteristic.h"
rgrover1 6:1730f66fb14d 20 #include "ble/DiscoveredService.h"
rgrover1 0:415d7f24cb91 21
andresag 11:023d96b0e427 22 DigitalOut alivenessLED(LED1, 1);
mamont090671 12:1cd9fd69a4f4 23 Serial pc(USBTX, USBRX);
rgrover1 6:1730f66fb14d 24
andresag 11:023d96b0e427 25 bool triggerLedCharacteristic = false;
rgrover1 0:415d7f24cb91 26 DiscoveredCharacteristic ledCharacteristic;
rgrover1 0:415d7f24cb91 27
mamont090671 12:1cd9fd69a4f4 28 BLE ble;
andresag 11:023d96b0e427 29 Ticker ticker;
andresag 11:023d96b0e427 30
rgrover1 0:415d7f24cb91 31 void periodicCallback(void) {
rgrover1 3:146eba831693 32 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 0:415d7f24cb91 33 }
rgrover1 0:415d7f24cb91 34
rgrover1 0:415d7f24cb91 35 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
mamont090671 12:1cd9fd69a4f4 36 /* if (params->peerAddr[0] != 0x37) { // !ALERT! Alter this filter to suit your device.
rgrover1 0:415d7f24cb91 37 return;
mamont090671 12:1cd9fd69a4f4 38 }*/
rgrover1 0:415d7f24cb91 39 printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
rgrover1 0:415d7f24cb91 40 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
rgrover1 0:415d7f24cb91 41 params->rssi, params->isScanResponse, params->type);
rgrover1 0:415d7f24cb91 42
andresag 11:023d96b0e427 43 BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
rgrover1 0:415d7f24cb91 44 }
rgrover1 0:415d7f24cb91 45
rgrover1 0:415d7f24cb91 46 void serviceDiscoveryCallback(const DiscoveredService *service) {
rgrover1 0:415d7f24cb91 47 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
rgrover1 0:415d7f24cb91 48 printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
rgrover1 0:415d7f24cb91 49 } else {
rgrover1 0:415d7f24cb91 50 printf("S UUID-");
rgrover1 0:415d7f24cb91 51 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
rgrover1 0:415d7f24cb91 52 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
rgrover1 0:415d7f24cb91 53 printf("%02x", longUUIDBytes[i]);
rgrover1 0:415d7f24cb91 54 }
rgrover1 0:415d7f24cb91 55 printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
rgrover1 0:415d7f24cb91 56 }
rgrover1 0:415d7f24cb91 57 }
rgrover1 0:415d7f24cb91 58
rgrover1 0:415d7f24cb91 59 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
rgrover1 10:507318f2afda 60 printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
rgrover1 10:507318f2afda 61 if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
rgrover1 1:1db45b17552e 62 ledCharacteristic = *characteristicP;
rgrover1 1:1db45b17552e 63 triggerLedCharacteristic = true;
rgrover1 0:415d7f24cb91 64 }
rgrover1 0:415d7f24cb91 65 }
rgrover1 0:415d7f24cb91 66
rgrover1 0:415d7f24cb91 67 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
rgrover1 0:415d7f24cb91 68 printf("terminated SD for handle %u\r\n", connectionHandle);
rgrover1 0:415d7f24cb91 69 }
rgrover1 0:415d7f24cb91 70
rgrover1 0:415d7f24cb91 71 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
rgrover1 0:415d7f24cb91 72 if (params->role == Gap::CENTRAL) {
andresag 11:023d96b0e427 73 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
andresag 11:023d96b0e427 74 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
rgrover1 0:415d7f24cb91 75 }
rgrover1 0:415d7f24cb91 76 }
rgrover1 0:415d7f24cb91 77
mamont090671 12:1cd9fd69a4f4 78 //Вызывается событием "onDataRead"
rgrover1 0:415d7f24cb91 79 void triggerToggledWrite(const GattReadCallbackParams *response) {
rgrover1 0:415d7f24cb91 80 if (response->handle == ledCharacteristic.getValueHandle()) {
mamont090671 12:1cd9fd69a4f4 81 //#if DUMP_READ_DATA
rgrover1 0:415d7f24cb91 82 printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
rgrover1 0:415d7f24cb91 83 for (unsigned index = 0; index < response->len; index++) {
rgrover1 0:415d7f24cb91 84 printf("%c[%02x]", response->data[index], response->data[index]);
rgrover1 0:415d7f24cb91 85 }
rgrover1 0:415d7f24cb91 86 printf("\r\n");
mamont090671 12:1cd9fd69a4f4 87 //#endif
mamont090671 12:1cd9fd69a4f4 88 //пишем в характеристику 0х00 или 0х01, т.е. моргаем диодом дистанционно
rgrover1 0:415d7f24cb91 89 uint8_t toggledValue = response->data[0] ^ 0x1;
rgrover1 0:415d7f24cb91 90 ledCharacteristic.write(1, &toggledValue);
mamont090671 12:1cd9fd69a4f4 91 printf("onDataRead\r\ntoggledValue: %02x\r\n", toggledValue);
rgrover1 0:415d7f24cb91 92 }
rgrover1 0:415d7f24cb91 93 }
rgrover1 0:415d7f24cb91 94
mamont090671 12:1cd9fd69a4f4 95 //Вызывается событием "onDataWrite"
rgrover1 0:415d7f24cb91 96 void triggerRead(const GattWriteCallbackParams *response) {
rgrover1 0:415d7f24cb91 97 if (response->handle == ledCharacteristic.getValueHandle()) {
rgrover1 0:415d7f24cb91 98 ledCharacteristic.read();
mamont090671 12:1cd9fd69a4f4 99 printf("onDataWrite\r\n");
rgrover1 0:415d7f24cb91 100 }
rgrover1 0:415d7f24cb91 101 }
mamont090671 12:1cd9fd69a4f4 102 /*
mamont090671 12:1cd9fd69a4f4 103 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {Gap::Handle_t handle, Gap::DisconnectionReason_t reason
mamont090671 12:1cd9fd69a4f4 104 printf("disconnected\r\n");
mamont090671 12:1cd9fd69a4f4 105 }*/
rgrover1 0:415d7f24cb91 106
rgrover1 10:507318f2afda 107 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
mamont090671 12:1cd9fd69a4f4 108 // BLE& ble = params->ble;
rgrover1 0:415d7f24cb91 109 printf("disconnected\r\n");
mamont090671 12:1cd9fd69a4f4 110 pc.printf("Rescan\r\n");
mamont090671 12:1cd9fd69a4f4 111 ble.gap().startScan(advertisementCallback);
rgrover1 0:415d7f24cb91 112 }
rgrover1 0:415d7f24cb91 113
andresag 11:023d96b0e427 114 /**
andresag 11:023d96b0e427 115 * This function is called when the ble initialization process has failed
andresag 11:023d96b0e427 116 */
andresag 11:023d96b0e427 117 void onBleInitError(BLE &ble, ble_error_t error)
andresag 11:023d96b0e427 118 {
andresag 11:023d96b0e427 119 /* Initialization error handling should go here */
andresag 11:023d96b0e427 120 }
rgrover1 0:415d7f24cb91 121
andresag 11:023d96b0e427 122 /**
andresag 11:023d96b0e427 123 * Callback triggered when the ble initialization process has finished
andresag 11:023d96b0e427 124 */
andresag 11:023d96b0e427 125 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 11:023d96b0e427 126 {
andresag 11:023d96b0e427 127 BLE& ble = params->ble;
andresag 11:023d96b0e427 128 ble_error_t error = params->error;
andresag 11:023d96b0e427 129
andresag 11:023d96b0e427 130 if (error != BLE_ERROR_NONE) {
andresag 11:023d96b0e427 131 /* In case of error, forward the error handling to onBleInitError */
andresag 11:023d96b0e427 132 onBleInitError(ble, error);
andresag 11:023d96b0e427 133 return;
andresag 11:023d96b0e427 134 }
andresag 11:023d96b0e427 135
andresag 11:023d96b0e427 136 /* Ensure that it is the default instance of BLE */
andresag 11:023d96b0e427 137 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 11:023d96b0e427 138 return;
andresag 11:023d96b0e427 139 }
andresag 11:023d96b0e427 140
rgrover1 5:3bbad34d1a85 141 ble.gap().onConnection(connectionCallback);
rgrover1 5:3bbad34d1a85 142 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 0:415d7f24cb91 143
rgrover1 7:61e2224ec9a0 144 ble.gattClient().onDataRead(triggerToggledWrite);
rgrover1 7:61e2224ec9a0 145 ble.gattClient().onDataWrite(triggerRead);
rgrover1 0:415d7f24cb91 146
rgrover1 5:3bbad34d1a85 147 ble.gap().setScanParams(500, 400);
rgrover1 5:3bbad34d1a85 148 ble.gap().startScan(advertisementCallback);
andresag 11:023d96b0e427 149 }
andresag 11:023d96b0e427 150
andresag 11:023d96b0e427 151 int main(void) {
mamont090671 12:1cd9fd69a4f4 152 pc.baud(9600);
mamont090671 12:1cd9fd69a4f4 153 printf("--- Start!!! ---\r\n");
mamont090671 12:1cd9fd69a4f4 154
andresag 11:023d96b0e427 155 ticker.attach(periodicCallback, 1);
andresag 11:023d96b0e427 156
andresag 11:023d96b0e427 157 BLE &ble = BLE::Instance();
andresag 11:023d96b0e427 158 ble.init(bleInitComplete);
andresag 11:023d96b0e427 159
andresag 11:023d96b0e427 160 /* SpinWait for initialization to complete. This is necessary because the
andresag 11:023d96b0e427 161 * BLE object is used in the main loop below. */
andresag 11:023d96b0e427 162 while (ble.hasInitialized() == false) { /* spin loop */ }
rgrover1 0:415d7f24cb91 163
rgrover1 0:415d7f24cb91 164 while (true) {
rgrover1 4:460ce53dc854 165 if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
rgrover1 1:1db45b17552e 166 triggerLedCharacteristic = false;
rgrover1 0:415d7f24cb91 167 ledCharacteristic.read(); /* We could have issued this read just as easily from
rgrover1 0:415d7f24cb91 168 * characteristicDiscoveryCallback(); but
rgrover1 2:3e1e967035cb 169 * invoking it here demonstrates the use
rgrover1 2:3e1e967035cb 170 * of isServiceDiscoveryActive() and also
rgrover1 2:3e1e967035cb 171 * the fact that it is permitted to
rgrover1 2:3e1e967035cb 172 * operate on application-local copies of
rgrover1 2:3e1e967035cb 173 * DiscoveredCharacteristic. */
rgrover1 0:415d7f24cb91 174 }
rgrover1 0:415d7f24cb91 175 ble.waitForEvent();
rgrover1 0:415d7f24cb91 176 }
rgrover1 0:415d7f24cb91 177 }