commandline/usb-led-fader.c

Go to the documentation of this file.
00001 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <usb.h>                /* this is libusb, see http://libusb.sourceforge.net/ */
00014 
00015 #include "usbledfader.h"
00016 #include "channels.h"
00017 
00018 #define USBDEV_SHARED_VENDOR    0x16C0  
00019 #define USBDEV_SHARED_PRODUCT   0x05DC  
00021 /* These are error codes for the communication via USB. */
00022 #define USB_ERROR_NOTFOUND  1 
00023 #define USB_ERROR_ACCESS    2 
00024 #define USB_ERROR_IO        3 
00031 void usage(char *name)
00032 {
00033     fprintf(stderr, "usage:\n");
00034     fprintf(stderr, "  %s status\n", name);
00035     fprintf(stderr, "  %s set ledId waveId waveformId periodDuration repetitionCount\n", name);
00036     fprintf(stderr, "  %s clear ledId\n", name);
00037     fprintf(stderr, "  %s reset\n", name);
00038     fprintf(stderr, "  %s show waveformId\n", name);
00039     fprintf(stderr, "  %s test\n\n", name);
00040     fprintf(stderr, "parameters:\n");
00041     fprintf(stderr, "  ledId: ID of the LED (0-%d).\n", CHANNELS - 1);
00042     fprintf(stderr, "  waveId: ID of the wave (0-1: constant waves, 2: override).\n");
00043     fprintf(stderr, "  waveformId: ID of the waveform (0-31: brightness, 32-37: patterns).\n");
00044     fprintf(stderr, "  periodDuration: Time in sec/10 for one repetition of the waveform.\n");
00045     fprintf(stderr, "                  A value of 0 can be used to reset the wave.\n");
00046     fprintf(stderr, "  repetitionCount: Number of repetitions before switching to the next wave.\n");
00047     fprintf(stderr, "                   A value of 0 can be used to repeat this forever.\n");
00048 }
00049 
00059 int usbGetStringAscii(usb_dev_handle * dev, int index, int langid, char *buf, int buflen) {
00060     char buffer[256];
00061     int rval, i;
00062 
00063     if ((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) {
00064         return rval;
00065     }
00066     if (buffer[1] != USB_DT_STRING) {
00067         return 0;
00068     }
00069     if ((unsigned char) buffer[0] < rval) {
00070         rval = (unsigned char) buffer[0];
00071     }
00072     rval /= 2;
00073     /* lossy conversion to ISO Latin1 */
00074     for (i = 1; i < rval; i++) {
00075         if (i > buflen) {
00076             /* destination buffer overflow */
00077             break;
00078         }
00079         buf[i - 1] = buffer[2 * i];
00080         if (buffer[2 * i + 1] != 0) {
00081             /* outside of ISO Latin1 range */
00082             buf[i - 1] = '?';
00083         }
00084     }
00085     buf[i - 1] = 0;
00086     return i - 1;
00087 }
00088 
00099 int usbOpenDevice(usb_dev_handle ** device, int vendor, char *vendorName, int product, char *productName) {
00100     struct usb_bus *bus;
00101     struct usb_device *dev;
00102     usb_dev_handle *handle = NULL;
00103     int errorCode = USB_ERROR_NOTFOUND;
00104     static int didUsbInit = 0;
00105 
00106     if (!didUsbInit) {
00107         didUsbInit = 1;
00108         usb_init();
00109     }
00110     usb_find_busses();
00111     usb_find_devices();
00112     for (bus = usb_get_busses(); bus; bus = bus->next) {
00113         for (dev = bus->devices; dev; dev = dev->next) {
00114             if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
00115                 char string[256];
00116                 int len;
00117                 handle = usb_open(dev); /* we need to open the device in order to query strings */
00118                 if (!handle) {
00119                     errorCode = USB_ERROR_ACCESS;
00120                     fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
00121                     continue;
00122                 }
00123                 if (vendorName == NULL && productName == NULL) {        /* name does not matter */
00124                     break;
00125                 }
00126                 /* now check whether the names match: */
00127                 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
00128                 if (len < 0) {
00129                     errorCode = USB_ERROR_IO;
00130                     fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
00131                 } else {
00132                     errorCode = USB_ERROR_NOTFOUND;
00133                     /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
00134                     if (strcmp(string, vendorName) == 0) {
00135                         len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
00136                         if (len < 0) {
00137                             errorCode = USB_ERROR_IO;
00138                             fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
00139                         } else {
00140                             errorCode = USB_ERROR_NOTFOUND;
00141                             /* fprintf(stderr, "seen product ->%s<-\n", string); */
00142                             if (strcmp(string, productName) == 0) {
00143                                 break;
00144                             }
00145                         }
00146                     }
00147                 }
00148                 usb_close(handle);
00149                 handle = NULL;
00150             }
00151         }
00152         if (handle) {
00153             break;
00154         }
00155     }
00156     if (handle != NULL) {
00157         errorCode = 0;
00158         *device = handle;
00159     }
00160     return errorCode;
00161 }
00162 
00171 void dev_test(usb_dev_handle *handle, int argc, char** argv) {
00172     unsigned char buffer[8];
00173     int nBytes;
00174     int i, v, r;
00175     if (argc != 2) {
00176         usage(argv[0]);
00177         exit(1);
00178     }
00179     for (i = 0; i < 1000; i++) {
00180         v = rand() & 0xffff;
00181         nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CMD_ECHO, v, 0, (char *) buffer, sizeof(buffer), 5000);
00182         if (nBytes < 2) {
00183             if (nBytes < 0) {
00184                 fprintf(stderr, "USB error: %s\n", usb_strerror());
00185             }
00186             fprintf(stderr, "only %d bytes received in iteration %d\n", nBytes, i);
00187             exit(1);
00188         }
00189         r = buffer[0] | (buffer[1] << 8);
00190         if (r != v) {
00191             fprintf(stderr, "data error: received 0x%x instead of 0x%x in iteration %d\n", r, v, i);
00192             exit(1);
00193         }
00194     }
00195     printf("test succeeded\n");
00196 }
00197 
00204 void dev_set(usb_dev_handle *handle, int argc, char** argv) {
00205     unsigned char buffer[8];
00206     int nBytes;
00207     int parameter;
00208     if ((argc < 7) || ((argc - 2) % 5 != 0)) {
00209         usage(argv[0]);
00210         exit(1);
00211     }
00212     for (parameter = 2; (parameter + 4) < argc; parameter += 5) {
00213         int ledId = atoi(argv[parameter + 0]);
00214         if ((ledId < 0) || (ledId > (CHANNELS - 1))) {
00215             fprintf(stderr, "invalid ledId: %d\n", ledId);
00216             exit(1);
00217         }
00218         int waveId = atoi(argv[parameter + 1]);
00219         if ((waveId < 0) || (waveId > 2)) {
00220             fprintf(stderr, "invalid waveId: %d\n", waveId);
00221             exit(1);
00222         }
00223         int waveformId = atoi(argv[parameter + 2]);
00224         if ((waveformId < 0) || (waveformId > 38)) {
00225             fprintf(stderr, "invalid waveformId: %d\n", waveformId);
00226             exit(1);
00227         }
00228         int periodDuration = atoi(argv[parameter + 3]);
00229         if ((periodDuration < 0) || (periodDuration > 255)) {
00230             fprintf(stderr, "invalid periodDuration: %d\n", periodDuration);
00231             exit(1);
00232         }
00233         int repetitionCount = atoi(argv[parameter + 4]);
00234         if ((repetitionCount < 0) || (repetitionCount > 255)) {
00235             fprintf(stderr, "invalid repetitionCount: %d\n", repetitionCount);
00236             exit(1);
00237         }
00238 
00239         buffer[0] = CMD_SET;
00240         buffer[1] = ledId;
00241         buffer[2] = waveId;
00242         buffer[3] = waveformId;
00243         buffer[4] = periodDuration;
00244         buffer[5] = repetitionCount;
00245 
00246         nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CMD_SET, ledId, 0, (char *) buffer, sizeof(buffer), 5000);
00247 
00248         if (nBytes < 0) {
00249             fprintf(stderr, "USB error: %s\n", usb_strerror());
00250             exit(1);
00251         }
00252     }
00253 }
00254 
00261 void dev_clear(usb_dev_handle *handle, int argc, char** argv) {
00262     unsigned char buffer[8];
00263     int nBytes;
00264     if (argc != 3) {
00265         usage(argv[0]);
00266         exit(1);
00267     }
00268     int ledId = atoi(argv[2]);
00269     if ((ledId < 0) || (ledId > (CHANNELS - 1))) {
00270         fprintf(stderr, "invalid LED: %d\n", ledId);
00271         exit(1);
00272     }
00273     nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CMD_CLEAR, ledId, 0, (char *) buffer, sizeof(buffer), 5000);
00274     if (nBytes < 0) {
00275         fprintf(stderr, "USB error: %s\n", usb_strerror());
00276         exit(1);
00277     }
00278 }
00279 
00286 void dev_status(usb_dev_handle *handle, int argc, char** argv) {
00287     int nBytes;
00288     int i, j;
00289     static fade_GlobalData fade_globalData; /* contains the state of all four LEDs. */
00290     if (argc != 2) {
00291         usage(argv[0]);
00292         exit(1);
00293     }
00294     nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CMD_GET, 0, 0, (char *) &fade_globalData, sizeof(fade_globalData), 5000);
00295     if (nBytes < 0) {
00296         fprintf(stderr, "USB error: %s\n", usb_strerror());
00297         exit(1);
00298     }
00299     if (nBytes != sizeof(fade_globalData)) {
00300         fprintf(stderr, "USB oddity: %d bytes received, %d bytes expected.\n", nBytes, sizeof(fade_globalData));
00301         exit(1);
00302     }
00303     for (i = 0; i < CHANNELS; i++) {
00304         printf("LED %d      %10s %10s %10s %10s %10s\n", i, "curid", "curvalue", "curpos", "currep", "nextupd");
00305         printf("           %10d %10d %10d %10d %10d\n",
00306             fade_globalData.led[i].waveCurrentId,
00307             fade_globalData.led[i].waveCurrentValue,
00308             fade_globalData.led[i].waveCurrentPosition,
00309             fade_globalData.led[i].waveCurrentRepetition,
00310             fade_globalData.led[i].waveNextUpdate);
00311         printf("%10s %10s %10s %10s %10s %10s\n", "wave", "waveform", "length", "repeat", "duration", "updtime");
00312         for (j = 0; j < 3; j++) {
00313             printf("%10d %10d %10d %10d %10d %10d\n",
00314                 j,
00315                 fade_globalData.led[i].wave[j].waveformId,
00316                 fade_globalData.led[i].wave[j].waveformLength,
00317                 fade_globalData.led[i].wave[j].waveformRepetition,
00318                 fade_globalData.led[i].wave[j].waveformDuration,
00319                 fade_globalData.led[i].wave[j].waveformUpdateTime);
00320         }
00321     }
00322 }
00323 
00330 void dev_reset(usb_dev_handle *handle, int argc, char** argv) {
00331     unsigned char buffer[8];
00332     int nBytes;
00333     if (argc != 2) {
00334         usage(argv[0]);
00335         exit(1);
00336     }
00337     nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CMD_RESET, 0, 0, (char *) buffer, sizeof(buffer), 5000);
00338     if (nBytes < 0) {
00339         fprintf(stderr, "USB error: %s\n", usb_strerror());
00340         exit(1);
00341     }
00342 }
00343 
00351 void dev_show(usb_dev_handle *handle, int argc, char** argv) {
00352     if (argc != 3) {
00353         usage(argv[0]);
00354         exit(1);
00355     }
00356     int waveformId = atoi(argv[2]);
00357     if ((waveformId < 0) || (waveformId > 38)) {
00358         fprintf(stderr, "invalid waveformId: %d\n", waveformId);
00359         exit(1);
00360     }
00361     int i, j;
00362     int length = fade_calculateWaveform(waveformId, 0);
00363     printf("wave %2d - length %2d\n", waveformId, length);
00364     for (i = 31; i > 0; i--) {
00365         printf("%2d: ", i);
00366         for (j = 1; j <= length; j++) {
00367             if (fade_calculateWaveform(waveformId, j) >= i) {
00368                 printf("*");
00369             } else {
00370                 printf(" ");
00371             }
00372         }
00373         printf("\n");
00374     }
00375     printf("    ");
00376     for (j = 1; j <= length; j++) {
00377         printf("=");
00378     }
00379     printf("\n");
00380     exit(0);
00381 }
00382 
00390 int main(int argc, char **argv)
00391 {
00392     usb_dev_handle *handle = NULL;
00393 
00394     if (argc < 2) {
00395         usage(argv[0]);
00396         exit(1);
00397     }
00398     usb_init();
00399     if (usbOpenDevice (&handle, USBDEV_SHARED_VENDOR, "www.schatenseite.de", USBDEV_SHARED_PRODUCT, "USB-LED-Fader") != 0) {
00400         fprintf(stderr, "Could not find USB device \"USB-LED-Fader\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT);
00401         exit(1);
00402     }
00403     /* We have searched all devices on all busses for our USB device above. Now
00404      * try to open it and perform the vendor specific control operations for the
00405      * function requested by the user.
00406      */
00407     if (strcmp(argv[1], "test") == 0) {
00408         dev_test(handle, argc, argv);
00409     } else if (strcmp(argv[1], "set") == 0) {
00410         dev_set(handle, argc, argv);
00411     } else if (strcmp(argv[1], "clear") == 0) {
00412         dev_clear(handle, argc, argv);
00413     } else if (strcmp(argv[1], "status") == 0) {
00414         dev_status(handle, argc, argv);
00415     } else if (strcmp(argv[1], "reset") == 0) {
00416         dev_reset(handle, argc, argv);
00417     } else if (strcmp(argv[1], "show") == 0) {
00418         dev_show(handle, argc, argv);
00419     } else {
00420         usage(argv[0]);
00421         exit(1);
00422     }
00423     usb_close(handle);
00424     return 0;
00425 }
00426 

Generated on Mon Oct 2 19:31:17 2006 for USB-LED-Fader by  doxygen 1.4.7