Skip to content

Commit

Permalink
add class
Browse files Browse the repository at this point in the history
  • Loading branch information
kento-oka committed Dec 12, 2017
1 parent f30a24a commit 62acbd3
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Gpio/Gpio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
*
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.
* Redistributions of files must retain the above copyright notice.
*
* @author Kento Oka <[email protected]>
* @copyright (c) Kento Oka
* @license MIT
* @since 1.0.0
*/
namespace Pi\Gpio;

/**
*
*/
class Gpio{

const PINS_REV1 = [
0, 1, 4, 7, 8, 9,
10, 11, 14, 15, 17, 18,
21, 22, 23, 24, 25
];

const PINS_REV2 = [
2, 3, 4, 7, 8, 9,
10, 11, 14, 15, 17, 18,
22, 23, 24, 25, 27
];

const PINS_NEW = [
2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27
];

const PATH_GPIO = "/sys/class/gpio/gpio";
const PATH_EXPORT = "/sys/class/gpio/gpio/export";
const PATH_UNEXPORT = "/sys/class/gpio/gpio/unexport";

/**
*
*
* @var Pin[]
*/
private $pins = [];

/**
* Constructor
*
* @param int[] $pins
*/
public function __construct(array $pins){
foreach($pins as $number){
if(is_int($number) && 0 <= $number){
$this->pins[$number] = new Pin($number, $this);
}
}
}

/**
* Get pin instance
*
* @param int $number
* Pin number
*
* @return Pin
*/
public function pin(int $number){
if(!isset($this->pins[$number])){
throw new \LogicException;
}

return $this->pins[$number];
}

/**
* Unexport all pin
*
* @return $this
*/
public function unexportAll(){
foreach($this->pins as $pin){
$pin->unexport();
}

return $this;
}
}
150 changes: 150 additions & 0 deletions src/Gpio/Pin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
*
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.
* Redistributions of files must retain the above copyright notice.
*
* @author Kento Oka <[email protected]>
* @copyright (c) Kento Oka
* @license MIT
* @since 1.0.0
*/
namespace Pi\Gpio;

/**
*
*/
class Pin{

/**
*
*
* @var Gpio
*/
private $gpio;

/**
*
*
* @var int
*/
private $number;

/**
* Constructor
*
* @param int $number
* Pin number
* @param Gpio $gpio
* wrapper instance
*/
public function __construct(int $number, Gpio $gpio){
$this->gpio = $gpio;
$this->number = $number;
}

public function isExported(): bool{
return file_exists($this->getGpioPath());
}

public function currentDirection(): string{
if(!$this->isExported()){
throw new \RuntimeException;
}

return (string)file_get_contents($this->getGpioPath("direction"));
}

/**
* Export this pin
*
* @param string $direction
* 'in' or 'out'
*
* @return $this
*/
public function export(string $direction = null){
if($direction !== "in" && $direction !== "out" && $direction !== null){
throw new \InvalidArgumentException;
}

$this->unexport();

//export
file_put_contents(Gpio::PATH_EXPORT, $this->number);

if($direction !== null){
file_put_contents($this->getGpioPath("direction"), $direction);
}

return $this;
}

public function exportIn(){
return $this->export("in");
}

public function exportOut(){
return $this->export("out");
}

/**
* Unexport thins pin
*
* @return $this
*/
public function unexport(){
if($this->isExported()){
file_put_contents(Gpio::PATH_UNEXPORT, $this->number);
}

return $this;
}

/**
* Get input
*
* @return string
*/
public function input(){
if($this->currentDirection() !== "in"){
throw new \LogicException;
}

$input = file_get_contents($this->getGpioPath("value"));

if($input === false){
throw new \RuntimeException;
}

return trim($input) === "1" ? true : false;
}

/**
* Send output
*
* @param bool $value
*
* @return $this
*/
public function output(bool $value){
if($this->currentDirection() !== "out"){
throw new \LogicException;
}

$result = file_put_contents($this->getGpioPath("value"), $value ? "1" : "0");

if($result === false){
throw new \RuntimeException;
}

return $this;
}

protected function getGpioPath(string $sub = null){
return Gpio::PATH_GPIO . $this->number . ($sub !== null ? "/$sub" : "");
}

}
54 changes: 54 additions & 0 deletions src/Pi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
*
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.
* Redistributions of files must retain the above copyright notice.
*
* @author Kento Oka <[email protected]>
* @copyright (c) Kento Oka
* @license MIT
* @since 1.0.0
*/
namespace Pi;

/**
* Interface used to define regex shortcut implementation.
*/
class Pi{

const CPU_TEMP = "/sys/class/thermal/thermal_zone0/temp";
const CPU_FREQUENCY = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq";

/**
*
* @var Gpio
*/
private $gpio;

/**
* Constructor
*/
public function __construct(Gpio $gpio){
$this->gpio = $gpio; // 一時的なコンストラクタインジェクションです。
}

// Piの状態の確認
// ファイル操作なども行えればベスト

public function getCpuLoad(){
return sys_getloadavg();
}

public function getCpuTemp(bool $fahrenheit = false){
$temp = floatval(file_get_contents(self::CPU_TEMP)) / 1000;
$temp = $fahrenheit ? 1.8 * $temp + 32 : $temp;

return $temp;
}

public function getCpuFrequency(){
return floatval(file_get_contents(self::CPU_FREQUENCY)) / 1000;
}
}

0 comments on commit 62acbd3

Please sign in to comment.