#include #define TYPE_VFD 0 #define TYPE_LCD 1 #define TYPE_UNDEF -1 #define PR 0 #define WR 1 int type = TYPE_UNDEF; int width = 0; int lines = 0; char *splash_16x4[] = { /* "1234567890123456" */ "+--------------+", "| %s Display |", "| by Jerry Han |", "+--------------+", }; char *splash_16x2[] = { /* "1234567890123456" */ " %s Display ", " by Jerry Han ", }; char *splash_20x4[] = { "+------------------+", "|Serial %s Display|", "|(c)2002, Jerry Han|", "+------------------+", }; char *splash_20x2[] = { " Serial %s Display ", " (c)2002, Jerry Han ", }; char *splash_40x4[] = { /* "1234567890123456789012345678901234567890" */ "+--------------------------------------+", "| Serial %s Display |", "| (c)2002, Jerry Han |", "+--------------------------------------+", }; char *splash_40x2[] = { /* "1234567890123456789012345678901234567890" */ " Serial %s Display ", " (c)2002, Jerry Han ", }; void set_type(FILE *fp, int flag) { if (flag == WR) { fputc(254, fp); // Set Config fputc(0x5a, fp); fputc(width, fp); // Columns fputc(lines, fp); // Rows fputc(type, fp); // LCD } else { if (type == TYPE_VFD) { printf("Type: VFD\n"); } else { printf("Type: LCD\n"); } printf("Size: %d x %d\n", width, lines); } } void set_splash(FILE *fp, int flag) { char buf[BUFSIZ]; char screen[BUFSIZ] = ""; int i; char *format; char **splash; if (width == 16 && lines == 2) splash = splash_16x2; else if (width == 16 && lines == 4) splash = splash_16x4; else if (width == 20 && lines == 2) splash = splash_20x2; else if (width == 20 && lines == 4) splash = splash_20x4; else if (width == 40 && lines == 2) splash = splash_40x2; else if (width == 40 && lines == 4) splash = splash_40x4; else splash = NULL; if (splash == NULL) { fprintf(stderr, "No fitable screen founded, sorry!\n"); return; } for (i = 0; i < lines; i++) { format = splash[i]; if (strstr(format,"%s") != NULL) { if (type == TYPE_VFD) { sprintf(buf, format, "VFD"); } else { sprintf(buf, format, "LCD"); } strcat(screen, buf); } else { strcat(screen, format); } if (flag == PR) strcat(screen, "\n"); } if (flag == WR) { if (strlen(screen) != (width * lines)) { fprintf(stderr, "Splash define mismatch, please check it!\n"); return; } fputc(254, fp); // Set Config fputc(0x5b, fp); fprintf(fp, "%s",screen); } else { printf("%s\n",screen); } } void prompt() { char buf[BUFSIZ]; int tmp; while(type == TYPE_UNDEF) { printf("Please select module type: [VFD|LCD] "); gets(buf); if (strncmp(buf, "VFD", 3) == 0) type = TYPE_VFD; else if (strncmp(buf, "LCD", 3) == 0) type = TYPE_LCD; } while(width < 1) { printf("Please select module width: [16|20|40] "); gets(buf); tmp = atoi(buf); switch (tmp) { case 40: width = 40; break; case 20: width = 20; break; case 16: width = 16; break; } } while(lines < 1) { printf("Please select module lines: [2|4] "); gets(buf); tmp = atoi(buf); switch (tmp) { case 4: lines = 4; break; case 2: lines = 2; break; } } } int main (int argc, char *argv[]) { FILE *fp; int i,k,f; int cmd = 1; char buf[BUFSIZ]; prompt(); while (cmd > 0) { printf("Preview:Write:Exit: ?"); gets(buf); if (strncmp(buf, "P", 1) == 0 || strncmp(buf, "p", 1) == 0) { set_type(stdout, PR); set_splash(stdout, PR); } else if (strncmp(buf, "W", 1) == 0 || strncmp(buf, "w", 1) == 0) { fp = fopen("COM1:19200","w"); if (fp == NULL) { printf("Error on open the file.\n"); exit(0); } set_type(fp, WR); set_splash(fp, WR); fflush(fp); cmd = 0; } else if (strncmp(buf, "E", 1) == 0 || strncmp(buf, "e", 1) == 0) { cmd = 0; } } }