com.diozero.api.PinInfo Maven / Gradle / Ivy
package com.diozero.api;
/*
* #%L
* Organisation: diozero
* Project: Device I/O Zero - Core
* Filename: PinInfo.java
*
* This file is part of the diozero project. More information about this project
* can be found at http://www.diozero.com/
* %%
* Copyright (C) 2016 - 2021 diozero
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import java.util.Collection;
import java.util.EnumSet;
/**
*
* Describe the various attributes of an individual General-Purpose Input/Output
* (GPIO) pin. GPIO pin functions include Digital Input / Output, PWM Output and
* Analog Input / Output.
*
*
*
* Always access instances of this class via the
* {@link com.diozero.sbc.BoardPinInfo BoardPinInfo} accessor methods rather
* than constructing instances yourself. For example,
* {@link com.diozero.sbc.BoardPinInfo#getByGpioNumber(int)
* BoardPinInfo.getByGpioNumber(gpio)} and
* {@link com.diozero.sbc.BoardPinInfo#getByChipAndLineOffset(int, int)
* BoardPinInfo.getByChipAndLineOffset(chip, lineOffset)}.
*
*
*
* A board has a number of headers, each header has a number of physical pins
* connected to it. For example, the Raspberry Pi model B has the main 40-pin J8
* header as well as the 8-pin P5 header (that doesn't have header pins soldered
* by default). Non-GPIO pins such as Vcc and GND are also included for
* information purposes only.
*
*
*
* A pin has the following attributes:
*
*
* - keyPrefix
- internal only attribute used by
* {@link com.diozero.internal.spi.AbstractDeviceFactory AbstractDeviceFactory}
* when provisioning GPIO devices
* - header
- the name of the board header to which this pin
* is attached
* - physicalPin
- the physical header pin number
* - deviceNumber
- the logical device GPIO number
* - sysFsNumber
- typically the same as the GPIO / device
* number; can be different for PWM pins that are controlled via Linux
* sysfs
* - chip
- the Linux GPIO chip number for this GPIO as
* defined by the Linux GPIO character device; see /sys/gpiochip<n>, run
*
gpiodetect
to list
* - lineOffset
- the line number offset for this GPIO on the
* GPIO chip - Linux GPIO character device;
run gpioinfo <n>
* to list
* - name
- the name of this pin
* - modes
- the set of valid {@link DeviceMode modes} for
* this pin
*
*/
public class PinInfo {
public static final EnumSet DIGITAL_IN = EnumSet.of(DeviceMode.DIGITAL_INPUT);
public static final EnumSet DIGITAL_OUT = EnumSet.of(DeviceMode.DIGITAL_OUTPUT);
public static final EnumSet DIGITAL_IN_OUT = EnumSet.of(DeviceMode.DIGITAL_INPUT,
DeviceMode.DIGITAL_OUTPUT);
public static final EnumSet DIGITAL_IN_OUT_PWM = EnumSet.of(DeviceMode.DIGITAL_INPUT,
DeviceMode.DIGITAL_OUTPUT, DeviceMode.PWM_OUTPUT);
public static final EnumSet PWM_OUTPUT = EnumSet.of(DeviceMode.PWM_OUTPUT);
public static final EnumSet DIGITAL_PWM_OUTPUT = EnumSet.of(DeviceMode.DIGITAL_OUTPUT,
DeviceMode.PWM_OUTPUT);
public static final EnumSet DIGITAL_ANALOG_INPUT = EnumSet.of(DeviceMode.DIGITAL_INPUT,
DeviceMode.ANALOG_INPUT);
public static final EnumSet ANALOG_INPUT = EnumSet.of(DeviceMode.ANALOG_INPUT);
public static final EnumSet ANALOG_OUTPUT = EnumSet.of(DeviceMode.ANALOG_OUTPUT);
public static final int NOT_DEFINED = -1;
public static final String DEFAULT_HEADER = "DEFAULT";
public static final String GROUND = "GND";
public static final String VCC_5V = "5v";
public static final String VCC_3V3 = "3v3";
private String keyPrefix;
private String header;
private int physicalPin;
private int deviceNumber;
private int sysFsNumber;
private int chip;
private int lineOffset;
private String name;
private Collection modes;
public PinInfo(String keyPrefix, String header, int deviceNumber, int physicalPin, String name,
Collection modes) {
this(keyPrefix, header, deviceNumber, physicalPin, name, modes, deviceNumber, NOT_DEFINED, NOT_DEFINED);
}
public PinInfo(String keyPrefix, String header, int deviceNumber, int physicalPin, String name,
Collection modes, int sysFsNumber, int chip, int line) {
this.keyPrefix = keyPrefix;
this.header = header;
this.physicalPin = physicalPin;
this.deviceNumber = deviceNumber;
this.name = name;
this.modes = modes;
this.sysFsNumber = sysFsNumber;
this.chip = chip;
this.lineOffset = line;
}
/**
* Internal only attribute used by
* {@link com.diozero.internal.spi.AbstractDeviceFactory AbstractDeviceFactory}
* when provisioning GPIO devices
*
* @return the key prefix for this pin
*/
public String getKeyPrefix() {
return keyPrefix;
}
/**
* Get the name of the board header to which this pin is attached
*
* @return the header name to which this pin is attached
*/
public String getHeader() {
return header;
}
/**
* Get the physical header pin number
*
* @return the physical header pin number
*/
public int getPhysicalPin() {
return physicalPin;
}
/**
* Get the logical device GPIO number
*
* @return the logical device GPIO number
*/
public int getDeviceNumber() {
return deviceNumber;
}
/**
* Get sysfs number for this pin. This is typically the same as the GPIO /
* device number; can be different for PWM pins that are controlled via Linux
* sysfs
*
* @return the sysfs number for this pin
*/
public int getSysFsNumber() {
return sysFsNumber;
}
/**
* the Linux GPIO chip number for this GPIO as defined by the Linux GPIO
* character device; see /sys/gpiochip<n>, run gpiodetect
to
* list
*
* @return the GPIO chip number for this pin
*/
public int getChip() {
return chip;
}
/**
* Internal method - do not call
*
* @param chip the GPIO chip number for this pin
*/
public void setChip(int chip) {
this.chip = chip;
}
/**
* Get the line number offset for this GPIO on the GPIO chip - Linux GPIO
* character device; run gpioinfo <n>
to list
*
* @return the GPIO line offset
*/
public int getLineOffset() {
return lineOffset;
}
/**
* Internal method - do not call
*
* @param lineOffset the GPIO chip line offset number for this pin
*/
public void setLineOffset(int lineOffset) {
this.lineOffset = lineOffset;
}
/**
* Get the name of this pin
*
* @return the name of this pin
*/
public String getName() {
return name;
}
/**
* Get the set of valid {@link DeviceMode modes} for this pin
*
* @return the set of valid {@link DeviceMode modes} for this pin
*/
public Collection getModes() {
return modes;
}
/**
* Check if the specified {@link DeviceMode mode} is supported by this pin
*
* @param mode the {@link DeviceMode device mode} to check
* @return true if the device mode is supported
*/
public boolean isSupported(DeviceMode mode) {
return modes.contains(mode);
}
public boolean isDigitalInputSupported() {
return modes.contains(DeviceMode.DIGITAL_INPUT);
}
public boolean isDigitalOutputSupported() {
return modes.contains(DeviceMode.DIGITAL_OUTPUT);
}
public boolean isPwmOutputSupported() {
return modes.contains(DeviceMode.PWM_OUTPUT);
}
public boolean isAnalogInputSupported() {
return modes.contains(DeviceMode.ANALOG_INPUT);
}
public boolean isAnalogOutputSupported() {
return modes.contains(DeviceMode.ANALOG_OUTPUT);
}
@Override
public String toString() {
return "PinInfo [keyPrefix=" + keyPrefix + ", header=" + header + ", deviceNumber=" + deviceNumber
+ ", physicalPin=" + physicalPin + ", name=" + name + ", chip=" + chip + ", lineOffset=" + lineOffset
+ ", modes=" + modes + "]";
}
}