Skip to content

Instantly share code, notes, and snippets.

@t-geindre
Created June 11, 2016 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save t-geindre/674280ad12f364c4ce386ff73cfe57fc to your computer and use it in GitHub Desktop.
Save t-geindre/674280ad12f364c4ce386ff73cfe57fc to your computer and use it in GitHub Desktop.
PLaying with Raspberry Pi GPIOs + LED
<?php
namespace PhpGPioDemo;
require(__DIR__.'/vendor/autoload.php');
use PhpGpio\Gpio;
use PhpGpio\GpioInterface;
$mapping = [
4 => 'red',
17 => 'yellow',
27 => 'red',
5 => 'yellow',
6 => 'red',
13 => 'yellow',
19 => 'red',
12 => 'yellow',
16 => 'red',
];
$gpio = new Gpio($pins = array_keys($mapping));
foreach ($pins as $pin) {
$gpio->setup($pin, GpioInterface::DIRECTION_OUT);
}
$effects = [
// 0 Blink
function() use ($gpio, $pins, $mapping) {
for ($i = 0; $i < 9; $i++) {
foreach ($pins as $pin) {
$gpio->write(
$pin,
$i % 2 ? GpioInterface::IO_VALUE_ON : GpioInterface::IO_VALUE_OFF
);
}
usleep(100000);
}
},
// 1 Blink alternate colors
function() use ($gpio, $pins, $mapping) {
$cColor = 'red';
for ($i = 0; $i < 9; $i++) {
$cColor = $i % 2 ? ($cColor == 'yellow' ? 'red' : 'yellow') : $cColor;
foreach ($mapping as $pin => $color) {
if ($color == $cColor) {
$gpio->write(
$pin,
$i % 2 ? GpioInterface::IO_VALUE_ON : GpioInterface::IO_VALUE_OFF
);
}
}
usleep(200000);
}
},
// 2 Chaser
function() use ($gpio, $pins, $mapping) {
for ($i=0; $i<2; $i++) {
foreach ($pins as $pin) {
$gpio->write($pin, GpioInterface::IO_VALUE_ON);
usleep(50000);
$gpio->write($pin, GpioInterface::IO_VALUE_OFF);
}
$pin = end($pins);
do {
$gpio->write($pin, GpioInterface::IO_VALUE_ON);
usleep(50000);
$gpio->write($pin, GpioInterface::IO_VALUE_OFF);
} while ($pin = prev($pins));
}
},
// 3 Beginning to end, then back to begining
function() use ($gpio, $pins, $mapping) {
foreach ($pins as $pin) {
$gpio->write($pin, GpioInterface::IO_VALUE_ON);
usleep(50000);
}
$pin = end($pins);
do {
$gpio->write($pin, GpioInterface::IO_VALUE_OFF);
usleep(50000);
} while ($pin = prev($pins));
},
// 4 Long chaser
function() use ($gpio, $pins, $mapping) {
foreach ($pins as $pin) {
$gpio->write($pin, GpioInterface::IO_VALUE_ON);
usleep(50000);
}
foreach ($pins as $pin) {
$gpio->write($pin, GpioInterface::IO_VALUE_OFF);
usleep(50000);
}
},
// 5 Random on/off
function() use ($gpio, $pins, $mapping) {
$on = [];
for ($i=0 ; $i < 10 ; $i++) {
$pin = mt_rand(0, count($pins)-1);
if (($index = array_search($pin, $on)) !== false ){
unset($on[$index]);
$newState = GpioInterface::IO_VALUE_OFF;
} else {
$newState = GpioInterface::IO_VALUE_ON;
$on[] = $pin;
}
$gpio->write($pins[$pin], $newState);
usleep(50000);
}
clear();
}
];
function clear() {
global $pins, $gpio;
foreach ($pins as $pin) {
$gpio->write($pin, GpioInterface::IO_VALUE_OFF);
}
}
function effect($id) {
global $effects;
call_user_func($effects[$id]);
}
$unexport = function () {
global $gpio;
$gpio->unexportAll();
exit;
};
declare(ticks=1);
pcntl_signal(SIGINT, $unexport);
pcntl_signal(SIGTERM, $unexport);
if (isset($argv[1]) && is_numeric($argv[1]) && $argv[1] >= 0 && $argv[1] < count($effects)) {
do {
effect($argv[1]);
} while (true);
} else {
do {
effect(mt_rand(0, count($effects) - 1));
} while (true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment