2014-06-06 18:32:40 +00:00
|
|
|
<?php
|
|
|
|
|
2014-06-12 12:22:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014, NoccyLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2014-06-06 18:32:40 +00:00
|
|
|
namespace NoccyLabs\Gpio\Device;
|
2014-06-19 10:27:31 +00:00
|
|
|
use NoccyLabs\Gpio\GpioPin;
|
2014-06-06 18:32:40 +00:00
|
|
|
|
2014-06-19 10:27:31 +00:00
|
|
|
abstract class Device
|
2014-06-06 18:32:40 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $name;
|
|
|
|
|
2014-06-19 10:27:31 +00:00
|
|
|
protected $description;
|
2014-06-06 18:32:40 +00:00
|
|
|
|
|
|
|
protected $pins = array();
|
|
|
|
|
2014-06-19 10:27:31 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->configure();
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:32:40 +00:00
|
|
|
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)
|
2014-06-19 10:27:31 +00:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
|
|
|
public function getName()
|
2014-06-19 10:27:31 +00:00
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
|
|
|
public function setDescription($description)
|
2014-06-19 10:27:31 +00:00
|
|
|
{
|
|
|
|
$this->description = $description;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
2014-06-19 10:27:31 +00:00
|
|
|
return $this->description;
|
2014-06-06 18:32:40 +00:00
|
|
|
}
|
2014-06-19 10:27:31 +00:00
|
|
|
|
|
|
|
public function addPin($name, $description=null)
|
2014-06-06 18:32:40 +00:00
|
|
|
{
|
2014-06-19 10:27:31 +00:00
|
|
|
$this->pins[$name] = null;
|
|
|
|
return $this;
|
2014-06-06 18:32:40 +00:00
|
|
|
}
|
2014-06-19 10:27:31 +00:00
|
|
|
|
2014-06-12 14:11:35 +00:00
|
|
|
/**
|
|
|
|
* Get all allocated pins
|
|
|
|
*
|
|
|
|
*/
|
2014-06-06 18:32:40 +00:00
|
|
|
public function getPins()
|
|
|
|
{
|
|
|
|
return $this->pins;
|
|
|
|
}
|
|
|
|
|
2014-06-12 14:11:35 +00:00
|
|
|
/**
|
|
|
|
* Get a pin by the name specified when allocating it
|
|
|
|
*
|
|
|
|
*/
|
2014-06-06 18:32:40 +00:00
|
|
|
public function getPin($name)
|
|
|
|
{
|
|
|
|
return $this->pins[$name];
|
|
|
|
}
|
2014-06-19 10:27:31 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2014-06-12 14:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a pin as a property from its name
|
|
|
|
*
|
|
|
|
*/
|
2014-06-19 10:27:31 +00:00
|
|
|
public function __get($pin_name)
|
2014-06-12 14:11:35 +00:00
|
|
|
{
|
|
|
|
return $this->getPin($pin_name);
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
2014-06-19 10:27:31 +00:00
|
|
|
public function __set($pin_name, GpioPin $pin)
|
|
|
|
{
|
|
|
|
$this->setPin($pin_name, $pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function delayMillis($ms)
|
|
|
|
{
|
|
|
|
usleep($ms*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function delayMicros($us)
|
|
|
|
{
|
|
|
|
usleep($us);
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
2014-06-19 10:27:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 18:32:40 +00:00
|
|
|
|
|
|
|
}
|