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/>.
25 #ifdef USE_USB_CDC_HID
27 #include "common/maths.h"
28 #include "fc/rc_controls.h"
31 #include "sensors/battery.h"
32 #include "usb_cdc_hid.h"
34 #define USB_CDC_HID_NUM_AXES 8
35 #define USB_CDC_HID_NUM_BUTTONS 8
37 #define USB_CDC_HID_RANGE_MIN -127
38 #define USB_CDC_HID_RANGE_MAX 127
40 // In the windows joystick driver, the axes are defined as shown in the second column.
42 const uint8_t hidChannelMapping
[] = {
48 THROTTLE
, // Y Rotation
61 void sendRcDataToHid(void)
65 for (unsigned i
= 0; i
< USB_CDC_HID_NUM_AXES
; i
++) {
66 const uint8_t channel
= hidChannelMapping
[i
];
67 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
);
68 if (channel
== PITCH
) {
69 // PITCH is inverted in Windows
70 report
[i
] = -report
[i
];
75 // Each bit in one byte represents one button so we have 8 buttons in one-byte-data
77 for (unsigned i
= 0; i
< USB_CDC_HID_NUM_BUTTONS
; i
++) {
78 const uint8_t channel
= hidChannelMapping
[i
+ USB_CDC_HID_NUM_AXES
];
79 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) {
80 report
[8] |= (1 << i
);
84 sendReport((uint8_t*)report
, sizeof(report
));
87 bool cdcDeviceIsMayBeActive(void)
89 return usbDevConfig()->type
== COMPOSITE
&& usbIsConnected() && (getBatteryState() == BATTERY_NOT_PRESENT
|| batteryConfig()->voltageMeterSource
== VOLTAGE_METER_NONE
);