|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ballen\GPIO\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use Ballen\GPIO\Exceptions\GPIOException; |
|
6
|
|
|
use Ballen\GPIO\Interfaces\AdapterInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* GPIO |
|
10
|
|
|
* A RaspberryPi GPIO library written in PHP. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Bobby Allen <[email protected]> |
|
13
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
|
14
|
|
|
* @link https://github.com/allebb/gpio |
|
15
|
|
|
* @link http://www.bobbyallen.me |
|
16
|
|
|
* @package Ballen\GPIO\Adapters |
|
17
|
|
|
* @codeCoverageIgnore |
|
18
|
|
|
*/ |
|
19
|
|
|
class RPiAdapter implements AdapterInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
const GPIO_PATH = "/sys/class/gpio"; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set the direction of the pin (input or output) |
|
26
|
|
|
* |
|
27
|
|
|
* @param int $pin The BCM pin number |
|
28
|
|
|
* @param string $direction The type/direction of the pin - Use GPIO::IN and GPIO::OUT |
|
29
|
|
|
* @param bool $invert Invert the logic so that high->low and low->high |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setDirection(int $pin, string $direction, bool $invert = false): bool |
|
33
|
|
|
{ |
|
34
|
|
|
$this->export($pin); |
|
35
|
|
|
|
|
36
|
|
|
if (file_exists("/sys/class/gpio/gpio{$pin}/direction") && trim(file_get_contents("/sys/class/gpio/gpio{$pin}/direction")) !== $direction) { |
|
37
|
|
|
system("echo {$direction} > /sys/class/gpio/gpio{$pin}/direction"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($invert) { |
|
41
|
|
|
$inverted = intval($invert); |
|
42
|
|
|
if (file_get_contents("/sys/class/gpio/gpio{$pin}/active_low") !== $inverted) { |
|
|
|
|
|
|
43
|
|
|
system("echo {$inverted} > /sys/class/gpio/gpio{$pin}/active_low"); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (file_exists("/sys/class/gpio/gpio{$pin}/direction") && (file_get_contents("/sys/class/gpio/gpio{$pin}/direction") == $direction)) { |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Write a value to an output pin. |
|
57
|
|
|
* |
|
58
|
|
|
* @param int $pin The BCM pin number. |
|
59
|
|
|
* @param int $value The output value (0,1) - Use GPIO::HIGH and GPIO::LOW |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function write(int $pin, int $value): bool |
|
63
|
|
|
{ |
|
64
|
|
|
system("echo {$value} > /sys/class/gpio/gpio{$pin}/value"); |
|
65
|
|
|
|
|
66
|
|
|
if (!file_exists("/sys/class/gpio/gpio{$pin}/value")) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (file_get_contents("/sys/class/gpio/gpio{$pin}/value") != $value) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Read the value of an input/output pin. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $pin The BCM pin number |
|
81
|
|
|
* @return int The current value of the pin. |
|
82
|
|
|
* @throws GPIOException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function read(int $pin): int |
|
85
|
|
|
{ |
|
86
|
|
|
if (!file_exists("/sys/class/gpio/gpio{$pin}/value")) { |
|
87
|
|
|
throw new GPIOException("The pin value does not appear to be set, did you forget to run setDirection?"); |
|
88
|
|
|
} |
|
89
|
|
|
return intval(file_get_contents("/sys/class/gpio/gpio{$pin}/value")); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Exports the Pin (enabling it's use) |
|
94
|
|
|
* |
|
95
|
|
|
* @param int $pin The pin to export/enable. |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
private function export(int $pin): bool |
|
99
|
|
|
{ |
|
100
|
|
|
if (!file_exists("/sys/class/gpio/gpio{$pin}")) { |
|
101
|
|
|
system("echo {$pin} > " . self::GPIO_PATH . "/export"); |
|
102
|
|
|
if (file_exists("/sys/class/gpio/gpio{$pin}")) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
} |