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"
29 #include "fc/rc_controls.h"
32 #include "sensors/battery.h"
33 #include "usb_cdc_hid.h"
35 #define USB_CDC_HID_NUM_AXES 8
36 #define USB_CDC_HID_NUM_BUTTONS 8
38 #define USB_CDC_HID_RANGE_MIN -127
39 #define USB_CDC_HID_RANGE_MAX 127
41 // In the windows joystick driver, the axes are defined as shown in the second column.
43 const uint8_t hidChannelMapping
[] = {
49 THROTTLE
, // Y Rotation
62 void sendRcDataToHid(void)
66 for (unsigned i
= 0; i
< USB_CDC_HID_NUM_AXES
; i
++) {
67 const uint8_t channel
= hidChannelMapping
[i
];
68 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
);
69 if (channel
== PITCH
) {
70 // PITCH is inverted in Windows
71 report
[i
] = -report
[i
];
76 // Each bit in one byte represents one button so we have 8 buttons in one-byte-data
78 for (unsigned i
= 0; i
< USB_CDC_HID_NUM_BUTTONS
; i
++) {
79 const uint8_t channel
= hidChannelMapping
[i
+ USB_CDC_HID_NUM_AXES
];
80 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) {
81 report
[8] |= (1 << i
);
85 sendReport((uint8_t*)report
, sizeof(report
));
88 bool cdcDeviceIsMayBeActive(void)
90 return usbDevConfig()->type
== COMPOSITE
&& usbIsConnected() && (getBatteryState() == BATTERY_NOT_PRESENT
|| batteryConfig()->voltageMeterSource
== VOLTAGE_METER_NONE
);