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:
Wed Nov 14 09:46:45 2018 +0000
Revision:
3:7d7822143a2d
Parent:
0:cb1939018833
Add simple debounce wait on interrupts for buttons

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 "HIDServiceBase.h"
JonnyA 0:cb1939018833 20
JonnyA 0:cb1939018833 21 enum ButtonState
JonnyA 0:cb1939018833 22 {
JonnyA 0:cb1939018833 23 BUTTON_UP,
JonnyA 0:cb1939018833 24 BUTTON_DOWN
JonnyA 0:cb1939018833 25 };
JonnyA 0:cb1939018833 26
JonnyA 0:cb1939018833 27 enum JoystickButton
JonnyA 0:cb1939018833 28 {
JonnyA 0:cb1939018833 29 JOYSTICK_BUTTON_1 = 0x1,
JonnyA 0:cb1939018833 30 JOYSTICK_BUTTON_2 = 0x2,
JonnyA 0:cb1939018833 31 };
JonnyA 0:cb1939018833 32
JonnyA 0:cb1939018833 33 report_map_t JOYSTICK_REPORT_MAP = {
JonnyA 0:cb1939018833 34 USAGE_PAGE(1), 0x01, // Generic Desktop
JonnyA 0:cb1939018833 35 USAGE(1), 0x04, // Joystick
JonnyA 0:cb1939018833 36 COLLECTION(1), 0x01, // Application
JonnyA 0:cb1939018833 37 COLLECTION(1), 0x00, // Physical
JonnyA 0:cb1939018833 38 USAGE_PAGE(1), 0x09, // Buttons
JonnyA 0:cb1939018833 39 USAGE_MINIMUM(1), 0x01,
JonnyA 0:cb1939018833 40 USAGE_MAXIMUM(1), 0x03,
JonnyA 0:cb1939018833 41 LOGICAL_MINIMUM(1), 0x00,
JonnyA 0:cb1939018833 42 LOGICAL_MAXIMUM(1), 0x01,
JonnyA 0:cb1939018833 43 REPORT_COUNT(1), 0x03, // 2 bits (Buttons)
JonnyA 0:cb1939018833 44 REPORT_SIZE(1), 0x01,
JonnyA 0:cb1939018833 45 INPUT(1), 0x02, // Data, Variable, Absolute
JonnyA 0:cb1939018833 46 REPORT_COUNT(1), 0x01, // 6 bits (Padding)
JonnyA 0:cb1939018833 47 REPORT_SIZE(1), 0x05,
JonnyA 0:cb1939018833 48 INPUT(1), 0x01, // Constant
JonnyA 0:cb1939018833 49 USAGE_PAGE(1), 0x01, // Generic Desktop
JonnyA 0:cb1939018833 50 USAGE(1), 0x30, // X
JonnyA 0:cb1939018833 51 USAGE(1), 0x31, // Y
JonnyA 0:cb1939018833 52 USAGE(1), 0x32, // Z
JonnyA 0:cb1939018833 53 USAGE(1), 0x33, // Rx
JonnyA 0:cb1939018833 54 LOGICAL_MINIMUM(1), 0x81, // -127
JonnyA 0:cb1939018833 55 LOGICAL_MAXIMUM(1), 0x7f, // 127
JonnyA 0:cb1939018833 56 REPORT_SIZE(1), 0x08, // Three bytes
JonnyA 0:cb1939018833 57 REPORT_COUNT(1), 0x04,
JonnyA 0:cb1939018833 58 INPUT(1), 0x02, // Data, Variable, Absolute (unlike mouse)
JonnyA 0:cb1939018833 59 END_COLLECTION(0),
JonnyA 0:cb1939018833 60 END_COLLECTION(0),
JonnyA 0:cb1939018833 61 };
JonnyA 0:cb1939018833 62
JonnyA 0:cb1939018833 63 uint8_t report[] = { 0, 0, 0, 0, 0 };
JonnyA 0:cb1939018833 64
JonnyA 0:cb1939018833 65 class JoystickService: public HIDServiceBase
JonnyA 0:cb1939018833 66 {
JonnyA 0:cb1939018833 67 public:
JonnyA 0:cb1939018833 68 JoystickService(BLE &_ble) :
JonnyA 0:cb1939018833 69 HIDServiceBase(_ble,
JonnyA 0:cb1939018833 70 JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP),
JonnyA 0:cb1939018833 71 inputReport = report,
JonnyA 0:cb1939018833 72 outputReport = NULL,
JonnyA 0:cb1939018833 73 featureReport = NULL,
JonnyA 0:cb1939018833 74 inputReportLength = sizeof(inputReport),
JonnyA 0:cb1939018833 75 outputReportLength = 0,
JonnyA 0:cb1939018833 76 featureReportLength = 0,
JonnyA 0:cb1939018833 77 reportTickerDelay = 20),
JonnyA 0:cb1939018833 78 buttonsState (0),
JonnyA 0:cb1939018833 79 failedReports (0)
JonnyA 0:cb1939018833 80 {
JonnyA 0:cb1939018833 81 speed[0] = 0;
JonnyA 0:cb1939018833 82 speed[1] = 0;
JonnyA 0:cb1939018833 83 speed[2] = 0;
JonnyA 0:cb1939018833 84 speed[3] = 0;
JonnyA 0:cb1939018833 85
JonnyA 0:cb1939018833 86 startReportTicker();
JonnyA 0:cb1939018833 87 }
JonnyA 0:cb1939018833 88
JonnyA 0:cb1939018833 89 int setSpeed(int8_t x, int8_t y, int8_t z)
JonnyA 0:cb1939018833 90 {
JonnyA 0:cb1939018833 91 speed[0] = x;
JonnyA 0:cb1939018833 92 speed[1] = y;
JonnyA 0:cb1939018833 93 speed[2] = z;
JonnyA 0:cb1939018833 94
JonnyA 0:cb1939018833 95 return 0;
JonnyA 0:cb1939018833 96 }
JonnyA 0:cb1939018833 97
JonnyA 0:cb1939018833 98 int setButton(JoystickButton button, ButtonState state)
JonnyA 0:cb1939018833 99 {
JonnyA 0:cb1939018833 100 if (state == BUTTON_UP)
JonnyA 0:cb1939018833 101 buttonsState &= ~(button);
JonnyA 0:cb1939018833 102 else
JonnyA 0:cb1939018833 103 buttonsState |= button;
JonnyA 0:cb1939018833 104
JonnyA 0:cb1939018833 105 return 0;
JonnyA 0:cb1939018833 106 }
JonnyA 0:cb1939018833 107
JonnyA 0:cb1939018833 108 virtual void sendCallback(void) {
JonnyA 0:cb1939018833 109 if (!connected)
JonnyA 0:cb1939018833 110 return;
JonnyA 0:cb1939018833 111
JonnyA 0:cb1939018833 112 report[0] = buttonsState & 0x7;
JonnyA 0:cb1939018833 113 report[1] = speed[0];
JonnyA 0:cb1939018833 114 report[2] = speed[1];
JonnyA 0:cb1939018833 115 report[3] = speed[2];
JonnyA 0:cb1939018833 116 report[4] = speed[3];
JonnyA 0:cb1939018833 117
JonnyA 0:cb1939018833 118 if (send(report))
JonnyA 0:cb1939018833 119 failedReports++;
JonnyA 0:cb1939018833 120 }
JonnyA 0:cb1939018833 121
JonnyA 0:cb1939018833 122 protected:
JonnyA 0:cb1939018833 123 uint8_t buttonsState;
JonnyA 0:cb1939018833 124 uint8_t speed[4];
JonnyA 0:cb1939018833 125
JonnyA 0:cb1939018833 126 public:
JonnyA 0:cb1939018833 127 uint32_t failedReports;
JonnyA 0:cb1939018833 128 };
JonnyA 0:cb1939018833 129