ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
KeyboardService.h@6:f1c3ea8bc850, 2016-07-21 (annotated)
- Committer:
- cho45
- Date:
- Thu Jul 21 01:25:31 2016 +0900
- Revision:
- 6:f1c3ea8bc850
- Parent:
- 5:65d4e94735b6
- Child:
- 7:b9270a37345b
act as keyboard
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cho45 |
5:65d4e94735b6 | 1 | /* mbed Microcontroller Library |
cho45 |
5:65d4e94735b6 | 2 | * Copyright (c) 2015 ARM Limited |
cho45 |
5:65d4e94735b6 | 3 | * |
cho45 |
5:65d4e94735b6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cho45 |
5:65d4e94735b6 | 5 | * you may not use this file except in compliance with the License. |
cho45 |
5:65d4e94735b6 | 6 | * You may obtain a copy of the License at |
cho45 |
5:65d4e94735b6 | 7 | * |
cho45 |
5:65d4e94735b6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cho45 |
5:65d4e94735b6 | 9 | * |
cho45 |
5:65d4e94735b6 | 10 | * Unless required by applicable law or agreed to in writing, software |
cho45 |
5:65d4e94735b6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
cho45 |
5:65d4e94735b6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cho45 |
5:65d4e94735b6 | 13 | * See the License for the specific language governing permissions and |
cho45 |
5:65d4e94735b6 | 14 | * limitations under the License. |
cho45 |
5:65d4e94735b6 | 15 | */ |
cho45 |
5:65d4e94735b6 | 16 | |
cho45 |
5:65d4e94735b6 | 17 | #include "HIDServiceBase.h" |
cho45 |
5:65d4e94735b6 | 18 | #include "keyboard.h" |
cho45 |
5:65d4e94735b6 | 19 | |
cho45 |
5:65d4e94735b6 | 20 | /** |
cho45 |
5:65d4e94735b6 | 21 | * Report descriptor for a standard 101 keys keyboard, following the HID specification example: |
cho45 |
5:65d4e94735b6 | 22 | * - 8 bytes input report (1 byte for modifiers and 6 for keys) |
cho45 |
5:65d4e94735b6 | 23 | * - 1 byte output report (LEDs) |
cho45 |
5:65d4e94735b6 | 24 | */ |
cho45 |
5:65d4e94735b6 | 25 | report_map_t KEYBOARD_REPORT_MAP = { |
cho45 |
5:65d4e94735b6 | 26 | USAGE_PAGE(1), 0x01, // Generic Desktop Ctrls |
cho45 |
5:65d4e94735b6 | 27 | USAGE(1), 0x06, // Keyboard |
cho45 |
5:65d4e94735b6 | 28 | COLLECTION(1), 0x01, // Application |
cho45 |
5:65d4e94735b6 | 29 | USAGE_PAGE(1), 0x07, // Kbrd/Keypad |
cho45 |
5:65d4e94735b6 | 30 | USAGE_MINIMUM(1), 0xE0, |
cho45 |
5:65d4e94735b6 | 31 | USAGE_MAXIMUM(1), 0xE7, |
cho45 |
5:65d4e94735b6 | 32 | LOGICAL_MINIMUM(1), 0x00, |
cho45 |
5:65d4e94735b6 | 33 | LOGICAL_MAXIMUM(1), 0x01, |
cho45 |
5:65d4e94735b6 | 34 | REPORT_SIZE(1), 0x01, // 1 byte (Modifier) |
cho45 |
5:65d4e94735b6 | 35 | REPORT_COUNT(1), 0x08, |
cho45 |
5:65d4e94735b6 | 36 | INPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
5:65d4e94735b6 | 37 | REPORT_COUNT(1), 0x01, // 1 byte (Reserved) |
cho45 |
5:65d4e94735b6 | 38 | REPORT_SIZE(1), 0x08, |
cho45 |
5:65d4e94735b6 | 39 | INPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
5:65d4e94735b6 | 40 | REPORT_COUNT(1), 0x05, // 5 bits (Num lock, Caps lock, Scroll lock, Compose, Kana) |
cho45 |
5:65d4e94735b6 | 41 | REPORT_SIZE(1), 0x01, |
cho45 |
5:65d4e94735b6 | 42 | USAGE_PAGE(1), 0x08, // LEDs |
cho45 |
5:65d4e94735b6 | 43 | USAGE_MINIMUM(1), 0x01, // Num Lock |
cho45 |
5:65d4e94735b6 | 44 | USAGE_MAXIMUM(1), 0x05, // Kana |
cho45 |
5:65d4e94735b6 | 45 | OUTPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile |
cho45 |
5:65d4e94735b6 | 46 | REPORT_COUNT(1), 0x01, // 3 bits (Padding) |
cho45 |
5:65d4e94735b6 | 47 | REPORT_SIZE(1), 0x03, |
cho45 |
5:65d4e94735b6 | 48 | OUTPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile |
cho45 |
5:65d4e94735b6 | 49 | REPORT_COUNT(1), 0x06, // 6 bytes (Keys) |
cho45 |
5:65d4e94735b6 | 50 | REPORT_SIZE(1), 0x08, |
cho45 |
5:65d4e94735b6 | 51 | LOGICAL_MINIMUM(1), 0x00, |
cho45 |
5:65d4e94735b6 | 52 | LOGICAL_MAXIMUM(1), 0x65, // 101 keys |
cho45 |
5:65d4e94735b6 | 53 | USAGE_PAGE(1), 0x07, // Kbrd/Keypad |
cho45 |
5:65d4e94735b6 | 54 | USAGE_MINIMUM(1), 0x00, |
cho45 |
5:65d4e94735b6 | 55 | USAGE_MAXIMUM(1), 0x65, |
cho45 |
5:65d4e94735b6 | 56 | INPUT(1), 0x00, // Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
5:65d4e94735b6 | 57 | END_COLLECTION(0), |
cho45 |
5:65d4e94735b6 | 58 | }; |
cho45 |
5:65d4e94735b6 | 59 | |
cho45 |
5:65d4e94735b6 | 60 | class KeyboardService : public HIDServiceBase { |
cho45 |
5:65d4e94735b6 | 61 | union { |
cho45 |
5:65d4e94735b6 | 62 | uint8_t raw[8]; |
cho45 |
5:65d4e94735b6 | 63 | struct { |
cho45 |
5:65d4e94735b6 | 64 | uint8_t modifier; |
cho45 |
5:65d4e94735b6 | 65 | uint8_t padding; |
cho45 |
5:65d4e94735b6 | 66 | uint8_t keycode[6]; |
cho45 |
5:65d4e94735b6 | 67 | } data; |
cho45 |
5:65d4e94735b6 | 68 | } inputReportData; |
cho45 |
5:65d4e94735b6 | 69 | |
cho45 |
5:65d4e94735b6 | 70 | union { |
cho45 |
5:65d4e94735b6 | 71 | uint8_t raw[1]; |
cho45 |
5:65d4e94735b6 | 72 | } outputReportData; |
cho45 |
5:65d4e94735b6 | 73 | |
cho45 |
5:65d4e94735b6 | 74 | static const uint8_t MODIFIER_LEFT_CONTROL = 1<<0; |
cho45 |
5:65d4e94735b6 | 75 | static const uint8_t MODIFIER_LEFT_SHIFT = 1<<1; |
cho45 |
5:65d4e94735b6 | 76 | static const uint8_t MODIFIER_LEFT_ALT = 1<<2; |
cho45 |
5:65d4e94735b6 | 77 | static const uint8_t MODIFIER_LEFT_GUI = 1<<3; |
cho45 |
5:65d4e94735b6 | 78 | static const uint8_t MODIFIER_RIGHT_CONTROL = 1<<4; |
cho45 |
5:65d4e94735b6 | 79 | static const uint8_t MODIFIER_RIGHT_SHIFT = 1<<5; |
cho45 |
5:65d4e94735b6 | 80 | static const uint8_t MODIFIER_RIGHT_ALT = 1<<6; |
cho45 |
5:65d4e94735b6 | 81 | static const uint8_t MODIFIER_RIGHT_GUI = 1<<7; |
cho45 |
5:65d4e94735b6 | 82 | |
cho45 |
5:65d4e94735b6 | 83 | public: |
cho45 |
5:65d4e94735b6 | 84 | KeyboardService(BLE& _ble) : |
cho45 |
5:65d4e94735b6 | 85 | HIDServiceBase( |
cho45 |
5:65d4e94735b6 | 86 | _ble, |
cho45 |
5:65d4e94735b6 | 87 | KEYBOARD_REPORT_MAP, |
cho45 |
5:65d4e94735b6 | 88 | sizeof(KEYBOARD_REPORT_MAP), |
cho45 |
5:65d4e94735b6 | 89 | inputReport = inputReportData.raw, |
cho45 |
5:65d4e94735b6 | 90 | outputReport = outputReportData.raw, |
cho45 |
5:65d4e94735b6 | 91 | featureReport = NULL, |
cho45 |
5:65d4e94735b6 | 92 | inputReportLength = sizeof(inputReportData), |
cho45 |
5:65d4e94735b6 | 93 | outputReportLength = sizeof(outputReportData), |
cho45 |
5:65d4e94735b6 | 94 | featureReportLength = 0, |
cho45 |
5:65d4e94735b6 | 95 | reportTickerDelay = 24 |
cho45 |
5:65d4e94735b6 | 96 | ) |
cho45 |
5:65d4e94735b6 | 97 | { |
cho45 |
6:f1c3ea8bc850 | 98 | for (int i = 0; i < 8; i++) { |
cho45 |
6:f1c3ea8bc850 | 99 | inputReportData.raw[i] = 0; |
cho45 |
6:f1c3ea8bc850 | 100 | } |
cho45 |
6:f1c3ea8bc850 | 101 | outputReportData.raw[0] = 0; |
cho45 |
5:65d4e94735b6 | 102 | } |
cho45 |
5:65d4e94735b6 | 103 | |
cho45 |
5:65d4e94735b6 | 104 | void appendReportData(uint8_t keycode) { |
cho45 |
5:65d4e94735b6 | 105 | uint8_t modifier = toModifierBit(keycode); |
cho45 |
5:65d4e94735b6 | 106 | if (modifier) { |
cho45 |
5:65d4e94735b6 | 107 | inputReportData.data.modifier |= modifier; |
cho45 |
5:65d4e94735b6 | 108 | startReportTicker(); |
cho45 |
5:65d4e94735b6 | 109 | return; |
cho45 |
5:65d4e94735b6 | 110 | } |
cho45 |
5:65d4e94735b6 | 111 | |
cho45 |
5:65d4e94735b6 | 112 | |
cho45 |
5:65d4e94735b6 | 113 | for (int i = 0; i < 6; i++) { |
cho45 |
5:65d4e94735b6 | 114 | if (inputReportData.data.keycode[i] == 0) { |
cho45 |
5:65d4e94735b6 | 115 | inputReportData.data.keycode[i] = keycode; |
cho45 |
5:65d4e94735b6 | 116 | startReportTicker(); |
cho45 |
5:65d4e94735b6 | 117 | return; |
cho45 |
5:65d4e94735b6 | 118 | } |
cho45 |
5:65d4e94735b6 | 119 | } |
cho45 |
5:65d4e94735b6 | 120 | |
cho45 |
5:65d4e94735b6 | 121 | // TODO: report data is full |
cho45 |
5:65d4e94735b6 | 122 | } |
cho45 |
5:65d4e94735b6 | 123 | |
cho45 |
5:65d4e94735b6 | 124 | void deleteReportData(uint8_t keycode) { |
cho45 |
5:65d4e94735b6 | 125 | uint8_t modifier = toModifierBit(keycode); |
cho45 |
5:65d4e94735b6 | 126 | if (modifier) { |
cho45 |
5:65d4e94735b6 | 127 | inputReportData.data.modifier &= ~modifier; |
cho45 |
5:65d4e94735b6 | 128 | startReportTicker(); |
cho45 |
5:65d4e94735b6 | 129 | return; |
cho45 |
5:65d4e94735b6 | 130 | } |
cho45 |
5:65d4e94735b6 | 131 | |
cho45 |
5:65d4e94735b6 | 132 | for (int i = 0; i < 6; i++) { |
cho45 |
5:65d4e94735b6 | 133 | if (inputReportData.data.keycode[i] == keycode) { |
cho45 |
5:65d4e94735b6 | 134 | inputReportData.data.keycode[i] = 0; |
cho45 |
5:65d4e94735b6 | 135 | startReportTicker(); |
cho45 |
5:65d4e94735b6 | 136 | return; |
cho45 |
5:65d4e94735b6 | 137 | } |
cho45 |
5:65d4e94735b6 | 138 | } |
cho45 |
5:65d4e94735b6 | 139 | } |
cho45 |
5:65d4e94735b6 | 140 | |
cho45 |
5:65d4e94735b6 | 141 | uint8_t toModifierBit(uint8_t keycode) const { |
cho45 |
5:65d4e94735b6 | 142 | switch (keycode) { |
cho45 |
5:65d4e94735b6 | 143 | case KEY_LeftControl: return MODIFIER_LEFT_CONTROL; |
cho45 |
5:65d4e94735b6 | 144 | case KEY_LeftShift: return MODIFIER_LEFT_SHIFT; |
cho45 |
5:65d4e94735b6 | 145 | case KEY_LeftAlt: return MODIFIER_LEFT_ALT; |
cho45 |
5:65d4e94735b6 | 146 | case KEY_LeftGUI: return MODIFIER_LEFT_GUI; |
cho45 |
5:65d4e94735b6 | 147 | case KEY_RightControl: return MODIFIER_RIGHT_CONTROL; |
cho45 |
5:65d4e94735b6 | 148 | case KEY_RightShift: return MODIFIER_RIGHT_SHIFT; |
cho45 |
5:65d4e94735b6 | 149 | case KEY_RightAlt: return MODIFIER_RIGHT_ALT; |
cho45 |
5:65d4e94735b6 | 150 | case KEY_RightGUI: return MODIFIER_RIGHT_GUI; |
cho45 |
5:65d4e94735b6 | 151 | } |
cho45 |
5:65d4e94735b6 | 152 | return 0; |
cho45 |
5:65d4e94735b6 | 153 | } |
cho45 |
5:65d4e94735b6 | 154 | |
cho45 |
5:65d4e94735b6 | 155 | bool isKeyPressed() { |
cho45 |
5:65d4e94735b6 | 156 | for (int i = 0; i < 8; i++) { |
cho45 |
5:65d4e94735b6 | 157 | if (inputReportData.raw[i]) { |
cho45 |
5:65d4e94735b6 | 158 | return 1; |
cho45 |
5:65d4e94735b6 | 159 | } |
cho45 |
5:65d4e94735b6 | 160 | } |
cho45 |
5:65d4e94735b6 | 161 | return 0; |
cho45 |
5:65d4e94735b6 | 162 | } |
cho45 |
5:65d4e94735b6 | 163 | |
cho45 |
5:65d4e94735b6 | 164 | virtual void sendCallback(void) { |
cho45 |
5:65d4e94735b6 | 165 | ble_error_t error = HIDServiceBase::send(inputReportData.raw); |
cho45 |
5:65d4e94735b6 | 166 | if (error == BLE_STACK_BUSY) { |
cho45 |
5:65d4e94735b6 | 167 | // retry after |
cho45 |
5:65d4e94735b6 | 168 | return; |
cho45 |
5:65d4e94735b6 | 169 | } |
cho45 |
5:65d4e94735b6 | 170 | |
cho45 |
5:65d4e94735b6 | 171 | if (!isKeyPressed()) { |
cho45 |
5:65d4e94735b6 | 172 | stopReportTicker(); |
cho45 |
5:65d4e94735b6 | 173 | } |
cho45 |
5:65d4e94735b6 | 174 | } |
cho45 |
5:65d4e94735b6 | 175 | }; |
cho45 |
5:65d4e94735b6 | 176 | |
cho45 |
5:65d4e94735b6 | 177 |