cleaned up gpio and device code, implemented dummy/dry run mode
This commit is contained in:
@ -18,16 +18,22 @@
|
||||
*/
|
||||
|
||||
namespace NoccyLabs\Gpio\Device;
|
||||
use NoccyLabs\Gpio\GpioPin;
|
||||
|
||||
abstract class Device implements GpioAwareInterface
|
||||
abstract class Device
|
||||
{
|
||||
|
||||
protected $name;
|
||||
|
||||
protected $gpio;
|
||||
protected $description;
|
||||
|
||||
protected $pins = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->configure();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
// call on ->addGpioPin etc here
|
||||
@ -44,39 +50,33 @@ abstract class Device implements GpioAwareInterface
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{}
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{}
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setDescription($description)
|
||||
{}
|
||||
{
|
||||
$this->description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Allocate a GPIO (as defined in hardware) pin to the device.
|
||||
*
|
||||
*/
|
||||
public function addGpioPin($gpio, $name, $description=null)
|
||||
{
|
||||
$pin = new GpioPin($gpio);
|
||||
$pin->setLabel($description);
|
||||
$this->pins[$name] = $pin;
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function addPin($name, $description=null)
|
||||
{
|
||||
$this->pins[$name] = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a logical (wiring-pi 0-based pin number) pin to the device.
|
||||
*
|
||||
*/
|
||||
public function addLogicalPin($logical, $name, $description=null)
|
||||
{
|
||||
$pin = $this->gpio[$logical];
|
||||
$pin->setLabel($description);
|
||||
$this->pins[$name] = $pin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all allocated pins
|
||||
*
|
||||
@ -94,20 +94,49 @@ abstract class Device implements GpioAwareInterface
|
||||
{
|
||||
return $this->pins[$name];
|
||||
}
|
||||
|
||||
public function setPin($pin_name, $pin)
|
||||
{
|
||||
if (array_key_exists($pin_name, $this->pins)) {
|
||||
$pin->setLabel($this->name.".".$pin_name);
|
||||
$this->pins[$pin_name] = $pin;
|
||||
return $this;
|
||||
}
|
||||
throw new \Exception();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a pin as a property from its name
|
||||
*
|
||||
*/
|
||||
protected function __get($pin_name)
|
||||
public function __get($pin_name)
|
||||
{
|
||||
return $this->getPin($pin_name);
|
||||
}
|
||||
|
||||
public function delayMillis($ms)
|
||||
{}
|
||||
public function __set($pin_name, GpioPin $pin)
|
||||
{
|
||||
$this->setPin($pin_name, $pin);
|
||||
}
|
||||
|
||||
public function delayMicros($us)
|
||||
{}
|
||||
protected function delayMillis($ms)
|
||||
{
|
||||
usleep($ms*1000);
|
||||
}
|
||||
|
||||
protected function delayMicros($us)
|
||||
{
|
||||
usleep($us);
|
||||
}
|
||||
|
||||
protected function shiftOut(GpioPin $pdata, GpioPin $pclk, $byte)
|
||||
{
|
||||
for ($bit = 0; $bit < 8; $bit++) {
|
||||
$bval = 1<<$bit;
|
||||
$pclk->setValue(0);
|
||||
$pdata->setValue(($byte & $bval) == $bval);
|
||||
$pclk->setValue(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user