sdfgh
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 00020 #include "ble/services/UARTService.h" 00021 00022 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; 00023 * it will have an impact on code-size and power consumption. */ 00024 00025 #if NEED_CONSOLE_OUTPUT 00026 #define DEBUG(...) { printf(__VA_ARGS__); } 00027 #else 00028 #define DEBUG(...) /* nothing */ 00029 #endif /* #if NEED_CONSOLE_OUTPUT */ 00030 00031 BLEDevice ble; 00032 DigitalOut led1(LED1); 00033 00034 UARTService *uartServicePtr; 00035 00036 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00037 { 00038 DEBUG("Disconnected!\n\r"); 00039 DEBUG("Restarting the advertising process\n\r"); 00040 ble.startAdvertising(); 00041 } 00042 00043 void onDataWritten(const GattWriteCallbackParams *params) 00044 { 00045 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { 00046 uint16_t bytesRead = params->len; 00047 DEBUG("received %u bytes\n\r", bytesRead); 00048 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00049 } 00050 } 00051 00052 void periodicCallback(void) 00053 { 00054 led1 = !led1; 00055 } 00056 00057 int main(void) 00058 { 00059 led1 = 1; 00060 Ticker ticker; 00061 ticker.attach(periodicCallback, 1); 00062 00063 DEBUG("Initialising the nRF51822\n\r"); 00064 ble.init(); 00065 ble.onDisconnection(disconnectionCallback); 00066 ble.onDataWritten(onDataWritten); 00067 00068 /* setup advertising */ 00069 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00070 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00071 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00072 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); 00073 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00074 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00075 00076 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ 00077 ble.startAdvertising(); 00078 00079 UARTService uartService(ble); 00080 uartServicePtr = &uartService; 00081 00082 while (true) { 00083 ble.waitForEvent(); 00084 } 00085 }
Generated on Mon Jul 18 2022 20:42:40 by 1.7.2