File source
<?php
/** registers set of screens
switches between them
handles touch actions - sends coordinates to Screen instances
sends screen image to device
*/
class ScreenManager{
// Port instance to send data to
protected $port;
// timeout of I/O
protected $timeout;
// list of registered screens
protected $screens = Array();
// instance of active screen
protected $active;
function __construct(Port $port){
$this->port = $port;
}
function register(Screen $screen){
$id = $screen->id();
$this->screens[$id] = $screen;
}
/* activates screen */
function activate($idOrScreen, $params=false){
if($idOrScreen instanceof Screen){
$this->active = $idOrScreen;
}else{
$this->active = $this->screens[$idOrScreen];
}
if(!$this->active) throw new Exception("No screen! $idOrScreen");
$this->active->activate($params);
}
/* main work function */
function work(){
if(!$this->active){
throw new Exception("no screen selected");
}
// let active screen do it work
$r = $this->active->work();
// handle actions
switch($r["action"]){
case "image":
$img = $r["image"];
$this->sendImage($img);
imagepng($img, "out.png");
if($r["destroy"]) imagedestroy($img);
break;
case "screen":
$this->activate($r["id"], $r["params"]);
$this->work();
return;
break;
case "nothing":
break;
default:
break;
}
// set port timeout to screen timeout
$this->port->setTimeout($this->active->timeout());
while(true){
$r = $this->port->read(1);
if($r !== false && strlen($r) > 0){
switch ($r) {
// incoming touch
case 'T':
$x = ord($this->port->read(1));
$y = ord($this->port->read(1));
// rotate to landscape !
$this->active->touch(160 - $y,$x);
break;
// incoming refresh request
case 'R':
clearBuf();
break;
}
// shorten timeout and repeat
// to get rid of all waiting events from device
$this->port->setTimeout(100);
}else{
break;
}
}
}
/** sends GD image $img to device */
function sendImage($img){
sendImg($img, $this->port);
}
}