From e2f23fd7994dae93def2c84e99fd2ad717bd045d Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Tue, 1 Oct 2024 16:25:05 +0200 Subject: [PATCH] fix s3 implementation --- esphome/components/usb_device/usb_device.cpp | 26 +++++++++++++++++--- esphome/components/usb_device/usb_device.h | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/esphome/components/usb_device/usb_device.cpp b/esphome/components/usb_device/usb_device.cpp index e5ab23397d..500b573b53 100644 --- a/esphome/components/usb_device/usb_device.cpp +++ b/esphome/components/usb_device/usb_device.cpp @@ -2,7 +2,12 @@ #include "usb_device.h" #include "esphome/core/log.h" #include "USB.h" - +// based on defines in HWCDC.cpp +#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 +#if ARDUINO_USB_MODE +#include "HWCDC.h" +#endif +#endif extern "C" { bool configured; @@ -26,20 +31,35 @@ void UsbDevice::update() { #ifdef USE_BINARY_SENSOR if (configured_ != nullptr) { // bool configured = USB; - configured_->publish_state(configured); + configured_->publish_state(get_configured_()); } #endif } void UsbDevice::dump_config() { // bool configured = USB; - ESP_LOGCONFIG(TAG, "USB device - configured: %s", YESNO(configured)); + ESP_LOGCONFIG(TAG, "USB device - configured: %s", YESNO(get_configured_())); } #ifdef USE_BINARY_SENSOR void UsbDevice::set_configured_binary_sensor(binary_sensor::BinarySensor *sensor) { configured_ = sensor; }; #endif +bool UsbDevice::get_configured_() { +// based on defines in HWCDC.cpp +#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 +#if ARDUINO_USB_MODE +#if ARDUINO_USB_CDC_ON_BOOT//Serial used for USB CDC + return Serial; +#else + return USBSerial; +#endif +#endif +#endif + return false; +} + + } // namespace usb_device } // namespace esphome #endif diff --git a/esphome/components/usb_device/usb_device.h b/esphome/components/usb_device/usb_device.h index c54f1876fc..55b2490e44 100644 --- a/esphome/components/usb_device/usb_device.h +++ b/esphome/components/usb_device/usb_device.h @@ -19,6 +19,7 @@ class UsbDevice : public PollingComponent { #ifdef USE_BINARY_SENSOR binary_sensor::BinarySensor *configured_; #endif + bool get_configured_(); }; } // namespace usb_device