/* * * Original File * -------------- * http://Linux-1U.net/LCD/scripts/test_lcd.c * http://wildsau.idv.uni-linz.ac.at/%7Eherp/Crystalfontz/lcdtest.c * http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/node115.html ( getip() ) * * * * 20-Mar-05 amo Added lcdtest2() for customized LCD messages * 21-Mar-05 amo Added getip() * * * Compile and run it: * ------------------ * gcc -g -Wall -O2 -o test_lcd test_lcd.c * ./test_lcd * */ #include #include #include #include #include #include #include /* for getip */ #include #include #include #include #include #include /* LCD codes for this serial piggie back adapter ============================================= 1 0x01 clear 2 0x02 home 128 0x80 line1 192 0xC0 line2 254 0xFE cmd */ char *NAM = "test_lcd.c"; char *VER = "2005.0320"; unsigned char LCDcmd = 254; unsigned char LCDclr = 1; unsigned char LCDrone = 128; unsigned char LCDrtwo = 192; int fd; void die(char *s) { if (errno) perror(s); else fprintf(stderr,"%s\n",s); exit(-1); } /* * print an unsigned ch to the lcd * ------------------------------- */ void pr_byte ( int lcd, unsigned char *ch ) { write ( lcd, (unsigned char *)ch, 1 ); } /* pr_byte */ /* * put a string to the lcd * ----------------------- */ void pr_str ( int lcd, char *str ) { while ( *str ) { pr_byte ( lcd, (char *)str++ ); } } /* pr_str */ /* * lcd commands * ------------- */ void lcd_cmd ( int lcd, int cmd ) { unsigned char *buff = ( unsigned char *) calloc ( 8, sizeof(char) ); buff[0] = LCDcmd; buff[1] = cmd; /* * Debug messages * int len = strlen ( buff ); printf ( "..buff=\\0x%02x\\0x%02x..len=%d..sizeof(buff)=%d..\n", buff[0],buff[1], len, sizeof(buff) ); */ /* LCD commands: 0xFE + 0xCMD */ write ( lcd, buff, 2 ); } /* lcd_cmd */ /* * go to line 1 or line 2 * ---------------------- */ void go_rc ( int lcd, int row ) { if ( row == 2 ) { lcd_cmd ( lcd, LCDrtwo ); /* line 2 */ } else { lcd_cmd ( lcd, LCDrone ); /* line 1 */ } } /* pr_rc */ /* * get IP# * http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/node115.html */ char* getip ( char *host ) { char *quad; struct in_addr adr; struct hostent *h = (struct hostent *) gethostbyname ( host ); bcopy ( h->h_addr, &adr.s_addr, h->h_length ); quad = inet_ntoa(adr); return ( quad ); } /* getip */ /* * Customize the LCD screen * ------------------------ */ void lcdtest2 ( char *devname ) { int lcd = open ( devname, O_RDWR | O_NOCTTY | O_NONBLOCK ); if ( lcd <= 0) { printf ( "ERROR: failed to open %s\n", devname ); exit (1); } /* * get the hostname.DomainName */ char HostName[255]; gethostname(HostName, 50); char DomainName[255]; getdomainname(DomainName, 100); /* * get the ip */ char *IP = getip ( HostName ); printf ( "..Host IP=%s..\n", IP ); /* * print these messages a message to the LCD * -- use even number of messages -- msg[0]=NAM, msg[1]=VER, ... */ char *msg[] = { NAM, VER, HostName, DomainName, "Linux-1U.net/LCD", IP, "Gateway", "DNS", "CPU Info", "Memory Info", "Disk Utilization", "extra line", "" }; /* manually adjust "last" */ int i = 0; int last = 12; /* first message is mag[0] */ /* * just print a test mesg and exit * * pr_str ( lcd, (char *)msg[2] ); * exit ( 1 ); * */ /* * infinite loop displaying the message * ------------------------------------ */ while ( i <= last ) { /* clear the screen */ lcd_cmd ( lcd, LCDclr ); go_rc ( lcd, 1 ); /* line 1 */ pr_str ( lcd, (char *)msg[i] ); /* printf ( "..last=%d..msg[%d]=%s..\n", last, i, msg[i] ); */ i +=1; go_rc ( lcd, 2 ); /* line 2 */ pr_str ( lcd, (char *)msg[i] ); /* printf ( "..last=%d..msg[%d]=%s..\n", last, i, msg[i] ); */ sleep ( 2 ) ; i += 1; if ( i >= last ) { i = 0 ; } } /* loop forever */ } /* lcdteste2 */ /* * Configure the LCD * ================== */ int main() { struct termios termios; /* fd=open("/dev/ttyS0",O_WRONLY|O_EXCL); */ fd=open("/dev/ttyS1",O_WRONLY|O_EXCL); if (fd==-1) die("open"); if (tcgetattr(fd,&termios)==-1) die("tcgetattr"); cfmakeraw(&termios); cfsetospeed(&termios,B9600); if (tcsetattr(fd,TCSANOW,&termios)==-1) die("tcsetattr"); usleep(350000); write(fd,"\004\024\030",3); /* hide cursor/wrap=off/scroll=off */ /* lcdtest(); */ close(fd); /* customize the LCD messages */ lcdtest2 ( "/dev/ttyS1" ); } /* * end of file */