[Update] Optimized /lib/gpio methods to use shell only

This commit is contained in:
xdrm-brackets 2017-02-20 22:50:21 +01:00
parent 006955229c
commit 012a46da61
9 changed files with 164 additions and 258 deletions

View File

@ -1,3 +1,43 @@
#!/bin/sh #!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/high.php $1;
# [1] If empty pin number
#========================================================#
if [ -z "$1" ]; then
echo 127;
exit 127;
fi;
# [2] Check if valid pin number (integer)
#========================================================#
test $1 -ge 0 2> /dev/null;
if [ $? -gt 1 ]; then
echo 127;
exit 127;
fi;
# [3] Activate (if not already)
#========================================================#
test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export;
# [4] Set mode to OUT
#========================================================#
echo 1 > /sys/class/gpio/gpio$1/value;
# [5] EXIT_CODE
#========================================================#
echo 0;
exit 0;

View File

@ -1,3 +1,43 @@
#!/bin/sh #!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/in.php $1;
# [1] If empty pin number
#========================================================#
if [ -z "$1" ]; then
echo 127;
exit 127;
fi;
# [2] Check if valid pin number (integer)
#========================================================#
test $1 -ge 0 2> /dev/null;
if [ $? -gt 1 ]; then
echo 127;
exit 127;
fi;
# [3] Activate (if not already)
#========================================================#
test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export;
# [4] Set mode to OUT
#========================================================#
echo "in" > /sys/class/gpio/gpio$1/direction;
# [5] EXIT_CODE
#========================================================#
echo 0;
exit 0;

View File

@ -1,3 +1,43 @@
#!/bin/sh #!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/low.php $1;
# [1] If empty pin number
#========================================================#
if [ -z "$1" ]; then
echo 127;
exit 127;
fi;
# [2] Check if valid pin number (integer)
#========================================================#
test $1 -ge 0 2> /dev/null;
if [ $? -gt 1 ]; then
echo 127;
exit 127;
fi;
# [3] Activate (if not already)
#========================================================#
test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export;
# [4] Set mode to OUT
#========================================================#
echo 0 > /sys/class/gpio/gpio$1/value;
# [5] EXIT_CODE
#========================================================#
echo 0;
exit 0;

View File

@ -1,3 +1,43 @@
#!/bin/sh #!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/out.php $1;
# [1] If empty pin number
#========================================================#
if [ -z "$1" ]; then
echo 127;
exit 127;
fi;
# [2] Check if valid pin number (integer)
#========================================================#
test $1 -ge 0 2> /dev/null;
if [ $? -gt 1 ]; then
echo 127;
exit 127;
fi;
# [3] Activate (if not already)
#========================================================#
test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export;
# [4] Set mode to OUT
#========================================================#
echo "out" > /sys/class/gpio/gpio$1/direction;
# [5] EXIT_CODE
#========================================================#
echo 0;
exit 0;

View File

@ -1,90 +0,0 @@
<?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( !preg_match('@^\d+$@', $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)
?>

View File

@ -1,41 +0,0 @@
#!/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;
return 0;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_high($argv[1]);
?>

View File

@ -1,41 +0,0 @@
#!/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;
return 0;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_in($argv[1]);
?>

View File

@ -1,41 +0,0 @@
#!/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;
return 0;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_low($argv[1]);
?>

View File

@ -1,41 +0,0 @@
#!/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;
return 0;
}
/* [1] Check argument
=========================================================*/
if( $argc < 2 || !preg_match('@^\d+$@', $argv[1]) ){
echo 127;
die();
}
/* [2] Launch main script
=========================================================*/
echo gpio_out($argv[1]);
?>