requires testing, (*1)
The GPIO manager is designed to house your hardware environment setup and allow you to manager and interact with the hardware., (*2)
The GPIO manager comes with a Laravel bridged service provider for easy integration with Laravel., (*3)
The GPIO class requires gpio to be installed, (*4)
apt-get install gpio
Add the package to your application via composer, (*5)
composer require chickentikkamasala/gpio
'providers' => [ ... \ChickenTikkaMasala\GPIO\Bridge\Laravel\GPIOServiceProvider::class, ... ];
Add the event listener to your Event Service Provider, (*6)
$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., (*7)
If you run your application and alter your environment setup, you may want to run
php artisan cache:clear
to clear the cached setup., (*8)
Publish the vendor to get the config files, (*9)
php artisan vendor:publish --provider="ChickenTikkaMasala\GPIO\Bridge\Laravel\GPIOServiceProvider"
Example setup, (*10)
'pins' => [ 'redled' => [ 'pin' => 1, 'mode' => 'pwm', ], ],
Turn the redled on pin 1 on full, (*11)
php artisan gpio:set redled 1023
List all GPIO pin input from your setup, (*12)
php artisan gpio:list
This will output an array of all the GPIOs setup with the manager, (*13)
Alternatively to config setup you can call the create function to add new connections., (*14)
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, (*15)
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., (*16)
You can however add your own GPIO modes/classes in 2 ways., (*17)
First being, (*18)
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, (*19)
$manager->create('red', 1, 'LED');
Our GPIO config array in app/gpio.php, (*20)
'modes' => [ 'LED' => \App\GPIO\Modes\LED::class, ],
gpio:set redled 500
=> set red LED to 500gpio:get sensor
=> print the sensor readinggpio:list
=> list all connectionsgpio: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)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., (*21)
//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';
You can get the class out of the manager if you wish using, (*22)
$redled = $manager->get('redled'); //if you wanted to put it back simply use the add function again $manager->add('redled', $redled);
In PWM GPIO I have added 2 function for incrementing and decrementing for incrementing/decrementing to a value within an interval., (*23)
$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);
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):, (*24)
'pins' => [ 'redled' => [ 'pin' => 18, //wiring pi pin 1 = BCM pin 18 'mode' => 'pwm', 'options' => [ '-g', ], ], ],
An extremely handy pin map https://pinout.xyz/, (*25)
'pins' => [ 'redled' => [ 'pin' => 18, 'mode' => 'pwm', 'defaultValue' => 1023,//turn pin on full on boot of application ], ],