定制 chickentikkamasala/gpio 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chickentikkamasala/gpio

Composer 安装命令:

composer create-project chickentikkamasala/gpio

包简介

README 文档

README

requires testing

The GPIO manager is designed to house your hardware environment setup and allow you to manager and interact with the hardware.

The GPIO manager comes with a Laravel bridged service provider for easy integration with Laravel.

The GPIO class requires gpio to be installed

apt-get install gpio

Installing into laravel

Add the package to your application via composer

composer require chickentikkamasala/gpio
'providers' => [

    ...
    
    \ChickenTikkaMasala\GPIO\Bridge\Laravel\GPIOServiceProvider::class,
    
    ...
    
];

Add the event listener to your Event Service Provider

$subscribe = [
    \ChickenTikkaMasala\GPIO\Bridge\Laravel\Events\EventSubscriber::class,
];

The above is for recaching the GPIO manager after a request to prevent redeclaration of the manager and resetting everything to their default value.

If you run your application and alter your environment setup, you may want to run php artisan cache:clear to clear the cached setup.

Publish the vendor to get the config files

php artisan vendor:publish --provider="ChickenTikkaMasala\GPIO\Bridge\Laravel\GPIOServiceProvider"

Example setup

    'pins' => [
        'redled' => [
            'pin' => 1,
            'mode' => 'pwm',
        ],
    ],

Testing with the command line

Turn the redled on pin 1 on full

php artisan gpio:set redled 1023

List all GPIO pin input from your setup

php artisan gpio:list

This will output an array of all the GPIOs setup with the manager

Creating a new connection

Alternatively to config setup you can call the create function to add new connections.

    public function index(GPIOManager $manager) 
    {
        $manager->create('greenled', 2);
        $manager->greenled = 'ON';
    }

The manager maps your named connections as parameters as shown above. When reading pins we can use the same method with the parameters to get the result

    public function index(GPIOManager $manager)
    {
        //create an analog sensor
        $manager->create('sensor', 3, 'aread');
        $value = $manager->sensor;
        
        return response()->json(['sensor' => $value]);
    }

Mapping also applies to the values 'OFF' and 'ON' where PWM expects 1023 as max and write expect 1. 'OFF' will equal to 0.

Custom GPIO Classes/Modes

You can however add your own GPIO modes/classes in 2 ways.

First being

use ChickenTikkaMasala\GPIO\GPIO;

class RedLED extends GPIO
{
    public function __construct()
    {
        parent::__construct(1, 'pwm', 'OFF');
    }
    
    public function getMethod()
    {
        return 'out';
    }
    
}
    public function index(GPIOManager $manager)
    {
        $redLED = new RedLED();
        $manager->add($redLED);
    }

Another method is to use the registerMode function to register the mode type for the manager so you can do something like this

    $manager->create('red', 1, 'LED');

Our GPIO config array in app/gpio.php

    'modes' => [
        'LED' => \App\GPIO\Modes\LED::class,
    ],

Terminal functions

  • gpio:set redled 500 => set red LED to 500
  • gpio:get sensor => print the sensor reading
  • gpio:list => list all connections
  • gpio:function redled increment 1023 1000 => call the increment function with the options (options needs implementing)
  • gpio:listen sensor --onChange => prints the state of the sensor (onChange option to only print if value has changed else consistently print incoming value)

Available default modes

  • PWM => for incrementing the voltage between 0 and max
  • Awrite
  • ARead
  • Write => for writing to pins either OFF or ON (0v or max voltage)
  • Read => for reading pins either OFF or ON

Value conversions

You may remember seeing (if you've ever used an arduino) values being set as HIGH or LOW. You can do this with the GPIO and depending on it's mode it will automatically fix your maximum value.

    //PWM all the below = 1023
    $manager->redled = 'HIGH';
    $manager->redled = 'UP';
    $manager->redled = 'ON';
    
    //Where as for write and awrite: these equal to 1
    $manager->redled = 'HIGH';
    $manager->redled = 'UP';
    $manager->redled = 'ON';
    //all equal to 1
    
    //The below all equal to 0
    $manager->redled = 'LOW';
    $manager->redled = 'DOWN'
    $manager->redled = 'OFF';

Getting the GPIO class out of the manager

You can get the class out of the manager if you wish using

    $redled = $manager->get('redled');
    
    //if you wanted to put it back simply use the add function again
    
    $manager->add('redled', $redled);

PWM functions

In PWM GPIO I have added 2 function for incrementing and decrementing for incrementing/decrementing to a value within an interval.

$redled = $manager->get('redled');
$redled->increment(1023, 200);
//redled will 'fade' from 0 to 1023 
//increment will increase every 200th millisecond

$redled->decrement(0, 200);

$manager->add('redled', $redled);

GPIO Options

If you see http://wiringpi.com/the-gpio-utility/ there is a usage section. These options are available to pass to the GPIOManager like so (example to use BCM pins):

    'pins' => [
        'redled' => [
            'pin' => 18, //wiring pi pin 1 = BCM pin 18
            'mode' => 'pwm',
            'options' => [
                '-g',
            ],
        ],
    ],

An extremely handy pin map https://pinout.xyz/

Default GPIO value

    'pins' => [
        'redled' => [
            'pin' => 18,
            'mode' => 'pwm',
            'defaultValue' => 1023,//turn pin on full on boot of application 
        ],
    ],

Coming soon

  • Symfony bridge
  • mapping values from 'low' to 'high' (for ESCs for example a pwm value of 60 is 0. For servos we may want to map 0 - 1023 to something like 0 - 180 degrees where a 'high' of 1023 = 180 degrees)
  • exceptions for command errors

chickentikkamasala/gpio 适用场景与选型建议

chickentikkamasala/gpio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 chickentikkamasala/gpio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chickentikkamasala/gpio 我们能提供哪些服务?
定制开发 / 二次开发

基于 chickentikkamasala/gpio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 98
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-04-26