BLE switch interface with GROVE joystic for micro:bit http://mahoro-ba.net/e2073.html

Dependencies:   microbit

Committer:
masakjm
Date:
Tue Apr 09 19:37:57 2019 +0000
Revision:
21:961da341b755
Parent:
0:28fb3e9ef81a
Added setting function of installation direction.

Who changed what in which revision?

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