Here's the full source code that works with the above posted code running on the DigiSpark:
#include <stdio.h>
#include <string.h>
#include <usb.h>
int main (int argc, char **argv)
{
struct usb_bus *bus = NULL;
struct usb_device *digiSpark = NULL;
struct usb_device *device = NULL;
printf("Detecting USB devices...\n");
// Initialize the USB library
usb_init();
// Enumerate the USB device tree
usb_find_busses();
usb_find_devices();
// Iterate through attached busses and devices
bus = usb_get_busses();
while(bus != NULL)
{
device = bus->devices;
while(device != NULL)
{
// Check to see if each USB device matches the DigiSpark Vendor and Product IDs
if((device->descriptor.idVendor == 0x16c0) && (device->descriptor.idProduct == 0x05df))
{
printf("Detected DigiSpark... ");
digiSpark = device;
}
device = device->next;
}
bus = bus->next;
}
// If a digiSpark was found
if(digiSpark != NULL)
{
int result = 0;
int stringLength = 0;
int numInterfaces = 0;
struct usb_dev_handle *devHandle = NULL;
struct usb_interface_descriptor *interface = NULL;
if(argc > 1)
{
stringLength = strlen(argv[1]);
devHandle = usb_open(digiSpark);
if(devHandle != NULL)
{
result = usb_set_configuration(devHandle, digiSpark->config->bConfigurationValue);
if(result < 0) printf("Error %i setting configuration to %i\n", result, digiSpark->config->bConfigurationV$
numInterfaces = digiSpark->config->bNumInterfaces;
interface = &(digiSpark->config->interface[0].altsetting[0]);
printf("Found %i interfaces, using interface %i\n", numInterfaces, interface->bInterfaceNumber);
result = usb_claim_interface(devHandle, interface->bInterfaceNumber);
if(result < 0) printf("Error %i claiming Interface %i\n", result, interface->bInterfaceNumber);
printf("Writing character \"%c\" to DigiSpark.\n", argv[1][0]);
result = usb_control_msg(devHandle, (0x01 << 5), 0x09, 0, argv[1][0], 0, 0, 1000);
if(result < 0) printf("Error %i writing to USB device\n", result);
result = usb_release_interface(devHandle, interface->bInterfaceNumber);
if(result < 0) printf("Error %i releasing Interface 0\n", result);
usb_close(devHandle);
}
}
}
return 0;
}
Sending 'a' turns the LED on, and sending 'b' turns it off.
[root@dev ~]# ./test a
Detecting USB devices...
Detected DigiSpark... Found 1 interfaces, using interface 0
Writing character "a" to DigiSpark.
[root@dev ~]# ./test b
Detecting USB devices...
Detected DigiSpark... Found 1 interfaces, using interface 0
Writing character "b" to DigiSpark.
[root@dev ~]#