cleaned up gpio and device code, implemented dummy/dry run mode

This commit is contained in:
2014-06-19 12:27:31 +02:00
parent 3e4765d071
commit 1449ec643f
8 changed files with 481 additions and 84 deletions

View File

@ -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);
}
}
}

View File

@ -28,12 +28,12 @@ class Pcd8544Device extends Device
$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(4, "res", "reset")
->addLogicalPin(9, "bl", "backlight")
->addPin("dc", "data/command")
->addPin("sce", "chip select")
->addPin("scl", "clock")
->addPin("sda", "data")
->addPin("res", "reset")
->addPin("bl", "backlight")
;
}
@ -46,4 +46,70 @@ class Pcd8544Device extends Device
{
$this->bl->setValue((bool)$state);
}
public function initialize()
{
$this->lcdInit();
}
public function lcdSend($byte, $command=false)
{
// assume clk is hi
// Enable display controller (active low).
$this->sce->setValue(0);
$this->dc->setValue((int)$command); // command or data
$this->shiftOut($this->sda, $this->scl, $byte);
// Disable display controller.
$this->sce->setValue(1);
/*
if (!$command) {
$this->cx++;
if ($this->cx > (LCD_X_RES - 1)) {
cx = 0; cy++;
#ifdef PCD8544_FIX_YALIGN
// Soft wrapping when FIX_YALIGN is defined
lcd_cursor(cx,cy);
#endif
}
}
*/
}
public function lcdInit()
{
$this->sce->setDirection("out");
$this->res->setDirection("out");
$this->dc->setDirection("out");
$this->scl->setDirection("out");
$this->sda->setDirection("out");
$this->res->setValue(1); // set RES
$this->sce->setValue(0); // reset SCE
$this->res->setValue(0); // pull RES low
$this->res->setValue(0); // and back hick
// Send sequence of command
$this->lcdSend( 0x21, true ); // LCD Extended Commands.
// lcd_send( 0xC8, true ); // Set LCD Vop (Contrast).
$this->lcdSend( 0x80 | 0x70, true ); // Set LCD Vop (Contrast).
$this->lcdSend( 0x06, true ); // Set Temp coefficent to 2.
$this->lcdSend( 0x13, true ); // LCD bias mode 1:100.
#ifdef PCD8544_FIX_YALIGN
$this->lcdSend( 0x45, true ); // LCD blank - Shift LCD 5 up (row starts at 1)
#endif
$this->lcdSend( 0x20, true ); // LCD Standard Commands, Horizontal addressing mode.
$this->lcdSend( 0x40, true ); // LCD blank
$this->lcdSend( 0x08, true ); // LCD blank
$this->lcdSend( 0x0C, true ); // LCD in inverse mode.
//$this->lcdClear();
}
}