001package com.pi4j.io.gpio; 002 003/* 004 * #%L 005 * ********************************************************************** 006 * ORGANIZATION : Pi4J 007 * PROJECT : Pi4J :: Java Library (Core) 008 * FILENAME : PinProvider.java 009 * 010 * This file is part of the Pi4J project. More information about 011 * this project can be found here: http://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2016 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.io.gpio.impl.PinImpl; 034 035import java.util.*; 036 037/** 038 * Pi4J pin definitions 039 * 040 * @author Robert Savage (<a 041 * href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>) 042 */ 043public abstract class PinProvider { 044 045 protected static final Map<String, Pin> pins = new HashMap<>(); 046 047 protected static Pin createDigitalPin(String providerName, int address, String name) { 048 return createDigitalPin(providerName, address, name, EnumSet.allOf(PinEdge.class)); 049 } 050 051 protected static Pin createDigitalPin(String providerName, int address, String name, EnumSet<PinPullResistance> resistance, EnumSet<PinEdge> edges) { 052 return createPin(providerName, address, name, 053 EnumSet.of(PinMode.DIGITAL_INPUT, PinMode.DIGITAL_OUTPUT, PinMode.SOFT_PWM_OUTPUT), 054 resistance, 055 edges); 056 } 057 058 protected static Pin createDigitalPin(String providerName, int address, String name, EnumSet<PinEdge> edges) { 059 return createPin(providerName, address, name, 060 EnumSet.of(PinMode.DIGITAL_INPUT, PinMode.DIGITAL_OUTPUT, PinMode.SOFT_PWM_OUTPUT), 061 PinPullResistance.all(), 062 edges); 063 } 064 065 protected static Pin createDigitalAndPwmPin(String providerName, int address, String name, EnumSet<PinEdge> edges) { 066 return createPin(providerName, address, name, 067 EnumSet.of(PinMode.DIGITAL_INPUT, PinMode.DIGITAL_OUTPUT, PinMode.PWM_OUTPUT), 068 PinPullResistance.all(), 069 edges); 070 } 071 072 protected static Pin createDigitalAndPwmPin(String providerName, int address, String name) { 073 return createDigitalAndPwmPin(providerName, address, name, EnumSet.allOf(PinEdge.class)); 074 } 075 076 protected static Pin createAnalogInputPin(String providerName, int address, String name) { 077 return createPin(providerName, address, name, EnumSet.of(PinMode.ANALOG_INPUT)); 078 } 079 080 protected static Pin createPin(String providerName, int address, String name, EnumSet<PinMode> modes) { 081 Pin pin = new PinImpl(providerName, address, name, modes); 082 pins.put(name, pin); 083 return pin; 084 } 085 086 protected static Pin createPin(String providerName, int address, String name, EnumSet<PinMode> modes, 087 EnumSet<PinPullResistance> resistance, EnumSet<PinEdge> edges) { 088 Pin pin = new PinImpl(providerName, address, name, modes, resistance, edges); 089 pins.put(name, pin); 090 return pin; 091 } 092 093 public static Pin getPinByName(String name) { 094 return pins.get(name); 095 } 096 097 public static Pin getPinByAddress(int address) { 098 for(Pin pin : pins.values()){ 099 if(pin.getAddress() == address){ 100 return pin; 101 } 102 } 103 return null; 104 } 105 106 /** 107 * Get all pin instances from this provider. 108 * @return all pin instances support by this provider 109 */ 110 public static Pin[] allPins() { 111 return pins.values().toArray(new Pin[0]); 112 } 113 114 /** 115 * Get all pin instances from this provider that support one of the provided pin modes. 116 * @param mode one or more pin modes that you wish to include in the result set 117 * @return pin instances that support the provided pin modes 118 */ 119 public static Pin[] allPins(PinMode ... mode) { 120 List<Pin> results = new ArrayList<>(); 121 for(Pin p : pins.values()){ 122 EnumSet<PinMode> supported_modes = p.getSupportedPinModes(); 123 for(PinMode m : mode){ 124 if(supported_modes.contains(m)){ 125 results.add(p); 126 continue; 127 } 128 } 129 } 130 return results.toArray(new Pin[0]); 131 } 132}