1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\GPIO; |
4
|
|
|
|
5
|
|
|
use Ballen\GPIO\Adapters\RPiAdapter; |
6
|
|
|
use Ballen\GPIO\Exceptions\GPIOException; |
7
|
|
|
use Ballen\GPIO\Interfaces\AdapterInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* GPIO |
11
|
|
|
* A RaspberryPi GPIO library written in PHP. |
12
|
|
|
* |
13
|
|
|
* @author Bobby Allen <[email protected]> |
14
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
15
|
|
|
* @link https://github.com/allebb/gpio |
16
|
|
|
* @link http://www.bobbyallen.me |
17
|
|
|
*/ |
18
|
|
|
class GPIO |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* GPIO Pin State Low |
23
|
|
|
*/ |
24
|
|
|
const LOW = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* GPIO Pin State High |
28
|
|
|
*/ |
29
|
|
|
const HIGH = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* GPIO Pin Type Input |
33
|
|
|
*/ |
34
|
|
|
const IN = "in"; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* GPIO Pin Type Output |
38
|
|
|
*/ |
39
|
|
|
const OUT = "out"; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Supported GPIO BCM Pin numbers. |
43
|
|
|
*/ |
44
|
|
|
const PINS = [4, 5, 6, 12, 13, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The GPIO Interface Adapter. |
48
|
|
|
* |
49
|
|
|
* @var AdapterInterface |
50
|
|
|
*/ |
51
|
|
|
private $ioAdapter; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* GPIO constructor. |
55
|
|
|
* |
56
|
|
|
* @param AdapterInterface|null $adapter |
57
|
|
|
*/ |
58
|
18 |
|
public function __construct(AdapterInterface $adapter = null) |
59
|
|
|
{ |
60
|
18 |
|
$this->ioAdapter = new RPiAdapter(); |
61
|
18 |
|
if ($adapter) { |
62
|
16 |
|
$this->ioAdapter = $adapter; |
63
|
|
|
} |
64
|
18 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Configure a pin |
68
|
|
|
* |
69
|
|
|
* @param int $pin The GPIO pin number |
70
|
|
|
* @param string $type The pin type (input or output) - Use GPIO::IN and GPIO::OUT |
71
|
|
|
* @param bool $invert Invert the logic so that high->low and low->high |
72
|
|
|
* @return Pin |
73
|
|
|
* @throws GPIOException |
74
|
|
|
*/ |
75
|
14 |
|
public function pin(int $pin, string $type, bool $invert = false): Pin |
76
|
|
|
{ |
77
|
14 |
|
return new Pin($pin, $type, $this->ioAdapter, $invert); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Reset all of the GPIO pins. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
* @throws GPIOException |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
2 |
|
public function clear() |
88
|
|
|
{ |
89
|
2 |
|
foreach (GPIO::PINS as $pin) { |
90
|
2 |
|
$reset = new Pin($pin, GPIO::OUT, $this->ioAdapter); |
91
|
2 |
|
$reset->setValue(GPIO::LOW); |
92
|
|
|
} |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
} |