2017 © Pedro Peláez
 

library phpi

Event driven bindings for the Raspberry Pi GPIO

image

calcinai/phpi

Event driven bindings for the Raspberry Pi GPIO

  • Sunday, May 6, 2018
  • by calcinai
  • Repository
  • 10 Watchers
  • 72 Stars
  • 336 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 6 Forks
  • 7 Open issues
  • 17 Versions
  • 10 % Grown

The README.md

PHPi

Build Status Latest Stable Version, (*1)

Event driven bindings for the Raspberry Pi GPIO. Supports A, A+, B, Brev2, B+, 2B, 3B, Compute Module and Pi Zero., (*2)

This library interacts (almost) directly with the peripheral registers for maximum functionality and speed. See note on mmap/dma, (*3)

As there is no ability to mmap in PHP, by default, this has been delegated to a python subprocess. The python has been kept to an absolute minimum in terms of complexity to allow flexibility at PHP level., (*4)

This means that you MUST have python installed alongside PHP for it to function. …sortof, (*5)

There is also a native PHP extension that is a drop-in replacement for the python subprocess which greatly improves performance. I'd strongly recommend using it, especially with less powerful Pis., (*6)

This library will function without any kernel drivers/sysfs etc enabled., (*7)

Setup

Using composer:, (*8)

composer require calcinai/phpi

Although it is possible to add this to your own autoloader, it's not recommended as you'll have no control of the dependencies. If you haven't used composer before, I strongly recommend you check it out at https://getcomposer.org, (*9)

Usage

All of this code is designed to be run in cli mode, as root to permit the memory access. It is not recommended to try and run this in a synchronous nature (namely under apache/nginx) as this would introduce stability and security issues. See below for more information about webservices., (*10)

You can test your install and get a visual display of the pin states by running ./bin/phpi info from the install directory., (*11)

The board factory:, (*12)

$board = \Calcinai\PHPi\Factory::create();

Minimal example of reading and setting a pin, (*13)

use Calcinai\PHPi\Pin\PinFunction;
use Calcinai\PHPi\Pin;

$pin = $board->getPin(17) //BCM pin number
             ->setFunction(PinFunction::INPUT)
             ->setPull(Pin::PULL_UP);

//Will be === to Pin::LEVEL_HIGH or Pin::LEVEL_LOW
var_dump($pin->getLevel());

$pin->setFunction(PinFunction::OUTPUT);
$pin->high();
$pin->low();

Higher level devices and events, (*14)

$button = new Button($board->getPin(17));
$led = new LED($board->getPin(18));

$button->on('press', [$led, 'on']);
$button->on('release', [$led, 'off']);

$board->getLoop()->run();

GPIO

GPIO (input) is the default mode of the pin objects. Alternate functions can be accessed by using the ->setFunction(PinFunction::x) method. It is recommended to use the function names as opposed to ALT0..5 unless you know exactly what you're doing, as quite a lot are reserved. A few useful classes are also included for digital interaction. With the default python-mmap, you can expect a raw transition speed of ~20kHz, with the native extension, it's more like 80kHz on a Pi 3., (*15)

PWM

Hardware PWM is supported by this library, and to an extent, so is soft PWM. As this code runs in the react event loop, it's not practical to interact with the ports more than a few hundred times/sec., (*16)

SPI

SPI is supported along with some device protocols (MPC300x etc). With the default python-mmap, it is limited to about 3kB/s before there is no CPU left! With the native extension, you can reach speeds of over 30kB/s., (*17)

The event loop

One of the original reasons for basing this project on the react event loop was for the other components that can be leveraged. A good example is the websocket server; it will run seamlessly inline with this library to provide real time, bidirectional, non-polling interaction with the Pi from any modern browser., (*18)

See phpi-websocket for more on this., (*19)

External Devices

There are included helper classes for interfacing with common devices, with more added regulary., (*20)

  • Generic Inputs/Outputs
  • Buttons
  • LEDs
  • Relays
  • ADC (MCP 3xxx series)
  • H Bridge Motors
  • 2 and 4 Phase stepper motors

The Versions

06/05 2018

dev-master

9999999-dev

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

06/05 2018

v0.3.3

0.3.3.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

21/02 2018

v0.3.2

0.3.2.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

30/10 2017

v0.3.1

0.3.1.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

30/10 2017

v0.3.0

0.3.0.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

07/09 2017

0.3.0

0.3.0.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

16/01 2017

v0.2.3

0.2.3.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

14/10 2016

v0.2.2

0.2.2.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

30/09 2016

v0.2.1

0.2.1.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

26/09 2016

0.2.0

0.2.0.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

15/08 2016

v0.1.6

0.1.6.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

02/08 2016

v0.1.5

0.1.5.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

gpio raspberry rpi websocket pi spi pwm

07/07 2016

v0.1.4

0.1.4.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

02/07 2016

v0.1.3

0.1.3.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

01/07 2016

v0.1.2

0.1.2.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

30/06 2016

v0.1.1

0.1.1.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai

17/06 2016

v0.1.0

0.1.0.0

Event driven bindings for the Raspberry Pi GPIO

  Sources   Download

MIT

The Requires

 

by Michael Calcinai