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 - 2021 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 * This pin provider should be used for Raspberry Pi models:
043 *  - RaspberryPi A, A+
044 *  - RaspberryPi B, B+
045 *  - RaspberryPi 2B,
046 *  - RaspberryPi 3A+
047 *  - RaspberryPi 3B, 3B+
048 *  - RaspberryPi Zero, Zero-W
049 *  - RaspberryPi 4B,
050 *  - RaspberryPi 400,
051 *  - RaspberryPi ComputeModule4
052 *
053 * @author Robert Savage (<a
054 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
055 */
056public class RaspiPin extends PinProvider {
057
058    public static final Pin GPIO_00 = createDigitalPin(0, "GPIO 0");
059    public static final Pin GPIO_01 = createDigitalAndPwmPin(1, "GPIO 1"); // supports PWM0 [ALT5]
060    public static final Pin GPIO_02 = createDigitalPin(2, "GPIO 2");
061    public static final Pin GPIO_03 = createDigitalPin(3, "GPIO 3");
062    public static final Pin GPIO_04 = createDigitalPin(4, "GPIO 4");
063    public static final Pin GPIO_05 = createDigitalPin(5, "GPIO 5");
064    public static final Pin GPIO_06 = createDigitalPin(6, "GPIO 6");
065    public static final Pin GPIO_07 = createDigitalPin(7, "GPIO 7");
066    public static final Pin GPIO_08 = createDigitalPinNoPullDown(8, "GPIO 8");  // SDA.1 pin has a physical pull-up resistor
067    public static final Pin GPIO_09 = createDigitalPinNoPullDown(9, "GPIO 9");  // SDC.1 pin has a physical pull-up resistor
068    public static final Pin GPIO_10 = createDigitalPin(10, "GPIO 10");
069    public static final Pin GPIO_11 = createDigitalPin(11, "GPIO 11");
070    public static final Pin GPIO_12 = createDigitalPin(12, "GPIO 12");
071    public static final Pin GPIO_13 = createDigitalPin(13, "GPIO 13");
072    public static final Pin GPIO_14 = createDigitalPin(14, "GPIO 14");
073    public static final Pin GPIO_15 = createDigitalPin(15, "GPIO 15");
074    public static final Pin GPIO_16 = createDigitalPin(16, "GPIO 16");
075
076    // the following GPIO pins are only available on the Raspberry Pi Model A, B (revision 2.0)
077    public static final Pin GPIO_17 = createDigitalPin(17, "GPIO 17"); // requires B rev2 or newer model (P5 header)
078    public static final Pin GPIO_18 = createDigitalPin(18, "GPIO 18"); // requires B rev2 or newer model (P5 header)
079    public static final Pin GPIO_19 = createDigitalPin(19, "GPIO 19"); // requires B rev2 or newer model (P5 header)
080    public static final Pin GPIO_20 = createDigitalPin(20, "GPIO 20"); // requires B rev2 or newer model (P5 header)
081
082    // the following GPIO pins are only available on the Raspberry Pi Model A+, B+, Model 2B, Model 3B, Zero, Model 4B, Model 400
083    public static final Pin GPIO_21 = createDigitalPin(21, "GPIO 21"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
084    public static final Pin GPIO_22 = createDigitalPin(22, "GPIO 22"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
085    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]
086    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]
087    public static final Pin GPIO_25 = createDigitalPin(25, "GPIO 25"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
088    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]
089    public static final Pin GPIO_27 = createDigitalPin(27, "GPIO 27"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
090    public static final Pin GPIO_28 = createDigitalPin(28, "GPIO 28"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
091    public static final Pin GPIO_29 = createDigitalPin(29, "GPIO 29"); // requires 3B, 2B, Zero, A+, B+ or newer model (40 pin header)
092    public static final Pin GPIO_30 = createDigitalPinNoPullDown(30, "GPIO 30");  // SDA.0 pin has a physical pull-up resistor
093    public static final Pin GPIO_31 = createDigitalPinNoPullDown(31, "GPIO 31");  // SDC.0 pin has a physical pull-up resistor
094
095    protected static Pin createDigitalPinNoPullDown(int address, String name) {
096        return createDigitalPin(RaspiGpioProvider.NAME, address, name,
097                EnumSet.of(PinPullResistance.OFF, PinPullResistance.PULL_UP),
098                PinEdge.all());
099    }
100
101    protected static Pin createDigitalPin(int address, String name) {
102        return createDigitalPin(RaspiGpioProvider.NAME, address, name);
103    }
104
105    protected static Pin createDigitalAndPwmPin(int address, String name) {
106        return createDigitalAndPwmPin(RaspiGpioProvider.NAME, address, name);
107    }
108
109    // *override* static method from subclass
110    // (overriding a static method is not supported in Java
111    //  so this method definition will hide the subclass static method)
112    public static Pin getPinByName(String name) {
113        return PinProvider.getPinByName(name);
114    }
115
116    // *override* static method from subclass
117    // (overriding a static method is not supported in Java
118    //  so this method definition will hide the subclass static method)
119    public static Pin getPinByAddress(int address) {
120        return PinProvider.getPinByAddress(address);
121    }
122
123    // *override* static method from subclass
124    // (overriding a static method is not supported in Java
125    //  so this method definition will hide the subclass static method)
126    public static Pin[] allPins() { return PinProvider.allPins(); }
127
128    // *override* static method from subclass
129    // (overriding a static method is not supported in Java
130    //  so this method definition will hide the subclass static method)
131    public static Pin[] allPins(PinMode ... mode) { return PinProvider.allPins(mode); }
132
133    // *override* static method from subclass
134    // (overriding a static method is not supported in Java
135    //  so this method definition will hide the subclass static method)
136    public static Pin[] allPins(SystemInfo.BoardType board) {
137        List<Pin> pins = new ArrayList<>();
138
139        // pins for all Raspberry Pi models
140        pins.add(RaspiPin.GPIO_00);
141        pins.add(RaspiPin.GPIO_01);
142        pins.add(RaspiPin.GPIO_02);
143        pins.add(RaspiPin.GPIO_03);
144        pins.add(RaspiPin.GPIO_04);
145        pins.add(RaspiPin.GPIO_05);
146        pins.add(RaspiPin.GPIO_06);
147        pins.add(RaspiPin.GPIO_07);
148        pins.add(RaspiPin.GPIO_08);
149        pins.add(RaspiPin.GPIO_09);
150        pins.add(RaspiPin.GPIO_10);
151        pins.add(RaspiPin.GPIO_11);
152        pins.add(RaspiPin.GPIO_12);
153        pins.add(RaspiPin.GPIO_13);
154        pins.add(RaspiPin.GPIO_14);
155        pins.add(RaspiPin.GPIO_15);
156        pins.add(RaspiPin.GPIO_16);
157
158        // no further pins to add for Model B Rev 1 boards
159        if(board == SystemInfo.BoardType.RaspberryPi_B_Rev1){
160            // return pins collection
161            return pins.toArray(new Pin[0]);
162        }
163
164        // add pins exclusive to Model A and Model B (Rev2)
165        if(board == SystemInfo.BoardType.RaspberryPi_A ||
166           board == SystemInfo.BoardType.RaspberryPi_B_Rev2){
167            pins.add(RaspiPin.GPIO_17);
168            pins.add(RaspiPin.GPIO_18);
169            pins.add(RaspiPin.GPIO_19);
170            pins.add(RaspiPin.GPIO_20);
171        }
172
173        // add pins exclusive to Models A+, B+, 2B, 3B, 4B, 400 and Zero
174        else{
175            pins.add(RaspiPin.GPIO_21);
176            pins.add(RaspiPin.GPIO_22);
177            pins.add(RaspiPin.GPIO_23);
178            pins.add(RaspiPin.GPIO_24);
179            pins.add(RaspiPin.GPIO_25);
180            pins.add(RaspiPin.GPIO_26);
181            pins.add(RaspiPin.GPIO_27);
182            pins.add(RaspiPin.GPIO_28);
183            pins.add(RaspiPin.GPIO_29);
184            pins.add(RaspiPin.GPIO_30);
185            pins.add(RaspiPin.GPIO_31);
186        }
187
188        // return pins collection
189        return pins.toArray(new Pin[0]);
190    }
191}