114 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.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;
 | 
						|
            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 == 2) {
 | 
						|
            switch ($gpio) {
 | 
						|
                case 27:    return 2;
 | 
						|
                case 2:     return 8;
 | 
						|
                case 3:     return 9;
 | 
						|
            }
 | 
						|
        } elseif ($this->version == 1) {
 | 
						|
            switch ($gpio) {
 | 
						|
                case 21:    return 2;
 | 
						|
                case 9:     return 8;
 | 
						|
                case 1:     return 9;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        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 == 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;
 | 
						|
        }
 | 
						|
        throw new GpioException("Unable to map logicak {$logical} to GPIO pin");
 | 
						|
    }
 | 
						|
 | 
						|
}
 |