pkj/raspberry-piface-api

Lets you control your PiFace for Raspberry PI with PHP.

dev-master 2013-10-01 21:30 UTC

This package is auto-updated.

Last update: 2024-04-29 03:20:56 UTC


README

Build Status

Raspberry PI - PiFace API for PHP 5.4+

I ported the original Python library to PHP. This library works almost the same as the python library.

Requirements

  • Hardware: Raspberry PI and PiFace module.
  • PHP 5.4+ installed
  • You also need to add the php SPI extension, compile it and configure, see: https://github.com/frak/php_spi
  • Apache if you want to run commands from web scripts.

Don't worry, follow the guides below to get started:

Setting up the environment

Few things you need to install your environment, here is some instructions.

# Install php + git
sudo apt-get install php5 php5-dev php5-cli git


# Install php SPI
git clone git://github.com/frak/php_spi.git ~/php_spi
cd ~/php_spi
phpize
./configure --enable-spi
make
make test
sudo make install

Install

Install with Composer:

Here is some commands to your new project in ~/myphpiface (in your home directory):

mkdir ~/myphpiface
cd ~/myphpiface
touch ~/myphpiface/composer.json
echo '{ "require": { "pkj/raspberry-piface-api": "dev-master" } }' >> ~/myphpiface/composer.json
curl -sS https://getcomposer.org/installer | php
php composer.phar install

This will create a vendor directory where the library is installed.

Usage:

Create a PHP script named test_piface.php (We assume that you place test_piface.php where composer.json file is.) and paste this in the file:

use Pkj\Raspberry\PiFace\PiFaceDigital;

dl("spi.so"); // Load the SPI extension.
require 'vendor/autoload.php';

$dev = PiFaceDigital::create();
// Run once.
$dev->init();

$dev->getLeds()[0]->turnOn();
sleep(2);
$dev->getLeds()[0]->turnOff();


// $dev->getInputPins();
// $dev->getOutputPins();
// $dev->getLeds();
// $dev->getRelays();
// $dev->getSwitches();


// Turn on relay 0
$dev->getRelays()[0]->turnOn();

// Get 0/1 of input pin 3 (There are 8 pins, 0-7)
$dev->getInputPins()[3]->getValue();

// Toggle a value on a output pin (5 in this example)
$dev->getOutputPins()[5]->toggle(); // 0
$dev->getOutputPins()[5]->toggle(); // 1
$dev->getOutputPins()[5]->toggle(); // 0



And test:

php -f test_piface.php