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
main.cpp
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 #include "BatteryService.h" 00004 #include "DeviceInformationService.h" 00005 #include "HIDService.h" 00006 00007 BLEDevice ble; 00008 00009 Serial uart(USBTX, USBRX); 00010 DigitalOut led01(LED1); 00011 DigitalOut RFSWIO(LED2); 00012 Ticker flipper; 00013 DigitalIn btn01(P0_17); 00014 DigitalIn btn02(P0_16); 00015 DigitalIn btn03(P0_21); 00016 00017 bool flip_lock = false; 00018 00019 void flip() { 00020 if (!flip_lock) RFSWIO = !RFSWIO; 00021 } 00022 00023 unsigned char keyData; 00024 static const char DEVICE_NAME[] = "HID_Android_ShortCutKey"; 00025 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE}; 00026 static volatile bool triggerSensorPolling = false; 00027 00028 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00029 { 00030 ble.startAdvertising(); // restart advertising 00031 } 00032 00033 char msg[25] = "NNN40 CONNECTED\n"; 00034 int main(void) 00035 { 00036 btn01.mode(PullUp); 00037 btn02.mode(PullUp); 00038 btn03.mode(PullUp); 00039 00040 uart.baud(115200); 00041 uart.printf("Starting HID Service\r\n"); 00042 RFSWIO = 1; 00043 led01 = 1; 00044 memset(msg, 0, 25); 00045 /*======BLE setup========*/ 00046 ble.init(); 00047 bool enableBonding = true; 00048 bool requireMITM = true; 00049 ble.initializeSecurity(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE); //IO_CAPS_DISPLAY_ONLY, IO_CAPS_NONE 00050 ble.onDisconnection(disconnectionCallback); 00051 00052 /* Setup primary service. */ 00053 HIDService hidService(ble); 00054 /* Setup auxiliary service. */ 00055 DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00056 /* Setup advertising. */ 00057 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00058 ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); 00059 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00060 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00061 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00062 ble.setAdvertisingInterval(1000); 00063 00064 RFSWIO = 1; 00065 ble.startAdvertising(); 00066 uart.printf("Starting advertising\r\n"); 00067 wait(5); 00068 flipper.attach(&flip, 0.15); 00069 00070 while (1) { 00071 if(ble.getGapState().connected){ 00072 if(!btn01){ 00073 hidService.updateReport(0x04, 0x2B);//0x04 ALT(LEFT) 0x2B Keyboard TAB 00074 wait(0.03); 00075 hidService.updateReport(0x04, 0x00);//0x04 ALT(LEFT) 0x2B 00076 wait(0.5); 00077 } 00078 if(!btn02){ 00079 hidService.updateReport(0x00, 0x28);//0x28 Keyboard RETURN(Enter) 00080 wait(0.03); 00081 hidService.updateReport(0x00, 0x00); 00082 wait(0.5); 00083 } 00084 if(!btn03){ 00085 hidService.updateReport(0x08, 0x07);//0x08 Left GUI 0x07 Keyboard d 00086 wait(0.03); 00087 hidService.updateReport(0x00, 0x00); 00088 wait(0.5); 00089 } 00090 } 00091 } 00092 }
Generated on Fri Jul 15 2022 01:56:10 by
1.7.2