/* a clock for TAKARA TOMY's Glitter panel (16x16 LED module) code by SASANO Takayoshi --- public domain, no warranty. */ #include #include #include #include #include // libusb /* font data */ static unsigned char digit4x8[][8] = { { 0xe, // ooo- 0xa, // o-o- 0xa, // o-o- 0xa, // o-o- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0x0, }, { 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x0, }, { 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0xe, // ooo- 0x8, // o--- 0x8, // o--- 0xe, // ooo- 0x0, }, { 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0xe, // ooo- 0x0, }, { 0xa, // o-o- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x0, }, { 0xe, // ooo- 0x8, // o--- 0x8, // o--- 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0xe, // ooo- 0x0, }, { 0xe, // ooo- 0x8, // o--- 0x8, // o--- 0xe, // ooo- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0x0, }, { 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x2, // --o- 0x0, }, { 0xe, // ooo- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0x0, }, { 0xe, // ooo- 0xa, // o-o- 0xa, // o-o- 0xe, // ooo- 0x2, // --o- 0x2, // --o- 0xe, // ooo- 0x0, }, { 0x0, // ---- 0x0, // ---- 0x2, // --o- 0x0, // ---- 0x2, // --o- 0x0, // ---- 0x0, // ---- 0x0, }, { 0x0, // ---- 0x0, // ---- 0x0, // ---- 0x0, // ---- 0x0, // ---- 0x0, // ---- 0x0, // ---- 0x0, }, }; /* display one character */ static void display_character(struct usb_dev_handle *dev, unsigned char *data) { #define EP_ADR_IN 0x81 #define EP_ADR_OUT 0x02 char buf[6 + 32 + 8]; buf[0] = 0x80; buf[1] = 0x03; buf[2] = 0x58; // bit[3:2] brightness (00:25% 01:50% 10:75% 11:100%) buf[3] = 0x08; buf[4] = 0x00; buf[5] = 0x00; /* display data */ memcpy(&buf[6], data, 32); /* add 1 packet to synch toggle bit */ // total 6 packets to device (the extra is ignored) memset(&buf[38], 0, 8); /* send data */ if (usb_interrupt_write(dev, EP_ADR_OUT, buf, sizeof(buf), 0) < 0) { printf("usb_interrupt_write %s\n", usb_strerror()); } return; } /* clock main */ static void do_clock(struct usb_dev_handle *dev) { int i, h, n; time_t t; struct tm *tm; unsigned char *hr10, *hr1, *colon, *min10, *min1; unsigned char buf[32]; memset(buf, 0, sizeof(buf)); n = 0; while (1) { time(&t); tm = localtime(&t); // printf("%d:%d\n", tm->tm_hour, tm->tm_min); h = tm->tm_hour % 12; if (h == 0) h = 12; hr10 = digit4x8[(h >= 10) ? 1 : 11]; hr1 = digit4x8[h % 10]; colon = digit4x8[(n++ & 2) ? 10 : 11]; min10 = digit4x8[tm->tm_min / 10]; min1 = digit4x8[tm->tm_min % 10]; for (i = 0; i < 8; i++) { buf[i * 2 + 8] = (*hr10++ << 6) | (*hr1++ << 2) | *colon++; buf[i * 2 + 9] = (*min10++ << 4) | *min1++; } display_character(dev, buf); usleep(250000); } return; } /* action */ static void do_action(struct usb_device *dev) { usb_dev_handle *dh; /* open device */ dh = usb_open(dev); if (dh == NULL) { printf("usb_open NULL\n"); goto fin0; } /* set configuration */ if (usb_set_configuration(dh, dev->config->bConfigurationValue) < 0) { printf("usb_set_configuration %s\n", usb_strerror()); goto fin1; } /* claim interface */ if (usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber) < 0) { printf("usb_claim_interface %s\n", usb_strerror()); goto fin1; } /* clock */ do_clock(dh); //fin2: usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber); fin1: usb_close(dh); fin0: return; } /* test main */ static void usb_test(void) { #define VENDOR_ID 0x0f30 #define PRODUCT_ID 0x0040 struct usb_bus *bus; struct usb_device *dev; int found, bnum, dnum; /* initialize libusb */ usb_init(); usb_find_busses(); usb_find_devices(); /* no device as default */ found = 0; /* find device */ for (bus = usb_get_busses(), bnum = 0; bus != NULL; bus = bus->next, bnum++) { for (dev = bus->devices, dnum = 0; dev != NULL; dev = dev->next, dnum++) { if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct == PRODUCT_ID) { found = 1; break; } } } /* do action when device is found */ if (found) { printf("device at %03d:%03d\n", bnum, dnum); do_action(dev); } else { printf("device not found\n"); } return; } int main(int argc, char *argv[]) { usb_test(); return 0; }