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)
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/>.
26 #ifdef USE_USB_CDC_HID
28 #include "common/maths.h"
30 #include "fc/rc_controls.h"
36 #include "sensors/battery.h"
38 //TODO: Make it platform independent in the future
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"
45 #include "vcp_hal/usbd_cdc_interface.h"
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
[] = {
62 THROTTLE
, // Y Rotation
75 void sendRcDataToHid(void)
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
];
89 // Each bit in one byte represents one button so we have 8 buttons in one-byte-data
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
);
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
));
102 # error "MCU does not support USB HID."
106 bool cdcDeviceIsMayBeActive()
108 return usbDevConfig()->type
== COMPOSITE
&& usbIsConnected() && (getBatteryState() == BATTERY_NOT_PRESENT
|| batteryConfig()->voltageMeterSource
== VOLTAGE_METER_NONE
);