Improved sysfs file access code
This commit is contained in:
		@@ -15,28 +15,16 @@ try {
 | 
			
		||||
$gpio->setMapper( new WiringPiMapper(2) );
 | 
			
		||||
 | 
			
		||||
// Access logical pin 0, since we got a mapper assigned. Otherwise this would
 | 
			
		||||
// be the actual GPIO0 pin.
 | 
			
		||||
// be the actual GPIO0 pin. 
 | 
			
		||||
$led = $gpio[0]
 | 
			
		||||
    ->export()
 | 
			
		||||
    ->setDirection("output")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setLabel("red led")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
$btn1 = $gpio[1]
 | 
			
		||||
    ->setDirection("input")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setEdge("rising")
 | 
			
		||||
    ->setHandler(function() { echo "INTERRUPT!\n"; })
 | 
			
		||||
    ->setLabel("button 1")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
$btn2 = $gpio[2]
 | 
			
		||||
    ->setDirection("input")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setEdge("rising")
 | 
			
		||||
    ->setHandler(function() { echo "INTERRUPT!\n"; })
 | 
			
		||||
    ->setLabel("button 2")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$x = 0;
 | 
			
		||||
while(true) {
 | 
			
		||||
    $x = (int)(!$x);
 | 
			
		||||
    $led->setValue($x);
 | 
			
		||||
    usleep(500000);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										42
									
								
								examples/buttons.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								examples/buttons.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Gpio\Gpio;
 | 
			
		||||
use NoccyLabs\Gpio\GpioMapper\WiringPiMapper;
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    $gpio = new Gpio();
 | 
			
		||||
} catch (Gpio\Exception $e) {
 | 
			
		||||
    error_log("Error: {$e}");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// The mapper translates GPIO to logical pins and vice versa
 | 
			
		||||
$gpio->setMapper( new WiringPiMapper(2) );
 | 
			
		||||
 | 
			
		||||
// Access logical pin 0, since we got a mapper assigned. Otherwise this would
 | 
			
		||||
// be the actual GPIO0 pin.
 | 
			
		||||
$led = $gpio[0]
 | 
			
		||||
    ->export()
 | 
			
		||||
    ->setDirection("output")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setLabel("red led")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
$btn1 = $gpio[1]
 | 
			
		||||
    ->setDirection("input")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setEdge("rising")
 | 
			
		||||
    ->setHandler(function() { echo "INTERRUPT!\n"; })
 | 
			
		||||
    ->setLabel("button 1")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
$btn2 = $gpio[2]
 | 
			
		||||
    ->setDirection("input")
 | 
			
		||||
    ->setValue(0)
 | 
			
		||||
    ->setEdge("rising")
 | 
			
		||||
    ->setHandler(function() { echo "INTERRUPT!\n"; })
 | 
			
		||||
    ->setLabel("button 2")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -2,6 +2,21 @@
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
/* 
 | 
			
		||||
 * This example demonstrates using a 4-button wireless remote control with the
 | 
			
		||||
 * GPIO, in this case with the pins mapped against WiringPi for a Rev2 board.
 | 
			
		||||
 * If you are using this code with any other hardware, please update the mapper
 | 
			
		||||
 * below.
 | 
			
		||||
 *
 | 
			
		||||
 * It is assumed that Pin 0 is the interrupt pin (VT) to signal when a button
 | 
			
		||||
 * press has been received, and Pin 1-4 are connected to the pins for button
 | 
			
		||||
 * A to D on the remote control.
 | 
			
		||||
 *
 | 
			
		||||
 * Note that the Raspberry Pi GPIO is not 5V tolerant, so use an optoisolator
 | 
			
		||||
 * or transistors to ensure that you don't zap your preciouos hardware.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Gpio\Gpio;
 | 
			
		||||
use NoccyLabs\Gpio\GpioMapper\WiringPiMapper;
 | 
			
		||||
 | 
			
		||||
@@ -21,7 +36,12 @@ $led = $gpio[0]
 | 
			
		||||
    ->setDirection("input")
 | 
			
		||||
    ->setEdge("rising")
 | 
			
		||||
    ->setHandler(function($e) use($gpio) {
 | 
			
		||||
        
 | 
			
		||||
            if ($gpio[1]->getValue()) { echo "A"; }
 | 
			
		||||
        elseif ($gpio[2]->getValue()) { echo "B"; }
 | 
			
		||||
        elseif ($gpio[3]->getValue()) { echo "C"; }
 | 
			
		||||
        elseif ($gpio[4]->getValue()) { echo "D"; }
 | 
			
		||||
        else { echo "None"; }
 | 
			
		||||
        echo "\n";
 | 
			
		||||
    })
 | 
			
		||||
    ->setLabel("rfint")
 | 
			
		||||
    ->dumpStatus(true);
 | 
			
		||||
@@ -33,3 +53,7 @@ for($n = 1; $n < 5; $n++) {
 | 
			
		||||
        ->dumpStatus(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
while(true) {
 | 
			
		||||
    $gpio->refresh();
 | 
			
		||||
    usleep(10000);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user