Added software spi stuff

This commit is contained in:
Chris 2014-06-14 03:10:07 +02:00
parent 75d6df400e
commit cdffd77a38
6 changed files with 241 additions and 2 deletions

25
examples/softwarespi.php Normal file
View File

@ -0,0 +1,25 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/GLOBALS.php";
use NoccyLabs\Gpio\Gpio;
use NoccyLabs\Gpio\GpioTickHandler;
use NoccyLabs\Gpio\Bus\SoftwareSpiBus as SPI;
use NoccyLabs\Crap\Dumper;
$gpio = new Gpio(true);
// create spi bus
$spi = new SPI();
// bind the spi pins
$spi->sclk = $gpio[1];
$spi->mosi = $gpio[2];
$spi->miso = $gpio[3];
// write to the spi bus
$spi->write("\xBE\xEF");
// read the two bytes exchanged with the write()
$read = $spi->read(2);
//
Dumper::dump($read);

64
lib/Bus/Bus.php Normal file
View File

@ -0,0 +1,64 @@
<?php
/*
* 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/>
*/
namespace NoccyLabs\Gpio\Bus;
abstract class Bus implements BusInterface
{
protected $pins = array();
public function __construct()
{
$this->configure();
}
public function addPin($name)
{
$this->pins[$name] = null;
}
public function __set($name, $value)
{
if (!array_key_exists($name,$this->pins)) {
throw new \Exception("No such pin: {$name}");
}
$this->pins[$name] = $value;
}
public function __get($name)
{
if (!array_key_exists($name,$this->pins)) {
throw new \Exception("No such pin: {$name}");
}
return $this->pins[$name];
}
public function __isSet($name)
{
return array_key_exists($name,$this->pins);
}
public function __unset($name)
{
if (!array_key_exists($name,$this->pins)) {
return;
}
$this->pins[$name] = null;
}
}

42
lib/Bus/BusInterface.php Normal file
View File

@ -0,0 +1,42 @@
<?php
/*
* 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/>
*/
namespace NoccyLabs\Gpio\Bus;
interface BusInterface
{
/**
* Write data to the bus. The data will be written immediately.
*
*/
public function write($data);
/**
* Read data from the buffer, or perform additional null reads over the
* (SPI) bus to acquire the data.
*
*/
public function read($bytes);
/**
* Return the number of bytes in the read buffer
*
*/
public function getBufferFill();
}

View File

@ -0,0 +1,92 @@
<?php
/*
* 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/>
*/
namespace NoccyLabs\Gpio\Bus;
class SoftwareSpiBus extends Bus
{
protected $buffer = null;
protected function configure()
{
$this->addPin("sclk");
$this->addPin("mosi");
$this->addPin("miso");
}
protected function spiExchangeByte($byte)
{
$in = 0;
if (is_string($byte)) { $byte = ord($byte); }
for($bit=0; $bit<8; $bit++) {
$this->setValue("sclk", 0);
$this->setValue("mosi", (($byte & 1<<$bit)>0));
usleep(5000);
$this->setValue("sclk", 1);
usleep(5000);
if ($this->getValue("miso")) {
$in |= 1<<$bit;
}
}
return chr($in);
}
protected function setValue($pin,$value)
{
echo "{$pin}{$value}\n";
}
protected function getValue($pin)
{
echo "{$pin}←0\n";
return 0;
}
public function write($data)
{
for($n = 0; $n < strlen($data); $n++) {
$this->buffer .= $this->spiExchangeByte($data[$n]);
}
}
public function read($bytes)
{
$ret = null;
if (strlen($this->buffer)>0) {
if (strlen($this->buffer)<$bytes) {
$ret = $this->buffer;
$bytes -= strlen($this->buffer);
$this->buffer = null;
} else {
$ret = substr($this->buffer,0,$bytes);
$this->buffer = substr($this->buffer,$bytes);
return $ret;
}
}
for($n = 0; $n < $bytes; $n++) {
$ret.= $this->spiExchangeByte(0x00);
}
return $ret;
}
public function getBufferFill()
{
return strlen($this->buffer);
}
}

View File

@ -52,8 +52,6 @@ class GpioPin
{ {
$this->pin = (int)$pin; $this->pin = (int)$pin;
$this->gpio = $gpio; $this->gpio = $gpio;
$this->export();
$this->hardware = $this->findHardware();
} }
private function findHardware() private function findHardware()
@ -139,6 +137,7 @@ class GpioPin
if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) { if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
throw new HardwareException("Unable to export pin {$this->pin}"); throw new HardwareException("Unable to export pin {$this->pin}");
} }
$this->hardware = $this->findHardware();
return $this; return $this;
} }

View File

@ -1,5 +1,22 @@
<?php <?php
/*
* 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/>
*/
namespace NoccyLabs\Gpio\Util; namespace NoccyLabs\Gpio\Util;
use NoccyLabs\Gpio\GpioPin; use NoccyLabs\Gpio\GpioPin;