Skip to content

Commit

Permalink
Merge pull request #3 from torfbolt/dev
Browse files Browse the repository at this point in the history
Add config options for USB device to specify custom VID & PID
  • Loading branch information
tomaszduda23 authored Jan 12, 2025
2 parents d50dfdf + 9340901 commit 419b200
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
19 changes: 19 additions & 0 deletions esphome/components/usb_device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

CODEOWNERS = ["@tomaszduda23"]
CONF_USB_DEVICE_ID = "usb_device_id"
CONF_VENDOR_ID = "vendor_id"
CONF_PRODUCT_ID = "product_id"
CONF_MANUFACTURER_NAME = "manufacturer_name"
CONF_PRODUCT_NAME = "product_name"


def _validate_variant(value):
Expand All @@ -33,6 +37,10 @@ def _validate_variant(value):
cv.Schema(
{
cv.GenerateID(): cv.declare_id(UsbDevice),
cv.Optional(CONF_VENDOR_ID): cv.hex_uint16_t,
cv.Optional(CONF_PRODUCT_ID): cv.hex_uint16_t,
cv.Optional(CONF_MANUFACTURER_NAME): cv.string,
cv.Optional(CONF_PRODUCT_NAME): cv.string,
}
).extend(cv.polling_component_schema("10s")),
cv.only_with_arduino,
Expand Down Expand Up @@ -64,6 +72,17 @@ def final_validate_number_of_device(config):

async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
if CONF_VENDOR_ID in config:
cg.add_define("USE_VENDOR_ID")
cg.add(var.set_vendor_id(config[CONF_VENDOR_ID]))
if CONF_PRODUCT_ID in config:
cg.add_define("USE_PRODUCT_ID")
cg.add(var.set_product_id(config[CONF_PRODUCT_ID]))
if CONF_MANUFACTURER_NAME in config:
cg.add(var.set_manufacturer_name(config[CONF_MANUFACTURER_NAME]))
if CONF_PRODUCT_NAME in config:
cg.add(var.set_product_name(config[CONF_PRODUCT_NAME]))

await cg.register_component(var, config)
cg.add_library("adafruit/Adafruit TinyUSB Library", "2.2.4", None)
cg.add_build_flag("-DCFG_TUSB_MCU=OPT_MCU_ESP32S2")
Expand Down
21 changes: 20 additions & 1 deletion esphome/components/usb_device/usb_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ namespace usb_device {

static const char *const TAG = "usb_device";

void UsbDevice::setup() { USB.begin(); }
void UsbDevice::setup() {
#ifdef USE_VENDOR_ID
USB.VID(this->vendor_id_);
#endif
#ifdef USE_PRODUCT_ID
USB.PID(this->product_id_);
#endif
if (!this->manufacturer_name_.empty()) {
USB.manufacturerName(this->manufacturer_name_.c_str());
}
if (!this->product_name_.empty()) {
USB.productName(this->product_name_.c_str());
}
USB.begin();
}

void UsbDevice::update() {
#ifdef USE_BINARY_SENSOR
Expand All @@ -35,6 +49,11 @@ void UsbDevice::dump_config() {
YESNO(TinyUSBDevice.suspended()), YESNO(TinyUSBDevice.ready()));
}

void UsbDevice::set_vendor_id(const uint16_t vid) { this->vendor_id_ = vid; }
void UsbDevice::set_product_id(const uint16_t pid) { this->product_id_ = pid; }
void UsbDevice::set_manufacturer_name(const std::string &manufacturer_name) { this->manufacturer_name_ = manufacturer_name; }
void UsbDevice::set_product_name(const std::string &product_name) { this->product_name_ = product_name; }

#ifdef USE_BINARY_SENSOR
void UsbDevice::set_mounted_binary_sensor(binary_sensor::BinarySensor *sensor) { mounted_ = sensor; };
void UsbDevice::set_ready_binary_sensor(binary_sensor::BinarySensor *sensor) { ready_ = sensor; };
Expand Down
8 changes: 8 additions & 0 deletions esphome/components/usb_device/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ class UsbDevice : public PollingComponent {
void update() override;
float get_setup_priority() const override;
void dump_config() override;
void set_vendor_id(uint16_t vendor_id);
void set_product_id(uint16_t product_id);
void set_manufacturer_name(const std::string &manufacturer_name);
void set_product_name(const std::string &product_name);
#ifdef USE_BINARY_SENSOR
void set_mounted_binary_sensor(binary_sensor::BinarySensor *sensor);
void set_ready_binary_sensor(binary_sensor::BinarySensor *sensor);
void set_suspended_binary_sensor(binary_sensor::BinarySensor *sensor);
#endif
protected:
uint16_t vendor_id_;
uint16_t product_id_;
std::string manufacturer_name_;
std::string product_name_;
#ifdef USE_BINARY_SENSOR
binary_sensor::BinarySensor *mounted_;
binary_sensor::BinarySensor *ready_;
Expand Down

0 comments on commit 419b200

Please sign in to comment.