001package com.pi4j.wiringpi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  Shift.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 - 2019 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.util.NativeLibraryLoader;
034
035/**
036 * <h1>WiringPi Shift Library</h1>
037 *
038 * <p>
039 * WiringPi includes a shift library which more or less mimics the one in the Arduino system. This
040 * allows you to shift 8-bit data values out of the Pi, or into the Pi from devices such as
041 * shift-registers (e.g. 74,595) and so-on, although it can also be used in some bit-banging
042 * scenarios.
043 * </p>
044 *
045 * <p>
046 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
047 * the following system libraries:
048 * <ul>
049 * <li>pi4j</li>
050 * <li>wiringPi</li>
051 * </ul>
052 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
053 * Gordon Henderson @ <a href="http://wiringpi.com">http://wiringpi.com</a>)
054 * </blockquote>
055 * </p>
056 *
057 * @see <a href="https://www.pi4j.com/">https://www.pi4j.com/</a>
058 * @see <a
059 *      href="http://wiringpi.com/reference/shift-library/">http://wiringpi.com/reference/shift-library/</a>
060 * @author Robert Savage (<a
061 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
062 */
063public class Shift {
064
065    public static final int LSBFIRST = 0;
066    public static final int MSBFIRST = 1;
067
068    // private constructor
069    private Shift() {
070        // forbid object construction
071    }
072
073    static {
074        // Load the platform library
075        NativeLibraryLoader.load("libpi4j.so");
076    }
077
078    /**
079     * <p>uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order);</p>
080     *
081     * <p>
082     * This shifts an 8-bit data value in with the data appearing on the dPin and the clock being
083     * sent out on the cPin. Order is either LSBFIRST or MSBFIRST. The data is sampled after the
084     * cPin goes high. (So cPin high, sample data, cPin low, repeat for 8 bits) The 8-bit value is
085     * returned by the function.
086     * </p>
087     *
088     * @see <a
089     *      href="http://wiringpi.com/reference/shift-library/">http://wiringpi.com/reference/shift-library/</a>
090     * @param dPin data pin
091     * @param cPin clock pin
092     * @param order order is either LSBFIRST or MSBFIRST
093     * @return <p>
094     *         The 8-bit shifted value is returned by the function.
095     *         </p>
096     */
097    public static native byte shiftIn(byte dPin, byte cPin, byte order);
098
099    /**
100     * <p>void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val);</p>
101     *
102     * <p>
103     * The shifts an 8-bit data value val out with the data being sent out on dPin and the clock
104     * being sent out on the cPin. order is as above. Data is clocked out on the rising or falling
105     * edge; ie. dPin is set, then cPin is taken high then low and repeated for the 8 bits.
106     * </p>
107     *
108     * @see <a
109     *      href="http://wiringpi.com/reference/shift-library/">http://wiringpi.com/reference/shift-library/</a>
110     * @param dPin data pin
111     * @param cPin clock pin
112     * @param order order is either LSBFIRST or MSBFIRST
113     * @param val 8-bit data value
114     */
115    public static native void shiftOut(byte dPin, byte cPin, byte order, byte val);
116}