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