added bitmappedgpio class to map several gpio in one byte
This commit is contained in:
parent
d7cfbcaf28
commit
477ea2605d
24
examples/bitmap.php
Normal file
24
examples/bitmap.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
/*
|
||||
* This example demonstrates mapping GpioPins to bit values and writing bits
|
||||
* in parallel.
|
||||
*
|
||||
*/
|
||||
|
||||
use NoccyLabs\Gpio\Gpio;
|
||||
use NoccyLabs\Gpio\Util\BitmappedGpio;
|
||||
use NoccyLabs\Gpio\GpioMapper\WiringPiMapper;
|
||||
|
||||
$gpio = new Gpio();
|
||||
$gpio->setMapper( new WiringPiMapper(2) );
|
||||
|
||||
$par = new BitmappedGpio();
|
||||
for($n = 0; $n < 7; $n++) {
|
||||
$par->setGpioPin($n,$gpio[$n]);
|
||||
}
|
||||
|
||||
$par->write(0xFF);
|
||||
$par->write(0x00);
|
46
lib/Util/BitmappedGpio.php
Normal file
46
lib/Util/BitmappedGpio.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Gpio\Util;
|
||||
|
||||
use NoccyLabs\Gpio\GpioPin;
|
||||
|
||||
class BitmappedGpio
|
||||
{
|
||||
protected $gpio = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->gpio = array_fill(0,7,null);
|
||||
}
|
||||
|
||||
public function setGpioPin($bit, GpioPin $gpiopin = null)
|
||||
{
|
||||
if (($bit >= 0) && ($bit <= 7)) {
|
||||
$this->gpio[$bit] = $gpiopin;
|
||||
} else {
|
||||
throw new \Exception("bit must be 0-7");
|
||||
}
|
||||
}
|
||||
|
||||
public function read()
|
||||
{
|
||||
$r = 0;
|
||||
foreach($this->gpio as $bit=>$gpio) {
|
||||
if ($gpio->getValue()) { $r |= 1<<$bit; }
|
||||
}
|
||||
return $r;
|
||||
}
|
||||
|
||||
public function write($byte)
|
||||
{
|
||||
$r = 0;
|
||||
foreach($this->gpio as $bit=>$gpio) {
|
||||
if ($gpio) {
|
||||
$gpio->setValue((bool)($byte & (1<<$bit)));
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user