back up of work during May 2019
Dependencies: microbit
JoystickService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 00019 #include "HIDServiceBase.h" 00020 00021 enum ButtonState 00022 { 00023 BUTTON_UP, 00024 BUTTON_DOWN 00025 }; 00026 00027 enum JoystickButton 00028 { 00029 JOYSTICK_BUTTON_1 = 0x1, 00030 JOYSTICK_BUTTON_2 = 0x2, 00031 }; 00032 00033 report_map_t JOYSTICK_REPORT_MAP = { 00034 USAGE_PAGE(1), 0x01, // Generic Desktop 00035 USAGE(1), 0x04, // Joystick 00036 COLLECTION(1), 0x01, // Application 00037 COLLECTION(1), 0x00, // Physical 00038 USAGE_PAGE(1), 0x09, // Buttons 00039 USAGE_MINIMUM(1), 0x01, 00040 USAGE_MAXIMUM(1), 0x03, 00041 LOGICAL_MINIMUM(1), 0x00, 00042 LOGICAL_MAXIMUM(1), 0x01, 00043 REPORT_COUNT(1), 0x03, // 2 bits (Buttons) 00044 REPORT_SIZE(1), 0x01, 00045 INPUT(1), 0x02, // Data, Variable, Absolute 00046 REPORT_COUNT(1), 0x01, // 6 bits (Padding) 00047 REPORT_SIZE(1), 0x05, 00048 INPUT(1), 0x01, // Constant 00049 USAGE_PAGE(1), 0x01, // Generic Desktop 00050 USAGE(1), 0x30, // X 00051 USAGE(1), 0x31, // Y 00052 USAGE(1), 0x32, // Z 00053 USAGE(1), 0x33, // Rx 00054 LOGICAL_MINIMUM(1), 0x81, // -127 00055 LOGICAL_MAXIMUM(1), 0x7f, // 127 00056 REPORT_SIZE(1), 0x08, // Three bytes 00057 REPORT_COUNT(1), 0x04, 00058 INPUT(1), 0x02, // Data, Variable, Absolute (unlike mouse) 00059 END_COLLECTION(0), 00060 END_COLLECTION(0), 00061 }; 00062 00063 uint8_t report[] = { 0, 0, 0, 0, 0 }; 00064 00065 class JoystickService: public HIDServiceBase 00066 { 00067 public: 00068 JoystickService(BLE &_ble) : 00069 HIDServiceBase(_ble, 00070 JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP), 00071 inputReport = report, 00072 outputReport = NULL, 00073 featureReport = NULL, 00074 inputReportLength = sizeof(inputReport), 00075 outputReportLength = 0, 00076 featureReportLength = 0, 00077 reportTickerDelay = 20), 00078 buttonsState (0), 00079 failedReports (0) 00080 { 00081 speed[0] = 0; 00082 speed[1] = 0; 00083 speed[2] = 0; 00084 speed[3] = 0; 00085 00086 startReportTicker(); 00087 } 00088 00089 int setSpeed(int8_t x, int8_t y, int8_t z) 00090 { 00091 speed[0] = x; 00092 speed[1] = y; 00093 speed[2] = z; 00094 00095 return 0; 00096 } 00097 00098 int setButton(JoystickButton button, ButtonState state) 00099 { 00100 if (state == BUTTON_UP) 00101 buttonsState &= ~(button); 00102 else 00103 buttonsState |= button; 00104 00105 return 0; 00106 } 00107 00108 virtual void sendCallback(void) { 00109 if (!connected) 00110 return; 00111 00112 report[0] = buttonsState & 0x7; 00113 report[1] = speed[0]; 00114 report[2] = speed[1]; 00115 report[3] = speed[2]; 00116 report[4] = speed[3]; 00117 00118 if (send(report)) 00119 failedReports++; 00120 } 00121 00122 protected: 00123 uint8_t buttonsState; 00124 uint8_t speed[4]; 00125 00126 public: 00127 uint32_t failedReports; 00128 }; 00129
Generated on Mon Jul 18 2022 14:25:12 by 1.7.2