Based on the example USB HID keyboard https://developer.mbed.org/users/jbru/code/BLE_HID_KeyboardStreamDemo/

Dependencies:   BLE_API mbed nRF51822

Fork of HID-kb by Microbug

Committer:
JonnyA
Date:
Mon Nov 02 18:25:58 2015 +0000
Revision:
0:cb1939018833
Child:
1:9e174f8fd9e9
Rename the keyboard

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonnyA 0:cb1939018833 1 /* mbed Microcontroller Library
JonnyA 0:cb1939018833 2 * Copyright (c) 2015 ARM Limited
JonnyA 0:cb1939018833 3 *
JonnyA 0:cb1939018833 4 * Licensed under the Apache License, Version 2.0 (the "License");
JonnyA 0:cb1939018833 5 * you may not use this file except in compliance with the License.
JonnyA 0:cb1939018833 6 * You may obtain a copy of the License at
JonnyA 0:cb1939018833 7 *
JonnyA 0:cb1939018833 8 * http://www.apache.org/licenses/LICENSE-2.0
JonnyA 0:cb1939018833 9 *
JonnyA 0:cb1939018833 10 * Unless required by applicable law or agreed to in writing, software
JonnyA 0:cb1939018833 11 * distributed under the License is distributed on an "AS IS" BASIS,
JonnyA 0:cb1939018833 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JonnyA 0:cb1939018833 13 * See the License for the specific language governing permissions and
JonnyA 0:cb1939018833 14 * limitations under the License.
JonnyA 0:cb1939018833 15 */
JonnyA 0:cb1939018833 16
JonnyA 0:cb1939018833 17 #include "mbed.h"
JonnyA 0:cb1939018833 18
JonnyA 0:cb1939018833 19 #include "ble/BLE.h"
JonnyA 0:cb1939018833 20 #include "KeyboardService.h"
JonnyA 0:cb1939018833 21
JonnyA 0:cb1939018833 22 #include "examples_common.h"
JonnyA 0:cb1939018833 23
JonnyA 0:cb1939018833 24 /**
JonnyA 0:cb1939018833 25 * This program implements a complete HID-over-Gatt Profile:
JonnyA 0:cb1939018833 26 * - HID is provided by KeyboardService
JonnyA 0:cb1939018833 27 * - Battery Service
JonnyA 0:cb1939018833 28 * - Device Information Service
JonnyA 0:cb1939018833 29 *
JonnyA 0:cb1939018833 30 * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string
JonnyA 0:cb1939018833 31 * will take about 500ms to transmit, principally because of the limited notification rate in BLE.
JonnyA 0:cb1939018833 32 * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail
JonnyA 0:cb1939018833 33 * once this buffer is full. This will result in partial strings being sent to the client.
JonnyA 0:cb1939018833 34 */
JonnyA 0:cb1939018833 35
JonnyA 0:cb1939018833 36 DigitalOut waiting_led(LED1);
JonnyA 0:cb1939018833 37 DigitalOut connected_led(LED2);
JonnyA 0:cb1939018833 38
JonnyA 0:cb1939018833 39 InterruptIn button1(BUTTON_A);
JonnyA 0:cb1939018833 40 InterruptIn button2(BUTTON_B);
JonnyA 0:cb1939018833 41
JonnyA 0:cb1939018833 42 BLE ble;
JonnyA 0:cb1939018833 43 KeyboardService *kbdServicePtr;
JonnyA 0:cb1939018833 44
JonnyA 0:cb1939018833 45 static const char DEVICE_NAME[] = "microbitkb";
JonnyA 0:cb1939018833 46 static const char SHORT_DEVICE_NAME[] = "kbd1";
JonnyA 0:cb1939018833 47
JonnyA 0:cb1939018833 48 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
JonnyA 0:cb1939018833 49 {
JonnyA 0:cb1939018833 50 HID_DEBUG("disconnected\r\n");
JonnyA 0:cb1939018833 51 connected_led = 0;
JonnyA 0:cb1939018833 52
JonnyA 0:cb1939018833 53 ble.gap().startAdvertising(); // restart advertising
JonnyA 0:cb1939018833 54 }
JonnyA 0:cb1939018833 55
JonnyA 0:cb1939018833 56 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
JonnyA 0:cb1939018833 57 {
JonnyA 0:cb1939018833 58 HID_DEBUG("connected\r\n");
JonnyA 0:cb1939018833 59 waiting_led = false;
JonnyA 0:cb1939018833 60 }
JonnyA 0:cb1939018833 61
JonnyA 0:cb1939018833 62 static void waiting() {
JonnyA 0:cb1939018833 63 if (!kbdServicePtr->isConnected())
JonnyA 0:cb1939018833 64 waiting_led = !waiting_led;
JonnyA 0:cb1939018833 65 else
JonnyA 0:cb1939018833 66 connected_led = !connected_led;
JonnyA 0:cb1939018833 67 }
JonnyA 0:cb1939018833 68
JonnyA 0:cb1939018833 69 void send_string(const char * c) {
JonnyA 0:cb1939018833 70 if (!kbdServicePtr)
JonnyA 0:cb1939018833 71 return;
JonnyA 0:cb1939018833 72
JonnyA 0:cb1939018833 73 if (!kbdServicePtr->isConnected()) {
JonnyA 0:cb1939018833 74 HID_DEBUG("we haven't connected yet...");
JonnyA 0:cb1939018833 75 } else {
JonnyA 0:cb1939018833 76 int len = strlen(c);
JonnyA 0:cb1939018833 77 kbdServicePtr->printf(c);
JonnyA 0:cb1939018833 78 HID_DEBUG("sending %d chars\r\n", len);
JonnyA 0:cb1939018833 79 }
JonnyA 0:cb1939018833 80 }
JonnyA 0:cb1939018833 81
JonnyA 0:cb1939018833 82 void send_stuff() {
JonnyA 0:cb1939018833 83 send_string("n");
JonnyA 0:cb1939018833 84 }
JonnyA 0:cb1939018833 85
JonnyA 0:cb1939018833 86 void send_more_stuff() {
JonnyA 0:cb1939018833 87 send_string("p");
JonnyA 0:cb1939018833 88 }
JonnyA 0:cb1939018833 89
JonnyA 0:cb1939018833 90 int main()
JonnyA 0:cb1939018833 91 {
JonnyA 0:cb1939018833 92 Ticker heartbeat;
JonnyA 0:cb1939018833 93
JonnyA 0:cb1939018833 94 button1.rise(send_stuff);
JonnyA 0:cb1939018833 95 button2.rise(send_more_stuff);
JonnyA 0:cb1939018833 96
JonnyA 0:cb1939018833 97 HID_DEBUG("initialising ticker\r\n");
JonnyA 0:cb1939018833 98
JonnyA 0:cb1939018833 99 heartbeat.attach(waiting, 1);
JonnyA 0:cb1939018833 100
JonnyA 0:cb1939018833 101 HID_DEBUG("initialising ble\r\n");
JonnyA 0:cb1939018833 102 ble.init();
JonnyA 0:cb1939018833 103
JonnyA 0:cb1939018833 104 ble.gap().onDisconnection(onDisconnect);
JonnyA 0:cb1939018833 105 ble.gap().onConnection(onConnect);
JonnyA 0:cb1939018833 106
JonnyA 0:cb1939018833 107 initializeSecurity(ble);
JonnyA 0:cb1939018833 108
JonnyA 0:cb1939018833 109 HID_DEBUG("adding hid service\r\n");
JonnyA 0:cb1939018833 110 KeyboardService kbdService(ble);
JonnyA 0:cb1939018833 111 kbdServicePtr = &kbdService;
JonnyA 0:cb1939018833 112
JonnyA 0:cb1939018833 113 HID_DEBUG("adding device info and battery service\r\n");
JonnyA 0:cb1939018833 114 initializeHOGP(ble);
JonnyA 0:cb1939018833 115
JonnyA 0:cb1939018833 116 HID_DEBUG("setting up gap\r\n");
JonnyA 0:cb1939018833 117 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
JonnyA 0:cb1939018833 118 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
JonnyA 0:cb1939018833 119 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
JonnyA 0:cb1939018833 120 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
JonnyA 0:cb1939018833 121 (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
JonnyA 0:cb1939018833 122
JonnyA 0:cb1939018833 123 HID_DEBUG("advertising\r\n");
JonnyA 0:cb1939018833 124 ble.gap().startAdvertising();
JonnyA 0:cb1939018833 125
JonnyA 0:cb1939018833 126 while (true) {
JonnyA 0:cb1939018833 127 ble.waitForEvent();
JonnyA 0:cb1939018833 128 }
JonnyA 0:cb1939018833 129 }
JonnyA 0:cb1939018833 130