From 64d338cf901f8f83cf740139a12c1446730e8592 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 6 Jun 2014 20:32:40 +0200 Subject: [PATCH] Device stubs and exceptions added --- lib/Device/Device.php | 75 ++++++++++++++++++++++++ lib/Device/Display/Pcd8544Device.php | 31 ++++++++++ lib/Exception/GpioException.php | 20 +++++++ lib/Exception/Handler/ConsoleHandler.php | 30 ++++++++++ lib/GpioMapper/WiringPiMapper.php | 35 +++++++++-- 5 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 lib/Device/Device.php create mode 100644 lib/Device/Display/Pcd8544Device.php create mode 100644 lib/Exception/GpioException.php create mode 100644 lib/Exception/Handler/ConsoleHandler.php diff --git a/lib/Device/Device.php b/lib/Device/Device.php new file mode 100644 index 0000000..04bc163 --- /dev/null +++ b/lib/Device/Device.php @@ -0,0 +1,75 @@ +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); + } +} diff --git a/lib/Device/Display/Pcd8544Device.php b/lib/Device/Display/Pcd8544Device.php new file mode 100644 index 0000000..d7769ec --- /dev/null +++ b/lib/Device/Display/Pcd8544Device.php @@ -0,0 +1,31 @@ +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); + } +} diff --git a/lib/Exception/GpioException.php b/lib/Exception/GpioException.php new file mode 100644 index 0000000..31178d8 --- /dev/null +++ b/lib/Exception/GpioException.php @@ -0,0 +1,20 @@ +pin = $pin; + } + + public function getPin() + { + return $this->pin; + } +} diff --git a/lib/Exception/Handler/ConsoleHandler.php b/lib/Exception/Handler/ConsoleHandler.php new file mode 100644 index 0000000..13089b0 --- /dev/null +++ b/lib/Exception/Handler/ConsoleHandler.php @@ -0,0 +1,30 @@ +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(); + } + } + } + + +} diff --git a/lib/GpioMapper/WiringPiMapper.php b/lib/GpioMapper/WiringPiMapper.php index 9e1799a..cfc82bd 100644 --- a/lib/GpioMapper/WiringPiMapper.php +++ b/lib/GpioMapper/WiringPiMapper.php @@ -36,9 +36,37 @@ class WiringPiMapper implements GpioMapperInterface switch ($gpio) { case 17: return 0; 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} */ @@ -78,9 +106,8 @@ class WiringPiMapper implements GpioMapperInterface case 18: return 29; case 19: return 30; case 20: return 31; - default: - throw new \Exception; } + throw new GpioException("Unable to map logicak {$logical} to GPIO pin"); } }