Skip to content

Instantly share code, notes, and snippets.

@Immortal-
Last active April 13, 2016 03:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Immortal-/a18f58ac5c21ba27921b7626b5a8b06e to your computer and use it in GitHub Desktop.
GPIO class for the Omega by Onion using php.
<?php
/**
* @Author: Chris McCaslin
* @Date: 4/8/2016
* @Orignal-Php Idealist: https://github.com/ringmaster/GPIOHelperPHP/ (Good idea, But I don't like reinventing the wheel why recode fast-gpio with php?)
* @Update: 4/12/2016 Added SetFlow, and GetDirection still need to write documentation on these methods.
*/
class OmegaPHP
{
const HIGH = 1, LOW = 0;
/**
* @MethodName: SetPin
* @Arg $pin The pin to be 'set'
* @Arg $value The value you want the pin to be set to ie. 1,0,HIGH,LOW
*/
public function SetPin($pin, $value)
{
if(strcasecmp($value,"HIGH") == 0)
{
$value = self::HIGH;
}elseif (strcasecmp($value,"LOW") == 0) //I wanted the HIGH, LOW constant I have with Arduino.
{
$value = self::LOW;
}
exec("fast-gpio set $pin $value 2>&1", $output);
return explode(': ',$output[0])[1];
}
/**
* @MethodName: ReadPin
* @Arg $pin The pin to be 'read'
*/
public function ReadPin($pin)
{
exec("fast-gpio read $pin 2>&1", $output);
return explode(': ',$output[0])[1];
}
/**
* @MethodName: SetPWM
* @Arg $pin The pin to be 'set'
* @Arg $hZ The value you want for frequency set in hertz.
* @Arg $dutyCyclePercent The Duty Cycle Percent
*/
public function SetPWM($pin, $hZ, $dutyCyclePercent)
{
exec("fast-gpio pwm $pin $hZ $dutyCyclePercent 2>&1", $output);
return explode(': ',$output[0])[1];
}
//TODO: Document function
public function SetFlow($pin,$direction)
{
$ins = array("IN","INPUT");
$outs = array("OUT","OUTPUT");
foreach ($ins as $o) // IN
{
if(strcasecmp($direction,$o) == 0)
{
exec("fast-gpio set-input $pin 2>&1", $output);
return explode(': ',$output[0])[1];
}
}
foreach ($outs as $o) // OUT
{
if(strcasecmp($direction,$o) == 0)
{
exec("fast-gpio set-outout $pin 2>&1", $output);
return explode(': ',$output[0])[1];
}
}
return "0x0";
}
//TODO: Document function
public function GetDirection($pin)
{
exec("fast-gpio get-direction $pin 2>&1", $output);
return explode(': ',$output[0])[1];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment