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: mbed BLE_API nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 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 00019 #include "ble/BLE.h" 00020 #include "KeyboardService.h" 00021 00022 #include "examples_common.h" 00023 00024 /** 00025 * This program implements a complete HID-over-Gatt Profile: 00026 * - HID is provided by KeyboardService 00027 * - Battery Service 00028 * - Device Information Service 00029 * 00030 * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string 00031 * will take about 500ms to transmit, principally because of the limited notification rate in BLE. 00032 * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail 00033 * once this buffer is full. This will result in partial strings being sent to the client. 00034 */ 00035 00036 //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it 00037 DigitalOut col9(P0_12, 0); 00038 00039 DigitalOut waiting_led(P0_13); 00040 DigitalOut connected_led(P0_15); 00041 00042 InterruptIn button1(BUTTON_A); 00043 InterruptIn button2(BUTTON_B); 00044 00045 BLE ble; 00046 KeyboardService *kbdServicePtr; 00047 00048 static const char DEVICE_NAME[] = "micro:bit Foot Paddle"; 00049 static const char SHORT_DEVICE_NAME[] = "footpaddle"; 00050 00051 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) 00052 { 00053 HID_DEBUG("disconnected\r\n"); 00054 connected_led = 0; 00055 00056 ble.gap().startAdvertising(); // restart advertising 00057 } 00058 00059 static void onConnect(const Gap::ConnectionCallbackParams_t *params) 00060 { 00061 HID_DEBUG("connected\r\n"); 00062 waiting_led = false; 00063 } 00064 00065 static void waiting() { 00066 if (!kbdServicePtr->isConnected()) 00067 waiting_led = !waiting_led; 00068 else 00069 connected_led = !connected_led; 00070 } 00071 00072 void send_string(const char * c) { 00073 if (!kbdServicePtr) 00074 return; 00075 00076 if (!kbdServicePtr->isConnected()) { 00077 HID_DEBUG("we haven't connected yet..."); 00078 } else { 00079 int len = strlen(c); 00080 kbdServicePtr->printf(c); 00081 HID_DEBUG("sending %d chars: %s\r\n", len, c); 00082 } 00083 } 00084 00085 void send_ctrl_shift_alt(const uint8_t key) { 00086 if (!kbdServicePtr) 00087 return; 00088 00089 if (!kbdServicePtr->isConnected()) { 00090 HID_DEBUG("we haven't connected yet..."); 00091 } else { 00092 kbdServicePtr->keyDownCode(key, KEY_CTRL + KEY_SHIFT + KEY_ALT); 00093 HID_DEBUG("sending 0x%X\r\n", key); 00094 kbdServicePtr->keyUpCode(); 00095 } 00096 } 00097 void pressed_button1() { 00098 send_ctrl_shift_alt(0x8A); 00099 wait(0.1); 00100 00101 } 00102 00103 void pressed_button2() { 00104 send_ctrl_shift_alt(0x8B); 00105 wait(0.1); 00106 00107 } 00108 00109 int main() 00110 { 00111 Ticker heartbeat; 00112 00113 button1.rise(pressed_button1); 00114 button2.rise(pressed_button2); 00115 00116 HID_DEBUG("initialising ticker\r\n"); 00117 00118 heartbeat.attach(waiting, 1); 00119 00120 HID_DEBUG("initialising ble\r\n"); 00121 ble.init(); 00122 00123 ble.gap().onDisconnection(onDisconnect); 00124 ble.gap().onConnection(onConnect); 00125 00126 initializeSecurity(ble); 00127 00128 HID_DEBUG("adding hid service\r\n"); 00129 KeyboardService kbdService(ble); 00130 kbdServicePtr = &kbdService; 00131 00132 HID_DEBUG("adding device info and battery service\r\n"); 00133 initializeHOGP(ble); 00134 00135 HID_DEBUG("setting up gap\r\n"); 00136 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); 00137 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 00138 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00139 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00140 (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); 00141 ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); 00142 00143 HID_DEBUG("advertising\r\n"); 00144 ble.gap().startAdvertising(); 00145 00146 while (true) { 00147 ble.waitForEvent(); 00148 } 00149 } 00150
Generated on Mon Jul 18 2022 09:17:47 by
