| 
									
										
										
										
											2014-06-12 18:31:39 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-14 03:10:07 +02: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/> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 18:31:39 +02:00
										 |  |  | 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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | } |