From 477ea2605dab3161f95a56a2af1fe4b886c6cbed Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 12 Jun 2014 18:31:39 +0200 Subject: [PATCH] added bitmappedgpio class to map several gpio in one byte --- examples/bitmap.php | 24 ++++++++++++++++++++ lib/Util/BitmappedGpio.php | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 examples/bitmap.php create mode 100644 lib/Util/BitmappedGpio.php diff --git a/examples/bitmap.php b/examples/bitmap.php new file mode 100644 index 0000000..3945f71 --- /dev/null +++ b/examples/bitmap.php @@ -0,0 +1,24 @@ +setMapper( new WiringPiMapper(2) ); + +$par = new BitmappedGpio(); +for($n = 0; $n < 7; $n++) { + $par->setGpioPin($n,$gpio[$n]); +} + +$par->write(0xFF); +$par->write(0x00); diff --git a/lib/Util/BitmappedGpio.php b/lib/Util/BitmappedGpio.php new file mode 100644 index 0000000..6d7971e --- /dev/null +++ b/lib/Util/BitmappedGpio.php @@ -0,0 +1,46 @@ +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; + } + + +}