Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Nucleo_BLE_API Nucleo_BLE_BlueNRG mbed
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 "BLEDevice.h" 00019 #include "DeviceInformationService.h" 00020 #include "UARTService.h" 00021 #include "Utils.h" 00022 00023 const static char DEVICE_NAME[] = "BlueNRG_UART"; 00024 uint8_t c = 'A'; 00025 00026 extern bool user_button_pressed; 00027 bool connected = false; 00028 bool UpdatedEnabled = false; 00029 00030 BLEDevice ble; 00031 UARTService *uartServicePtr; 00032 00033 /* Callback called when the device is disconnected */ 00034 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00035 { 00036 DEBUG("Disconnected!\n\r"); 00037 DEBUG("Restarting the advertising process\n\r"); 00038 00039 ble.startAdvertising(); 00040 connected = false; 00041 } 00042 00043 /* Callback called when the device is connected */ 00044 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason) 00045 { 00046 DEBUG("Connected\r\n"); 00047 00048 connected = true; 00049 } 00050 00051 /* Not working */ 00052 void onDataSent(unsigned count) 00053 { 00054 DEBUG("onDataSent\r\n"); 00055 } 00056 00057 /* Not working */ 00058 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00059 { 00060 DEBUG("--onDataWritten\r\n"); 00061 DEBUG("---charHandle : %d\r\n", params->charHandle); 00062 DEBUG("---getTXCharacteristicHandle: %d\r\n", uartServicePtr->getTXCharacteristicHandle()); 00063 DEBUG("---len: %d\r\n", params->len); 00064 00065 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) 00066 { 00067 DEBUG("received %u bytes\n\r", params->len); 00068 00069 if(params->data[0] == 0x00) 00070 { 00071 //Do something 00072 } 00073 else 00074 { 00075 //Do something else 00076 } 00077 } 00078 } 00079 00080 /* Callback called when the client enables updates */ 00081 void onUpdatesEnabled(uint16_t attributeHandle) 00082 { 00083 DEBUG("**onUpdatesEnabled**\r\n"); 00084 UpdatedEnabled = true; 00085 } 00086 00087 /* Callback called when the client disable updates */ 00088 void onUpdatesDisabled(uint16_t attributeHandle) 00089 { 00090 DEBUG("**onUpdatesDisabled**\r\n"); 00091 UpdatedEnabled = false; 00092 } 00093 00094 /* Main */ 00095 int main(void) 00096 { 00097 DEBUG("Initialising \n\r"); 00098 ble.init(); 00099 #if 1 00100 /* Set callback functions */ 00101 ble.onDisconnection(disconnectionCallback); 00102 ble.onConnection(connectionCallback); 00103 ble.onDataWritten(onDataWritten); 00104 ble.onDataSent(onDataSent); 00105 ble.onUpdatesEnabled(onUpdatesEnabled); 00106 ble.onUpdatesDisabled(onUpdatesDisabled); 00107 00108 DeviceInformationService deviceInfo(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00109 /* setup advertising */ 00110 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00111 ble.setAdvertisingType (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00112 00113 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME , (const uint8_t *)"BlueNRG_UART" , sizeof("BlueNRG_UART") - 1); 00114 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00115 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME , sizeof(DEVICE_NAME)); 00116 00117 /* Start advertising */ 00118 ble.setAdvertisingInterval(160); 00119 ble.startAdvertising(); 00120 00121 UARTService uartService(ble); 00122 uartServicePtr = &uartService; 00123 00124 00125 while (true) 00126 { 00127 ble.waitForEvent(); 00128 00129 if(connected == true) 00130 { 00131 if ((user_button_pressed == true) && (UpdatedEnabled == true)) 00132 { 00133 user_button_pressed = false; 00134 DEBUG("Current Char: %c\r\n",c); 00135 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), &c, 1); 00136 00137 c++; 00138 00139 if(c == ('Z'+1)) 00140 { 00141 c = 'A'; 00142 } 00143 } 00144 } 00145 } 00146 #else 00147 while (true) 00148 { 00149 if (user_button_pressed == true) 00150 { 00151 user_button_pressed = false; 00152 DEBUG("Current Char: %c\r\n",c); 00153 00154 c++; 00155 00156 if(c == ('Z'+1)) 00157 { 00158 c = 'A'; 00159 } 00160 } 00161 } 00162 #endif 00163 }
Generated on Tue Jul 12 2022 14:04:50 by
1.7.2