Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split input and output pins #9

Merged
merged 1 commit into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,38 @@ A library for low level access to the GPIO pins on a Raspberry Pi

### Setting Output Pins
```php
use PiPHP\GPIO\PinInterface;
use PiPHP\GPIO\PinFactory;
use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\PinInterface;

// Retrieve a pin object using the factory class
$pin = (new PinFactory)->getPin(18);
// Create a GPIO object
$gpio = new GPIO();

// Export the pin (so that it is available to use)
$pin->export();

// Set the pin as an output pin
$pin->setDirection(PinInterface::DIRECTION_OUT);
// Retrieve pin 18 and configure it as an output pin
$pin = $gpio->getOutputPin(18);

// Set the value of the pin high (turn it on)
$pin->setValue(PinInterface::VALUE_HIGH);
```

### Input Pin Interrupts
```php
use PiPHP\GPIO\InterruptWatcherFactory;
use PiPHP\GPIO\PinInterface;
use PiPHP\GPIO\PinFactory;

// Retrieve a pin object using the factory class
$pin = (new PinFactory)->getPin(18);
use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\InputPinInterface;

// Export the pin (so that it is available to use)
$pin->export();
// Create a GPIO object
$gpio = new GPIO();

// Set the pin as an input pin
$pin->setDirection(PinInterface::DIRECTION_IN);
// Retrieve pin 18 and configure it as an input pin
$pin = $gpio->getInputPin(18);

// Configure the pin to trigger interrupts on both rising and falling edges
$pin->setEdge(PinInterface::EDGE_BOTH);
// Configure interrupts for both rising and falling edges
$pin->setEdge(InputPinInterface::EDGE_BOTH);

// Create an interrupt watcher using the factory class
$interruptWatcher = (new InterruptWatcherFactory)->createWatcher();
// Create an interrupt watcher
$interruptWatcher = $gpio->createWatcher();

// Register a callback to be triggered on pin interrupts
$interruptWatcher->register($pin, function (PinInterface $pin, $value) {
$interruptWatcher->register($pin, function (InputPinInterface $pin, $value) {
echo 'Pin ' . $pin->getNumber() . ' changed to: ' . $value . PHP_EOL;

// Returning false will make the watcher return false immediately
Expand Down
51 changes: 51 additions & 0 deletions src/GPIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace PiPHP\GPIO;

use PiPHP\GPIO\FileSystem\FileSystem;
use PiPHP\GPIO\FileSystem\FileSystemInterface;
use PiPHP\GPIO\Interrupt\InterruptWatcher;
use PiPHP\GPIO\Pin\InputPin;
use PiPHP\GPIO\Pin\OutputPin;

final class GPIO implements GPIOInterface
{
private $fileSystem;
private $streamSelect;

/**
* Constructor.
*
* @param FileSystemInterface $fileSystem Optional file system object to use
* @param callable $streamSelect Optional sream select callable
*/
public function __construct(FileSystemInterface $fileSystem = null, callable $streamSelect = null)
{
$this->fileSystem = $fileSystem ?: new FileSystem();
$this->streamSelect = $streamSelect ?: 'stream_select';
}

/**
* {@inheritdoc}
*/
public function getInputPin($number)
{
return new InputPin($this->fileSystem, $number);
}

/**
* {@inheritdoc}
*/
public function getOutputPin($number)
{
return new OutputPin($this->fileSystem, $number);
}

/**
* {@inheritdoc}
*/
public function createWatcher()
{
return new InterruptWatcher($this->fileSystem, $this->streamSelect);
}
}
35 changes: 35 additions & 0 deletions src/GPIOInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace PiPHP\GPIO;

use PiPHP\GPIO\Interrupt\InterruptWatcherInterface;
use PiPHP\GPIO\Pin\InputPinInterface;
use PiPHP\GPIO\Pin\OutputPinInterface;

interface GPIOInterface
{
/**
* Get an input pin.
*
* @param int $number The pin number
*
* @return InputPinInterface
*/
public function getInputPin($number);

/**
* Get an output pin.
*
* @param int $number The pin number
*
* @return OutputPinInterface
*/
public function getOutputPin($number);

/**
* Create an interrupt watcher.
*
* @return InterruptWatcherInterface
*/
public function createWatcher();
}
10 changes: 6 additions & 4 deletions src/InterruptWatcher.php → src/Interrupt/InterruptWatcher.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace PiPHP\GPIO;
namespace PiPHP\GPIO\Interrupt;

use PiPHP\GPIO\FileSystem\FileSystemInterface;
use PiPHP\GPIO\Pin\InputPinInterface;

class InterruptWatcher implements InterruptWatcherInterface
{
Expand Down Expand Up @@ -39,13 +40,14 @@ public function __destruct()
/**
* {@inheritdoc}
*/
public function register(PinInterface $pin, callable $callback)
public function register(InputPinInterface $pin, callable $callback)
{
$pinNumber = $pin->getNumber();

if (!isset($this->streams[$pinNumber])) {
$file = '/sys/class/gpio/gpio' . $pinNumber . '/value';
$this->streams[$pinNumber] = $this->fileSystem->open($file, 'r');
stream_set_blocking($this->streams[$pinNumber], false);
}

$this->pins[$pinNumber] = $pin;
Expand All @@ -55,7 +57,7 @@ public function register(PinInterface $pin, callable $callback)
/**
* {@inheritdoc}
*/
public function unregister(PinInterface $pin)
public function unregister(InputPinInterface $pin)
{
$pinNumber = $pin->getNumber();

Expand Down Expand Up @@ -90,7 +92,7 @@ public function watch($timeout)
$triggers = [];

foreach ($except as $pinNumber => $stream) {
$value = fread($stream, 1024);
$value = fread($stream, 1);
@rewind($stream);

if ($value !== false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

namespace PiPHP\GPIO;
namespace PiPHP\GPIO\Interrupt;

use PiPHP\GPIO\Pin\InputPinInterface;

interface InterruptWatcherInterface
{
/**
* Register a callback to fire on pin interrupts. Only one callback can be registered per pin, this method will overwrite.
*
* @param PinInterface $pin
* @param callable $callback
* @param InputPinInterface $pin
* @param callable $callback
*/
public function register(PinInterface $pin, callable $callback);
public function register(InputPinInterface $pin, callable $callback);

/**
* Unregister a pin callback.
*
* @param PinInterface $pin
* @param InputPinInterface $pin
*/
public function unregister(PinInterface $pin);
public function unregister(InputPinInterface $pin);

/**
* Watch for pin interrupts.
Expand Down
32 changes: 0 additions & 32 deletions src/InterruptWatcherFactory.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/InterruptWatcherFactoryInterface.php

This file was deleted.

44 changes: 44 additions & 0 deletions src/Pin/InputPin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace PiPHP\GPIO\Pin;

use PiPHP\GPIO\FileSystem\FileSystemInterface;

final class InputPin extends Pin implements InputPinInterface
{
const GPIO_PIN_FILE_EDGE = 'edge';

/**
* Constructor.
*
* @param FileSystemInterface $fileSystem An object that provides file system access
* @param int $number The number of the pin
*/
public function __construct(FileSystemInterface $fileSystem, $number)
{
$this->fileSystem = $fileSystem;
$this->number = $number;

parent::__construct($fileSystem, $number);

$this->setDirection(self::DIRECTION_IN);
}

/**
* {@inheritdoc}
*/
public function getEdge()
{
$edgeFile = $this->getPinFile(self::GPIO_PIN_FILE_EDGE);
return $this->fileSystem->getContents($edgeFile);
}

/**
* {@inheritdoc}
*/
public function setEdge($edge)
{
$edgeFile = $this->getPinFile(self::GPIO_PIN_FILE_EDGE);
$this->fileSystem->putContents($edgeFile, $edge);
}
}
25 changes: 25 additions & 0 deletions src/Pin/InputPinInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PiPHP\GPIO\Pin;

interface InputPinInterface extends PinInterface
{
const EDGE_NONE = 'none';
const EDGE_BOTH = 'both';
const EDGE_RISING = 'rising';
const EDGE_FALLING = 'falling';

/**
* Get the pin edge.
*
* @return string The pin edge value
*/
public function getEdge();

/**
* Set the pin edge.
*
* @param string $edge The pin edge value to set
*/
public function setEdge($edge);
}
33 changes: 33 additions & 0 deletions src/Pin/OutputPin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PiPHP\GPIO\Pin;

use PiPHP\GPIO\FileSystem\FileSystemInterface;

final class OutputPin extends Pin implements OutputPinInterface
{
/**
* Constructor.
*
* @param FileSystemInterface $fileSystem An object that provides file system access
* @param int $number The number of the pin
*/
public function __construct(FileSystemInterface $fileSystem, $number)
{
$this->fileSystem = $fileSystem;
$this->number = $number;

parent::__construct($fileSystem, $number);

$this->setDirection(self::DIRECTION_OUT);
}

/**
* {@inheritdoc}
*/
public function setValue($value)
{
$valueFile = $this->getPinFile(self::GPIO_PIN_FILE_VALUE);
$this->fileSystem->putContents($valueFile, $value);
}
}
13 changes: 13 additions & 0 deletions src/Pin/OutputPinInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PiPHP\GPIO\Pin;

interface OutputPinInterface extends PinInterface
{
/**
* Set the pin value.
*
* @param int $value The value to set
*/
public function setValue($value);
}
Loading