#!/usr/bin/perl -w # # Original Code # -------------- # http://www.Linux-1U.net/LCD/scripts/test_lcd.2.pl # # #--------------------------------------- # # test_lcd.2.pl display stuff to the LCD # # 07-Jul-00 amo Date-of-Birth ( stolen from record_sensors.pl ) # 20-Apr-01 amo Use ifconfig instead ( os independent ) # 30-Oct-02 amo Cleanup wierd characters, reformat script for BPI-216L # 10-Nov-02 amo Added Codes for MatrixOrbital LK202-25 # # # # =========================================== # use strict ; # # # Name of the program # my ( $NM ) = "test_lcd.pl"; my ( $VER ) = "2.1.0"; # my ( $LCDTYPE ) = "BPI-216L"; $LCDTYPE = "LK202-25"; # MatrixOrbital # # my ( $DEBUG ) = 0; # debug my ( $SLEEP ) = 2; # time between scroll/updates # # # which serial port is the display attached to # #y ( $SERIALPORT ) = "/dev/ttyS0"; # RS232 connector on back my ( $SERIALPORT ) = "/dev/ttyS1"; # 9-pin header inside on motherboard # my ( $SERIALRATE ) = "9600"; # Default to 9600 baud my ( $WIDTH ) = 16; # Default to 16 x 2 LCD # # # Cursor Code For BPI-216L # ------------------------ # my ( $CMD ) = "\xFE"; # general command prefix my ( $CLS ) = "\x01"; # clear display my ( $HOME ) = "\x02"; # Home my ( $Line1 ) = "\xEF"; # Line1 my ( $Line2 ) = "\xC0"; # Line2 # # if ( $LCDTYPE eq "LK202-25" ) { # # For MatrixOrbital LK202-25 # -------------------------- $SERIALRATE = "19200"; $WIDTH = 20; # 20x2 serial lcd baudrate # $CMD = ""; # no command code $CLS = "\x0C"; $Line1 = "\xFE\x48"; # why does it need 2 hex numbers ?? $Line2 = "\x0A"; # # my ( $KeyF1 ) = "C"; # Function Key my ( $KeyF2 ) = "H"; # Function Key my ( $KeyF3 ) = "M"; # Function Key # my ( $KeyUP ) = "D"; # Up key my ( $KeyDN ) = "J"; # Down key my ( $KeyLE ) = "I"; # Left key my ( $KeyRI ) = "N"; # Right key # my ( $KeyEsc ) = "E"; # Esc key my ( $KeyOK ) = "O"; # OK key # } # MatrixOrbital # # # End User Changes # # =========================================== # # Check command line arguments # ---------------------------- &cmd_args; # # # Get some info about the hosts # ----------------------------- # &lcd_init; # initialize the LCD # # load up the data block for LCD display # my ( @DATA ) = &lcd_data; # # # display the data # &lcd_display ( @DATA ); # exit 0; # # # =========================================================== # # Initialize the Serial Port ( LCD ) # ----------------------------------- # sub lcd_init { # system ( "stty $SERIALRATE <$SERIALPORT" ); # # open ( LCDOUT, "> $SERIALPORT"); # } # lcd_init # # # Clear LCD screen # ---------------- sub lcd_cls { # printf LCDOUT "${CMD}$CLS"; # } # lcd_cls # # # Home # ---------------- sub lcd_home { # printf LCDOUT "$CMD" . "$HOME"; # } # lcd_home # # # Get the Hostname # ----------------- sub lcd_get_hostname { # my ( $opt ) = $_[0]; # my ( $host ) = `hostname $opt`; chomp ( $host ); # return ( $host ); # } # lcd_get_hostname # # # Get the IP # ----------- sub lcd_get_ip { # my ( $ip_gw ) = $_[0]; # my ( @ipaddr, @f ) = (); my ( $ip, $str ) = ""; # if ( $ip_gw eq "gw" ) { # # default w.x.y.z 0.0.0.0 UG 0 0 0 eth0 @ipaddr = split ( /\s+/, ` route -v | grep default ` ); # } else { # # inet addr:166.90.172.11 Bcast:166.90.172.255 Mask:255.255.255.0 @ipaddr = split ( /:/, `ifconfig -v | grep -i "inet addr" | grep -v 127.0.0 ` ); # } # $str = $ipaddr[1]; ( $ip, @f) = split ( /\s+/, $str ); # return ( $ip ); # } # lcd_get_ip # # # Get the ProcessID # ------------------ # sub lcd_get_pid { # my ( $str ) = ` ps auxw | grep $NM `; my ( $x_y, $pid, @foo ) = split ( /\s+/, $str ); # return ( $pid ); # } # lcd_get_pid # # # Get CPU Info # ------------ # model name : AMD Athlon(tm) # cpu MHz : 1152.633 # sub lcd_get_cpuinfo { my ( $key ) = $_[0]; # my ( $cpuinfo ) = `cat /proc/cpuinfo | grep -i "$key"`; # $cpuinfo =~ s/\(tm\)//; # my ( $str, $info ) = split ( /:/, $cpuinfo ); # $info =~ s/^\s+//; # get rid of leading spaces $info =~ s/\s+$//; # trailing spaces # return ( "$info" ); } # lcd_get_cpuinfo # # # Get CPU load # ------------- # sub lcd_get_cpuload { my ( $str ) = `cat /proc/loadavg`; my ( $min, $typ, $max, @foo ) = split ( /\s+/, $str ); # return ( "$min..$typ..$max" ); } # lcd_get_cpuload # # # Get Mem load # ------------- # sub lcd_get_memswapload { # my ( $ms ) = $_[0]; # Mem: or Swap: # my ( $str ) = `cat /proc/meminfo | grep $ms:`; my ( $key, $tot, $used, $free, @foo ) = split ( /\s+/, $str ); # my ( $t, $u, $f ) = ""; # $t = &lcd_MK ( $tot ); $u = &lcd_MK ( $used ); $f = &lcd_MK ( $free ); # # printf "$t..$tot....$u..$used....$f..$free..\n"; # return ( "$t..$u..$f" ); } # lcd_get_memswapload # # # netstat -iv # ----------- # Kernel Interface table # Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg # eth0 1500 0 7984 0 0 0 2244 0 0 0 BMRU # lo 16436 0 218 0 0 0 218 0 0 0 LRU # # Get Network load # ----------------- # sub lcd_get_netload { my ( $str ) = `netstat -iv | grep eth0`; my ( $eth, $mtu, $met, $RCV, $e, $d, $o, $TX, @foo ) = split ( /\s+/, $str ); # return ( "R=$RCV X=$TX" ); } # lcd_get_netload # # # Get Disk load # ------------- # df - hangs on NFS issues # /dev/hda1 256667 123084 120331 51% / # /dev/hda2 256665 8263 235149 4% /tmp # /dev/hda3 521780 175404 319872 36% /var # # cat /proc/mounts # /dev/root / ext3 rw 0 0 # /dev/hda2 /tmp ext3 rw 0 0 # /dev/hda3 /var ext3 rw 0 0 # # sub lcd_get_diskload { my ( $part ) = $_[0]; # which partition # my ( @df ) = `df | grep "$part" `; my ( $p, $m, $u, $l, $per, $name ) = split ( /\s+/, "$df[0]" ) ; my ( $mk ) = "xx"; # $mk = &lcd_MK ( "$m" ) if ( $#df >= 0 ); # return ( "$name $mk $per" ); # } # lcd_get_diskload # # # Get CPU Temperature, fan speed # ------------------------------ # fan2: 6750 RPM (min = 3000 RPM, div = 2) # temp2: +53.5°C (limit = +60°C, hysteresis = +50°C) sensor = thermistor # sub lcd_get_cpu_tf { my ( $tf ) = $_[0]; # cpu temp or cpu fan my ( @data3 ) = `sensors | grep $tf`; my ( @res, @f, @t ) = (); # my ( $id, $key, $r, $t ) = 0; # for ( $id=0; $id <= $#data3; $id++ ) { ( $key, $res[$id], @f ) = split ( /\s+/, $data3[$id] ); # # Get rid of the whacky degree symbol before C ( centigrade ) # $r = $res[$id]; # if ( $tf eq "temp" ) { chop ( $r ); chop ( $r ); $r =~ s/^\+//; $r .= "C"; # append centigrade letter } # temp # $t[$id] = "$r"; # } # fan and temp sensors # $r = join ( " ", @t ); # return ( "$r" ); } # lcd_get_cpu_tf # # # # Convert the number 1234567 to 1234K # ------------------------------------ # sub lcd_MK { my ( $n ) = $_[0]; # my ( $kk ) = $n / 1000; my ( $mm ) = $n / 1000000; # my ( $m, $k, @f ) = 0; # if ( $mm > 1 ) { ( $m, @f ) = split ( /\./, $mm ); return ( "${m}M" ); } elsif ( $kk > 1 ) { ( $k, @f) = split ( /\./, $kk ); return ( "${k}K" ); } else { return ( $n ); } # } # lcd_MK # # # Time and Date stamp # ------------------- sub lcd_get_ts { my ( $id ) = $_[0]; # # Thu Oct 31 19:58:50 PST 2002 my ( $dow, $mon, $day, $time, $zone, $year ) = split ( /\s+/, ` date `); # if ( $id == 1 ) { return ( "$dow $mon $day $year" ); } else { return ( "$time $zone" ); } # } # lcd_get_ts # # # Load a bunch of Data to the Array for display selection # -------------------------------------------------------- # sub lcd_data { # my ( @DATA ) = (); # printf "..Loading data ..\n" if ( $DEBUG ); # # NOT all data need to be re-read # $DATA[0] = 0; # initialize $DATA[1] = "$NM-$VER"; $DATA[2] = &lcd_get_pid; $DATA[3] = &lcd_get_hostname ( "-s" ); # hostname -s $DATA[4] = &lcd_get_hostname ( "-d" ); # hostname -d $DATA[5] = &lcd_get_ip ( "ip" ); # ip# $DATA[6] = &lcd_get_ip ( "gw" ); # gateway $DATA[7] = &lcd_get_cpuinfo ( "model name" ); # model name : AMD Athlon(tm) $DATA[8] = &lcd_get_cpuinfo ( "cpu Mhz" ) ; # cpu MHz : 1152.633 # # dynamic data # $DATA[9] = "CPU Load"; $DATA[10] = &lcd_get_cpuload; $DATA[11] = "Memory Load"; $DATA[12] = &lcd_get_memswapload ( "Mem" ); $DATA[13] = "Swap Load"; $DATA[14] = &lcd_get_memswapload ( "Swap" ); $DATA[15] = "eth0: Rcv/Xmit"; $DATA[16] = &lcd_get_netload; $DATA[17] = "Disk Load"; $DATA[18] = &lcd_get_diskload ( "/" ); $DATA[19] = &lcd_get_diskload ( "/tmp" ); $DATA[20] = &lcd_get_diskload ( "/var" ); $DATA[21] = &lcd_get_diskload ( "/usr" ); $DATA[22] = &lcd_get_diskload ( "/opt" ); # $DATA[23] = "Temp1 Temp2 Temp3"; $DATA[24] = &lcd_get_cpu_tf ( "temp" ); $DATA[25] = "Fan1 Fan2 Fan3"; $DATA[26] = &lcd_get_cpu_tf ( "fan" ); $DATA[27] = &lcd_get_ts ( 1 ); # date $DATA[28] = &lcd_get_ts ( 2 ); # time $DATA[29] = 0; # initialize # return ( @DATA ); # } # lcd_data # # # Show Something on the LCD # -------------------------- # sub lcd_display { # my ( @data ) = 0; # my ( $status, $id, $jd, $d, $e ) = 0; # # # Initial setup info dumped to the screen # printf "\n"; printf "$NM: $LCDTYPE LCD on serial port=$SERIALPORT..$SERIALRATE..pid=%s..\n", &lcd_get_pid; printf "\n"; # # # close LCDOUT; # system ( "stty $SERIALRATE <$SERIALPORT" ); $status = open ( LCDOUT, "> $SERIALPORT"); # if ( $status == 0 ) { printf "ERROR: Could not open LCDport=$SERIALPORT\n"; exit 1; } # # Indefinitely loop around and show data # -------------------------------------- # while ( 1 ) { # # # Get new block of data for lcd updates # -------------------------------------- @data = &lcd_data; # # Print the data to the LCD # ------------------------- # for ( $id = 1; $id < $#data; $id +=2 ) { # $d = $data[$id]; $jd = $id+1; $e = $data[$jd]; # if ( $DEBUG ) { printf "data[$id]=$d\n"; printf "data[$jd]=$e\n"; } # # # Clear the LCD display # ------------------------------ # printf LCDOUT "${CMD}$CLS"; # CLS # # # Output ( left justified ) string to the LCD # printf LCDOUT "${CMD}$Line1"; # Line1 printf LCDOUT "%*s", -$WIDTH, $d; # printf LCDOUT "${CMD}$Line2"; # Line2 printf LCDOUT "%*s", -$WIDTH, $e; # printf LCDOUT "\n"; # flush the lcd # sleep $SLEEP ; # wait a while # # display time ( last entry ) for little longer sleep ( 5 ) if ( $id +2 >= $#data ); # } # for loop # } # while loop - indefinitely # close ( LCDOUT ); # } # lcd_display # # # Check the Command line arguments # -------------------------------- # sub cmd_args { my ( $i ) = 0; my ( $arg ) = ""; # while ( $i <= $#ARGV ) { $arg = $ARGV[$i]; $DEBUG = 1 if ( $arg eq "-d" ); $i += 1; } # } # cmd_args # # # end of file