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.
Fork of BLE_RCBController by
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include "RCBController.h" 00004 00005 #define DBG 1 00006 00007 BLEDevice ble; 00008 Serial pc(USBTX, USBRX); 00009 /* LEDs for indication: */ 00010 DigitalOut ConnectStateLed(LED1); 00011 PwmOut ControllerStateLed(LED2); 00012 00013 00014 /* RCBController Service */ 00015 static const uint16_t RCBController_service_uuid = 0xFFF0; 00016 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; 00017 uint8_t RCBControllerPayload[10] = {0,}; 00018 00019 GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10, 00020 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 00021 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00022 GattCharacteristic *ControllerChars[] = {&ControllerChar}; 00023 GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); 00024 00025 RCBController controller; 00026 00027 void onConnected(uint16_t h) 00028 { 00029 ConnectStateLed = 0; 00030 #if DBG 00031 pc.printf("Connected\n\r"); 00032 #endif 00033 } 00034 00035 void onDisconnected(uint16_t h) 00036 { 00037 ble.startAdvertising(); 00038 ConnectStateLed = 1; 00039 #if DBG 00040 pc.printf("Disconnected\n\r"); 00041 #endif 00042 } 00043 00044 00045 // GattEvent 00046 void onDataWritten(uint16_t charHandle) 00047 { 00048 if (charHandle == ControllerChar.getHandle()) { 00049 uint16_t bytesRead; 00050 ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead); 00051 memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); 00052 #if DBG 00053 00054 pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4], 00055 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); 00056 #endif 00057 ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0; 00058 } 00059 00060 } 00061 00062 /**************************************************************************/ 00063 /*! 00064 @brief Program entry point 00065 */ 00066 /**************************************************************************/ 00067 int main(void) 00068 { 00069 #if DBG 00070 pc.printf("Start\n\r"); 00071 #endif 00072 00073 ble.init(); 00074 ble.onConnection(onConnected); 00075 ble.onDisconnection(onDisconnected); 00076 ble.onDataWritten(onDataWritten); 00077 00078 /* setup advertising */ 00079 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00080 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00081 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00082 (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1); 00083 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00084 (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid)); 00085 00086 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00087 ble.startAdvertising(); 00088 00089 ble.addService(RCBControllerService); 00090 00091 while (true) { 00092 ble.waitForEvent(); 00093 } 00094 } 00095
Generated on Tue Jul 12 2022 19:14:16 by
