[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / io / usb_cdc_hid.c
blobae6942fdaab8b232b105846eb7bce1f5b40b4280
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 #include <stdbool.h>
24 #include "platform.h"
26 #ifdef USE_USB_CDC_HID
28 #include "common/maths.h"
30 #include "fc/rc_controls.h"
32 #include "rx/rx.h"
34 #include "pg/usb.h"
36 #include "sensors/battery.h"
38 //TODO: Make it platform independent in the future
39 #if defined(STM32F4)
40 #include "vcpf4/usbd_cdc_vcp.h"
41 #include "usbd_hid_core.h"
42 #elif defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
43 #include "drivers/serial_usb_vcp.h"
44 #include "usbd_hid.h"
45 #include "vcp_hal/usbd_cdc_interface.h"
46 #endif
48 #define USB_CDC_HID_NUM_AXES 8
49 #define USB_CDC_HID_NUM_BUTTONS 8
51 #define USB_CDC_HID_RANGE_MIN -127
52 #define USB_CDC_HID_RANGE_MAX 127
54 // In the windows joystick driver, the axes are defined as shown in the second column.
56 const uint8_t hidChannelMapping[] = {
57 ROLL, // X
58 PITCH, // Y
59 AUX3, // Z
60 YAW, // X Rotation
61 AUX1, // Z Rotation
62 THROTTLE, // Y Rotation
63 AUX4, // Slider
64 AUX2, // Dial
65 AUX5, // Button 1
66 AUX6, // Button 2
67 AUX7, // Button 3
68 AUX8, // Button 4
69 AUX9, // Button 5
70 AUX10, // Button 6
71 AUX11, // Button 7
72 AUX12 // Button 8
75 void sendRcDataToHid(void)
77 int8_t report[9];
78 // Axes
79 for (unsigned i = 0; i < USB_CDC_HID_NUM_AXES; i++) {
80 const uint8_t channel = hidChannelMapping[i];
81 report[i] = scaleRange(constrain(rcData[channel], PWM_RANGE_MIN, PWM_RANGE_MAX), PWM_RANGE_MIN, PWM_RANGE_MAX, USB_CDC_HID_RANGE_MIN, USB_CDC_HID_RANGE_MAX);
82 if (channel == PITCH) {
83 // PITCH is inverted in Windows
84 report[i] = -report[i];
88 // Buttons
89 // Each bit in one byte represents one button so we have 8 buttons in one-byte-data
90 report[8] = 0;
91 for (unsigned i = 0; i < USB_CDC_HID_NUM_BUTTONS; i++) {
92 const uint8_t channel = hidChannelMapping[i + USB_CDC_HID_NUM_AXES];
93 if (scaleRange(constrain(rcData[channel], PWM_RANGE_MIN, PWM_RANGE_MAX), PWM_RANGE_MIN, PWM_RANGE_MAX, USB_CDC_HID_RANGE_MIN, USB_CDC_HID_RANGE_MAX) > 0) {
94 report[8] |= (1 << i);
97 #if defined(STM32F4)
98 USBD_HID_SendReport(&USB_OTG_dev, (uint8_t*)report, sizeof(report));
99 #elif defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
100 USBD_HID_SendReport(&USBD_Device, (uint8_t*)report, sizeof(report));
101 #else
102 # error "MCU does not support USB HID."
103 #endif
106 bool cdcDeviceIsMayBeActive(void)
108 return usbDevConfig()->type == COMPOSITE && usbIsConnected() && (getBatteryState() == BATTERY_NOT_PRESENT || batteryConfig()->voltageMeterSource == VOLTAGE_METER_NONE);
110 #endif