Implemented the tick handler
This commit is contained in:
		
							
								
								
									
										34
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								README.md
									
									
									
									
									
								
							@@ -40,4 +40,38 @@ be portable/supported/efficient/a good idea):
 | 
			
		||||
    
 | 
			
		||||
    // The interrupts will now be polled approx every 5th php vm "tick"
 | 
			
		||||
 | 
			
		||||
## Devices
 | 
			
		||||
 | 
			
		||||
    $lcd = new NoccyLabs\Gpio\Device\Display\Pcd8544Device;
 | 
			
		||||
    // Set I/O pins
 | 
			
		||||
    $lcd->res = $gpio[4];
 | 
			
		||||
    $lcd->dc  = $gpio[8];
 | 
			
		||||
    ...
 | 
			
		||||
    // Activate the device
 | 
			
		||||
    $lcd->activate();
 | 
			
		||||
    // Clear and draw text
 | 
			
		||||
    $lcd->clear();
 | 
			
		||||
    $lcd->writeAt(0,0,"Hello World");
 | 
			
		||||
    echo $lcd->getRows(); // 5
 | 
			
		||||
    echo $lcd->getCols(); // 19
 | 
			
		||||
 | 
			
		||||
### LCD Bridge
 | 
			
		||||
 | 
			
		||||
The LCD bridge will be able to bridge any device class implementing `LcdDeviceInterface`
 | 
			
		||||
to stdin or a named pipe. This basically creates a user-space daemon to interface with
 | 
			
		||||
the display without having to fiddle with bits.
 | 
			
		||||
 | 
			
		||||
    $ lcdbridge -t pcd8544 --stdin
 | 
			
		||||
    CLR
 | 
			
		||||
    LOC 0 0
 | 
			
		||||
    FONT 0
 | 
			
		||||
    OUT Hello World
 | 
			
		||||
    ^C
 | 
			
		||||
    $ 
 | 
			
		||||
 | 
			
		||||
Commands should include:
 | 
			
		||||
 | 
			
		||||
 * `CLR` clears the display (`LcdDeviceInterface#clear()`)
 | 
			
		||||
 * `LOC n m` moves the cursor to line n column m (0-indexed) (`LcdDeviceInterface#setCursorPos(n,m)`) 
 | 
			
		||||
 * `FONT n` loads the default font (0) or a custom font file (`LcdDeviceInterface#setFont()`)
 | 
			
		||||
 * `OUT` Write text to the display (`LcdDeviceInterface#write()`)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								examples/tickhandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								examples/tickhandler.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * This example demonstrate a possibility, but not a best practice. When using
 | 
			
		||||
 * the tick handler, make sure the code being executed has ticks declared.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Gpio\Gpio;
 | 
			
		||||
use NoccyLabs\Gpio\GpioTickHandler;
 | 
			
		||||
 | 
			
		||||
$gpio = new Gpio(true);
 | 
			
		||||
 | 
			
		||||
$gpiotick = new GpioTickHandler();
 | 
			
		||||
$gpiotick->registerGpio($gpio);
 | 
			
		||||
 | 
			
		||||
declare(ticks=5);
 | 
			
		||||
 | 
			
		||||
while(true) {
 | 
			
		||||
    usleep(10000);
 | 
			
		||||
}
 | 
			
		||||
@@ -57,13 +57,15 @@ class Gpio implements \ArrayAccess
 | 
			
		||||
        $read = $this->fd_gpio;
 | 
			
		||||
        $write = array();
 | 
			
		||||
        $except = $read;
 | 
			
		||||
        select($read, $write, $except, 0);
 | 
			
		||||
        if (count($read)>0) {
 | 
			
		||||
            stream_select($read, $write, $except, 0);
 | 
			
		||||
            foreach($except as $fd) {
 | 
			
		||||
                $pin = $this->getPinFromFd($fd);
 | 
			
		||||
                $pin->doInterrupt();
 | 
			
		||||
            
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function getPinFromFd($fd)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										46
									
								
								lib/GpioTickHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								lib/GpioTickHandler.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2014, NoccyLabs
 | 
			
		||||
 * 
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 * 
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 * 
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\Gpio;
 | 
			
		||||
 | 
			
		||||
declare(ticks=5);
 | 
			
		||||
 | 
			
		||||
class GpioTickHandler
 | 
			
		||||
{
 | 
			
		||||
    protected $gpio = array();
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        register_tick_function(array($this,"onTick"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function registerGpio(Gpio $gpio)
 | 
			
		||||
    {
 | 
			
		||||
        $this->gpio[] = $gpio;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function onTick()
 | 
			
		||||
    {
 | 
			
		||||
        foreach($this->gpio as $g) {
 | 
			
		||||
            $g->refresh();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user