/* 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[] = { 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, }; /* 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, v, h, n = 0; time_t t; struct tm *tm; 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); for (i = 0; i < 8; i++) { /* 1min */ v = tm->tm_min % 10; buf[8 + i * 2 + 1] = digit4x8[v * 8 + i]; /* 10min */ v = tm->tm_min / 10; buf[8 + i * 2 + 1] |= digit4x8[v * 8 + i] << 4; /* 1hr */ h = tm->tm_hour % 12; if (h == 0) h = 12; v = h % 10; buf[8 + i * 2 + 0] = digit4x8[v * 8 + i] << 2; /* 10hr */ if (h >= 10) { buf[8 + i * 2 + 0] |= digit4x8[1 * 8 + i] << 6; } /* : */ if (n & 1) { buf[8 + i * 2 + 0] |= digit4x8[10 * 8 + i]; } } display_character(dev, buf); usleep(500000); n++; } 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; }