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