001package com.pi4j.wiringpi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  GpioUtil.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 - 2013 Pi4J
015 * %%
016 * Licensed under the Apache License, Version 2.0 (the "License");
017 * you may not use this file except in compliance with the License.
018 * You may obtain a copy of the License at
019 * 
020 *      http://www.apache.org/licenses/LICENSE-2.0
021 * 
022 * Unless required by applicable law or agreed to in writing, software
023 * distributed under the License is distributed on an "AS IS" BASIS,
024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025 * See the License for the specific language governing permissions and
026 * limitations under the License.
027 * #L%
028 */
029
030
031import com.pi4j.util.NativeLibraryLoader;
032
033/**
034 * <p>This utility class is provided to export, unexport, and manipulate pin direction.</p>
035 * 
036 * <p>
037 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
038 * the following system libraries:
039 * <ul>
040 * <li>pi4j</li>
041 * <li>wiringPi</li>
042 * </ul>
043 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
044 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>)
045 * </blockquote>
046 * </p>
047 * 
048 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
049 * @see <a
050 *      href="https://projects.drogon.net/raspberry-pi/wiringpi/">https://projects.drogon.net/raspberry-pi/wiringpi/</a>
051 * @author Robert Savage (<a
052 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
053 */
054public class GpioUtil {
055    // private constructor 
056    private GpioUtil() {
057        // forbid object construction 
058    }
059    
060    static {
061        // Load the platform library
062        NativeLibraryLoader.load("pi4j", "libpi4j.so");
063    }
064
065    /**
066     * <p>GPIO PIN DIRECTION</p>
067     * <p>
068     * GPIO pin constant for IN direction for reading pin states
069     * </p>
070     * 
071     * @see #export(int,int)
072     * @see #setDirection(int,int)
073     * @see #getDirection(int)
074     */
075    public static final int DIRECTION_IN = 0;
076
077    /**
078     * <p>GPIO PIN DIRECTION</p>
079     * <p>
080     * GPIO pin constant for OUT direction for writing digital pin states (0/1).
081     * </p>
082     * 
083     * @see #export(int,int)
084     * @see #setDirection(int,int)
085     * @see #getDirection(int)
086     */
087    public static final int DIRECTION_OUT = 1;
088
089    /**
090     * <p>GPIO PIN EDGE DETECTION</p>
091     * <p>
092     * This constant is provided as an edge detection mode for use with the 'edge' method. This
093     * constants instructs the edge detection to be disabled.
094     * </p>
095     * 
096     * @see #setEdgeDetection(int,int)
097     */
098    public static final int EDGE_NONE = 0;
099
100    /**
101     * <p>GPIO PIN EDGE DETECTION</p>
102     * <p>
103     * This constant is provided as an edge detection mode for use with the 'edge' method. This
104     * constants instructs the edge detection to only look for rising and falling pins states; pins
105     * changing from LOW to HIGH or HIGH to LOW.
106     * </p>
107     * 
108     * @see #setEdgeDetection(int,int)
109     */
110    public static final int EDGE_BOTH = 1;
111
112    /**
113     * <p>GPIO PIN EDGE DETECTION</p>
114     * <p>
115     * This constant is provided as an edge detection mode for use with the 'edge' method. This
116     * constants instructs the edge detection to only look for rising pins states; pins changing
117     * from LOW to HIGH.
118     * </p>
119     * 
120     * @see #setEdgeDetection(int,int)
121     */
122    public static final int EDGE_RISING = 2;
123
124    /**
125     * <p>GPIO PIN EDGE DETECTION</p>
126     * <p>
127     * This constant is provided as an edge detection mode for use with the 'edge' method. This
128     * constants instructs the edge detection to only look for falling pins states; pins changing
129     * from HIGH to LOW.
130     * </p>
131     * 
132     * @see #setEdgeDetection(int,int)
133     */
134    public static final int EDGE_FALLING = 3;
135
136    /**
137     * <p>
138     * This method will export the selected GPIO pin.
139     * </p>
140     * <p>
141     * This method required root permissions access.
142     * </p>
143     * 
144     * @see #DIRECTION_IN
145     * @see #DIRECTION_OUT
146     * 
147     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
148     * @param direction
149     */
150    public static native void export(int pin, int direction) throws RuntimeException;
151
152    /**
153     * <p>This method will unexport the selected GPIO pin.</p>
154     * <p>This method required root permissions access.</p>
155     * 
156     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
157     */
158    public static native void unexport(int pin) throws RuntimeException;
159
160    /**
161     * <p>This method determines if the requested GPIO pin is already exported.</p>
162     * 
163     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
164     * @return A return value of '0' represents that the pin is not exported. </br> A return value
165     *         of '1' represents that the pin is exported.
166     */
167    public static native boolean isExported(int pin) throws RuntimeException;
168
169    /**
170     * <p>This method will set the selected GPIO pin's edge detection. Edge detection instructs when
171     * the hardware GPIO changes raise interrupts on the system.</p>
172     * <p>
173     * NOTE: Calling this method will automatically export the pin and set the pin direction to
174     * INPUT.</br> This method required root permissions access.
175     * </p>
176     * 
177     * @see #EDGE_NONE
178     * @see #EDGE_BOTH
179     * @see #EDGE_RISING
180     * @see #EDGE_FALLING
181     * 
182     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
183     * @param edge The edge condition to detect: none, rising, falling, or both. </br>The following
184     *            constants are provided for use with this parameter:
185     *            <ul>
186     *            <li>EDGE_NONE</li>
187     *            <li>EDGE_BOTH</li>
188     *            <li>EDGE_RISING</li>
189     *            <li>EDGE_FALLING</li>
190     *            </ul>
191     * @return A return value of '0' represents success. Errors are returned as negative numbers.
192     */
193    public static native boolean setEdgeDetection(int pin, int edge) throws RuntimeException;
194
195    /**
196     * <p>This method will get the selected GPIO pin's edge detection setting. Edge detection instructs
197     * when the hardware GPIO changes raise interrupts on the system.
198     * </p>
199     * 
200     * @see #EDGE_NONE
201     * @see #EDGE_BOTH
202     * @see #EDGE_RISING
203     * @see #EDGE_FALLING
204     * 
205     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
206     * @return The edge condition detected on the selected pin: none, rising, falling, or both.
207     *         </br>The following constants are provided for use with this parameter:
208     *         <ul>
209     *         <li>EDGE_NONE</li>
210     *         <li>EDGE_BOTH</li>
211     *         <li>EDGE_RISING</li>
212     *         <li>EDGE_FALLING</li>
213     *         </ul>
214     */
215    public static native int getEdgeDetection(int pin) throws RuntimeException;
216
217    /**
218     * <p>This method will set the selected GPIO pin's export direction.</p>
219     * 
220     * @see #DIRECTION_IN
221     * @see #DIRECTION_OUT
222     * 
223     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
224     * @param direction 
225     *            The export direction to apply: IN, OUT. </br>The following constants are provided
226     *            for use with this parameter:
227     *            <ul>
228     *            <li>DIRECTION_IN</li>
229     *            <li>DIRECTION_OUT</li>
230     *            </ul>
231     * @return A return value of '0' represents success. Errors are returned as negative numbers.
232     */
233    public static native boolean setDirection(int pin, int direction) throws RuntimeException;
234
235    /**
236     * <p>
237     * This method will get the selected GPIO pin's export direction.
238     * </p>
239     * 
240     * @see #DIRECTION_IN
241     * @see #DIRECTION_OUT
242     * 
243     * @param pin GPIO pin number (not header pin number; not wiringPi pin number)
244     * @return The GPIO pin's configured export direction is returned: IN (0), OUT (1). </br>The
245     *         following constants are provided for use with this parameter:
246     *         <ul>
247     *         <li>DIRECTION_IN</li>
248     *         <li>DIRECTION_OUT</li>
249     *         </ul>
250     */
251    public static native int getDirection(int pin) throws RuntimeException;
252}