From cb4a0e5921b4921475c60d1cc9d9b8dc5c64ee9d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 12 Oct 2017 19:19:46 +0200 Subject: [PATCH] Motheure-simple 1st implementation --- feature/motheure-simple/main | 3 + feature/motheure-simple/source/loop.php | 194 ++++++++++++++++++++++++ lib/gpio/in | 10 +- lib/include/php/func | 8 +- 4 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 feature/motheure-simple/main create mode 100644 feature/motheure-simple/source/loop.php diff --git a/feature/motheure-simple/main b/feature/motheure-simple/main new file mode 100644 index 0000000..4af246f --- /dev/null +++ b/feature/motheure-simple/main @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo -u sats /usr/bin/env php $(realpath $(dirname $0))/source/loop.php; \ No newline at end of file diff --git a/feature/motheure-simple/source/loop.php b/feature/motheure-simple/source/loop.php new file mode 100644 index 0000000..409a42b --- /dev/null +++ b/feature/motheure-simple/source/loop.php @@ -0,0 +1,194 @@ +#!/usr/bin/php + continue watching + return false; + + slog('Motor active detected', 'motheure-simple:loop', 'motheure'); + + /* [3] Wait for machine to stop + =========================================================*/ + /* (0) Userful variables */ + $offset = 0; // offset (number of 0 reads) + $loop_ts = 0; // loop start time + $stop_ts = microtime(true); // last active time for now + + while( $offset <= $THRESHOLD ){ // exit when motor active for $threshold cycles + + /* (1) Start loop timer */ + $loop_ts = microtime(true); + + /* (2) If stopped -> increment offset */ + if( !motheure_read() ){ + $offset++; + + /* (3) If not stopped */ + }else{ + $offset = 0; + $stop_ts = microtime(true); // last active time for now + } + + /* (4) Wait for loop to last @LOOP_FREQ seconds (if not already elapsed) */ + while( microtime(true) - $loop_ts < ($LOOP_FREQ/1000) ); + } + + + + /* [2] Main loop + =========================================================*/ + /* (1) Log action to default log file */ + $f_data->fwrite( json_encode([ + $stop_ts - $start_ts + ]).PHP_EOL ); + + /* (2) Return status */ + return true; + + } + + + + + + + + + + + + + + function motheure_setup(){ + + /* [0] Initialize global variables + =========================================================*/ + /* (1) File descriptiors */ + global $f_data; + + /* (3) Useful data */ + global $FEATURE; + global $MOTOR_PIN; + + /* [1] Open file descriptors on useful files + =========================================================*/ + /* (1) Append accesses (logs) */ + if( !file_exists(DATA_DIR."/$FEATURE") ) + file_put_contents(DATA_DIR."/$FEATURE", ''); + + $f_data = new SplFileObject(DATA_DIR."/$FEATURE", 'a'); + + + /* [2] Set up GPIO pin + =========================================================*/ + /* (1) Set as input */ + if( !syscall(SOURCE_DIR."/lib/gpio/in $MOTOR_PIN") ){ + slog('Cannot set up motor pin as INPUT', 'motheure-simple:setup'); + return 127; + } + + + + return 0; + } + + + + + + /* [1] Set up daemon + =========================================================*/ + /* (1) Set up */ + $exec = motheure_setup(); + + /* (2) Manage error */ + if( $exec != 0 ){ + slog('cannot set up the daemon', 'motheure-simple:loop'); + die($exec); + } + + /* (3) Success message */ + slog('daemon started (loop)', 'motheure-simple:loop'); + + + + + /* [2] Daemon loop + =========================================================*/ + while( true ){ + + /* (1) Store time */ + $start_ts = microtime(true); + + /* (2) Execute loop code */ + motheure_loop(); + + } +?> + diff --git a/lib/gpio/in b/lib/gpio/in index ae446e7..2e383bb 100755 --- a/lib/gpio/in +++ b/lib/gpio/in @@ -30,14 +30,16 @@ test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export; -# [4] Set mode to OUT +# [4] Set mode to IN #========================================================# cat /sys/class/gpio/gpio$1/direction | grep -v "in" && echo "in" > /sys/class/gpio/gpio$1/direction; -# [5] EXIT_CODE +# [5] RETURN VALUE #========================================================# -echo 0; -exit 0; +VALUE="`cat /sys/class/gpio/gpio$1/value`"; + +echo $VALUE; +exit $VALUE; diff --git a/lib/include/php/func b/lib/include/php/func index 9fcfcf9..c02d025 100755 --- a/lib/include/php/func +++ b/lib/include/php/func @@ -19,8 +19,12 @@ $out = preg_replace('/\s+$/', '', $out); /* (4) Manage result */ - if( is_numeric($out) ) return ("$out"==="0"); - else return $out; + if( is_numeric($out) && $out === "0" ) + return false; + elseif( is_numeric($out) && $out === "127" ) + return true; + else + return $out; } /* [2] Log management