Use the microbit as a foot paddle Send Shift-Ctrl-Alt F11 and F12 for the buttons, you can use those a global hotkeys for actions, e.g. mute/unmute microphone

Dependencies:   mbed BLE_API nRF51822

Committer:
schlomo
Date:
Mon Apr 08 22:13:34 2019 +0000
Revision:
4:c7384b889baa
Parent:
keyboard_stream.cpp@3:7d7822143a2d
Send Shift-Ctrl-Alt F11 and F12 for left and right button; Renamed to micro:bit Foot Paddle

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 1:9e174f8fd9e9 35
JonnyA 1:9e174f8fd9e9 36 //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it
JonnyA 1:9e174f8fd9e9 37 DigitalOut col9(P0_12, 0);
JonnyA 0:cb1939018833 38
JonnyA 1:9e174f8fd9e9 39 DigitalOut waiting_led(P0_13);
JonnyA 1:9e174f8fd9e9 40 DigitalOut connected_led(P0_15);
JonnyA 0:cb1939018833 41
JonnyA 0:cb1939018833 42 InterruptIn button1(BUTTON_A);
JonnyA 0:cb1939018833 43 InterruptIn button2(BUTTON_B);
JonnyA 0:cb1939018833 44
JonnyA 0:cb1939018833 45 BLE ble;
JonnyA 0:cb1939018833 46 KeyboardService *kbdServicePtr;
JonnyA 0:cb1939018833 47
schlomo 4:c7384b889baa 48 static const char DEVICE_NAME[] = "micro:bit Foot Paddle";
schlomo 4:c7384b889baa 49 static const char SHORT_DEVICE_NAME[] = "footpaddle";
JonnyA 0:cb1939018833 50
JonnyA 0:cb1939018833 51 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
JonnyA 0:cb1939018833 52 {
JonnyA 0:cb1939018833 53 HID_DEBUG("disconnected\r\n");
JonnyA 0:cb1939018833 54 connected_led = 0;
JonnyA 0:cb1939018833 55
JonnyA 0:cb1939018833 56 ble.gap().startAdvertising(); // restart advertising
JonnyA 0:cb1939018833 57 }
JonnyA 0:cb1939018833 58
JonnyA 0:cb1939018833 59 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
JonnyA 0:cb1939018833 60 {
JonnyA 0:cb1939018833 61 HID_DEBUG("connected\r\n");
JonnyA 0:cb1939018833 62 waiting_led = false;
JonnyA 0:cb1939018833 63 }
JonnyA 0:cb1939018833 64
JonnyA 0:cb1939018833 65 static void waiting() {
JonnyA 0:cb1939018833 66 if (!kbdServicePtr->isConnected())
JonnyA 0:cb1939018833 67 waiting_led = !waiting_led;
JonnyA 0:cb1939018833 68 else
JonnyA 0:cb1939018833 69 connected_led = !connected_led;
JonnyA 0:cb1939018833 70 }
JonnyA 0:cb1939018833 71
JonnyA 0:cb1939018833 72 void send_string(const char * c) {
JonnyA 0:cb1939018833 73 if (!kbdServicePtr)
JonnyA 0:cb1939018833 74 return;
JonnyA 0:cb1939018833 75
JonnyA 0:cb1939018833 76 if (!kbdServicePtr->isConnected()) {
JonnyA 0:cb1939018833 77 HID_DEBUG("we haven't connected yet...");
JonnyA 0:cb1939018833 78 } else {
JonnyA 0:cb1939018833 79 int len = strlen(c);
JonnyA 0:cb1939018833 80 kbdServicePtr->printf(c);
schlomo 4:c7384b889baa 81 HID_DEBUG("sending %d chars: %s\r\n", len, c);
JonnyA 0:cb1939018833 82 }
JonnyA 0:cb1939018833 83 }
JonnyA 0:cb1939018833 84
schlomo 4:c7384b889baa 85 void send_ctrl_shift_alt(const uint8_t key) {
schlomo 4:c7384b889baa 86 if (!kbdServicePtr)
schlomo 4:c7384b889baa 87 return;
schlomo 4:c7384b889baa 88
schlomo 4:c7384b889baa 89 if (!kbdServicePtr->isConnected()) {
schlomo 4:c7384b889baa 90 HID_DEBUG("we haven't connected yet...");
schlomo 4:c7384b889baa 91 } else {
schlomo 4:c7384b889baa 92 kbdServicePtr->keyDownCode(key, KEY_CTRL + KEY_SHIFT + KEY_ALT);
schlomo 4:c7384b889baa 93 HID_DEBUG("sending 0x%X\r\n", key);
schlomo 4:c7384b889baa 94 kbdServicePtr->keyUpCode();
schlomo 4:c7384b889baa 95 }
schlomo 4:c7384b889baa 96 }
schlomo 4:c7384b889baa 97 void pressed_button1() {
schlomo 4:c7384b889baa 98 send_ctrl_shift_alt(0x8A);
JonnyA 3:7d7822143a2d 99 wait(0.1);
JonnyA 3:7d7822143a2d 100
JonnyA 0:cb1939018833 101 }
JonnyA 0:cb1939018833 102
schlomo 4:c7384b889baa 103 void pressed_button2() {
schlomo 4:c7384b889baa 104 send_ctrl_shift_alt(0x8B);
JonnyA 3:7d7822143a2d 105 wait(0.1);
JonnyA 3:7d7822143a2d 106
JonnyA 0:cb1939018833 107 }
JonnyA 0:cb1939018833 108
JonnyA 0:cb1939018833 109 int main()
JonnyA 0:cb1939018833 110 {
JonnyA 0:cb1939018833 111 Ticker heartbeat;
JonnyA 0:cb1939018833 112
schlomo 4:c7384b889baa 113 button1.rise(pressed_button1);
schlomo 4:c7384b889baa 114 button2.rise(pressed_button2);
JonnyA 0:cb1939018833 115
JonnyA 0:cb1939018833 116 HID_DEBUG("initialising ticker\r\n");
JonnyA 0:cb1939018833 117
JonnyA 0:cb1939018833 118 heartbeat.attach(waiting, 1);
JonnyA 0:cb1939018833 119
JonnyA 0:cb1939018833 120 HID_DEBUG("initialising ble\r\n");
JonnyA 0:cb1939018833 121 ble.init();
JonnyA 0:cb1939018833 122
JonnyA 0:cb1939018833 123 ble.gap().onDisconnection(onDisconnect);
JonnyA 0:cb1939018833 124 ble.gap().onConnection(onConnect);
JonnyA 0:cb1939018833 125
JonnyA 0:cb1939018833 126 initializeSecurity(ble);
JonnyA 0:cb1939018833 127
JonnyA 0:cb1939018833 128 HID_DEBUG("adding hid service\r\n");
JonnyA 0:cb1939018833 129 KeyboardService kbdService(ble);
JonnyA 0:cb1939018833 130 kbdServicePtr = &kbdService;
JonnyA 0:cb1939018833 131
JonnyA 0:cb1939018833 132 HID_DEBUG("adding device info and battery service\r\n");
JonnyA 0:cb1939018833 133 initializeHOGP(ble);
JonnyA 0:cb1939018833 134
JonnyA 0:cb1939018833 135 HID_DEBUG("setting up gap\r\n");
JonnyA 0:cb1939018833 136 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
JonnyA 0:cb1939018833 137 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
JonnyA 0:cb1939018833 138 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
JonnyA 0:cb1939018833 139 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
JonnyA 0:cb1939018833 140 (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
JonnyA 1:9e174f8fd9e9 141 ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
JonnyA 0:cb1939018833 142
JonnyA 0:cb1939018833 143 HID_DEBUG("advertising\r\n");
JonnyA 0:cb1939018833 144 ble.gap().startAdvertising();
JonnyA 0:cb1939018833 145
JonnyA 0:cb1939018833 146 while (true) {
JonnyA 0:cb1939018833 147 ble.waitForEvent();
JonnyA 0:cb1939018833 148 }
JonnyA 0:cb1939018833 149 }
JonnyA 0:cb1939018833 150