001package com.pi4j.io.gpio;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  RaspiPin.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.system.SystemInfo;
034
035import java.util.ArrayList;
036import java.util.EnumSet;
037import java.util.List;
038
039/**
040 * Raspberry Pi pin definitions for (default) WiringPi pin numbering scheme.
041 *
042 * @author Robert Savage (<a
043 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
044 */
045public class RaspiPin extends PinProvider {
046
047    public static final Pin GPIO_00 = createDigitalPin(0, "GPIO 0");
048    public static final Pin GPIO_01 = createDigitalAndPwmPin(1, "GPIO 1"); // supports PWM0 [ALT5]
049    public static final Pin GPIO_02 = createDigitalPin(2, "GPIO 2");
050    public static final Pin GPIO_03 = createDigitalPin(3, "GPIO 3");
051    public static final Pin GPIO_04 = createDigitalPin(4, "GPIO 4");
052    public static final Pin GPIO_05 = createDigitalPin(5, "GPIO 5");
053    public static final Pin GPIO_06 = createDigitalPin(6, "GPIO 6");
054    public static final Pin GPIO_07 = createDigitalPin(7, "GPIO 7");
055    public static final Pin GPIO_08 = createDigitalPinNoPullDown(8, "GPIO 8");  // SDA.1 pin has a physical pull-up resistor
056    public static final Pin GPIO_09 = createDigitalPinNoPullDown(9, "GPIO 9");  // SDC.1 pin has a physical pull-up resistor
057    public static final Pin GPIO_10 = createDigitalPin(10, "GPIO 10");
058    public static final Pin GPIO_11 = createDigitalPin(11, "GPIO 11");
059    public static final Pin GPIO_12 = createDigitalPin(12, "GPIO 12");
060    public static final Pin GPIO_13 = createDigitalPin(13, "GPIO 13");
061    public static final Pin GPIO_14 = createDigitalPin(14, "GPIO 14");
062    public static final Pin GPIO_15 = createDigitalPin(15, "GPIO 15");
063    public static final Pin GPIO_16 = createDigitalPin(16, "GPIO 16");
064
065    // the following GPIO pins are only available on the Raspberry Pi Model A, B (revision 2.0)
066    public static final Pin GPIO_17 = createDigitalPin(17, "GPIO 17"); // requires B rev2 or newer model (P5 header)
067    public static final Pin GPIO_18 = createDigitalPin(18, "GPIO 18"); // requires B rev2 or newer model (P5 header)
068    public static final Pin GPIO_19 = createDigitalPin(19, "GPIO 19"); // requires B rev2 or newer model (P5 header)
069    public static final Pin GPIO_20 = createDigitalPin(20, "GPIO 20"); // requires B rev2 or newer model (P5 header)
070
071    // the following GPIO pins are only available on the Raspberry Pi Model A+, B+, Model 2B, Model 3B, Zero
072    public static final Pin GPIO_21 = createDigitalPin(21, "GPIO 21"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
073    public static final Pin GPIO_22 = createDigitalPin(22, "GPIO 22"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
074    public static final Pin GPIO_23 = createDigitalAndPwmPin(23, "GPIO 23"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header) : supports PWM1 [ALT0]
075    public static final Pin GPIO_24 = createDigitalAndPwmPin(24, "GPIO 24"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header) : supports PWM1 [ALT5]
076    public static final Pin GPIO_25 = createDigitalPin(25, "GPIO 25"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
077    public static final Pin GPIO_26 = createDigitalAndPwmPin(26, "GPIO 26"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header) : supports PWM0 [ALT0]
078    public static final Pin GPIO_27 = createDigitalPin(27, "GPIO 27"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
079    public static final Pin GPIO_28 = createDigitalPin(28, "GPIO 28"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
080    public static final Pin GPIO_29 = createDigitalPin(29, "GPIO 29"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
081    public static final Pin GPIO_30 = createDigitalPinNoPullDown(30, "GPIO 30");  // SDA.0 pin has a physical pull-up resistor
082    public static final Pin GPIO_31 = createDigitalPinNoPullDown(31, "GPIO 31");  // SDC.0 pin has a physical pull-up resistor
083
084    protected static Pin createDigitalPinNoPullDown(int address, String name) {
085        return createDigitalPin(RaspiGpioProvider.NAME, address, name,
086                EnumSet.of(PinPullResistance.OFF, PinPullResistance.PULL_UP),
087                PinEdge.all());
088    }
089
090    protected static Pin createDigitalPin(int address, String name) {
091        return createDigitalPin(RaspiGpioProvider.NAME, address, name);
092    }
093
094    protected static Pin createDigitalAndPwmPin(int address, String name) {
095        return createDigitalAndPwmPin(RaspiGpioProvider.NAME, address, name);
096    }
097
098    // *override* static method from subclass
099    // (overriding a static method is not supported in Java
100    //  so this method definition will hide the subclass static method)
101    public static Pin getPinByName(String name) {
102        return PinProvider.getPinByName(name);
103    }
104
105    // *override* static method from subclass
106    // (overriding a static method is not supported in Java
107    //  so this method definition will hide the subclass static method)
108    public static Pin getPinByAddress(int address) {
109        return PinProvider.getPinByAddress(address);
110    }
111
112    // *override* static method from subclass
113    // (overriding a static method is not supported in Java
114    //  so this method definition will hide the subclass static method)
115    public static Pin[] allPins() { return PinProvider.allPins(); }
116
117    // *override* static method from subclass
118    // (overriding a static method is not supported in Java
119    //  so this method definition will hide the subclass static method)
120    public static Pin[] allPins(PinMode ... mode) { return PinProvider.allPins(mode); }
121
122    // *override* static method from subclass
123    // (overriding a static method is not supported in Java
124    //  so this method definition will hide the subclass static method)
125    public static Pin[] allPins(SystemInfo.BoardType board) {
126        List<Pin> pins = new ArrayList<>();
127
128        // pins for all Raspberry Pi models
129        pins.add(RaspiPin.GPIO_00);
130        pins.add(RaspiPin.GPIO_01);
131        pins.add(RaspiPin.GPIO_02);
132        pins.add(RaspiPin.GPIO_03);
133        pins.add(RaspiPin.GPIO_04);
134        pins.add(RaspiPin.GPIO_05);
135        pins.add(RaspiPin.GPIO_06);
136        pins.add(RaspiPin.GPIO_07);
137        pins.add(RaspiPin.GPIO_08);
138        pins.add(RaspiPin.GPIO_09);
139        pins.add(RaspiPin.GPIO_10);
140        pins.add(RaspiPin.GPIO_11);
141        pins.add(RaspiPin.GPIO_12);
142        pins.add(RaspiPin.GPIO_13);
143        pins.add(RaspiPin.GPIO_14);
144        pins.add(RaspiPin.GPIO_15);
145        pins.add(RaspiPin.GPIO_16);
146
147        // no further pins to add for Model B Rev 1 boards
148        if(board == SystemInfo.BoardType.RaspberryPi_B_Rev1){
149            // return pins collection
150            return pins.toArray(new Pin[0]);
151        }
152
153        // add pins exclusive to Model A and Model B (Rev2)
154        if(board == SystemInfo.BoardType.RaspberryPi_A ||
155           board == SystemInfo.BoardType.RaspberryPi_B_Rev2){
156            pins.add(RaspiPin.GPIO_17);
157            pins.add(RaspiPin.GPIO_18);
158            pins.add(RaspiPin.GPIO_19);
159            pins.add(RaspiPin.GPIO_20);
160        }
161
162        // add pins exclusive to Models A+, B+, 2B, 3B, and Zero
163        else{
164            pins.add(RaspiPin.GPIO_21);
165            pins.add(RaspiPin.GPIO_22);
166            pins.add(RaspiPin.GPIO_23);
167            pins.add(RaspiPin.GPIO_24);
168            pins.add(RaspiPin.GPIO_25);
169            pins.add(RaspiPin.GPIO_26);
170            pins.add(RaspiPin.GPIO_27);
171            pins.add(RaspiPin.GPIO_28);
172            pins.add(RaspiPin.GPIO_29);
173            pins.add(RaspiPin.GPIO_30);
174            pins.add(RaspiPin.GPIO_31);
175        }
176
177        // return pins collection
178        return pins.toArray(new Pin[0]);
179    }
180}