php-linux-gpio/lib/Util/BitmappedGpio.php

47 lines
899 B
PHP

<?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;
}
}