All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pi4j.plugin.linuxfs.internal.LinuxGpio Maven / Gradle / Ivy

The newest version!
package com.pi4j.plugin.linuxfs.internal;

/*-
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: PLUGIN   :: LinuxFS I/O Providers
 * FILENAME      :  LinuxGpio.java
 *
 * This file is part of the Pi4J project. More information about
 * this project can be found here:  https://pi4j.com/
 * **********************************************************************
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.pi4j.io.gpio.digital.DigitalState;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 

LinuxGpio class.

* * @see "https://www.kernel.org/doc/Documentation/gpio/sysfs.txt" * @author Robert Savage (http://www.savagehomeautomation.com) * @version $Id: $Id */ public class LinuxGpio { /** Constant DEFAULT_SYSTEM_PATH="/sys/class/gpio" */ public static String DEFAULT_SYSTEM_PATH = "/sys/class/gpio"; protected final String systemPath; protected final int address; protected final String pinPath; public enum Direction{ IN, OUT, UNKNOWN } public enum Edge{ NONE, RISING, FALLING, BOTH, UNKNOWN } /** *

Constructor for LinuxGpio.

* * @param systemPath a {@link java.lang.String} object. * @param address a int. */ public LinuxGpio(String systemPath, int address){ this.address = address; this.systemPath = systemPath; this.pinPath = Paths.get(systemPath, String.format("gpio%d", address)).toString(); } /** *

Constructor for LinuxGpio.

* * @param address a int. */ public LinuxGpio(int address){ this(DEFAULT_SYSTEM_PATH, address); } /** * Export GPIO pin by SoC address * * @throws java.io.IOException if any. */ public void export() throws IOException { var path = Paths.get(systemPath, "export"); Files.writeString(path, Integer.toString(address)); } /** *

unexport.

* * @throws java.io.IOException if any. */ public void unexport() throws IOException { var path = Paths.get(systemPath, "unexport"); Files.writeString(path, Integer.toString(address)); } /** *

isExported.

* * @return a boolean. * @throws java.io.IOException if any. */ public boolean isExported() throws IOException { return Files.exists(Paths.get(pinPath)); } /** *

isInterruptSupported.

* * @return a boolean. * @throws java.io.IOException if any. */ public boolean isInterruptSupported() throws IOException { return Files.exists(Paths.get(pinPath, "edge")); } /** *

direction.

* * @param direction a {@link LinuxGpio.Direction} object. * @throws java.io.IOException if any. */ public void direction(Direction direction) throws IOException { setDirection(direction); } /** *

setDirection.

* * @param direction a {@link LinuxGpio.Direction} object. * @throws java.io.IOException if any. */ public void setDirection(Direction direction) throws IOException { var path = Paths.get(pinPath, "direction"); Files.writeString(path, direction.name().toLowerCase()); } /** *

direction.

* * @return a {@link LinuxGpio.Direction} object. * @throws java.io.IOException if any. */ public Direction direction() throws IOException { return getDirection(); } /** *

getDirection.

* * @return a {@link LinuxGpio.Direction} object. * @throws java.io.IOException if any. */ public Direction getDirection() throws IOException { var path = Paths.get(pinPath, "direction"); switch(Files.readString(path).trim().toLowerCase()){ case "in": return Direction.IN; case "out": return Direction.OUT; default: return Direction.UNKNOWN; } } /** *

state.

* * @param state a {@link com.pi4j.io.gpio.digital.DigitalState} object. * @throws java.io.IOException if any. */ public void state(DigitalState state) throws IOException { setState(state); } /** *

setState.

* * @param state a {@link com.pi4j.io.gpio.digital.DigitalState} object. * @throws java.io.IOException if any. */ public void setState(DigitalState state) throws IOException { var path = Paths.get(pinPath,"value"); Files.writeString(path, (state.isHigh() ? "1" : "0")); } /** *

state.

* * @return a {@link com.pi4j.io.gpio.digital.DigitalState} object. * @throws java.io.IOException if any. */ public DigitalState state() throws IOException { return getState(); } /** *

getState.

* * @return a {@link com.pi4j.io.gpio.digital.DigitalState} object. * @throws java.io.IOException if any. */ public DigitalState getState() throws IOException { var path = Paths.get(pinPath,"value"); return DigitalState.parse(Files.readString(path).trim()); } /** *

interruptEdge.

* * @param edge a {@link LinuxGpio.Edge} object. * @throws java.io.IOException if any. */ public void interruptEdge(Edge edge) throws IOException { setInterruptEdge(edge); } /** *

setInterruptEdge.

* * @param edge a {@link LinuxGpio.Edge} object. * @throws java.io.IOException if any. */ public void setInterruptEdge(Edge edge) throws IOException { var path = Paths.get(pinPath, "edge"); Files.writeString(path, edge.name().toLowerCase()); } /** *

interruptEdge.

* * @return a {@link LinuxGpio.Edge} object. * @throws java.io.IOException if any. */ public Edge interruptEdge() throws IOException { return getInterruptEdge(); } /** *

getInterruptEdge.

* * @return a {@link LinuxGpio.Edge} object. * @throws java.io.IOException if any. */ public Edge getInterruptEdge() throws IOException { var path = Paths.get(pinPath, "edge"); switch(Files.readString(path).trim().toLowerCase()){ case "none": return Edge.NONE; case "rising": return Edge.RISING; case "falling": return Edge.FALLING; case "both": return Edge.BOTH; default: return Edge.UNKNOWN; } } /** *

activeLow.

* * @param enabled a boolean. * @throws java.io.IOException if any. */ public void activeLow(boolean enabled) throws IOException { setActiveLow(enabled); } /** *

setActiveLow.

* * @param enabled a boolean. * @throws java.io.IOException if any. */ public void setActiveLow(boolean enabled) throws IOException { var path = Paths.get(pinPath,"active_low"); Files.writeString(path, (enabled ? "1" : "0")); } /** *

activeLow.

* * @return a boolean. * @throws java.io.IOException if any. */ public boolean activeLow() throws IOException { return getActiveLow(); } /** *

getActiveLow.

* * @return a boolean. * @throws java.io.IOException if any. */ public boolean getActiveLow() throws IOException { var path = Paths.get(pinPath,"active_low"); return Files.readString(path).trim().equalsIgnoreCase("1"); } /** * Get Linux File System path for GPIO * @return Linux File System path for GPIO */ public String systemPath(){ return getSystemPath(); } /** * Get Linux File System path for GPIO * @return Linux File System path for GPIO */ public String getSystemPath(){ return this.systemPath; } /** * Get Linux File System path for this GPIO pin instance * @return Linux File System path for this GPIO pin instance */ public String pinPath(){ return getPinPath(); } /** * Get Linux File System path for this GPIO pin instance * @return Linux File System path for this GPIO pin instance */ public String getPinPath(){ return this.pinPath; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy