#!/usr/bin/php 1s. $fadespeed=5; //Get the current contents of the display based on the current time and the //contents of the file contents.txt. function getcontents() { $f=fopen("contents.txt","r"); $l="No content."; while(!feof($f)) { $line=fgets($f,10240); $e=explode("|",$line,2); $t=strtotime($e[0]); if ($t>100) { if ($t>=time()) break; $l=$e[1]; } } fclose($f); return chop($l); } //Send the image for handle $im to the serial port $com function sendimage($com,$im) { $boff=array( array(0,0), array(1,0), array(2,0), array(3,0), array(0,1), array(1,1), array(2,1), array(3,1)); static $n=0; $data=chr(0x80); $data.=chr(0x80); $data.=chr(0x80); $data.=chr(0x80); $data.=chr(0x80); for ($brd=0; $brd<8; $brd++) { $xx=$boff[$brd][0]*32; $yy=$boff[$brd][1]*16; for ($y=0; $y<16; $y++) { for ($x=0; $x<32; $x++) { $c=imagecolorat($im,$x+$xx,$y+$yy); $data.=chr(($c>>18)&0x3f); } } } fwrite($com,$data); $n++; // imagepng($im,sprintf("disp-%04d.png",$n)); } function getImageName($base, $no) { $r=str_replace("#", $no, $base); if (file_exists($r)) return $r; $r=str_replace("#", sprintf("%02d", $no), $base); if (file_exists($r)) return $r; $r=str_replace("#", sprintf("%03d", $no), $base); if (file_exists($r)) return $r; $r=str_replace("#", sprintf("%04d", $no), $base); if (file_exists($r)) return $r; return ""; } system("stty 1000000 raw < /dev/ttyUSB0"); $im=imagecreatetruecolor(128,32); $bg=imagecolorallocate($im,0,0,0); $fg=imagecolorallocate($im,255,0,0); $com=fopen("/dev/ttyUSB0","w"); while(1) { $str=getcontents(); $lines=explode("|",$str); echo "Current message: $str\n"; //Get type according to the content. //Type 0: Static single or multiple lines. //Type 1: Single line, too large for the display so scrolling. //Type 2: Animation if (sizeof($lines)>1) { $type=0; if (substr($lines[0],0,1)=="$") $type=2; } else { $a=imagettftext($im, 26, 0, $xpos, 25, $fg, realpath($font), $str); $w=$a[2]-$a[0]; if ($w>imagesx($im)) { $type=1; } else { $type=0; } } if ($type==0) { $pt=26/sizeof($lines); imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bg); if (sizeof(lines)>1) { $f=$font3lines; } else { $f=$font; } for ($i=0; $i0); } if ($type==2) { //image $pic=0; while(($n=getImageName(substr($lines[0],1),$pic))!="") { $bim=imagecreatefrompng($n); $oldim=imagecreate(imagesx($im), imagesy($im)); imagecopy($oldim, $im, 0, 0, 0, 0, imagesx($im), imagesy($im)); if (intval($lines[1])>1000) { for ($pct=0; $pct<100; $pct+=$fadespeed) { imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bg); imagecopy($im, $bim, 0, 0, 0, 0, imagesx($im), imagesy($im)); imagecopymerge($im, $oldim, 0, 0, 0, 0, imagesx($im), imagesy($im), 100-$pct); sendimage($com,$im); } } imagedestroy($oldim); imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bg); echo $n."\n"; imagecopy($im, $bim, 0, 0, 0, 0, imagesx($bim), imagesy($bim)); imagedestroy($bim); sendimage($com,$im); if ($lines[1]!="") { usleep($lines[1]*1000); } else { usleep(10000); } $pic++; } } } ?>