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 servo1(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 const int deg0msec = 600; // msec. 00044 const int deg180msec = 2200; // msec. 00045 00046 //In float degree -90 to +90 00047 void SetServoDegree(float degree) 00048 { 00049 float range = (float)(deg180msec - deg0msec) / 180; 00050 float pwitdh = (float)((degree+90)*range)+(float)deg0msec; 00051 servo1.pulsewidth(pwitdh/1000000); 00052 } 00053 00054 // GattEvent 00055 void onDataWritten(uint16_t charHandle) 00056 { 00057 if (charHandle == ControllerChar.getHandle()) { 00058 uint16_t bytesRead; 00059 ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead); 00060 memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); 00061 #if DBG 00062 00063 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], 00064 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); 00065 #endif 00066 00067 if (controller.data[0] == 1){ 00068 SetServoDegree(-45); 00069 } 00070 else if (controller.data[1] == 0x40){ 00071 SetServoDegree(45); 00072 } 00073 else if (controller.data[1] == 0 && controller.data[0] == 0){ 00074 SetServoDegree(0); 00075 } 00076 } 00077 } 00078 00079 /**************************************************************************/ 00080 /*! 00081 @brief Program entry point 00082 */ 00083 /**************************************************************************/ 00084 int main(void) 00085 { 00086 #if DBG 00087 ConnectStateLed = 1; 00088 pc.baud(115200); 00089 pc.printf("Start\n\r"); 00090 #endif 00091 00092 ble.init(); 00093 ble.onConnection(onConnected); 00094 ble.onDisconnection(onDisconnected); 00095 ble.onDataWritten(onDataWritten); 00096 00097 /* setup advertising */ 00098 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00099 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00100 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00101 (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1); 00102 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00103 (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid)); 00104 00105 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00106 ble.startAdvertising(); 00107 00108 ble.addService(RCBControllerService); 00109 servo1.period_ms(20); 00110 SetServoDegree(0); 00111 00112 while (true) { 00113 ble.waitForEvent(); 00114 } 00115 } 00116
Generated on Tue Jul 12 2022 19:06:03 by
