From 4bd6ce33f8b13b40da167b63328b89b55bec78ca Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 18 Feb 2017 18:38:55 +0100 Subject: [PATCH] Added gpio lib in /lib/gpio --- lib/gpio/high | 3 ++ lib/gpio/in | 3 ++ lib/gpio/low | 3 ++ lib/gpio/out | 3 ++ lib/gpio/source/Pin.php | 90 ++++++++++++++++++++++++++++++++++++++++ lib/gpio/source/high.php | 39 +++++++++++++++++ lib/gpio/source/in.php | 39 +++++++++++++++++ lib/gpio/source/low.php | 39 +++++++++++++++++ lib/gpio/source/out.php | 39 +++++++++++++++++ 9 files changed, 258 insertions(+) create mode 100755 lib/gpio/high create mode 100755 lib/gpio/in create mode 100755 lib/gpio/low create mode 100755 lib/gpio/out create mode 100755 lib/gpio/source/Pin.php create mode 100755 lib/gpio/source/high.php create mode 100755 lib/gpio/source/in.php create mode 100755 lib/gpio/source/low.php create mode 100755 lib/gpio/source/out.php diff --git a/lib/gpio/high b/lib/gpio/high new file mode 100755 index 0000000..bd5e9ed --- /dev/null +++ b/lib/gpio/high @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/high.php $1; diff --git a/lib/gpio/in b/lib/gpio/in new file mode 100755 index 0000000..1c9fdb3 --- /dev/null +++ b/lib/gpio/in @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/in.php $1; diff --git a/lib/gpio/low b/lib/gpio/low new file mode 100755 index 0000000..764325d --- /dev/null +++ b/lib/gpio/low @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/low.php $1; diff --git a/lib/gpio/out b/lib/gpio/out new file mode 100755 index 0000000..3dcf259 --- /dev/null +++ b/lib/gpio/out @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/out.php $1; diff --git a/lib/gpio/source/Pin.php b/lib/gpio/source/Pin.php new file mode 100755 index 0000000..6e5fb4a --- /dev/null +++ b/lib/gpio/source/Pin.php @@ -0,0 +1,90 @@ +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 new file mode 100755 index 0000000..6f6790b --- /dev/null +++ b/lib/gpio/source/high.php @@ -0,0 +1,39 @@ +#!/usr/bin/php + +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]); +?> + diff --git a/lib/gpio/source/in.php b/lib/gpio/source/in.php new file mode 100755 index 0000000..4a74ccf --- /dev/null +++ b/lib/gpio/source/in.php @@ -0,0 +1,39 @@ +#!/usr/bin/php + +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]); +?> + diff --git a/lib/gpio/source/low.php b/lib/gpio/source/low.php new file mode 100755 index 0000000..552bc91 --- /dev/null +++ b/lib/gpio/source/low.php @@ -0,0 +1,39 @@ +#!/usr/bin/php + +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]); +?> + diff --git a/lib/gpio/source/out.php b/lib/gpio/source/out.php new file mode 100755 index 0000000..087bb2b --- /dev/null +++ b/lib/gpio/source/out.php @@ -0,0 +1,39 @@ +#!/usr/bin/php + +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]); +?> +