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

64 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?php
2014-06-14 01:10:07 +00:00
/*
* 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\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;
}
}