001package com.pi4j.jni;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  I2C.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
030import com.pi4j.util.NativeLibraryLoader;
031
032/**
033 * <h1>I2C Communication</h1>
034 * 
035 * <p>
036 * Set of native methods for interacting with i2c bus on RPi.
037 * </p>
038 * 
039 * <p>
040 * Note: The file descriptor (fd) returned is a standard Linux filehandle. You can use the standard
041 * read(), write(), etc. system calls on this filehandle as required. 
042 * </p>
043 * 
044 * @author Daniel Sendula 
045 */
046public class I2C {
047
048    // private constructor 
049    private I2C() {
050        // forbid object construction 
051    }
052    
053    static {
054        // Load the platform library
055        NativeLibraryLoader.load("pi4j", "libpi4j.so");
056    }
057
058    /**
059     * Opens linux file for r/w returning file handle.
060     * 
061     * @param device file name of device. For i2c should be /dev/i2c-0 or /dev/i2c-1 for first or second bus.
062     * @return file descriptor or i2c bus.
063     */
064    public static native int i2cOpen(String device);
065
066    /**
067     * Closes linux file.
068     * 
069     * @param fd file descriptor
070     */
071    public static native void i2cClose(int fd);
072
073    /**
074     * Writes one byte to i2c. It uses ioctl to define device address and then writes one byte.
075     * 
076     * @param fd file descriptor of i2c bus
077     * @param deviceAddress device address
078     * @param data byte to be written to the device
079     * @return result of operation. Zero if everything is OK, less than zero if there was an error.
080     */
081    public static native int i2cWriteByteDirect(int fd, int deviceAddress, byte data);
082
083    /**
084     * Writes several bytes to i2c. It uses ioctl to define device address and then writes number of bytes defined 
085     * in size argument.
086     * 
087     * @param fd file descriptor of i2c bus
088     * @param deviceAddress device address
089     * @param size number of bytes to be written 
090     * @param offset offset in buffer to read from
091     * @param buffer data buffer to be written
092     * @return result of operation. Zero if everything is OK, less than zero if there was an error.
093     */
094    public static native int i2cWriteBytesDirect(int fd, int deviceAddress, int size, int offset, byte[] buffer);
095
096    /**
097     * Writes one byte to i2c. It uses ioctl to define device address and then writes two bytes: address in
098     * the device itself and value.
099     * 
100     * @param fd file descriptor of i2c bus
101     * @param deviceAddress device address
102     * @param localAddress address in the device
103     * @param data byte to be written to the device
104     * @return result of operation. Zero if everything is OK, less than zero if there was an error.
105     */
106    public static native int i2cWriteByte(int fd, int deviceAddress, int localAddress, byte data);
107
108    /**
109     * Writes several bytes to i2c. It uses ioctl to define device address and then writes number of bytes defined 
110     * in size argument plus one.
111     * 
112     * @param fd file descriptor of i2c bus
113     * @param deviceAddress device address
114     * @param localAddress address in the device
115     * @param size number of bytes to be written 
116     * @param offset offset in buffer to read from
117     * @param buffer data buffer to be written
118     * @return result of operation. Zero if everything is OK, less than zero if there was an error.
119     */
120    public static native int i2cWriteBytes(int fd, int deviceAddress, int localAddress, int size, int offset, byte[] buffer);
121
122    /**
123     * Reads one byte from i2c device. It uses ioctl to define device address and then reads one byte.
124     * 
125     * @param fd file descriptor of i2c bus
126     * @param deviceAddress device address
127     * @return positive number (or zero) to 255 if read was successful. Negative number if reading failed.
128     */
129    public static native int i2cReadByteDirect(int fd, int deviceAddress);
130
131    /**
132     * Reads more bytes from i2c device. It uses ioctl to define device address and then reads
133     * size number of bytes.
134     * 
135     * @param fd file descriptor of i2c bus
136     * @param deviceAddress device address
137     * @param localAddress address in the device
138     * @param size number of bytes to be read
139     * @param offset offset in buffer to stored read data
140     * @param buffer buffer for data to be written to
141     * @return number of bytes read or negative number if reading failed.
142     */
143    public static native int i2cReadBytesDirect(int fd, int deviceAddress, int size, int offset, byte[] buffer);
144
145    /**
146     * Reads one byte from i2c device. It uses ioctl to define device address, writes addres in device and then reads
147     * one byte.
148     * 
149     * @param fd file descriptor of i2c bus
150     * @param deviceAddress device address
151     * @param localAddress address in the device
152     * @return positive number (or zero) to 255 if read was successful. Negative number if reading failed.
153     */
154    public static native int i2cReadByte(int fd, int deviceAddress, int localAddress);
155
156    /**
157     * Reads more bytes from i2c device. It uses ioctl to define device address, writes addres in device and then reads
158     * size number of bytes.
159     * 
160     * @param fd file descriptor of i2c bus
161     * @param deviceAddress device address
162     * @param localAddress address in the device
163     * @param size number of bytes to be read
164     * @param offset offset in buffer to stored read data
165     * @param buffer buffer for data to be written to
166     * @return number of bytes read or negative number if reading failed.
167     */
168    public static native int i2cReadBytes(int fd, int deviceAddress, int localAddress, int size, int offset, byte[] buffer);
169}