From 634b86d63e3c7795774f34305aa4aa4d4109bf0a Mon Sep 17 00:00:00 2001 From: marschap Date: Mon, 16 May 2005 17:12:36 +0000 Subject: [PATCH] better checks for some necessary arrays, alternative implementation for CF633_flush (hidden behind #ifdef) more space between functions --- server/drivers/CFontz633.c | 180 ++++++++++++++++++++++--------------- 1 file changed, 108 insertions(+), 72 deletions(-) diff --git a/server/drivers/CFontz633.c b/server/drivers/CFontz633.c index 055b57a..9ffbc01 100644 --- a/server/drivers/CFontz633.c +++ b/server/drivers/CFontz633.c @@ -62,12 +62,12 @@ #include "report.h" #include "lcd_lib.h" -#define CF633_KEY_UP 1 -#define CF633_KEY_DOWN 2 -#define CF633_KEY_LEFT 3 -#define CF633_KEY_RIGHT 4 -#define CF633_KEY_ENTER 5 -#define CF633_KEY_ESCAPE 6 +#define CF633_KEY_UP 1 +#define CF633_KEY_DOWN 2 +#define CF633_KEY_LEFT 3 +#define CF633_KEY_RIGHT 4 +#define CF633_KEY_ENTER 5 +#define CF633_KEY_ESCAPE 6 static int custom = 0; typedef enum { @@ -77,8 +77,8 @@ typedef enum { } custom_type; static int fd; -static char *framebuf = NULL; -static char *old = NULL; +static unsigned char *framebuf = NULL; +static unsigned char *old = NULL; static int width = 0; static int height = 0; static int cellwidth = DEFAULT_CELL_WIDTH; @@ -129,12 +129,12 @@ CFontz633_init (Driver * drvthis, char *args) /* Read config file */ /* Which serial device should be used */ strncpy(device, drvthis->config_get_string ( drvthis->name , "Device" , 0 , DEFAULT_DEVICE),sizeof(device)); - device[sizeof(device)-1]=0; + device[sizeof(device)-1] = '\0'; debug (RPT_INFO,"CFontz633: Using device: %s", device); /* Which size */ strncpy(size, drvthis->config_get_string ( drvthis->name , "Size" , 0 , DEFAULT_SIZE),sizeof(size)); - size[sizeof(size)-1]=0; + size[sizeof(size)-1] = '\0'; if( sscanf(size , "%dx%d", &w, &h ) != 2 || (w <= 0) || (w > LCD_MAX_WIDTH) || (h <= 0) || (h > LCD_MAX_HEIGHT)) { @@ -146,21 +146,24 @@ CFontz633_init (Driver * drvthis, char *args) } /* Which contrast */ - if (0<=drvthis->config_get_int ( drvthis->name , "Contrast" , 0 , DEFAULT_CONTRAST) && drvthis->config_get_int ( drvthis->name , "Contrast" , 0 , DEFAULT_CONTRAST) <= 1000) { + if (0<=drvthis->config_get_int ( drvthis->name , "Contrast" , 0 , DEFAULT_CONTRAST) && + drvthis->config_get_int ( drvthis->name , "Contrast" , 0 , DEFAULT_CONTRAST) <= 1000) { contrast = drvthis->config_get_int ( drvthis->name , "Contrast" , 0 , DEFAULT_CONTRAST); } else { report (RPT_WARNING, "CFontz633_init: Contrast must be between 0 and 1000. Using default value.\n"); } /* Which backlight brightness */ - if (0<=drvthis->config_get_int ( drvthis->name , "Brightness" , 0 , DEFAULT_BRIGHTNESS) && drvthis->config_get_int ( drvthis->name , "Brightness" , 0 , DEFAULT_BRIGHTNESS) <= 100) { + if (0<=drvthis->config_get_int ( drvthis->name , "Brightness" , 0 , DEFAULT_BRIGHTNESS) && + drvthis->config_get_int ( drvthis->name , "Brightness" , 0 , DEFAULT_BRIGHTNESS) <= 100) { brightness = drvthis->config_get_int ( drvthis->name , "Brightness" , 0 , DEFAULT_BRIGHTNESS); } else { report (RPT_WARNING, "CFontz633_init: Brightness must be between 0 and 100. Using default value.\n"); } /* Which backlight-off "brightness" */ - if (0<=drvthis->config_get_int ( drvthis->name , "OffBrightness" , 0 , DEFAULT_OFFBRIGHTNESS) && drvthis->config_get_int ( drvthis->name , "OffBrightness" , 0 , DEFAULT_OFFBRIGHTNESS) <= 100) { + if (0<=drvthis->config_get_int ( drvthis->name , "OffBrightness" , 0 , DEFAULT_OFFBRIGHTNESS) && + drvthis->config_get_int ( drvthis->name , "OffBrightness" , 0 , DEFAULT_OFFBRIGHTNESS) <= 100) { offbrightness = drvthis->config_get_int ( drvthis->name , "OffBrightness" , 0 , DEFAULT_OFFBRIGHTNESS); } else { report (RPT_WARNING, "CFontz633_init: OffBrightness must be between 0 and 100. Using default value.\n"); @@ -181,7 +184,7 @@ CFontz633_init (Driver * drvthis, char *args) * I will try to behave differently for firmware 0.6 or above. * Currently this is not in use. */ - if(drvthis->config_get_bool( drvthis->name , "NewFirmware" , 0 , 0)) { + if (drvthis->config_get_bool( drvthis->name , "NewFirmware" , 0 , 0)) { newfirmware = 1; } @@ -192,17 +195,13 @@ CFontz633_init (Driver * drvthis, char *args) } /*Am I USB or not?*/ - if(drvthis->config_get_bool( drvthis->name , "USB" , 0 , 0)) { + if (drvthis->config_get_bool( drvthis->name , "USB" , 0 , 0)) { usb = 1; } /* Set up io port correctly, and open it... */ debug( RPT_DEBUG, "CFontz633: Opening serial device: %s", device); - if ( usb ) { - fd = open (device, O_RDWR | O_NOCTTY); - } else { - fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY); - } + fd = open (device, (usb) ? (O_RDWR | O_NOCTTY) : (O_RDWR | O_NOCTTY | O_NDELAY)); if (fd == -1) { report (RPT_ERR, "CFontz633_init: failed (%s)\n", strerror (errno)); return -1; @@ -211,17 +210,17 @@ CFontz633_init (Driver * drvthis, char *args) tcgetattr (fd, &portset); /* We use RAW mode */ - if ( usb ) { - // The USB way - portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP - | INLCR | IGNCR | ICRNL | IXON ); - portset.c_oflag &= ~OPOST; - portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ); - portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); - portset.c_cflag |= CS8 | CREAD | CLOCAL ; - portset.c_cc[VMIN] = 0; - portset.c_cc[VTIME] = 0; - } else { + if ( usb ) { + // The USB way + portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP + | INLCR | IGNCR | ICRNL | IXON ); + portset.c_oflag &= ~OPOST; + portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ); + portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); + portset.c_cflag |= CS8 | CREAD | CLOCAL ; + portset.c_cc[VMIN] = 0; + portset.c_cc[VTIME] = 0; + } else { #ifdef HAVE_CFMAKERAW /* The easy way */ cfmakeraw( &portset ); @@ -234,7 +233,7 @@ CFontz633_init (Driver * drvthis, char *args) portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); portset.c_cflag |= CS8 | CREAD | CLOCAL ; #endif - } + } /* Set port speed */ cfsetospeed (&portset, speed); @@ -245,8 +244,20 @@ CFontz633_init (Driver * drvthis, char *args) /* Make sure the frame buffer is there... */ framebuf = (unsigned char *) malloc (width * height); + if (framebuf == NULL) { + report(RPT_ERR, "CFontz633_init: unable to create framebuffer.\n"); + return -1; + } memset (framebuf, ' ', width * height); + /* make sure the framebuffer backing store is there... */ + old = (unsigned char *) malloc (width * height); + if (old == NULL) { + report(RPT_ERR, "CFontz633_init: unable to create framebuffer backing store.\n"); + return -1; + } + memset (old, ' ', width * height); + /* Set display-specific stuff.. */ if (reboot) { CFontz633_reboot (); @@ -257,12 +268,14 @@ CFontz633_init (Driver * drvthis, char *args) CFontz633_set_contrast (drvthis, contrast); CFontz633_no_live_report (); + CFontz633_hardware_clear (drvthis); report (RPT_DEBUG, "CFontz633_init: done\n"); return 0; } + /* * Clean-up */ @@ -278,6 +291,7 @@ CFontz633_close (Driver * drvthis) old = NULL; } + /* * Returns the display width */ @@ -291,6 +305,7 @@ CFontz633_width (Driver *drvthis) return width; } + /* * Returns the display height */ @@ -300,6 +315,7 @@ CFontz633_height (Driver *drvthis) return height; } + /* * Flushes all output to the lcd... */ @@ -307,43 +323,53 @@ MODULE_EXPORT void CFontz633_flush (Driver * drvthis) { int i; - char *xp, *xq; + +#if defined(CF635_FLUSH) + int len = width * height; + char out[4]; + + for (i = 0; i < len; i++) { + if (framebuf[i] != old[i]) { + out[1] = i / width; // line + out[0] = i - (out[1] * width); // column + out[2] = framebuf[i]; // character + out[2] = '\0'; + send_bytes_message(fd, 3, CF633_Send_Data_to_LCD, out); + old[i] = framebuf[i]; + } + } +#else /* * We don't use delta update yet. * It is possible but not easy, we can only update a line, full or begining. */ - if (old==NULL) { - old = (unsigned char *) malloc (width * height); - memset (old, ' ', width * height); - CFontz633_hardware_clear (drvthis); - } + unsigned char *xp = framebuf; + unsigned char *xq = old; -xp = framebuf; -xq = old; - -for (i=0; i