PHP code example of ikarus / raspberry-pinout

1. Go to this page and download the library: Download ikarus/raspberry-pinout library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ikarus / raspberry-pinout example snippets



use Ikarus\Raspberry\RaspberryPiDevice;

$dev = RaspberryPiDevice::getDevice();
echo $dev->getModelName();


use Ikarus\Raspberry\Pinout\Revision_1\AbstractBoardPinout;
use Ikarus\Raspberry\Pinout\Revision_2\AbstractBCMPinout;
use Ikarus\Raspberry\Pinout\Revision_3\AbstractWpiPinout;
// Take the abstract pinout class you need (device specific like rev 1-3) and you want to declare the pinout (bcm, wpi or board).

class MyPinout extends AbstractWpiPinout {
    const MOTOR_A = 0;
    const MOTOR_B = 1;
    const CONTACT_A = 2;
    const CONTACT_B = 3;
    
    const ASPIRATION = 4;
    
    protected $inputPins = [
        self::CONTACT_A => self::INPUT_RESISTOR_UP,
        self::CONTACT_B => self::INPUT_RESISTOR_DOWN
    ];
    
    protected $outputPins = [
        self::MOTOR_A => false,
        self::MOTOR_B => false,
        self::ASPIRATION => true    // Use PWM
    ];
    
    protected $activeLowPins = [
        self::CONTACT_A     // Inverts its value => if the pin is high, the value is 0.
    ];
}


karus\Raspberry\RaspberryPiDevice;
$dev = RaspberryPiDevice::getDevice();

$dev->getInputPin( MyPinout::CONTACT_A )->getValue() )
    usleep(10000);

// Motor stop
$dev->getOutputPin( MyPinout::MOTOR_A )->setValue(0);
sleep(2);

// Drive backwards
$dev->getOutputPin( MyPinout::MOTOR_B )->setValue(1);
while ( $dev->getInputPin( MyPinout::CONTACT_B )->getValue() == 0 )
    usleep(10000);

// Motor stop
$dev->getOutputPin( MyPinout::MOTOR_B )->setValue(0);

// Releases all pins and brings them into a secure state (mode = input, value = 0 and resistor = none).
$dev->cleanup();


$dev->loop(1/1000, function() {
    // Safe loop call
});


use Ikarus\Raspberry\Edge\Edge;

// Change
/** @var \Ikarus\Raspberry\RaspberryPiDevice $dev */
while ( $dev->getInputPin( MyPinout::CONTACT_A )->getValue() )
    usleep(10000);
// Into
$edge = new Edge($dev->getInputPin( MyPinout::CONTACT_A ), Edge::EDGE_FALLING);

// Watch for 2.5 seconds for a falling edge
// This method blocks until the requested edge (raising|falling|both) did occur or time is up.
if($dev->watchEdge(2.5, $edge)) {
    echo "OK, Motor stopp\n";
} else {
    echo "Failed! Action took too long!\n";
}
$dev->getOutputPin( MyPinout::MOTOR_A )->setValue(0);

// You can also check the $edge like
switch ($edge->getValue()) {
    case Edge::VALUE_DID_FALL: echo "did fall"; break;
    case Edge::VALUE_DID_RISE: echo "did rise"; break;
    default:
        echo "Nothing happen";
}