Logging of the S -term values in blackbox for Fixed wings. (#14012)
[betaflight.git] / src / main / io / usb_cdc_hid.c
blob6e9c189e8df23a8efd54cc8c1661aa72b0f53242
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/>.
21 #include <stdbool.h>
23 #include "platform.h"
25 #ifdef USE_USB_CDC_HID
27 #include "common/maths.h"
28 #include "fc/rc_controls.h"
29 #include "rx/rx.h"
30 #include "pg/usb.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[] = {
43 ROLL, // X
44 PITCH, // Y
45 AUX3, // Z
46 YAW, // X Rotation
47 AUX1, // Z Rotation
48 THROTTLE, // Y Rotation
49 AUX4, // Slider
50 AUX2, // Dial
51 AUX5, // Button 1
52 AUX6, // Button 2
53 AUX7, // Button 3
54 AUX8, // Button 4
55 AUX9, // Button 5
56 AUX10, // Button 6
57 AUX11, // Button 7
58 AUX12 // Button 8
61 void sendRcDataToHid(void)
63 int8_t report[9];
64 // Axes
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];
74 // Buttons
75 // Each bit in one byte represents one button so we have 8 buttons in one-byte-data
76 report[8] = 0;
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);
91 #endif