ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
KeyboardService.h@75:351d7ffe81d1, 2016-09-02 (annotated)
- Committer:
- cho45
- Date:
- Fri Sep 02 21:16:15 2016 +0900
- Revision:
- 75:351d7ffe81d1
- Parent:
- 60:b899414e1d34
- Child:
- 79:0095bfb18c57
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cho45 | 42:2c3be8694896 | 1 | |
cho45 |
5:65d4e94735b6 | 2 | /* mbed Microcontroller Library |
cho45 |
5:65d4e94735b6 | 3 | * Copyright (c) 2015 ARM Limited |
cho45 |
5:65d4e94735b6 | 4 | * |
cho45 |
5:65d4e94735b6 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cho45 |
5:65d4e94735b6 | 6 | * you may not use this file except in compliance with the License. |
cho45 |
5:65d4e94735b6 | 7 | * You may obtain a copy of the License at |
cho45 |
5:65d4e94735b6 | 8 | * |
cho45 |
5:65d4e94735b6 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
cho45 |
5:65d4e94735b6 | 10 | * |
cho45 |
5:65d4e94735b6 | 11 | * Unless required by applicable law or agreed to in writing, software |
cho45 |
5:65d4e94735b6 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
cho45 |
5:65d4e94735b6 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cho45 |
5:65d4e94735b6 | 14 | * See the License for the specific language governing permissions and |
cho45 |
5:65d4e94735b6 | 15 | * limitations under the License. |
cho45 |
5:65d4e94735b6 | 16 | */ |
cho45 |
5:65d4e94735b6 | 17 | |
cho45 |
5:65d4e94735b6 | 18 | #include "HIDServiceBase.h" |
cho45 |
5:65d4e94735b6 | 19 | #include "keyboard.h" |
cho45 |
7:b9270a37345b | 20 | #include "CircularBuffer.h" |
cho45 |
5:65d4e94735b6 | 21 | |
cho45 |
5:65d4e94735b6 | 22 | /** |
cho45 |
5:65d4e94735b6 | 23 | * Report descriptor for a standard 101 keys keyboard, following the HID specification example: |
cho45 |
5:65d4e94735b6 | 24 | * - 8 bytes input report (1 byte for modifiers and 6 for keys) |
cho45 |
5:65d4e94735b6 | 25 | * - 1 byte output report (LEDs) |
cho45 |
5:65d4e94735b6 | 26 | */ |
cho45 | 59:2d6c0bff2151 | 27 | const report_map_t KEYBOARD_REPORT_MAP = { |
cho45 |
5:65d4e94735b6 | 28 | USAGE_PAGE(1), 0x01, // Generic Desktop Ctrls |
cho45 |
5:65d4e94735b6 | 29 | USAGE(1), 0x06, // Keyboard |
cho45 |
5:65d4e94735b6 | 30 | COLLECTION(1), 0x01, // Application |
cho45 |
75:351d7ffe81d1 | 31 | USAGE_PAGE(1), 0x07, // Kbrd/Keypad |
cho45 |
75:351d7ffe81d1 | 32 | USAGE_MINIMUM(1), 0xE0, |
cho45 |
75:351d7ffe81d1 | 33 | USAGE_MAXIMUM(1), 0xE7, |
cho45 |
75:351d7ffe81d1 | 34 | LOGICAL_MINIMUM(1), 0x00, |
cho45 |
75:351d7ffe81d1 | 35 | LOGICAL_MAXIMUM(1), 0x01, |
cho45 |
75:351d7ffe81d1 | 36 | REPORT_SIZE(1), 0x01, // 1 byte (Modifier) |
cho45 |
75:351d7ffe81d1 | 37 | REPORT_COUNT(1), 0x08, |
cho45 |
75:351d7ffe81d1 | 38 | INPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
75:351d7ffe81d1 | 39 | |
cho45 |
75:351d7ffe81d1 | 40 | REPORT_COUNT(1), 0x01, // 1 byte (Reserved) |
cho45 |
75:351d7ffe81d1 | 41 | REPORT_SIZE(1), 0x08, |
cho45 |
75:351d7ffe81d1 | 42 | INPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
75:351d7ffe81d1 | 43 | |
cho45 |
75:351d7ffe81d1 | 44 | REPORT_COUNT(1), 0x05, // 5 bits (Num lock, Caps lock, Scroll lock, Compose, Kana) |
cho45 |
75:351d7ffe81d1 | 45 | REPORT_SIZE(1), 0x01, |
cho45 |
75:351d7ffe81d1 | 46 | USAGE_PAGE(1), 0x08, // LEDs |
cho45 |
75:351d7ffe81d1 | 47 | USAGE_MINIMUM(1), 0x01, // Num Lock |
cho45 |
75:351d7ffe81d1 | 48 | USAGE_MAXIMUM(1), 0x05, // Kana |
cho45 |
75:351d7ffe81d1 | 49 | OUTPUT(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile |
cho45 |
75:351d7ffe81d1 | 50 | |
cho45 |
75:351d7ffe81d1 | 51 | REPORT_COUNT(1), 0x01, // 3 bits (Padding) |
cho45 |
75:351d7ffe81d1 | 52 | REPORT_SIZE(1), 0x03, |
cho45 |
75:351d7ffe81d1 | 53 | OUTPUT(1), 0x01, // Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile |
cho45 |
75:351d7ffe81d1 | 54 | |
cho45 |
75:351d7ffe81d1 | 55 | REPORT_COUNT(1), 0x06, // 6 bytes (Keys) |
cho45 |
75:351d7ffe81d1 | 56 | REPORT_SIZE(1), 0x08, |
cho45 |
75:351d7ffe81d1 | 57 | LOGICAL_MINIMUM(1), 0x00, |
cho45 |
75:351d7ffe81d1 | 58 | LOGICAL_MAXIMUM(1), 0x65, // 101 keys |
cho45 |
75:351d7ffe81d1 | 59 | USAGE_PAGE(1), 0x07, // Kbrd/Keypad |
cho45 |
75:351d7ffe81d1 | 60 | USAGE_MINIMUM(1), 0x00, |
cho45 |
75:351d7ffe81d1 | 61 | USAGE_MAXIMUM(1), 0x65, |
cho45 |
75:351d7ffe81d1 | 62 | INPUT(1), 0x00, // Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position |
cho45 |
75:351d7ffe81d1 | 63 | |
cho45 |
75:351d7ffe81d1 | 64 | USAGE_PAGE(1), 0x00, // Undefined |
cho45 |
75:351d7ffe81d1 | 65 | USAGE_MINIMUM(1), 0x00, |
cho45 |
75:351d7ffe81d1 | 66 | USAGE_MAXIMUM(1), 0xFF, |
cho45 |
75:351d7ffe81d1 | 67 | REPORT_COUNT(1), 0x01, // 1 byte |
cho45 |
75:351d7ffe81d1 | 68 | REPORT_SIZE(1), 0x08, |
cho45 |
75:351d7ffe81d1 | 69 | FEATURE(1), 0x02, // Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile |
cho45 |
5:65d4e94735b6 | 70 | END_COLLECTION(0), |
cho45 |
5:65d4e94735b6 | 71 | }; |
cho45 |
5:65d4e94735b6 | 72 | |
cho45 |
75:351d7ffe81d1 | 73 | class KeyboardService : public HIDServiceBase { |
cho45 | 60:b899414e1d34 | 74 | |
cho45 |
75:351d7ffe81d1 | 75 | union InputReportData { |
cho45 |
75:351d7ffe81d1 | 76 | uint8_t raw[8]; |
cho45 |
75:351d7ffe81d1 | 77 | struct { |
cho45 |
75:351d7ffe81d1 | 78 | uint8_t modifier; |
cho45 |
75:351d7ffe81d1 | 79 | uint8_t padding; |
cho45 |
75:351d7ffe81d1 | 80 | uint8_t keycode[6]; |
cho45 |
75:351d7ffe81d1 | 81 | } data; |
cho45 |
75:351d7ffe81d1 | 82 | }; |
cho45 | 60:b899414e1d34 | 83 | |
cho45 |
75:351d7ffe81d1 | 84 | union OutputReportData { |
cho45 |
75:351d7ffe81d1 | 85 | uint8_t raw[1]; |
cho45 |
75:351d7ffe81d1 | 86 | }; |
cho45 |
75:351d7ffe81d1 | 87 | |
cho45 |
75:351d7ffe81d1 | 88 | union FeatureReportData { |
cho45 |
75:351d7ffe81d1 | 89 | uint8_t raw[1]; |
cho45 |
75:351d7ffe81d1 | 90 | }; |
cho45 |
7:b9270a37345b | 91 | |
cho45 |
9:d1daefbf1fbd | 92 | InputReportData inputReportDataPublished; |
cho45 |
7:b9270a37345b | 93 | InputReportData inputReportData; |
cho45 |
5:65d4e94735b6 | 94 | |
cho45 | 60:b899414e1d34 | 95 | // 5ms ごとのスイッチ判定・20ms ごとの送信 |
cho45 | 60:b899414e1d34 | 96 | // 人間の連打限界を16press/secとし keydown/keyup それぞれでレポートを送ることを考えると |
cho45 | 60:b899414e1d34 | 97 | // 32report/sec = 31msec で、限界で連打しても20msごとの送信にまにあう |
cho45 | 60:b899414e1d34 | 98 | // ただし上記は1キーに対してであり、複数キーを全力で押すとキューに溜まることがある |
cho45 | 60:b899414e1d34 | 99 | // 実際にカウントして測ってみたところ、どんなに乱暴に連打しても8ぐらいまでしかいかない |
cho45 | 60:b899414e1d34 | 100 | CircularBuffer<InputReportData, 8, uint8_t> inputReportBuffer; |
cho45 |
7:b9270a37345b | 101 | InputReportData inputReportDataSending; |
cho45 |
75:351d7ffe81d1 | 102 | OutputReportData outputReportData; |
cho45 |
75:351d7ffe81d1 | 103 | FeatureReportData featureReportData; |
cho45 |
7:b9270a37345b | 104 | bool isSending; |
cho45 |
7:b9270a37345b | 105 | |
cho45 |
5:65d4e94735b6 | 106 | static const uint8_t MODIFIER_LEFT_CONTROL = 1<<0; |
cho45 |
5:65d4e94735b6 | 107 | static const uint8_t MODIFIER_LEFT_SHIFT = 1<<1; |
cho45 |
5:65d4e94735b6 | 108 | static const uint8_t MODIFIER_LEFT_ALT = 1<<2; |
cho45 |
5:65d4e94735b6 | 109 | static const uint8_t MODIFIER_LEFT_GUI = 1<<3; |
cho45 |
5:65d4e94735b6 | 110 | static const uint8_t MODIFIER_RIGHT_CONTROL = 1<<4; |
cho45 |
5:65d4e94735b6 | 111 | static const uint8_t MODIFIER_RIGHT_SHIFT = 1<<5; |
cho45 |
5:65d4e94735b6 | 112 | static const uint8_t MODIFIER_RIGHT_ALT = 1<<6; |
cho45 |
5:65d4e94735b6 | 113 | static const uint8_t MODIFIER_RIGHT_GUI = 1<<7; |
cho45 |
5:65d4e94735b6 | 114 | |
cho45 |
5:65d4e94735b6 | 115 | public: |
cho45 | 45:f4be69c936f6 | 116 | bool sendAvailable; |
cho45 | 60:b899414e1d34 | 117 | uint8_t bufferCount; |
cho45 | 45:f4be69c936f6 | 118 | |
cho45 |
5:65d4e94735b6 | 119 | KeyboardService(BLE& _ble) : |
cho45 |
5:65d4e94735b6 | 120 | HIDServiceBase( |
cho45 |
5:65d4e94735b6 | 121 | _ble, |
cho45 |
5:65d4e94735b6 | 122 | KEYBOARD_REPORT_MAP, |
cho45 |
5:65d4e94735b6 | 123 | sizeof(KEYBOARD_REPORT_MAP), |
cho45 |
9:d1daefbf1fbd | 124 | inputReport = inputReportDataPublished.raw, |
cho45 |
5:65d4e94735b6 | 125 | outputReport = outputReportData.raw, |
cho45 |
75:351d7ffe81d1 | 126 | featureReport = featureReportData.raw, |
cho45 |
5:65d4e94735b6 | 127 | inputReportLength = sizeof(inputReportData), |
cho45 |
5:65d4e94735b6 | 128 | outputReportLength = sizeof(outputReportData), |
cho45 |
75:351d7ffe81d1 | 129 | featureReportLength = sizeof(featureReportData), |
cho45 | 45:f4be69c936f6 | 130 | reportTickerDelay = 20 |
cho45 |
7:b9270a37345b | 131 | ), |
cho45 | 45:f4be69c936f6 | 132 | isSending(false), |
cho45 | 60:b899414e1d34 | 133 | sendAvailable(false), |
cho45 | 60:b899414e1d34 | 134 | bufferCount(0) |
cho45 |
5:65d4e94735b6 | 135 | { |
cho45 |
6:f1c3ea8bc850 | 136 | for (int i = 0; i < 8; i++) { |
cho45 |
6:f1c3ea8bc850 | 137 | inputReportData.raw[i] = 0; |
cho45 |
6:f1c3ea8bc850 | 138 | } |
cho45 |
6:f1c3ea8bc850 | 139 | outputReportData.raw[0] = 0; |
cho45 |
7:b9270a37345b | 140 | |
cho45 |
7:b9270a37345b | 141 | inputReportBuffer.reset(); |
cho45 |
5:65d4e94735b6 | 142 | } |
cho45 |
5:65d4e94735b6 | 143 | |
cho45 | 48:d6938de02f62 | 144 | void appendReportData(const uint8_t keycode) { |
cho45 |
5:65d4e94735b6 | 145 | uint8_t modifier = toModifierBit(keycode); |
cho45 |
5:65d4e94735b6 | 146 | if (modifier) { |
cho45 |
5:65d4e94735b6 | 147 | inputReportData.data.modifier |= modifier; |
cho45 |
5:65d4e94735b6 | 148 | return; |
cho45 |
5:65d4e94735b6 | 149 | } |
cho45 |
5:65d4e94735b6 | 150 | |
cho45 |
5:65d4e94735b6 | 151 | |
cho45 |
5:65d4e94735b6 | 152 | for (int i = 0; i < 6; i++) { |
cho45 |
5:65d4e94735b6 | 153 | if (inputReportData.data.keycode[i] == 0) { |
cho45 |
5:65d4e94735b6 | 154 | inputReportData.data.keycode[i] = keycode; |
cho45 |
5:65d4e94735b6 | 155 | return; |
cho45 |
5:65d4e94735b6 | 156 | } |
cho45 |
5:65d4e94735b6 | 157 | } |
cho45 |
5:65d4e94735b6 | 158 | |
cho45 |
5:65d4e94735b6 | 159 | // TODO: report data is full |
cho45 |
5:65d4e94735b6 | 160 | } |
cho45 |
5:65d4e94735b6 | 161 | |
cho45 | 48:d6938de02f62 | 162 | void deleteReportData(const uint8_t keycode) { |
cho45 |
5:65d4e94735b6 | 163 | uint8_t modifier = toModifierBit(keycode); |
cho45 |
5:65d4e94735b6 | 164 | if (modifier) { |
cho45 |
5:65d4e94735b6 | 165 | inputReportData.data.modifier &= ~modifier; |
cho45 |
5:65d4e94735b6 | 166 | return; |
cho45 |
5:65d4e94735b6 | 167 | } |
cho45 |
5:65d4e94735b6 | 168 | |
cho45 |
5:65d4e94735b6 | 169 | for (int i = 0; i < 6; i++) { |
cho45 |
5:65d4e94735b6 | 170 | if (inputReportData.data.keycode[i] == keycode) { |
cho45 |
5:65d4e94735b6 | 171 | inputReportData.data.keycode[i] = 0; |
cho45 |
5:65d4e94735b6 | 172 | return; |
cho45 |
5:65d4e94735b6 | 173 | } |
cho45 |
5:65d4e94735b6 | 174 | } |
cho45 |
5:65d4e94735b6 | 175 | } |
cho45 |
5:65d4e94735b6 | 176 | |
cho45 |
9:d1daefbf1fbd | 177 | void queueCurrentReportData() { |
cho45 | 60:b899414e1d34 | 178 | bufferCount++; |
cho45 |
9:d1daefbf1fbd | 179 | inputReportBuffer.push(inputReportData); |
cho45 |
9:d1daefbf1fbd | 180 | startReportTicker(); |
cho45 |
9:d1daefbf1fbd | 181 | } |
cho45 |
9:d1daefbf1fbd | 182 | |
cho45 | 48:d6938de02f62 | 183 | uint8_t toModifierBit(const uint8_t keycode) const { |
cho45 |
5:65d4e94735b6 | 184 | switch (keycode) { |
cho45 |
5:65d4e94735b6 | 185 | case KEY_LeftControl: return MODIFIER_LEFT_CONTROL; |
cho45 |
5:65d4e94735b6 | 186 | case KEY_LeftShift: return MODIFIER_LEFT_SHIFT; |
cho45 |
5:65d4e94735b6 | 187 | case KEY_LeftAlt: return MODIFIER_LEFT_ALT; |
cho45 |
5:65d4e94735b6 | 188 | case KEY_LeftGUI: return MODIFIER_LEFT_GUI; |
cho45 |
5:65d4e94735b6 | 189 | case KEY_RightControl: return MODIFIER_RIGHT_CONTROL; |
cho45 |
5:65d4e94735b6 | 190 | case KEY_RightShift: return MODIFIER_RIGHT_SHIFT; |
cho45 |
5:65d4e94735b6 | 191 | case KEY_RightAlt: return MODIFIER_RIGHT_ALT; |
cho45 |
5:65d4e94735b6 | 192 | case KEY_RightGUI: return MODIFIER_RIGHT_GUI; |
cho45 |
5:65d4e94735b6 | 193 | } |
cho45 |
5:65d4e94735b6 | 194 | return 0; |
cho45 |
5:65d4e94735b6 | 195 | } |
cho45 |
5:65d4e94735b6 | 196 | |
cho45 |
5:65d4e94735b6 | 197 | bool isKeyPressed() { |
cho45 |
5:65d4e94735b6 | 198 | for (int i = 0; i < 8; i++) { |
cho45 |
5:65d4e94735b6 | 199 | if (inputReportData.raw[i]) { |
cho45 |
5:65d4e94735b6 | 200 | return 1; |
cho45 |
5:65d4e94735b6 | 201 | } |
cho45 |
5:65d4e94735b6 | 202 | } |
cho45 |
5:65d4e94735b6 | 203 | return 0; |
cho45 |
5:65d4e94735b6 | 204 | } |
cho45 |
5:65d4e94735b6 | 205 | |
cho45 |
5:65d4e94735b6 | 206 | virtual void sendCallback(void) { |
cho45 |
7:b9270a37345b | 207 | // do not call printf in this function... it cause BLE_STACK_BUSY |
cho45 | 45:f4be69c936f6 | 208 | sendAvailable = true; |
cho45 | 45:f4be69c936f6 | 209 | } |
cho45 | 45:f4be69c936f6 | 210 | |
cho45 | 45:f4be69c936f6 | 211 | void processSend() { |
cho45 | 45:f4be69c936f6 | 212 | if (!sendAvailable) { |
cho45 | 45:f4be69c936f6 | 213 | return; |
cho45 | 45:f4be69c936f6 | 214 | } |
cho45 | 45:f4be69c936f6 | 215 | sendAvailable = false; |
cho45 | 45:f4be69c936f6 | 216 | |
cho45 | 45:f4be69c936f6 | 217 | // isSending の場合現在送信中の report があり、再送中である可能性がある |
cho45 | 45:f4be69c936f6 | 218 | // そうではない場合のみ queue から pop して新しく send する |
cho45 |
9:d1daefbf1fbd | 219 | if (!isSending) { |
cho45 |
9:d1daefbf1fbd | 220 | if (!inputReportBuffer.pop(inputReportDataSending)) { |
cho45 | 51:d9297f8a60b7 | 221 | // 送るデータがないなら送信をやめる |
cho45 | 51:d9297f8a60b7 | 222 | stopReportTicker(); |
cho45 | 51:d9297f8a60b7 | 223 | return; |
cho45 |
7:b9270a37345b | 224 | } |
cho45 | 60:b899414e1d34 | 225 | bufferCount--; |
cho45 |
7:b9270a37345b | 226 | } |
cho45 |
7:b9270a37345b | 227 | |
cho45 |
7:b9270a37345b | 228 | static uint8_t busyCount = 0; |
cho45 |
7:b9270a37345b | 229 | isSending = true; |
cho45 |
9:d1daefbf1fbd | 230 | ble_error_t error = HIDServiceBase::send(inputReportDataSending.raw); |
cho45 |
5:65d4e94735b6 | 231 | if (error == BLE_STACK_BUSY) { |
cho45 |
7:b9270a37345b | 232 | if (busyCount++ > 10) { |
cho45 |
7:b9270a37345b | 233 | busyCount = 0; |
cho45 |
7:b9270a37345b | 234 | stopReportTicker(); |
cho45 |
7:b9270a37345b | 235 | } |
cho45 |
5:65d4e94735b6 | 236 | // retry after |
cho45 |
5:65d4e94735b6 | 237 | return; |
cho45 |
5:65d4e94735b6 | 238 | } |
cho45 |
7:b9270a37345b | 239 | isSending = false; |
cho45 |
7:b9270a37345b | 240 | } |
cho45 |
5:65d4e94735b6 | 241 | |
cho45 |
7:b9270a37345b | 242 | virtual void onDataSent(unsigned int count) { |
cho45 |
7:b9270a37345b | 243 | startReportTicker(); |
cho45 |
5:65d4e94735b6 | 244 | } |
cho45 | 21:d801c32231b0 | 245 | |
cho45 | 21:d801c32231b0 | 246 | virtual void stopReportTicker(void) { |
cho45 | 34:7da766a8aa96 | 247 | if (reportTickerIsActive) { |
cho45 | 34:7da766a8aa96 | 248 | HIDServiceBase::stopReportTicker(); |
cho45 | 34:7da766a8aa96 | 249 | } |
cho45 | 21:d801c32231b0 | 250 | } |
cho45 |
5:65d4e94735b6 | 251 | }; |
cho45 |
5:65d4e94735b6 | 252 | |
cho45 |
5:65d4e94735b6 | 253 |