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 #ifndef HID_SERVICE_BASE_H_
JonnyA 0:cb1939018833 18 #define HID_SERVICE_BASE_H_
JonnyA 0:cb1939018833 19
JonnyA 0:cb1939018833 20 #include "mbed.h"
JonnyA 0:cb1939018833 21
JonnyA 0:cb1939018833 22 #include "ble/BLE.h"
JonnyA 0:cb1939018833 23 #include "HID_types.h"
JonnyA 0:cb1939018833 24
JonnyA 0:cb1939018833 25 #define BLE_UUID_DESCRIPTOR_REPORT_REFERENCE 0x2908
JonnyA 0:cb1939018833 26
JonnyA 0:cb1939018833 27 typedef const uint8_t report_map_t[];
JonnyA 0:cb1939018833 28 typedef const uint8_t * report_t;
JonnyA 0:cb1939018833 29
JonnyA 0:cb1939018833 30 typedef struct {
JonnyA 0:cb1939018833 31 uint16_t bcdHID;
JonnyA 0:cb1939018833 32 uint8_t bCountryCode;
JonnyA 0:cb1939018833 33 uint8_t flags;
JonnyA 0:cb1939018833 34 } HID_information_t;
JonnyA 0:cb1939018833 35
JonnyA 0:cb1939018833 36 enum ReportType {
JonnyA 0:cb1939018833 37 INPUT_REPORT = 0x1,
JonnyA 0:cb1939018833 38 OUTPUT_REPORT = 0x2,
JonnyA 0:cb1939018833 39 FEATURE_REPORT = 0x3,
JonnyA 0:cb1939018833 40 };
JonnyA 0:cb1939018833 41
JonnyA 0:cb1939018833 42 enum ProtocolMode {
JonnyA 0:cb1939018833 43 BOOT_PROTOCOL = 0x0,
JonnyA 0:cb1939018833 44 REPORT_PROTOCOL = 0x1,
JonnyA 0:cb1939018833 45 };
JonnyA 0:cb1939018833 46
JonnyA 0:cb1939018833 47 typedef struct {
JonnyA 0:cb1939018833 48 uint8_t ID;
JonnyA 0:cb1939018833 49 uint8_t type;
JonnyA 0:cb1939018833 50 } report_reference_t;
JonnyA 0:cb1939018833 51
JonnyA 0:cb1939018833 52
JonnyA 0:cb1939018833 53 class HIDServiceBase {
JonnyA 0:cb1939018833 54 public:
JonnyA 0:cb1939018833 55 /**
JonnyA 0:cb1939018833 56 * Constructor
JonnyA 0:cb1939018833 57 *
JonnyA 0:cb1939018833 58 * @param _ble
JonnyA 0:cb1939018833 59 * BLE object to add this service to
JonnyA 0:cb1939018833 60 * @param reportMap
JonnyA 0:cb1939018833 61 * Byte array representing the input/output report formats. In USB HID jargon, it
JonnyA 0:cb1939018833 62 * is called "HID report descriptor".
JonnyA 0:cb1939018833 63 * @param reportMapLength
JonnyA 0:cb1939018833 64 * Size of the reportMap array
JonnyA 0:cb1939018833 65 * @param outputReportLength
JonnyA 0:cb1939018833 66 * Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
JonnyA 0:cb1939018833 67 * @param inputReportLength
JonnyA 0:cb1939018833 68 * Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
JonnyA 0:cb1939018833 69 * @param inputReportTickerDelay
JonnyA 0:cb1939018833 70 * Delay between input report notifications, in ms. Acceptable values depend directly on
JonnyA 0:cb1939018833 71 * GAP's connInterval parameter, so it shouldn't be less than 12ms
JonnyA 0:cb1939018833 72 * Preferred GAP connection interval is set after this value, in order to send
JonnyA 0:cb1939018833 73 * notifications as quick as possible: minimum connection interval will be set to
JonnyA 0:cb1939018833 74 * (inputReportTickerDelay / 2)
JonnyA 0:cb1939018833 75 */
JonnyA 0:cb1939018833 76 HIDServiceBase(BLE &_ble,
JonnyA 0:cb1939018833 77 report_map_t reportMap,
JonnyA 0:cb1939018833 78 uint8_t reportMapLength,
JonnyA 0:cb1939018833 79 report_t inputReport,
JonnyA 0:cb1939018833 80 report_t outputReport,
JonnyA 0:cb1939018833 81 report_t featureReport,
JonnyA 0:cb1939018833 82 uint8_t inputReportLength = 0,
JonnyA 0:cb1939018833 83 uint8_t outputReportLength = 0,
JonnyA 0:cb1939018833 84 uint8_t featureReportLength = 0,
JonnyA 0:cb1939018833 85 uint8_t inputReportTickerDelay = 50);
JonnyA 0:cb1939018833 86
JonnyA 0:cb1939018833 87 /**
JonnyA 0:cb1939018833 88 * Send Report
JonnyA 0:cb1939018833 89 *
JonnyA 0:cb1939018833 90 * @param report Report to send. Must be of size @ref inputReportLength
JonnyA 0:cb1939018833 91 * @return The write status
JonnyA 0:cb1939018833 92 *
JonnyA 0:cb1939018833 93 * @note Don't call send() directly for multiple reports! Use reportTicker for that, in order
JonnyA 0:cb1939018833 94 * to avoid overloading the BLE stack, and let it handle events between each report.
JonnyA 0:cb1939018833 95 */
JonnyA 0:cb1939018833 96 virtual ble_error_t send(const report_t report);
JonnyA 0:cb1939018833 97
JonnyA 0:cb1939018833 98 /**
JonnyA 0:cb1939018833 99 * Read Report
JonnyA 0:cb1939018833 100 *
JonnyA 0:cb1939018833 101 * @param report Report to fill. Must be of size @ref outputReportLength
JonnyA 0:cb1939018833 102 * @return The read status
JonnyA 0:cb1939018833 103 */
JonnyA 0:cb1939018833 104 virtual ble_error_t read(report_t report);
JonnyA 0:cb1939018833 105
JonnyA 0:cb1939018833 106 virtual void onConnection(const Gap::ConnectionCallbackParams_t *params);
JonnyA 0:cb1939018833 107 virtual void onDisconnection(const Gap::DisconnectionCallbackParams_t *params);
JonnyA 0:cb1939018833 108
JonnyA 0:cb1939018833 109 virtual bool isConnected(void)
JonnyA 0:cb1939018833 110 {
JonnyA 0:cb1939018833 111 return connected;
JonnyA 0:cb1939018833 112 }
JonnyA 0:cb1939018833 113
JonnyA 0:cb1939018833 114 protected:
JonnyA 0:cb1939018833 115 /**
JonnyA 0:cb1939018833 116 * Called by BLE API when data has been successfully sent.
JonnyA 0:cb1939018833 117 *
JonnyA 0:cb1939018833 118 * @param count Number of reports sent
JonnyA 0:cb1939018833 119 *
JonnyA 0:cb1939018833 120 * @note Subclasses can override this to avoid starting the report ticker when there is nothing
JonnyA 0:cb1939018833 121 * to send
JonnyA 0:cb1939018833 122 */
JonnyA 0:cb1939018833 123 virtual void onDataSent(unsigned count);
JonnyA 0:cb1939018833 124
JonnyA 0:cb1939018833 125 /**
JonnyA 0:cb1939018833 126 * Start the ticker that sends input reports at regular interval
JonnyA 0:cb1939018833 127 *
JonnyA 0:cb1939018833 128 * @note reportTickerIsActive describes the state of the ticker and can be used by HIDS
JonnyA 0:cb1939018833 129 * implementations.
JonnyA 0:cb1939018833 130 */
JonnyA 0:cb1939018833 131 virtual void startReportTicker(void);
JonnyA 0:cb1939018833 132
JonnyA 0:cb1939018833 133 /**
JonnyA 0:cb1939018833 134 * Stop the input report ticker
JonnyA 0:cb1939018833 135 */
JonnyA 0:cb1939018833 136 virtual void stopReportTicker(void);
JonnyA 0:cb1939018833 137
JonnyA 0:cb1939018833 138 /**
JonnyA 0:cb1939018833 139 * Called by input report ticker at regular interval (reportTickerDelay). This must be
JonnyA 0:cb1939018833 140 * overriden by HIDS implementations to call the @ref send() with a report, if necessary.
JonnyA 0:cb1939018833 141 */
JonnyA 0:cb1939018833 142 virtual void sendCallback(void) = 0;
JonnyA 0:cb1939018833 143
JonnyA 0:cb1939018833 144 /**
JonnyA 0:cb1939018833 145 * Create the Gatt descriptor for a report characteristic
JonnyA 0:cb1939018833 146 */
JonnyA 0:cb1939018833 147 GattAttribute** inputReportDescriptors();
JonnyA 0:cb1939018833 148 GattAttribute** outputReportDescriptors();
JonnyA 0:cb1939018833 149 GattAttribute** featureReportDescriptors();
JonnyA 0:cb1939018833 150
JonnyA 0:cb1939018833 151 /**
JonnyA 0:cb1939018833 152 * Create the HID information structure
JonnyA 0:cb1939018833 153 */
JonnyA 0:cb1939018833 154 HID_information_t* HIDInformation();
JonnyA 0:cb1939018833 155
JonnyA 0:cb1939018833 156 protected:
JonnyA 0:cb1939018833 157 BLE &ble;
JonnyA 0:cb1939018833 158 bool connected;
JonnyA 0:cb1939018833 159
JonnyA 0:cb1939018833 160 int reportMapLength;
JonnyA 0:cb1939018833 161
JonnyA 0:cb1939018833 162 report_t inputReport;
JonnyA 0:cb1939018833 163 report_t outputReport;
JonnyA 0:cb1939018833 164 report_t featureReport;
JonnyA 0:cb1939018833 165
JonnyA 0:cb1939018833 166 uint8_t inputReportLength;
JonnyA 0:cb1939018833 167 uint8_t outputReportLength;
JonnyA 0:cb1939018833 168 uint8_t featureReportLength;
JonnyA 0:cb1939018833 169
JonnyA 0:cb1939018833 170 uint8_t controlPointCommand;
JonnyA 0:cb1939018833 171 uint8_t protocolMode;
JonnyA 0:cb1939018833 172
JonnyA 0:cb1939018833 173 report_reference_t inputReportReferenceData;
JonnyA 0:cb1939018833 174 report_reference_t outputReportReferenceData;
JonnyA 0:cb1939018833 175 report_reference_t featureReportReferenceData;
JonnyA 0:cb1939018833 176
JonnyA 0:cb1939018833 177 GattAttribute inputReportReferenceDescriptor;
JonnyA 0:cb1939018833 178 GattAttribute outputReportReferenceDescriptor;
JonnyA 0:cb1939018833 179 GattAttribute featureReportReferenceDescriptor;
JonnyA 0:cb1939018833 180
JonnyA 0:cb1939018833 181 // Optional gatt characteristics:
JonnyA 0:cb1939018833 182 GattCharacteristic protocolModeCharacteristic;
JonnyA 0:cb1939018833 183
JonnyA 0:cb1939018833 184 // Report characteristics (each sort of optional)
JonnyA 0:cb1939018833 185 GattCharacteristic inputReportCharacteristic;
JonnyA 0:cb1939018833 186 GattCharacteristic outputReportCharacteristic;
JonnyA 0:cb1939018833 187 GattCharacteristic featureReportCharacteristic;
JonnyA 0:cb1939018833 188
JonnyA 0:cb1939018833 189 // Required gatt characteristics: Report Map, Information, Control Point
JonnyA 0:cb1939018833 190 GattCharacteristic reportMapCharacteristic;
JonnyA 0:cb1939018833 191 ReadOnlyGattCharacteristic<HID_information_t> HIDInformationCharacteristic;
JonnyA 0:cb1939018833 192 GattCharacteristic HIDControlPointCharacteristic;
JonnyA 0:cb1939018833 193
JonnyA 0:cb1939018833 194 Ticker reportTicker;
JonnyA 0:cb1939018833 195 uint32_t reportTickerDelay;
JonnyA 0:cb1939018833 196 bool reportTickerIsActive;
JonnyA 0:cb1939018833 197 };
JonnyA 0:cb1939018833 198
JonnyA 0:cb1939018833 199 #endif /* !HID_SERVICE_BASE_H_ */
JonnyA 0:cb1939018833 200