/* * $Id: USBLCD.xs,v 1.7 2011-10-20 18:04:37 scg Exp $ * * (C)2010 St(u)dio of Computer Games * Alexander Ozumenko a.ozumenko@gmail.com */ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include #include #ifndef MIN #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif /* MIN() */ typedef t_usblcd USBLCD; typedef gdImagePtr GD__Image; static double constant(char *name) { errno = 0; switch (*name) { case 'A': break; case 'B': break; case 'C': break; case 'D': break; case 'E': break; case 'F': break; case 'G': break; case 'H': break; case 'I': break; case 'J': break; case 'K': break; case 'L': break; case 'M': break; case 'N': break; case 'O': break; case 'P': break; case 'Q': break; case 'R': break; case 'S': break; case 'T': break; case 'U': if(strEQ(name, "USBLCD_DEV_TYPE")) return USBLCD_DEV_TYPE; if(strEQ(name, "USBLCD_POWER_ON")) return USBLCD_POWER_ON; if(strEQ(name, "USBLCD_POWER_SAVE")) return USBLCD_POWER_SAVE; if(strEQ(name, "USBLCD_POWER_OFF")) return USBLCD_POWER_OFF; if(strEQ(name, "USBLCD_LED_UNDEF")) return USBLCD_LED_UNDEF; if(strEQ(name, "USBLCD_LED_OK")) return USBLCD_LED_OK; if(strEQ(name, "USBLCD_LED_WARNING")) return USBLCD_LED_WARNING; if(strEQ(name, "USBLCD_LED_ERROR")) return USBLCD_LED_ERROR; if(strEQ(name, "USBLCD_LED_WHITE")) return USBLCD_LED_WHITE; if(strEQ(name, "USBLCD_LED_RED")) return USBLCD_LED_RED; if(strEQ(name, "USBLCD_LED_ORANGE")) return USBLCD_LED_ORANGE; if(strEQ(name, "USBLCD_LED_YELLOW")) return USBLCD_LED_YELLOW; if(strEQ(name, "USBLCD_LED_GREEN")) return USBLCD_LED_GREEN; if(strEQ(name, "USBLCD_LED_BLUE")) return USBLCD_LED_BLUE; if(strEQ(name, "USBLCD_LED_OFF")) return USBLCD_LED_OFF; if(strEQ(name, "USBLCD_LED_ON")) return USBLCD_LED_ON; if(strEQ(name, "USBLCD_ROTATE0")) return USBLCD_ROTATE0; if(strEQ(name, "USBLCD_ROTATE90")) return USBLCD_ROTATE90; if(strEQ(name, "USBLCD_ROTATE180")) return USBLCD_ROTATE180; if(strEQ(name, "USBLCD_ROTATE270")) return USBLCD_ROTATE270; break; case 'V': break; case 'W': break; case 'X': break; case 'Y': break; case 'Z': break; case 'a': break; case 'b': break; case 'c': break; case 'd': break; case 'e': break; case 'f': break; case 'g': break; case 'h': break; case 'i': break; case 'j': break; case 'k': break; case 'l': break; case 'm': break; case 'n': break; case 'o': break; case 'p': break; case 'q': break; case 'r': break; case 's': break; case 't': break; case 'u': break; case 'v': break; case 'w': break; case 'x': break; case 'y': break; case 'z': break; } errno = EINVAL; return 0; } static int bitbltgd_type1_bpp1(t_usblcd *dev, int x, int y, gdImagePtr im) { int ix, iy, w, h, stride; char *data, *ptr; int ret; gdImageTrueColorToPalette(im, 0, 2); stride = (im->sx + 8 - 1) / 8; w = im->sx; h = im->sy; ptr = data = malloc(stride * h); if(data == NULL) { return -1; } for(iy = 0; iy < h; iy++) { for(ix = 0; ix < w; ix += 8) { *ptr = (gdImageGetPixel(im, ix + 0, iy) << 7) + (gdImageGetPixel(im, ix + 1, iy) << 6) + (gdImageGetPixel(im, ix + 2, iy) << 5) + (gdImageGetPixel(im, ix + 3, iy) << 4) + (gdImageGetPixel(im, ix + 4, iy) << 3) + (gdImageGetPixel(im, ix + 5, iy) << 2) + (gdImageGetPixel(im, ix + 6, iy) << 1) + (gdImageGetPixel(im, ix + 7, iy) << 0); ptr++; } } ret = usblcd_bitblt(dev, x, y, w, h, data); free(data); return ret; } MODULE = USBLCD PACKAGE = USBLCD double constant(name) char *name USBLCD * new(...) PROTOTYPE: ; PREINIT: t_usblcd *dev; CODE: dev = usblcd_claim(); if(dev == NULL) XSRETURN_UNDEF; RETVAL = dev; OUTPUT: RETVAL MODULE = USBLCD PACKAGE = USBLCDPtr void DESTROY(dev) USBLCD *dev PROTOTYPE: $ CODE: usblcd_unclaim(dev); HV * getinfo(dev) USBLCD *dev; PROTOTYPE: $ PREINIT: HV *info; CODE: info = newHV(); hv_store(info, "type", strlen("type"), newSVuv(dev->info.type), 0); hv_store(info, "num_modes", strlen("num_modes"), newSVuv(dev->info.num_modes), 0); hv_store(info, "num_leds", strlen("num_leds"), newSVuv(dev->info.num_leds), 0); RETVAL = info; OUTPUT: RETVAL AV * getmodes(dev) USBLCD *dev PROTOTYPE: $ PREINIT: AV *modes; HV *mode; int i; CODE: modes = newAV(); for(i = 0; i < dev->info.num_modes; i ++) { mode = newHV(); hv_store(mode, "mode", strlen("mode"), newSVuv(i), 0); hv_store(mode, "width", strlen("width"), newSVuv(dev->mode[i].width), 0); hv_store(mode, "height", strlen("height"), newSVuv(dev->mode[i].height), 0); hv_store(mode, "bpp", strlen("bpp"), newSVuv(dev->mode[i].bpp), 0); av_push(modes, newRV((SV *)mode)); } RETVAL = modes; OUTPUT: RETVAL HV * getmode(dev) USBLCD *dev PROTOTYPE: $ PREINIT: HV *mode; CODE: mode = newHV(); hv_store(mode, "mode", strlen("mode"), newSVuv(dev->mode_current), 0); hv_store(mode, "width", strlen("width"), newSVuv(dev->mode[dev->mode_current].width), 0); hv_store(mode, "height", strlen("height"), newSVuv(dev->mode[dev->mode_current].height), 0); hv_store(mode, "bpp", strlen("bpp"), newSVuv(dev->mode[dev->mode_current].bpp), 0); RETVAL = mode; OUTPUT: RETVAL int setmode(dev, mode = 0) USBLCD *dev int mode PROTOTYPE: $;$ CODE: if(usblcd_setmode(dev, mode)) XSRETURN_UNDEF; RETVAL = 1; OUTPUT: RETVAL int setpower(dev, mode) USBLCD *dev int mode PROTOTYPE: $$ CODE: if(usblcd_setpower(dev, mode)) XSRETURN_UNDEF; RETVAL = 1; OUTPUT: RETVAL AV * getleds(dev) USBLCD *dev PROTOTYPE: $ PREINIT: AV *leds; HV *led; int i; CODE: leds = newAV(); for(i = 0; i < dev->info.num_leds; i ++) { led = newHV(); hv_store(led, "led", strlen("led"), newSVuv(i), 0); hv_store(led, "type", strlen("type"), newSVuv(dev->led[i].type), 0); hv_store(led, "color", strlen("color"), newSVuv(dev->led[i].color), 0); hv_store(led, "max", strlen("max"), newSVuv(dev->led[i].max), 0); av_push(leds, newRV((SV *)led)); } RETVAL = leds; OUTPUT: RETVAL int setled(dev, led, value) USBLCD *dev int led int value PROTOTYPE: $$$ CODE: if(usblcd_setled(dev, led, value)) XSRETURN_UNDEF; RETVAL = 1; OUTPUT: RETVAL int setrotate(dev, rotate = 0) USBLCD *dev int rotate PROTOTYPE: $;$ CODE: if(usblcd_setrotate(dev, rotate)) XSRETURN_UNDEF; RETVAL = 1; OUTPUT: RETVAL int fillrect(dev, x, y, w, h, color) USBLCD *dev int x int y int w int h unsigned int color PROTOTYPE: $$$$$$ CODE: if(usblcd_fillrect(dev, x, y, w, h, color)) XSRETURN_UNDEF; else XSRETURN_UNDEF; OUTPUT: RETVAL int bitbltgd(dev, x, y, im) USBLCD *dev int x int y GD::Image im PROTOTYPE: $$$$ PREINIT: SV **value; STRLEN len; void *iptr; gdImagePtr imd; CODE: switch(dev->info.type) { case USBLCD_DEV_TYPE: switch(dev->bpp) { case 1: if(bitbltgd_type1_bpp1(dev, x, y, im)) XSRETURN_UNDEF; break; default: XSRETURN_UNDEF; } break; default: XSRETURN_UNDEF; } RETVAL = 1; OUTPUT: RETVAL