added bitmappedgpio class to map several gpio in one byte
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user