-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO.php
70 lines (55 loc) · 2.05 KB
/
GPIO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
//TheFreeElectron 2015, http://www.instructables.com/member/TheFreeElectron/
//This page is requested by the JavaScript, it updates the pin's status and then print it
//Getting and using values
//read a value from the GPIO return 1 if high, 0 if low
function readGPIO($target){
return invert($target, exec("/usr/bin/gpio read ".$target["gpioNumber"] ));
}
//writes a target GPIO with a commanded state, 1 if high, 0 if low. Returns commanded state
function writeGPIO($target, $state){
return invert($target, exec("/usr/bin/gpio write ".$target["gpioNumber"]." ".invert($target, $state) ));
}
//writes the default value to the target
function writeDefaultToGPIO($target){
$state=$target['state'];
return writeGPIO($target, $state);
}
//sets gpio modes
function setmode($target, $mode){
return system("/usr/bin/gpio mode ".$target["gpioNumber"]." $mode" );
}
//Takes the settings, a target, and performs an operation.
function gpio($ettings, $target){
$tatus=readGPIO($target);
$gpio=$target['gpioNumber'];
//read in cmd flag if set
$cmd= isset($_GET["cmd"]) ? $_GET["cmd"] : null;
//test if value is a number
if (is_numeric($target['gpioNumber'])){
//set the gpio's mode to output
setMode($target, "out");
//toggle the gpio to high/low
$tatus=($tatus=="0" ? 1 : 0);
//check for commanded status flag and act upon it.
if (isset($cmd)){
$tatus=($cmd=="off"? 0:1);
}
writeGPIO($target,$tatus);
//reading pin's status
$status=readGPIO($target);
echo $status;
writeTimeStamp();
//only wait to change state if default state is not current state
if ($target['state'] != readGPIO($target)){
if (isset($target['timer']) && $target['timer'] > 0){
usleep($target['timer']*1000000);
writeDefaultToGPIO($target);
}
}
} else {
echo ("fail");
}
writeTimeStamp();
}
?>