PHP Library for controlling a 633(serial) LCD & Keypad

jdhawk

New member
I wrote a php class that interfaces with the CrystalFontz 633 Serial LCD & Keypad and thought I'd share. This currently requires PHP5 with streams built in, but with some quick changes to the class header, I believe it could work with PHP4.

The class provides all the basic methods to generate a packet, along with its CRC, and send it over a serial port connection (UNIX socket) and some quick wrappers to do common tasks. It also provides a function to monitor for keypresses.

This will currently only work on UNIX variants since it uses the /dev/tty socket to read and write data to the 633.

I'm sure there are some typos and maybe a few bugs, but feel free to modify and use it however you want. Just thought I'd thow it out there in case someone was trying to do something similar...might be a start in the right direction.


PHP:
class crystalfontz_control_class {

   public $in;
   public $out;

   public $MAX_LENGTH = 16;

   function __construct () {

   }//fi

   function connect ($device,$baud) {

      //init the serial port
      system("stty -F $device cs8 $baud \
                     ignbrk \
                     -brkint \
                     -icrnl \
                     -imaxbel \
                     -opost \
                     -onlcr \
                     -isig \
                     -icanon \
                     -iexten \
                     -echo \
                     -echoe \
                     -echok \
                     -echoctl \
                     -echoke \
                     noflsh \
                     ixon \
                     -crtscts");

      $this->in = fopen ($device,"r");
      $this->out = fopen ($device,"w");

      socket_set_blocking($this->out,0);
      socket_set_blocking($this->in,0);

      if ( (!$this->in) && (!$this->out) ) exit ("Could not open device [$device]. Running as root?");
      return(true);
   }//connect


   //monitors the stream for incomming packets. the select will 
   //timeout every 10 seconds giving you opportunity to
   //switch the screen, or you can perform an action when a packet is 
   //received based on the contents of the packet.

   function select () {

      $read[] = $this->in;
      $write = array();
      $except = array();

      //time out every 5 seconds in case the program wants to do some action.
      $tv_sec = 5;

      $nc = stream_select($read,$write,$except,$tv_sec);

      foreach ($read as $handle) {

         $aline .= stream_get_contents($handle);
         if (strlen($aline)>0) {
            $pkt = $this->parse_packet($aline);
            $aline = '';
         }//fi
      }//fi

      //if we have a packet return it.

      if (is_array($pkt)) return($pkt);

      return(false);

   }//fi



   //make the crc16 of the packet.
   function crc16 ($buffer) {

      $lookup_table = array (
            0x00000,0x01189,0x02312,0x0329B,0x04624,0x057AD,0x06536,0x074BF,
            0x08C48,0x09DC1,0x0AF5A,0x0BED3,0x0CA6C,0x0DBE5,0x0E97E,0x0F8F7,
            0x01081,0x00108,0x03393,0x0221A,0x056A5,0x0472C,0x075B7,0x0643E,
            0x09CC9,0x08D40,0x0BFDB,0x0AE52,0x0DAED,0x0CB64,0x0F9FF,0x0E876,
            0x02102,0x0308B,0x00210,0x01399,0x06726,0x076AF,0x04434,0x055BD,
            0x0AD4A,0x0BCC3,0x08E58,0x09FD1,0x0EB6E,0x0FAE7,0x0C87C,0x0D9F5,
            0x03183,0x0200A,0x01291,0x00318,0x077A7,0x0662E,0x054B5,0x0453C,
            0x0BDCB,0x0AC42,0x09ED9,0x08F50,0x0FBEF,0x0EA66,0x0D8FD,0x0C974,
            0x04204,0x0538D,0x06116,0x0709F,0x00420,0x015A9,0x02732,0x036BB,
            0x0CE4C,0x0DFC5,0x0ED5E,0x0FCD7,0x08868,0x099E1,0x0AB7A,0x0BAF3,
            0x05285,0x0430C,0x07197,0x0601E,0x014A1,0x00528,0x037B3,0x0263A,
            0x0DECD,0x0CF44,0x0FDDF,0x0EC56,0x098E9,0x08960,0x0BBFB,0x0AA72,
            0x06306,0x0728F,0x04014,0x0519D,0x02522,0x034AB,0x00630,0x017B9,
            0x0EF4E,0x0FEC7,0x0CC5C,0x0DDD5,0x0A96A,0x0B8E3,0x08A78,0x09BF1,
            0x07387,0x0620E,0x05095,0x0411C,0x035A3,0x0242A,0x016B1,0x00738,
            0x0FFCF,0x0EE46,0x0DCDD,0x0CD54,0x0B9EB,0x0A862,0x09AF9,0x08B70,
            0x08408,0x09581,0x0A71A,0x0B693,0x0C22C,0x0D3A5,0x0E13E,0x0F0B7,
            0x00840,0x019C9,0x02B52,0x03ADB,0x04E64,0x05FED,0x06D76,0x07CFF,
            0x09489,0x08500,0x0B79B,0x0A612,0x0D2AD,0x0C324,0x0F1BF,0x0E036,
            0x018C1,0x00948,0x03BD3,0x02A5A,0x05EE5,0x04F6C,0x07DF7,0x06C7E,
            0x0A50A,0x0B483,0x08618,0x09791,0x0E32E,0x0F2A7,0x0C03C,0x0D1B5,
            0x02942,0x038CB,0x00A50,0x01BD9,0x06F66,0x07EEF,0x04C74,0x05DFD,
            0x0B58B,0x0A402,0x09699,0x08710,0x0F3AF,0x0E226,0x0D0BD,0x0C134,
            0x039C3,0x0284A,0x01AD1,0x00B58,0x07FE7,0x06E6E,0x05CF5,0x04D7C,
            0x0C60C,0x0D785,0x0E51E,0x0F497,0x08028,0x091A1,0x0A33A,0x0B2B3,
            0x04A44,0x05BCD,0x06956,0x078DF,0x00C60,0x01DE9,0x02F72,0x03EFB,
            0x0D68D,0x0C704,0x0F59F,0x0E416,0x090A9,0x08120,0x0B3BB,0x0A232,
            0x05AC5,0x04B4C,0x079D7,0x0685E,0x01CE1,0x00D68,0x03FF3,0x02E7A,
            0x0E70E,0x0F687,0x0C41C,0x0D595,0x0A12A,0x0B0A3,0x08238,0x093B1,
            0x06B46,0x07ACF,0x04854,0x059DD,0x02D62,0x03CEB,0x00E70,0x01FF9,
            0x0F78F,0x0E606,0x0D49D,0x0C514,0x0B1AB,0x0A022,0x092B9,0x08330,
            0x07BC7,0x06A4E,0x058D5,0x0495C,0x03DE3,0x02C6A,0x01EF1,0x00F78);

      $crc= 0xFFFF;


      for ($lc=0;$lc<strlen($buffer);$lc++) {
         $char = $buffer[$lc];
         $crc = ($crc >> 8) ^ $lookup_table[($crc^ord($char) ) & 0xFF];
      }//for

      $crc = ~$crc;
      $crc = ($crc & 0xFFFF);
      $crc = chr($crc & 0x00FF).chr($crc >> 8);
      return ($crc);

   }//function

   //send the data to /dev/ttyS_
   function send_packet ($pkt) {

      if (is_resource($this->out)) {
         print ("putting packet:   ");
         $this->inspect_packet($pkt);
         fputs($this->out,$pkt);
         return (true);
      }//fi
      return(false);
   }//send_packet

   //pack the binary data into a packet
   function generate_packet ($type,$paramdata) {

      $data = pack ('a*',$paramdata);
      $length = strlen($data);
      $packet = pack ("C2a*",$type,$length,$data);
      $packet .= $this->crc16($packet);
      return($packet);

   }//gen_pkt


   //prints out a packet in hex so we can read it.
   function inspect_packet ($packet) {

      for ($x=0;$x<strlen($packet);$x++) {
         printf ("%x ",ord($packet[$x]));
      }//for
      print ("\n");
   }//fi


   //parses the return packet into something readable
   function parse_packet ($packet) {

      //$this->inspect_packet($packet);

      $pkt['type']   = ord($packet{0});
      $pkt['length']    = ord($packet[1]);
      $pkt['data'] = substr($packet,2,$pkt[length]);
      $pkt['crc']    = substr($packet,$pkt[length]+2);

      switch($pkt['type']) {
         case 70:  {

                      $pkt['action'] = 'response';
                      $pkt['response'] = 'CLEAR_LCD_OK';

                   }break; //case

         case 95:  {

                      $pkt['action'] = 'response';
                      $pkt['response'] = 'SET_LINE_OK';

                   }break; //case


         case 128: {

                      $pkt['data'] = ord($pkt['data']);

                      $KEY_MAP[1]='UP_ARROW_PRESS';
                      $KEY_MAP[2]='DOWN_ARROW_PRESS';
                      $KEY_MAP[3]='LEFT_ARROW_PRESS';
                      $KEY_MAP[4]='RIGHT_ARROW_PRESS';
                      $KEY_MAP[5]='CHECK_BUTTON_PRESS';
                      $KEY_MAP[6]='X_BUTTON_PRESS';
                      $KEY_MAP[7]='UP_ARROW_RELEASE';
                      $KEY_MAP[8]='DOWN_ARROW_RELEASE';
                      $KEY_MAP[9]='LEFT_ARROW_RELEASE';
                      $KEY_MAP[10]='RIGHT_ARROW_RELEASE';
                      $KEY_MAP[11]='CHECK_BUTTON_RELEASE';
                      $KEY_MAP[12]='X_BUTTON_RELEASE';

                      $pkt['action']   = 'keypress';
                      $pkt['keypress'] = $KEY_MAP[$pkt['data']];

                   }break; //case




      }//switch

      return($pkt);
   }

   //creates a custom font based on a matrix of binary values.
   //create_font(0,chr(1).chr(1).chr(2).chr(18).chr(18).chr(12).chr(4).chr(0));
   //makes a check mark and stores it in memory slot 0.

   function create_font ($font_register, $font_data) {

      $pkt = $this->generate_packet(9,chr($font_register).$font_data);
      $this->send_packet($pkt);
   }//create_font


   //saves all the current settings to the default state.
   function save_current_state () {

      $pkt = $this->generate_packet(4,'');
      $this->send_packet($pkt);

   }//save_current_state


   function set_contrast ($contrast) {

      //the contrast values for this lcd is 0-50. default is 16.

      //adjust $contrast (which is out of 100) to be in range.

      if ($contrast > 100) $contrast = 100;
      if ($contrast < 0) $contrast = 0;

      $contrast = 50 * ($contrast/100);

      $pkt = $this->generate_packet(13,chr($contrast));
      $this->send_packet($pkt);

   }//fi

   //wrapper to write line1 and line2 on the LCD.
   function set_screen ($line1, $line2) {

      $line1 = $this->trim_pad ($line1);
      $line2 = $this->trim_pad ($line2);

      $line1_pkt = $this->generate_packet(31,chr(0).chr(0).$line1);
      $line2_pkt = $this->generate_packet(31,chr(0).chr(1).$line2);

      $this->send_packet($line1_pkt);
      $this->send_packet($line2_pkt);

   }//fi

   function clear_screen () {

      $pkt = $this->generate_packet(6,'');
      $this->send_packet($pkt);

   }//clear_screen


   //ping just has the LCD return the same packet.
   function ping () {
      $pkt = $this->generate_packet(0,'');
      $this->send_packet($pkt);

   }//ping



   //this will trim a string over MAX_LENGTH or pad it to MAX LENGTH depending on its size.
   function trim_pad ($str) {

      if (strlen($str) > $this->MAX_LENGTH) {

         $str = substr($str,0,16);

      } elseif (strlen($str) < $this->MAX_LENGTH) {

         $str = str_pad($str,16,' ');

      }//fi

      return($str);
   }//trim_pad

}//class


and example of using it:

PHP:
   include_once('crystalfontz.inc');

   $lcd = new crystalfonts_control_class();

   $lcd->connect($tty_device,$baud) or die ("could not connect to $tty_device ($baud)\n");

   //make a check mark
   $lcd->create_font(0,chr(1).chr(1).chr(2).chr(18).chr(18).chr(12).chr(4).chr(0));
   $lcd->clear_screen();

   //show a welcome screen along w/ our custom character.
   $lcd->set_screen("LCD Status","Monitoring...".chr(0));

   while (true) {
      $pkt = $lcd->select();
      print_r($pkt);

      if ($pkt['action']=='keypress'){
         switch($pkt['keypress']) {

            case "DOWN_ARROW_RELEASE":{
               $lcd->set_screen("Key Pressed:","Down Key Release");
               }break;

            case "UP_ARROW_RELEASE":{
               $lcd->set_screen("Key Pressed:","Up Key Release");
               }break;

         }//end switch
       }//fi

   }//while
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 

Attachments

Top