Skip to content

Instantly share code, notes, and snippets.

@deldrid1
Forked from ElectricImpSampleCode/basic.usb.device.nut
Last active November 16, 2016 19:00
Show Gist options
  • Save deldrid1/282cc63e8a8ae9cadb3b56416e3df5b2 to your computer and use it in GitHub Desktop.
Save deldrid1/282cc63e8a8ae9cadb3b56416e3df5b2 to your computer and use it in GitHub Desktop.
Electric Imp imp API usb.configure() example
function processUSB(type, details) {
// Function called in response to a USB bus event
switch (type) {
case USB_DEVICE_CONNECTED:
local descriptors = details.descriptors;
server.log("USB device " + descriptors.product + " connected at speed " + details.speed + "Mbps");
server.log(format("USB = 0x%04x", descriptors.usb));
server.log(format("class = 0x%02x", descriptors["class"]));
server.log(format("subclass = 0x%02x", descriptors.subclass));
server.log(format("protocol = 0x%02x", descriptors.protocol));
server.log("Searching for driver...");
if (getDriver(descriptors.vendorid, descriptors.productid) == null) {
// USB device not recognized
server.log("Incompatible USB device connected — please remove");
} else {
// USB device is supported by your device
server.log("USB device driver loaded");
}
break;
case USB_DEVICE_DISCONNECTED:
server.log("Device at " + details.address + " has disconnected");
break;
case USB_TRANSFER_COMPLETED:
server.log("Interaction with USB device at " + details.address + " has completed");
server.log(details.length + " bytes transferred");
break;
default:
server.log("Unknown bus event");
}
}
function getDriver(vendorID, prodID) {
// This is a stub for a full driver locator. A fuller implementation
// would check the connected device’s Vendor ID and Product ID
// against known values and on a match load the correct driver class.
// For now it returns a fail condition: null
return null;
}
// START
hardware.pinR.configure(DIGITAL_OUT, 1) //In most applications, pinR will be used as an active-high USB load switch and hence will be high when USB is in use, but if it is not you should leave this pin unconnected but still drive it high as GPIO with this line of code
hardware.pinW.configure(DIGITAL_IN_PULLUP) //In the case of using a load switch with an open drain nFAULT output, pinW would have an external pull-up, but if this is not present, the pin should be left unconnected and configured with this line of code before USB is initialized.
hardware.usb.configure(processUSB);
server.log("Please connect a compatible USB device to this unit");
@deldrid1
Copy link
Author

I think the addition of lines 46 and 47 should be safe, regardless of if an active-high USB load switch is used?

Regardless, they are required for the imp-005 dev board to work correctly...

@deldrid1
Copy link
Author

Also, class is a reserved squirrel keyword, so the syntax to retrieve that property is a bit different...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment