Device stubs and exceptions added

This commit is contained in:
Chris 2014-06-06 20:32:40 +02:00
parent 633e254958
commit 64d338cf90
5 changed files with 187 additions and 4 deletions

75
lib/Device/Device.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace NoccyLabs\Gpio\Device;
abstract class Device implements GpioAwareInterface
{
protected $name;
protected $gpio;
protected $pins = array();
protected function configure()
{
// call on ->addGpioPin etc here
}
public function setGpio(Gpio $gpio=null)
{
$this->gpio = $gpio;
}
public function getGpio()
{
return $this->gpio;
}
public function setName($name)
{}
public function getName()
{}
public function setDescription($description)
{}
public function getDescription()
{}
public function addGpioPin($gpio, $name, $description=null)
{
$pin = new GpioPin($gpio);
$pin->setLabel($description);
$this->pins[$name] = $pin;
}
public function addLogicalPin($logical, $name, $description=null)
{
$pin = $this->gpio[$logical];
$pin->setLabel($description);
$this->pins[$name] = $pin;
}
public function getPins()
{
return $this->pins;
}
public function getPin($name)
{
return $this->pins[$name];
}
public function delayMillis($ms)
{}
public function delayMicros($us)
{}
public function __get($pin_name)
{
return $this->getPin($pin_name);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace NoccyLabs\Gpio\Device\Display;
use NoccyLabs\Gpio\Device\Device;
class Pcd8544Device extends Device
{
protected function configure()
{
$this
->setName("pcd8544")
->setDescription("Philips PCD8544 LCD Display Driver")
->addLogicalPin(0, "dc", "data/command")
->addLogicalPin(1, "sce", "chip select")
->addLogicalPin(2, "scl", "clock")
->addLogicalPin(3, "sda", "data")
->addLogicalPin(9, "bl", "backlight")
;
}
protected function startup()
{
}
public function setBacklightState($state)
{
$this->bl->write((int)$state);
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace NoccyLabs\Gpio\Exception;
use NoccyLabs\Gpio\GpioPin;
class GpioException extends \Exception
{
protected $pin;
public function setPin(GpioPin $pin=null)
{
$this->pin = $pin;
}
public function getPin()
{
return $this->pin;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace NoccyLabs\Gpio\Exception\Handler;
use NoccyLabs\Gpio\Exception\GpioException;
class ConsoleExceptionHandler
{
protected $last;
public function register()
{
$this->last = set_exception_handler( array($this, "onException") );
}
public function onException(Exception $exception)
{
if (is_callable($this->last)) {
call_user_func($this->last, $exception);
}
if ($exception instanceof GpioException) {
$pin = $exception->getPin();
if ($pin) {
$pin->dumpState();
}
}
}
}

View File

@ -36,9 +36,37 @@ class WiringPiMapper implements GpioMapperInterface
switch ($gpio) { switch ($gpio) {
case 17: return 0; case 17: return 0;
case 18: return 1; case 18: return 1;
// .. case 22: return 3;
case 23: return 4;
case 24: return 5;
case 25: return 6;
case 11: return 7;
case 8: return 10;
case 7: return 11;
case 10: return 12;
case 9: return 13;
case 11: return 14;
case 14: return 15;
case 15: return 16;
case 28: return 17;
case 29: return 18;
case 30: return 19;
case 31: return 20;
} }
if ($this->version == 2) {
switch ($gpio) {
case 27: return 2;
case 2: return 8;
case 3: return 9;
}
} elseif ($this->version == 1) {
switch ($gpio) {
case 21: return 2;
case 9: return 8;
case 1: return 9;
}
}
throw new GpioException("Unable to map GPIO{$gpio} to logical pin");
} }
/** {@inheritdoc} */ /** {@inheritdoc} */
@ -78,9 +106,8 @@ class WiringPiMapper implements GpioMapperInterface
case 18: return 29; case 18: return 29;
case 19: return 30; case 19: return 30;
case 20: return 31; case 20: return 31;
default:
throw new \Exception;
} }
throw new GpioException("Unable to map logicak {$logical} to GPIO pin");
} }
} }