Completed
Push — master ( 0210b9...f2ca63 )
by Bobby
01:22
created

RPiAdapter::setDirection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
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
        system("echo {$direction} > /sys/class/gpio/gpio{$pin}/direction");
34
35
        if ($invert) {
36
            $inverted = intval($invert);
37
            system("echo {$inverted} > /sys/class/gpio/gpio{$pin}/active_low");
38
        }
39
40
        if (file_exists("/sys/class/gpio/gpio{$pin}/direction") && (file_get_contents("/sys/class/gpio/gpio{$pin}/direction") == $direction)) {
41
            return true;
42
        }
43
44
        return false;
45
46
    }
47
48
    /**
49
     * Write a value to an output pin.
50
     *
51
     * @param int $pin The BCM pin number.
52
     * @param int $value The output value (0,1) - Use GPIO::HIGH and GPIO::LOW
53
     * @return bool
54
     */
55
    public function write(int $pin, int $value): bool
56
    {
57
        system("echo {$value} > /sys/class/gpio/gpio{$pin}/value");
58
59
        if (!file_exists("/sys/class/gpio/gpio{$pin}/value")) {
60
            return false;
61
        }
62
63
        if (file_get_contents("/sys/class/gpio/gpio{$pin}/value") != $value) {
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * Read the value of an input/output pin.
72
     *
73
     * @param int $pin The BCM pin number
74
     * @return int The current value of the pin.
75
     * @throws GPIOException
76
     */
77
    public function read(int $pin): int
78
    {
79
        if (!file_exists("/sys/class/gpio/gpio{$pin}/value")) {
80
            throw new GPIOException("The pin value does not appear to be set, did you forget to run setDirection?");
81
        }
82
        return intval(file_get_contents("/sys/class/gpio/gpio{$pin}/value"));
83
    }
84
85
    /**
86
     * Exports the Pin (enabling it's use)
87
     *
88
     * @param int $pin The pin to export/enable.
89
     * @return bool
90
     */
91
    private function export(int $pin)
92
    {
93
        system("echo {$pin} > " . self::GPIO_PATH . "/export");
94
        if (file_exists("/sys/class/gpio/gpio{$pin}")) {
95
            return true;
96
        }
97
        return false;
98
    }
99
}