-
Notifications
You must be signed in to change notification settings - Fork 76
/
GPIO.php
88 lines (74 loc) · 2.44 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class GPIO {
// Using BCM pin numbers.
private $pins = ['0', '1', '4', '7', '8', '9', '10', '11', '14', '15', '17', '18', '21', '22', '23', '24', '25'];
// exported pins for when we unexport all
private $exportedPins = array();
// Setup pin, takes pin number and direction (in or out)
public function setup($pinNo, $direction) {
// Check if valid BCM number
if($this->isValidPin($pinNo)) {
// if exported, unexport it first
if($this->isExported($pinNo)) {
$this->unexport($pinNo);
}
// Export pin
file_put_contents('/sys/class/gpio/export', $pinNo);
// if valid direction then set direction
if($this->isValidDirection($direction)) {
file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/direction', $direction);
}
// Add to exported pins array
$exportedPins[] = $pinNo;
} else {
echo 'Error! Not a valid pin!';
}
}
public function input($pinNo) {
if($this->isExported($pinNo)) {
if($this->currentDirection($pinNo) != "out") {
return file_get_contents('/sys/class/gpio/gpio'.$pinNo.'/value');
} else {
echo 'Error! Wrong Direction for this pin!';
}
}
}
// Value == 1 or 0, where 1 = on, 0 = off
public function output($pinNo, $value) {
if($this->isExported($pinNo)) {
if($this->currentDirection($pinNo) != "in") {
file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/value', $value);
} else {
echo 'Error! Wrong Direction for this pin! Meant to be out while it is ' . $this->currentDirection($pinNo);
}
}
}
public function unexport($pinNo) {
if($this->isExported($pinNo)) {
file_put_contents('/sys/class/gpio/unexport', $pinNo);
foreach ($this->exportedPins as $key => $value) {
if($value == $pinNo) unset($key);
}
}
}
public function unexportAll() {
foreach ($this->exportedPins as $key => $pinNo) file_put_contents('/sys/class/gpio/unexport', $pinNo);
$this->exportedPins = array();
}
// Check if exported
public function isExported($pinNo) {
return file_exists('/sys/class/gpio/gpio'.$pinNo);
}
public function currentDirection($pinNo) {
return file_get_contents('/sys/class/gpio/gpio'.$pinNo.'/direction');
}
// Check for valid direction, in or out
public function isValidDirection($direction) {
return (($direction == "in") || ($direction == "out"));
}
// Check for valid pin
public function isValidPin($pinNo) {
return in_array($pinNo, $this->pins);
}
}
?>