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: BLE_API mbed nRF51822
Fork of BLE_GATT_test1 by
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include "BroadcasterService.h" 00004 #include <string> 00005 00006 using namespace std; 00007 00008 BLEDevice ble; 00009 DigitalOut led1(LED1); 00010 InterruptIn indicatorIn(p5); 00011 InterruptIn brakeIn(p2); 00012 Serial pc(USBTX, USBRX); 00013 00014 /*Variable Declarations*/ 00015 const static char DEVICE_NAME[] = "BLE_CarPeripheral"; 00016 uint8_t cmdIndicatorOn[8] = { 0x69,0x6e,0x64,0x69,0xff,0xff,0xff,0xff }; // = I N D I 255 255 255 255 00017 uint8_t cmdIndicatorOff[8] = { 0x69,0x6e,0x64,0x69,0x00,0x00,0x00,0x00 }; // = I N D I 0 0 0 0 00018 uint8_t cmdBrakeOn[8] = { 0x42, 0x52, 0x41, 0x4b, 0xff,0xff,0xff,0xff }; // = B R A K 255 255 255 255 00019 uint8_t cmdBrakeOff[8] = { 0x42, 0x52, 0x41, 0x4b,0x00,0x00,0x00,0x00 }; // = B R A K 0 0 0 0 00020 static const uint16_t uuid16_list[] = {BroadcasterService::BROADCAST_SERVICE_UUID}; 00021 00022 BroadcasterService* broadcasterService; 00023 00024 /** Callback function for ticker */ 00025 void blink(void) 00026 { 00027 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00028 } 00029 00030 /** Callback routine is interrupt activated by a debounced button hit*/ 00031 void indicatorSwitchOn(void) { 00032 pc.printf("Command Indicator on\r\n"); 00033 broadcasterService->sendCommand (cmdIndicatorOn); 00034 } 00035 00036 /** Callback routine is interrupt activated by a debounced button release*/ 00037 void indicatorSwitchOff(void) { 00038 pc.printf("Command Indicator off\r\n"); 00039 broadcasterService->sendCommand (cmdIndicatorOff); 00040 } 00041 00042 /** Callback routine is interrupt activated by a debounced button hit*/ 00043 void brakeOn(void) { 00044 pc.printf("Command Brake on\r\n"); 00045 broadcasterService->sendCommand (cmdBrakeOn); 00046 } 00047 00048 /** Callback routine is interrupt activated by a debounced button release*/ 00049 void brakeOff(void) { 00050 pc.printf("Command Brake off\r\n"); 00051 broadcasterService->sendCommand (cmdBrakeOff); 00052 } 00053 00054 /** Callback function for BLE disconnection */ 00055 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00056 { 00057 pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason); 00058 ble.startAdvertising(); 00059 } 00060 00061 /** Callback function for BLE connection */ 00062 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms) 00063 { 00064 pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Addr:%u\r\n", handle, peerAddrType, peerAddr); 00065 ble.stopAdvertising(); 00066 } 00067 00068 /** Callback function for error ticker*/ 00069 void raiseGenericError() 00070 { 00071 pc.printf("Error raised - %u\r\n", BroadcasterService::ERROR_GENERIC); 00072 broadcasterService->registerError(BroadcasterService::ERROR_GENERIC); 00073 } 00074 00075 int main(void) 00076 { 00077 /*Setup switch input*/ 00078 indicatorIn.mode(PullUp); 00079 brakeIn.mode(PullUp); 00080 // Delay for initial pullup to take effect 00081 wait(.01); 00082 // Setup Interrupt callback function for a button hit 00083 indicatorIn.fall(&indicatorSwitchOn); 00084 indicatorIn.rise(&indicatorSwitchOff); 00085 00086 brakeIn.fall(&brakeOn); 00087 brakeIn.rise(&brakeOff); 00088 00089 /*Setup Blinky*/ 00090 led1 = 1; 00091 Ticker t; 00092 t.attach(&blink, 1); 00093 00094 /*Setup random error timer*/ 00095 int interval = rand() % 100 + 10; 00096 Ticker errTicker; 00097 errTicker.attach(&raiseGenericError, interval); 00098 00099 /*Setup BLE*/ 00100 ble.init(); 00101 ble.onDisconnection(disconnectionCallback); 00102 ble.onConnection(connectionCallback); 00103 00104 //create services 00105 broadcasterService = new BroadcasterService(ble); 00106 00107 /* 00108 * BREDR_NOT_SUPPORTED = BLE only 00109 * LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out) 00110 * ADV_CONNECTABLE_UNDIRECTED = Any central device can connect 00111 */ 00112 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00113 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00114 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00115 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00116 00117 ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ 00118 ble.startAdvertising(); 00119 00120 pc.printf("Advertising node %s\n\r", DEVICE_NAME); 00121 00122 //loop forever 00123 while(true) { 00124 ble.waitForEvent(); // this should return upon any system event (such as an interrupt or a ticker wakeup) 00125 } 00126 }
Generated on Sun Jul 17 2022 08:25:34 by
1.7.2
