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

87 lines
2.4 KiB
PHP

<?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;
class WiringPiMapper implements GpioMapperInterface
{
protected $version;
public function __construct($version=1)
{
$this->version = (int)$version;
}
/** {@inheritdoc} */
public function mapGpioToLogicalPin($gpio)
{
switch ($gpio) {
case 17: return 0;
case 18: return 1;
// ..
}
}
/** {@inheritdoc} */
public function mapLogicalToGpioPin($logical)
{
switch ($logical) {
case 0: return 17;
case 1: return 18;
case 2:
if ($this->version == 2) {
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 == 2) {
return 2;
}
return 0;
case 9:
if ($this->version == 2) {
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;
default:
throw new \Exception;
}
}
}