
With this example the latency time of a simple toggle command can be measured. Central device is looking for a peripheral called LED, connects with it and toggles the LED periodically. The time until the service of the peripheral is updated, is measured as latency time
Dependencies: BLE_API mbed nRF51822
main.cpp
- Committer:
- Alexgerni
- Date:
- 2017-10-24
- Revision:
- 0:c201f85a188c
- Child:
- 1:e79b40b523b4
File content as of revision 0:c201f85a188c:
/* mbed Microcontroller Library * Copyright (c) 2006-2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "ble/BLE.h" #include "ble/DiscoveredCharacteristic.h" #include "LowPowerTimer.h" DigitalOut alivenessLED(p16, 1); DigitalOut gateLED(p15,1); //InterruptIn button(p9); Serial pc(p5, p4); Ticker ticker, loop; Timer latency; uint16_t latencyTime; uint8_t toggledValue = 0x1; uint8_t counter = 0; uint8_t randNumber = 5; uint8_t randCount = 10; bool connectionValid = false; static DiscoveredCharacteristic ledCharacteristic; static const char PEER_NAME[] = "LED"; void periodicCallback(void) { if(!connectionValid){ alivenessLED = !alivenessLED; /* Do blinky on LED1 while waiting for BLE events */ gateLED = 1; } counter += 1; } void buttonPressedCallback(void) { randCount = 0; if(!connectionValid){ //pc.printf("not connected \n"); return; } else{ if(counter > 5){ alivenessLED = 1; latency.reset(); latency.start(); toggledValue = toggledValue ^ 0x1; ledCharacteristic.write(1, &toggledValue); //pc.printf("LED toggled... \n"); } } } void randCallback(void) { randCount += 1; if(randCount > randNumber+1 && connectionValid){ buttonPressedCallback(); gateLED = !gateLED; } } void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { for (uint8_t i = 0; i < params->advertisingDataLen; ++i) { const uint8_t record_length = params->advertisingData[i]; if (record_length == 0) { continue; } const uint8_t type = params->advertisingData[i + 1]; const uint8_t* value = params->advertisingData + i + 2; const uint8_t value_length = record_length - 1; if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) { if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) { /* compares name LED with advertisingData*/ pc.printf( "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type ); BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); break; } } i += record_length; } } void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* LED characteristic of device (Read/Write) */ ledCharacteristic = *characteristicP; connectionValid = true; } } void triggerReadCallback(const GattWriteCallbackParams *response) { if (response->handle == ledCharacteristic.getValueHandle()) { ledCharacteristic.read(); latency.stop(); latencyTime = latency.read_us(); //pc.printf("ledCharacteristic: %d %d\r\n",response->data[0],response->data[1]); pc.printf("Latency Time: %d",latencyTime); pc.printf(" us \n"); //writeVal(latencyTime); randNumber = rand()%10; //number between 0 and 9 } } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { if (params->role == Gap::CENTRAL) { BLE &ble = BLE::Instance(); //ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); ble.gattClient().launchServiceDiscovery(params->handle, NULL, characteristicDiscoveryCallback, 0xa000, 0xa001); } } void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { pc.printf("disconnected\r\n"); /* Start scanning and try to connect again */ BLE::Instance().gap().startScan(advertisementCallback); connectionValid = false; counter = 0; } void onBleInitError(BLE &ble, ble_error_t error) { /* Initialization error handling should go here */ } void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error; if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ onBleInitError(ble, error); return; } /* Ensure that it is the default instance of BLE */ if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } ble.gap().onDisconnection(disconnectionCallback); ble.gap().onConnection(connectionCallback); ble.gattClient().onDataWrite(triggerReadCallback); // scan interval: 400ms and scan window: 400ms. // Every 400ms the device will scan for 400ms // This means that the device will scan continuously. ble.gap().setScanParams(400, 400); ble.gap().startScan(advertisementCallback); } int main(void) { pc.baud(115200); pc.printf("Initialization starts... \n"); ticker.attach(periodicCallback, 1); /* Blink LED every second */ //button.rise(buttonPressedCallback); loop.attach(randCallback, 0.1); srand(time(NULL)); BLE &ble = BLE::Instance(); ble.init(bleInitComplete); counter = 0; latency.reset(); while (true) { ble.waitForEvent(); } }