From 60cc80825aaf193b6107e138e703e7879a614afa Mon Sep 17 00:00:00 2001 From: dkustec Date: Thu, 13 Aug 2020 18:37:33 +0200 Subject: [PATCH] Disable stick commands when HID is active and USB is connected Moved to shared code and cms.c refactoring Added missing line endings and comments Passing tests - defines added Added missing definition Added check for USE_USB_CDC_HID in cmsUpdate Code refactoring and condition change Remove unnecessary include Disable stick commands when HID is active and USB is connected cdcDeviceIsMayBeActive refactored Disable stick commands when HID is active and USB is connected --- src/main/cms/cms.c | 10 ++-------- src/main/fc/rc_controls.c | 8 ++++++++ src/main/io/usb_cdc_hid.c | 12 ++++++++++++ src/main/io/usb_cdc_hid.h | 1 + 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/cms/cms.c b/src/main/cms/cms.c index de8395e9e7..0669c15127 100644 --- a/src/main/cms/cms.c +++ b/src/main/cms/cms.c @@ -61,22 +61,16 @@ #include "flight/mixer.h" #include "io/rcdevice_cam.h" +#include "io/usb_cdc_hid.h" #include "pg/pg.h" #include "pg/pg_ids.h" #include "pg/rx.h" -#ifdef USE_USB_CDC_HID -#include "pg/usb.h" -#endif #include "osd/osd.h" #include "rx/rx.h" -#ifdef USE_USB_CDC_HID -#include "sensors/battery.h" -#endif - // DisplayPort management #ifndef CMS_MAX_DEVICE @@ -1202,7 +1196,7 @@ static void cmsUpdate(uint32_t currentTimeUs) || rcdeviceInMenu #endif #ifdef USE_USB_CDC_HID - || (getBatteryCellCount() == 0 && usbDevConfig()->type == COMPOSITE) + || cdcDeviceIsMayBeActive() // If this target is used as a joystick, we should leave here. #endif ) { return; diff --git a/src/main/fc/rc_controls.c b/src/main/fc/rc_controls.c index 8678812888..beeab54cb5 100644 --- a/src/main/fc/rc_controls.c +++ b/src/main/fc/rc_controls.c @@ -46,6 +46,7 @@ #include "flight/failsafe.h" #include "io/beeper.h" +#include "io/usb_cdc_hid.h" #include "io/dashboard.h" #include "io/gps.h" #include "io/vtx_control.h" @@ -228,6 +229,13 @@ void processRcStickPositions() } doNotRepeat = true; + #ifdef USE_USB_CDC_HID + // If this target is used as a joystick, we should leave here. + if (cdcDeviceIsMayBeActive()) { + return; + } + #endif + // actions during not armed if (rcSticks == THR_LO + YAW_LO + PIT_LO + ROL_CE) { diff --git a/src/main/io/usb_cdc_hid.c b/src/main/io/usb_cdc_hid.c index 96b86fb7b0..99a5233be6 100644 --- a/src/main/io/usb_cdc_hid.c +++ b/src/main/io/usb_cdc_hid.c @@ -18,6 +18,9 @@ * If not, see . */ + +#include + #include "platform.h" #ifdef USE_USB_CDC_HID @@ -28,6 +31,10 @@ #include "rx/rx.h" +#include "pg/usb.h" + +#include "sensors/battery.h" + //TODO: Make it platform independent in the future #if defined(STM32F4) #include "vcpf4/usbd_cdc_vcp.h" @@ -75,4 +82,9 @@ void sendRcDataToHid(void) # error "MCU does not support USB HID." #endif } + +bool cdcDeviceIsMayBeActive() +{ + return usbDevConfig()->type == COMPOSITE && usbIsConnected() && (getBatteryState() == BATTERY_NOT_PRESENT || batteryConfig()->voltageMeterSource == VOLTAGE_METER_NONE); +} #endif diff --git a/src/main/io/usb_cdc_hid.h b/src/main/io/usb_cdc_hid.h index ff0b538862..38261701a0 100644 --- a/src/main/io/usb_cdc_hid.h +++ b/src/main/io/usb_cdc_hid.h @@ -21,3 +21,4 @@ #pragma once void sendRcDataToHid(void); +bool cdcDeviceIsMayBeActive();