Completed
Push — master ( a3010f...0210b9 )
by Bobby
01:41
created

RPiAdapter::setDirection()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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