|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ballen\GPIO; |
|
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 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Pin |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The BCM pin number |
|
23
|
|
|
* |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $pin; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The pin type (Input or Output) |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $type; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The GPIO Interface Adapter. |
|
37
|
|
|
* |
|
38
|
|
|
* @var AdapterInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $adapter; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Pin constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param int $pin The BCM pin number |
|
46
|
|
|
* @param string $type The pin type (Input or Output) |
|
47
|
|
|
* @param AdapterInterface $adapter The GPIO adapter interface |
|
48
|
|
|
* @param bool $invert Invert the logic so that high->low and low->high |
|
49
|
|
|
* @throws GPIOException |
|
50
|
|
|
*/ |
|
51
|
14 |
|
public function __construct(int $pin, string $type, AdapterInterface $adapter, bool $invert = false) |
|
52
|
|
|
{ |
|
53
|
14 |
|
$this->adapter = $adapter; |
|
54
|
|
|
|
|
55
|
14 |
|
$this->adapter->setDirection($pin, $type, $invert); |
|
56
|
|
|
|
|
57
|
14 |
|
$this->validatePin($pin); |
|
58
|
12 |
|
$this->pin = $pin; |
|
59
|
|
|
|
|
60
|
12 |
|
$this->validateType($type); |
|
61
|
10 |
|
$this->type = $type; |
|
62
|
10 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Validates that the pin number is a valid GPIO pin. |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $pin The BCM pin number |
|
68
|
|
|
* @throws GPIOException |
|
69
|
|
|
*/ |
|
70
|
14 |
|
private function validatePin(int $pin) |
|
71
|
|
|
{ |
|
72
|
14 |
|
if (!in_array($pin, GPIO::PINS)) { |
|
73
|
2 |
|
throw new GPIOException("Pin number {$pin} is not supported and therefore cannot be set."); |
|
74
|
|
|
} |
|
75
|
12 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Validates that the pin type is valid. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $type The GPIO pin type. |
|
81
|
|
|
* @throws GPIOException |
|
82
|
|
|
*/ |
|
83
|
12 |
|
private function validateType(string $type) |
|
84
|
|
|
{ |
|
85
|
12 |
|
if (!in_array($type, ["in", "out"])) { |
|
86
|
2 |
|
throw new GPIOException("Invalid pin type specified, supported types are 'in' and 'out'."); |
|
87
|
|
|
} |
|
88
|
10 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the value of a GPIO output pin. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $state The output state to set (GPIO::HIGH or GPIO::LOW) |
|
94
|
|
|
* @return bool |
|
95
|
|
|
* @throws GPIOException |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function setValue(int $state): bool |
|
98
|
|
|
{ |
|
99
|
6 |
|
if ($this->type == GPIO::IN) { |
|
100
|
2 |
|
throw new GPIOException('Setting the value of a GPIO input pin is not supported!'); |
|
101
|
|
|
} |
|
102
|
4 |
|
return $this->adapter->write($this->pin, $state); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the state of the input/output pin. |
|
107
|
|
|
* |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
6 |
|
public function getValue(): int |
|
111
|
|
|
{ |
|
112
|
6 |
|
return $this->adapter->read($this->pin); |
|
113
|
|
|
} |
|
114
|
|
|
} |