From 012a46da61e1184eac39c4158bedebec15a8d3a5 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 22:50:21 +0100 Subject: [PATCH] [Update] Optimized /lib/gpio methods to use shell only --- lib/gpio/high | 42 ++++++++++++++++++- lib/gpio/in | 42 ++++++++++++++++++- lib/gpio/low | 42 ++++++++++++++++++- lib/gpio/out | 42 ++++++++++++++++++- lib/gpio/source/Pin.php | 90 ---------------------------------------- lib/gpio/source/high.php | 41 ------------------ lib/gpio/source/in.php | 41 ------------------ lib/gpio/source/low.php | 41 ------------------ lib/gpio/source/out.php | 41 ------------------ 9 files changed, 164 insertions(+), 258 deletions(-) delete mode 100755 lib/gpio/source/Pin.php delete mode 100755 lib/gpio/source/high.php delete mode 100755 lib/gpio/source/in.php delete mode 100755 lib/gpio/source/low.php delete mode 100755 lib/gpio/source/out.php diff --git a/lib/gpio/high b/lib/gpio/high index bd5e9ed..b38f1c5 100755 --- a/lib/gpio/high +++ b/lib/gpio/high @@ -1,3 +1,43 @@ #!/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; diff --git a/lib/gpio/in b/lib/gpio/in index 1c9fdb3..d58957b 100755 --- a/lib/gpio/in +++ b/lib/gpio/in @@ -1,3 +1,43 @@ #!/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; diff --git a/lib/gpio/low b/lib/gpio/low index 764325d..db7a24e 100755 --- a/lib/gpio/low +++ b/lib/gpio/low @@ -1,3 +1,43 @@ #!/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; diff --git a/lib/gpio/out b/lib/gpio/out index 3dcf259..c4b1948 100755 --- a/lib/gpio/out +++ b/lib/gpio/out @@ -1,3 +1,43 @@ #!/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; diff --git a/lib/gpio/source/Pin.php b/lib/gpio/source/Pin.php deleted file mode 100755 index 7403885..0000000 --- a/lib/gpio/source/Pin.php +++ /dev/null @@ -1,90 +0,0 @@ -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) - - -?> diff --git a/lib/gpio/source/high.php b/lib/gpio/source/high.php deleted file mode 100755 index 45e6fe1..0000000 --- a/lib/gpio/source/high.php +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/php - -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]); -?> - diff --git a/lib/gpio/source/in.php b/lib/gpio/source/in.php deleted file mode 100755 index 7dca599..0000000 --- a/lib/gpio/source/in.php +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/php - -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]); -?> - diff --git a/lib/gpio/source/low.php b/lib/gpio/source/low.php deleted file mode 100755 index 3ab526d..0000000 --- a/lib/gpio/source/low.php +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/php - -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]); -?> - diff --git a/lib/gpio/source/out.php b/lib/gpio/source/out.php deleted file mode 100755 index 3909207..0000000 --- a/lib/gpio/source/out.php +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/php - -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]); -?> -