diff --git a/README.md b/README.md index bad7fe4..86f14fb 100644 --- a/README.md +++ b/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()`) diff --git a/examples/tickhandler.php b/examples/tickhandler.php new file mode 100644 index 0000000..caa7554 --- /dev/null +++ b/examples/tickhandler.php @@ -0,0 +1,23 @@ +registerGpio($gpio); + +declare(ticks=5); + +while(true) { + usleep(10000); +} diff --git a/lib/Gpio.php b/lib/Gpio.php index ee5f4f8..a94a690 100644 --- a/lib/Gpio.php +++ b/lib/Gpio.php @@ -57,11 +57,13 @@ class Gpio implements \ArrayAccess $read = $this->fd_gpio; $write = array(); $except = $read; - select($read, $write, $except, 0); - foreach($except as $fd) { - $pin = $this->getPinFromFd($fd); - $pin->doInterrupt(); - + if (count($read)>0) { + stream_select($read, $write, $except, 0); + foreach($except as $fd) { + $pin = $this->getPinFromFd($fd); + $pin->doInterrupt(); + + } } } diff --git a/lib/GpioTickHandler.php b/lib/GpioTickHandler.php new file mode 100644 index 0000000..0090eda --- /dev/null +++ b/lib/GpioTickHandler.php @@ -0,0 +1,46 @@ + + */ + +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(); + } + } + +}