Added gpio lib in /lib/gpio

This commit is contained in:
xdrm-brackets 2017-02-18 18:38:55 +01:00
parent 09c0251389
commit 4bd6ce33f8
9 changed files with 258 additions and 0 deletions

3
lib/gpio/high Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/high.php $1;

3
lib/gpio/in Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/in.php $1;

3
lib/gpio/low Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/low.php $1;

3
lib/gpio/out Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/out.php $1;

90
lib/gpio/source/Pin.php Executable file
View File

@ -0,0 +1,90 @@
<?php
class Pin{
const GPIO_DIR = '/sys/class/gpio';
const GPIO_IN = 'in';
const GPIO_OUT = 'out';
const GPIO_HIGH = 1;
const GPIO_LOW = 0;
private $pin;
private $mode;
private $value;
public function __construct($pin){
if( !is_int($pin) )
throw new Exception("pin must be an integer");
/* (1) Make gpio pin available */
if( !file_exists(Pin::GPIO_DIR.'/gpio'.$pin) )
file_put_contents(Pin::GPIO_DIR.'/export', $pin);
/* (2) add attribute */
$this->pin = $pin;
}
public function __get($attr){
// if try to fetch value
if( $attr == 'value' ){
return ( file_get_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/value') == '1' ) ? Pin::GPIO_HIGH : Pin::GPIO_LOW;
// if try to fetch mode
}else if( $attr == 'mode' ){
return ( file_get_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/value') == 'in' ) ? Pin::GPIO_IN : Pin::GPIO_OUT;
}
}
public function __set($attr, $value){
// if try to set value
if( $attr == 'value' ){
if( $value == Pin::GPIO_HIGH )
file_put_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/value', 1);
else if( $value == Pin::GPIO_LOW )
file_put_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/value', 0);
// if try to set mode
}else if( $attr == 'mode' ){
if( $value == Pin::GPIO_IN )
file_put_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/direction', 'in');
else if( $value == Pin::GPIO_OUT )
file_put_contents(Pin::GPIO_DIR.'/gpio'.$this->pin.'/direction', 'out');
}
}
}
// USAGE
//
// $red = new GPIOPin(16);
// $green = new GPIOPin(20);
// $blue = new GPIOPin(21);
// $red->setMode(GPIO_OUT);
// $green->setMode(GPIO_OUT);
// $blue->setMode(GPIO_OUT);
// $red->set(GPIO_HIGH)
// $green->set(GPIO_LOW)
// $blue->set(GPIO_HIGH)
?>

39
lib/gpio/source/high.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
require_once __DIR__.'/Pin.php';
function gpio_high($pin){
/* (1) Create GPIO Pin instance */
try{
$gpio = new Pin($pin);
}catch(Exception $e){
return 127;
}
/* (2) Set mode to in */
$gpio->value = Pin::GPIO_HIGH;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_high($argv[1]);
?>

39
lib/gpio/source/in.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
require_once __DIR__.'/Pin.php';
function gpio_in($pin){
/* (1) Create GPIO Pin instance */
try{
$gpio = new Pin($pin);
}catch(Exception $e){
return 127;
}
/* (2) Set mode to in */
$gpio->mode = Pin::GPIO_IN;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_in($argv[1]);
?>

39
lib/gpio/source/low.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
require_once __DIR__.'/Pin.php';
function gpio_low($pin){
/* (1) Create GPIO Pin instance */
try{
$gpio = new Pin($pin);
}catch(Exception $e){
return 127;
}
/* (2) Set mode to in */
$gpio->value = Pin::GPIO_LOW;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_low($argv[1]);
?>

39
lib/gpio/source/out.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
require_once __DIR__.'/Pin.php';
function gpio_out($pin){
/* (1) Create GPIO Pin instance */
try{
$gpio = new Pin($pin);
}catch(Exception $e){
return 127;
}
/* (2) Set mode to in */
$gpio->mode = Pin::GPIO_OUT;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_out($argv[1]);
?>