Last active
September 22, 2017 12:40
-
-
Save ElectricImpSampleCode/95b62541ffba8102a2eb to your computer and use it in GitHub Desktop.
Electric Imp imp API usb.configure() example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.device + " has disconnected"); | |
break; | |
case USB_TRANSFER_COMPLETED: | |
server.log("Interaction with USB device at " + details.device + " 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.usb.configure(processUSB); | |
server.log("Please connect a compatible USB device to this unit"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI - I think there are a few bugs here... See the minor updates I made at https://gist.github.com/deldrid1/282cc63e8a8ae9cadb3b56416e3df5b2