SATS/feature/motheure-simple/source/loop.php

198 lines
4.0 KiB
PHP
Raw Normal View History

2017-10-12 17:19:46 +00:00
#!/usr/bin/php
<?php
require_once __DIR__.'/../../../lib/include/php/const';
$f_data;
$start_ts;
$FEATURE = basename(dirname(__DIR__));
2017-10-14 07:12:16 +00:00
$MOTOR_PIN = 19;
2017-10-12 17:19:46 +00:00
$LOOP_FREQ = 100; // check frequency in ms
$THRESHOLD = 4; // maximum number of reads to wait for motor to be active back
/* READ MOTOR STATE
*
*/
function motheure_read(){
/* [1] Export global caches + variables
=========================================================*/
/* (1) Motor pin */
global $MOTOR_PIN;
/* [2] Try to read motor state
=========================================================*/
/* (1) Get its state */
$STATE = syscall( SOURCE_DIR."/lib/gpio/in $MOTOR_PIN" );
2017-10-14 07:22:03 +00:00
/* (3) If active, pass */
2017-10-14 07:29:00 +00:00
var_dump($STATE);
2017-10-14 07:24:05 +00:00
if( $STATE == 0 ){
2017-10-14 07:29:00 +00:00
// slog('read: 1', 'motheure-simple:loop', 'motheure');
2017-10-14 07:22:03 +00:00
return true;
2017-10-14 07:24:05 +00:00
}
2017-10-12 17:19:46 +00:00
2017-10-14 07:22:03 +00:00
/* (4) Return FALSE means motor inactive */
2017-10-14 07:29:00 +00:00
// slog('read: 0', 'motheure-simple:loop', 'motheure');
2017-10-14 07:22:03 +00:00
return false;
2017-10-12 17:19:46 +00:00
}
/* MAIN LOOP
*
*/
function motheure_loop(){
/* [1] Export global caches + variables
=========================================================*/
/* (1) Log history file descriptor */
global $f_data;
/* (2) Motor pin */
global $MOTOR_PIN;
/* (3) Loop data */
global $start_ts;
global $LOOP_FREQ;
global $THRESHOLD;
/* [2] Try to read motor state
=========================================================*/
/* (1) Read motor pin */
if( !motheure_read() ) // if not active -> continue watching
return false;
2017-10-14 07:16:21 +00:00
slog('Motor start', 'motheure-simple:loop', 'motheure');
2017-10-12 17:19:46 +00:00
/* [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() ){
2017-10-14 07:16:21 +00:00
2017-10-12 17:19:46 +00:00
$offset++;
2017-10-14 07:16:21 +00:00
slog("Motor offset: $offset/$THRESHOLD", 'motheure-simple:loop', 'motheure');
2017-10-12 17:19:46 +00:00
/* (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([
2017-10-14 07:14:56 +00:00
floor( $stop_ts - $start_ts )
2017-10-12 17:19:46 +00:00
]).PHP_EOL );
2017-10-14 07:16:21 +00:00
slog("Motor stop", 'motheure-simple:loop', 'motheure');
2017-10-12 17:19:46 +00:00
/* (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();
}
?>