Code Monkey home page Code Monkey logo

phpi's Introduction

PHPi

Build Status Latest Stable Version

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

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

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 (<25 lines) in terms of complexity to allow flexibility at PHP level.

This means that you MUST have python installed alongside PHP for it to function. …sortof

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.

This library will function without any kernel drivers/sysfs etc enabled.

Setup

Using composer:

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

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.

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

The board factory:

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

Minimal example of reading and setting a pin

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

$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.

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.

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.

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.

See phpi-websocket for more on this.

External Devices

There are included helper classes for interfacing with common devices, with more added regulary.

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

phpi's People

Contributors

alcalyn avatar alexquinlivan avatar calcinai avatar caveman99 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpi's Issues

Fix I2C

I2C needs updating to use the aux register

Support board revision 900093/BCM2708

My Pi Zero does throw this Exception when executing info.php

PHP Fatal error: Uncaught Calcinai\PHPi\Exception\UnsupportedBoardException: Board revision [900093/BCM2708] is not (yet) supported.

raspbian stretch lite 2018-04-18 freeze

Hi,
I'm using you're phpi library for more than one year on a raspbian stretch lite 2017-09-07 without any issue, but I just upgrade to the last version of raspbian for the Rpi 3 B+ support and I'm having a big problem !
I can reproduce my issue with the sample websocket application you provide. When I start the server.php sometimes it freezes the Rpi immediately, sometimes I can access the example at http://raspberrypi.local:9999/ but after playing with the gpio status (output on/off) a few times, it freezes.
I don't know if there are some log files to check to get more informations ...
Thank you for your reply,
Marc-Antoine.

Find another way for event detection ? This lib can crash the OS.

New issue to not pollute the previous issue.

You should warn people that using this lib can crash the system, using a simple php class with sysfs is more realiable if you don't need advanced functionalities.

As I dont need SPI, I2C, PWM, I build another simple class with sysfs and event dection (3Hz is really enought for my needs, I just compare the lastest state to detect an event, and high event stay a lot of seconds (about 30s), so I dont need to detect a 100ms lvl high).

I dont I time to find the line, because it crash the RPI OS on every event low -> up without any low, only reboot is possible, it's very long, that's why I prefer to use sysfs, it's safe enought faster and light.

To reproduce : install raspbian lastest, add buster repo, update/upgrade, install php-7.2.4?, php-curl, php-event.
Note : I have a websocket server in the event loop too and php-redis as cache, I use laravel command to run the program (in root).

I have build my own library with sysfs/file_get_content file_put_content and custom Hz, without extension, it's safe and dont crash the rpi, you cant do 60kHz but about 30kHz so it's enought.
I think that accessing gpio registers is not realiable to use in production (because of system crash).

Remember : the crash is on raspbian buster (not "stable"), with php 7.1 / 7.2 Maybe no crash with raspbian stretch (but outdated php7.0 not compatible with my app).
Maybe you should find another (safe) way to do it (like pure C ext with real event, but it's very diffucult to code !).

You can do real edge detection with sysfs (gpioX/edge) but it is blocking, so you need other php extension, like swoole with true multithread/event, but I didn't played with it yet, react loop is non blocking only. Try to look at this : https://www.swoole.co.uk
It's faster than node, and can be compatible with react plugin, you can sleep(X) in event or do blocking tasks like gpioX/edge) without blocking the program.

Request for obtaining a PHP extension php-ext-mmap compatible with PHP 8 as an SO file.

Hello,

I've been studying how to operate Raspberry Pi using PHP recently. The model I'm working with is 3b, and the operating system is almalinux 9. I've looked into many libraries and found that most of them operate on /sys/class/gpio. After comparing, I believe your library has the most comprehensive functionality and better performance. However, I've encountered some issues while using it. These issues involve low-level operations of the operating system, and my understanding of the system is insufficient to resolve them. Therefore, I'm seeking your help.

I've noticed that your library hasn't been updated for quite some time. There are some deprecated syntax warnings when using PHP 8.1, but I've made modifications to address these issues, and they have been resolved.

The current issue lies with mmap. When using the Python script method, I encounter the following errors:

Traceback (most recent call last):
  File "/var/www/6002/calcinai/vendor/calcinai/php-mmap/lib/MMap/../../subprocess/mmap-proxy.py", line 19, in <module>
    address = struct.unpack('<H', sys.stdin.read(2))[0];
TypeError: a bytes-like object is required, not 'str'

I attempted to compile your https://github.com/calcinai/php-ext-mmap, but it also resulted in an error:

┌─[root@alma9-v4-2-12-4] - [/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap] - [2024-03-30 12:00:00]
└─[0] <> make install
/bin/sh /var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/libtool --mode=compile cc -I. -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/include -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/main -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2    -c /var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/mmap.c -o mmap.lo 
libtool: compile:  cc -I. -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/include -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/main -I/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/mmap.c  -fPIC -DPIC -o .libs/mmap.o
In file included from /var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/mmap.c:5:
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/php_mmap.h:11:80: error: expected ‘;’, ‘,’ or ‘)’ before ‘TSRMLS_DC’
   11 | size_t mmap_stream_write(php_stream *stream, const char *buffer, size_t length TSRMLS_DC); size_t mmap_stream_read(php_stream *stream, char *buffer, size_t length TSRMLS_DC); int mmap_stream_flush(php_stream *stream TSRMLS_DC); int mmap_stream_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset);
      |                                                                                ^~~~~~~~~
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/php_mmap.h:12:73: error: expected ‘;’, ‘,’ or ‘)’ before ‘TSRMLS_DC’
   12 | int mmap_stream_close(php_stream *stream, int close_handle TSRMLS_DC);
      |                                                                         ^        
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/php_mmap.h:13:42: error: expected ‘;’, ‘,’ or ‘)’ before ‘TSRMLS_DC’
   13 | 
      |                                          ^        
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/php_mmap.h:15:72: error: expected ‘;’, ‘,’ or ‘)’ before ‘TSRMLS_DC’
   15 |         void *base_offset;      void *current_offset;   int length; };
      |                                                                        ^        
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/mmap.c:39:5: error: ‘mmap_stream_write’ undeclared here (not in a function); did you mean ‘_php_stream_write’?
   39 |     mmap_stream_write,
      |     ^~~~~~~~~~~~~~~~~
      |     _php_stream_write
/var/www/6025/componentsTest/gpio/mmap/php-ext-mmap/mmap.c:40:5: error: ‘mmap_stream_read’ undeclared here (not in a function); did you mean ‘mmap_stream_seek’?
   40 |     mmap_stream_read,
      |     ^~~~~~~~~~~~~~~~
      |     mmap_stream_seek

So I'm not sure if you have the energy and time, but if possible, could you compile php-ext-mmap into an SO file and include it in the release, compatible with PHP 8.1 to 8.2?

appreciate it!

Example on using PWM

Hello, I'm interested in this library as it seems to support PWM.

But I can't found any example of using it. Is it possible to have a simple example on creating a PWM on a pin, and choosing duty cycle ?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.