[Done] Implemented chip state management

This commit is contained in:
xdrm-brackets 2017-02-20 21:58:29 +01:00
parent ee177181f8
commit 291190475d
2 changed files with 104 additions and 0 deletions

101
lib/chip/source/state.php Executable file
View File

@ -0,0 +1,101 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
function chip_state($p_id_chip, $p_state){
$g_pins = null;
$g_state = null;
/* [1] Cache chips
=========================================================*/
/* (1) Get file handler */
$f_chips = new SplFileObject(CHIPS_CONF, 'r');
/* (2) Parse line by line */
while( !$f_chips->eof() ){
// {1} Try to parse current line //
$parsed = json_decode($f_chips->fgets(), true);
// {2} If cannot parse, go to next //
if( is_null($parsed) )
continue;
// {3} Check if choosen pin //
if( $parsed[0] == $p_id_chip ){
// store pin list
$g_pins = $parsed[1];
/* (3) Check if choosen state */
foreach($parsed[2] as $state=>$pinValues){
if( "$state" === "$p_state" ){
// store state values
$g_state = $pinValues;
break;
}
}
break;
}
}
/* (3) If chip not found -> abort */
if( is_null($g_pins) || is_null($g_state) )
return 127;
/* [2] Set chip to choosen state
=========================================================*/
/* (1) Activate pin + set OUT mode */
foreach($g_pins as $pinNumber)
// if failed -> propagate error
if( syscall(SOURCE_DIR."/gpio/out {$pinNumber}") == false )
return 127;
/* (2) For each pin, set the associated value */
foreach($g_state as $pinIndex=>$pinValue){
// {1} set value to LOW if 0 //
if( $pinValue == 0 )
if( syscall(SOURCE_DIR."/gpio/low ".$g_pins[$pinIndex]) == false )
return 127;
// {2} set value to HIGH if not 0 //
else
if( syscall(SOURCE_DIR."/gpio/high ".$g_pins[$pinIndex]) == false )
return 127;
}
return 0;
}
/* [1] Check argument (chip position, chip state)
=========================================================*/
if( $argc < 3 || !preg_match('@^\d+$@', $argv[1]) || !preg_match('@^[A-Za-z0-9]{1}$@', $argv[2]) ){
echo 127;
die(127);
}
/* [2] Launch main script
=========================================================*/
$exec = chip_state($argv[1], $argv[2]);
echo $exec;
die($exec);
?>

3
lib/chip/state Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/state.php $*;