php-linux-gpio/lib/GpioMapper/WiringPiMapper.php

128 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/*
* Copyright (C) 2014, NoccyLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
namespace NoccyLabs\Gpio\GpioMapper;
use NoccyLabs\Gpio\GpioMapperInterface;
use NoccyLabs\Gpio\Exception\GpioException;
class WiringPiMapper implements GpioMapperInterface
{
const REV_A = 1;
const REV_B = 2;
const REV_BPLUS = 3;
protected $version;
public function __construct($version=1)
{
$this->version = $version;
}
/** {@inheritdoc} */
public function mapGpioToLogicalPin($gpio)
{
switch ($gpio) {
case 17: return 0;
case 18: return 1;
2014-06-06 18:32:40 +00:00
case 22: return 3;
case 23: return 4;
case 24: return 5;
case 25: return 6;
case 11: return 7;
case 8: return 10;
case 7: return 11;
case 10: return 12;
case 9: return 13;
case 11: return 14;
case 14: return 15;
case 15: return 16;
case 28: return 17;
case 29: return 18;
case 30: return 19;
case 31: return 20;
}
if ($this->version == self::REV_B) {
switch ($gpio) {
case 27: return 2;
case 2: return 8;
case 3: return 9;
}
} elseif ($this->version == self::REV_BPLUS) {
2014-06-06 18:32:40 +00:00
switch ($gpio) {
case 27: return 2;
case 2: return 8;
case 3: return 9;
}
} elseif ($this->version == self::REV_A) {
2014-06-06 18:32:40 +00:00
switch ($gpio) {
case 21: return 2;
case 9: return 8;
case 1: return 9;
}
}
2014-06-06 18:32:40 +00:00
throw new GpioException("Unable to map GPIO{$gpio} to logical pin");
}
/** {@inheritdoc} */
public function mapLogicalToGpioPin($logical)
{
switch ($logical) {
case 0: return 17;
case 1: return 18;
case 2:
if (($this->version == self::REV_B)
||($this->version == self::REV_BPLUS)) {
return 27;
}
return 21;
case 3: return 22;
case 4: return 23;
case 5: return 24;
case 6: return 25;
case 7: return 11;
case 8:
if (($this->version == self::REV_B)
||($this->version == self::REV_BPLUS)) {
return 2;
}
return 0;
case 9:
if (($this->version == self::REV_B)
||($this->version == self::REV_BPLUS)) {
return 3;
}
return 1;
case 10: return 8;
case 11: return 7;
case 12: return 10;
case 13: return 9;
case 14: return 11;
case 15: return 14;
case 16: return 15;
case 17: return 28;
case 18: return 29;
case 19: return 30;
case 20: return 31;
}
throw new GpioException("Unable to map logical pin {$logical} to GPIO pin");
}
}