back up of work during May 2019

Dependencies:   microbit

Committer:
tht216
Date:
Wed Jun 05 15:21:14 2019 +0000
Branch:
class_implmentation
Revision:
6:f372773ad32f
Parent:
1:c840c2b6f490
TODO:; 1. multi keypresses; 2. integration

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xx316 1:c840c2b6f490 1 /* mbed Microcontroller Library
xx316 1:c840c2b6f490 2 * Copyright (c) 2015 ARM Limited
xx316 1:c840c2b6f490 3 *
xx316 1:c840c2b6f490 4 * Licensed under the Apache License, Version 2.0 (the "License");
xx316 1:c840c2b6f490 5 * you may not use this file except in compliance with the License.
xx316 1:c840c2b6f490 6 * You may obtain a copy of the License at
xx316 1:c840c2b6f490 7 *
xx316 1:c840c2b6f490 8 * http://www.apache.org/licenses/LICENSE-2.0
xx316 1:c840c2b6f490 9 *
xx316 1:c840c2b6f490 10 * Unless required by applicable law or agreed to in writing, software
xx316 1:c840c2b6f490 11 * distributed under the License is distributed on an "AS IS" BASIS,
xx316 1:c840c2b6f490 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
xx316 1:c840c2b6f490 13 * See the License for the specific language governing permissions and
xx316 1:c840c2b6f490 14 * limitations under the License.
xx316 1:c840c2b6f490 15 */
xx316 1:c840c2b6f490 16
xx316 1:c840c2b6f490 17 #include "mbed.h"
xx316 1:c840c2b6f490 18
xx316 1:c840c2b6f490 19 #include "HIDServiceBase.h"
xx316 1:c840c2b6f490 20
xx316 1:c840c2b6f490 21 enum ButtonState
xx316 1:c840c2b6f490 22 {
xx316 1:c840c2b6f490 23 BUTTON_UP,
xx316 1:c840c2b6f490 24 BUTTON_DOWN
xx316 1:c840c2b6f490 25 };
xx316 1:c840c2b6f490 26
xx316 1:c840c2b6f490 27 enum JoystickButton
xx316 1:c840c2b6f490 28 {
xx316 1:c840c2b6f490 29 JOYSTICK_BUTTON_1 = 0x1,
xx316 1:c840c2b6f490 30 JOYSTICK_BUTTON_2 = 0x2,
xx316 1:c840c2b6f490 31 };
xx316 1:c840c2b6f490 32
xx316 1:c840c2b6f490 33 report_map_t JOYSTICK_REPORT_MAP = {
xx316 1:c840c2b6f490 34 USAGE_PAGE(1), 0x01, // Generic Desktop
xx316 1:c840c2b6f490 35 USAGE(1), 0x04, // Joystick
xx316 1:c840c2b6f490 36 COLLECTION(1), 0x01, // Application
xx316 1:c840c2b6f490 37 COLLECTION(1), 0x00, // Physical
xx316 1:c840c2b6f490 38 USAGE_PAGE(1), 0x09, // Buttons
xx316 1:c840c2b6f490 39 USAGE_MINIMUM(1), 0x01,
xx316 1:c840c2b6f490 40 USAGE_MAXIMUM(1), 0x03,
xx316 1:c840c2b6f490 41 LOGICAL_MINIMUM(1), 0x00,
xx316 1:c840c2b6f490 42 LOGICAL_MAXIMUM(1), 0x01,
xx316 1:c840c2b6f490 43 REPORT_COUNT(1), 0x03, // 2 bits (Buttons)
xx316 1:c840c2b6f490 44 REPORT_SIZE(1), 0x01,
xx316 1:c840c2b6f490 45 INPUT(1), 0x02, // Data, Variable, Absolute
xx316 1:c840c2b6f490 46 REPORT_COUNT(1), 0x01, // 6 bits (Padding)
xx316 1:c840c2b6f490 47 REPORT_SIZE(1), 0x05,
xx316 1:c840c2b6f490 48 INPUT(1), 0x01, // Constant
xx316 1:c840c2b6f490 49 USAGE_PAGE(1), 0x01, // Generic Desktop
xx316 1:c840c2b6f490 50 USAGE(1), 0x30, // X
xx316 1:c840c2b6f490 51 USAGE(1), 0x31, // Y
xx316 1:c840c2b6f490 52 USAGE(1), 0x32, // Z
xx316 1:c840c2b6f490 53 USAGE(1), 0x33, // Rx
xx316 1:c840c2b6f490 54 LOGICAL_MINIMUM(1), 0x81, // -127
xx316 1:c840c2b6f490 55 LOGICAL_MAXIMUM(1), 0x7f, // 127
xx316 1:c840c2b6f490 56 REPORT_SIZE(1), 0x08, // Three bytes
xx316 1:c840c2b6f490 57 REPORT_COUNT(1), 0x04,
xx316 1:c840c2b6f490 58 INPUT(1), 0x02, // Data, Variable, Absolute (unlike mouse)
xx316 1:c840c2b6f490 59 END_COLLECTION(0),
xx316 1:c840c2b6f490 60 END_COLLECTION(0),
xx316 1:c840c2b6f490 61 };
xx316 1:c840c2b6f490 62
xx316 1:c840c2b6f490 63 uint8_t report[] = { 0, 0, 0, 0, 0 };
xx316 1:c840c2b6f490 64
xx316 1:c840c2b6f490 65 class JoystickService: public HIDServiceBase
xx316 1:c840c2b6f490 66 {
xx316 1:c840c2b6f490 67 public:
xx316 1:c840c2b6f490 68 JoystickService(BLE &_ble) :
xx316 1:c840c2b6f490 69 HIDServiceBase(_ble,
xx316 1:c840c2b6f490 70 JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP),
xx316 1:c840c2b6f490 71 inputReport = report,
xx316 1:c840c2b6f490 72 outputReport = NULL,
xx316 1:c840c2b6f490 73 featureReport = NULL,
xx316 1:c840c2b6f490 74 inputReportLength = sizeof(inputReport),
xx316 1:c840c2b6f490 75 outputReportLength = 0,
xx316 1:c840c2b6f490 76 featureReportLength = 0,
xx316 1:c840c2b6f490 77 reportTickerDelay = 20),
xx316 1:c840c2b6f490 78 buttonsState (0),
xx316 1:c840c2b6f490 79 failedReports (0)
xx316 1:c840c2b6f490 80 {
xx316 1:c840c2b6f490 81 speed[0] = 0;
xx316 1:c840c2b6f490 82 speed[1] = 0;
xx316 1:c840c2b6f490 83 speed[2] = 0;
xx316 1:c840c2b6f490 84 speed[3] = 0;
xx316 1:c840c2b6f490 85
xx316 1:c840c2b6f490 86 startReportTicker();
xx316 1:c840c2b6f490 87 }
xx316 1:c840c2b6f490 88
xx316 1:c840c2b6f490 89 int setSpeed(int8_t x, int8_t y, int8_t z)
xx316 1:c840c2b6f490 90 {
xx316 1:c840c2b6f490 91 speed[0] = x;
xx316 1:c840c2b6f490 92 speed[1] = y;
xx316 1:c840c2b6f490 93 speed[2] = z;
xx316 1:c840c2b6f490 94
xx316 1:c840c2b6f490 95 return 0;
xx316 1:c840c2b6f490 96 }
xx316 1:c840c2b6f490 97
xx316 1:c840c2b6f490 98 int setButton(JoystickButton button, ButtonState state)
xx316 1:c840c2b6f490 99 {
xx316 1:c840c2b6f490 100 if (state == BUTTON_UP)
xx316 1:c840c2b6f490 101 buttonsState &= ~(button);
xx316 1:c840c2b6f490 102 else
xx316 1:c840c2b6f490 103 buttonsState |= button;
xx316 1:c840c2b6f490 104
xx316 1:c840c2b6f490 105 return 0;
xx316 1:c840c2b6f490 106 }
xx316 1:c840c2b6f490 107
xx316 1:c840c2b6f490 108 virtual void sendCallback(void) {
xx316 1:c840c2b6f490 109 if (!connected)
xx316 1:c840c2b6f490 110 return;
xx316 1:c840c2b6f490 111
xx316 1:c840c2b6f490 112 report[0] = buttonsState & 0x7;
xx316 1:c840c2b6f490 113 report[1] = speed[0];
xx316 1:c840c2b6f490 114 report[2] = speed[1];
xx316 1:c840c2b6f490 115 report[3] = speed[2];
xx316 1:c840c2b6f490 116 report[4] = speed[3];
xx316 1:c840c2b6f490 117
xx316 1:c840c2b6f490 118 if (send(report))
xx316 1:c840c2b6f490 119 failedReports++;
xx316 1:c840c2b6f490 120 }
xx316 1:c840c2b6f490 121
xx316 1:c840c2b6f490 122 protected:
xx316 1:c840c2b6f490 123 uint8_t buttonsState;
xx316 1:c840c2b6f490 124 uint8_t speed[4];
xx316 1:c840c2b6f490 125
xx316 1:c840c2b6f490 126 public:
xx316 1:c840c2b6f490 127 uint32_t failedReports;
xx316 1:c840c2b6f490 128 };
xx316 1:c840c2b6f490 129