📄 u2cwrapper.java
字号:
package i2c;
/**
* <P>Main class for communication with U2C-Devices. The Java Native Interface
* is used to communicate with the wrapper library.</p>
*
* <hr>
* <p>Copyright (c) 2006, Stefan Engelke (contact@stefanengelke.de)<br>
* All rights reserved.</p>
*
* <p>Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:</p>
*
* <p>* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.</p>
*
* <p>* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.</p>
*
* <p>* Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.</p>
*
* <p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.</p>
*
* @author Stefan Engelke
* @version 1.0
*/
public class U2CWrapper {
// I2C bus frequency values:
public final static int U2C_I2C_FREQ_FAST = 0,
U2C_I2C_FREQ_STD = 1,
U2C_I2C_FREQ_83KHZ = 2,
U2C_I2C_FREQ_71KHZ = 3,
U2C_I2C_FREQ_62KHZ = 4,
U2C_I2C_FREQ_50KHZ = 6,
U2C_I2C_FREQ_25KHZ = 16,
U2C_I2C_FREQ_10KHZ = 46,
U2C_I2C_FREQ_5KHZ = 96,
U2C_I2C_FREQ_2KHZ = 242;
// SPI bus frequency values:
public final static int U2C_SPI_FREQ_200KHZ = 0,
U2C_SPI_FREQ_100KHZ = 1,
U2C_SPI_FREQ_83KHZ = 2,
U2C_SPI_FREQ_71KHZ = 3,
U2C_SPI_FREQ_62KHZ = 4,
U2C_SPI_FREQ_50KHZ = 6,
U2C_SPI_FREQ_25KHZ = 16,
U2C_SPI_FREQ_10KHZ = 46,
U2C_SPI_FREQ_5KHZ = 96,
U2C_SPI_FREQ_2KHZ = 242;
// Line state:
public final static int LS_RELEASED = 1,
LS_DROPPED_BY_I2C_BRIDGE = 2,
LS_DROPPED_BY_SLAVE=3,
LS_RAISED_BY_I2C_BRIDGE=4;
private static boolean dllLoaded=false;
// --------------------------------------------------------------------------
// I2CBridge Board Initialization Routines
/**
* Checks how many U2C-12 devices are currently attached.
* @return The number of the U2C-12 devices detected on current computer.
*/
public static byte getDeviceCount() {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
return getDeviceCountN();
}
public static native byte getDeviceCountN();
/**
* Retrieves the Serial Number of the current device. This is unique Serial
* Number. It can be used to identify device when you are using a number of
* U2C-12 devices simultaneously.
* @param device U2C-12 device that will be checked.
* @return Serial Number.
*/
public static long getSerialNum(U2CDevice device) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
int status=getSerialNumN(device);
U2CException.checkDeviceStatus(status);
return device.getSerial();
}
private static native int getSerialNumN(U2CDevice device);
/**
* Checks whether the given device reference is currently attached to the USB
* and can be used by SW.
* @param device U2C-12 device that will be checked.
* @return Boolean, whether the device is present or not.
*/
public static boolean isDeviceValid(U2CDevice device) throws U2CException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
int status=isDeviceValidN(device);
return status==U2CException.U2C_SUCCESS;
}
private static native int isDeviceValidN(U2CDevice device);
/**
* Opens a U2C-12 device.
* @param nDevice The device number to open.
* @return U2CDevice object.
*/
public static U2CDevice openDevice(int nDevice) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
U2CDevice device=new U2CDevice();
int status=openDeviceN(nDevice,device);
U2CException.checkDeviceStatus(status);
return device;
}
private static native int openDeviceN(int nDevice,U2CDevice device);
/**
* Opens the U2C-12 device with specified Serial Number. This is a unique
* Serial Number. It can be used to identify device when you are using a
* number of U2C-12 devices simultaneously.
* @param nSerialNum The Serial Number of the device to open.
* @return U2CDevice object.
*/
public static U2CDevice openDeviceBySerialNum(long nSerialNum) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
U2CDevice device=new U2CDevice();
int status=openDeviceBySerialNumN(nSerialNum,device);
U2CException.checkDeviceStatus(status);
return device;
}
private static native int openDeviceBySerialNumN(long nSerialNum,U2CDevice device);
/**
* Closes a open device.
* @param device U2C-12 device to close.
*/
public static void closeDevice(U2CDevice device) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
int status=closeDeviceN(device);
U2CException.checkDeviceStatus(status);
}
private static native int closeDeviceN(U2CDevice device);
/**
* Retrieves the version of the firmware
* currently loaded into the U2C-12 device
* @param device U2C-12 device to obtain the version firmware version from.
* @return Firmware version number.
*/
public static U2CVersionInfo getFirmwareVersion(U2CDevice device) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
U2CVersionInfo ver=new U2CVersionInfo();
int status=getFirmwareVersionN(device,ver);
U2CException.checkDeviceStatus(status);
return ver;
}
private static native int getFirmwareVersionN(U2CDevice device,U2CVersionInfo ver);
/**
* Retrieves the version of the driver used to communicate with U2C-12 device.
* @param device U2C-12 device to obtain the version of the driver.
* @return Driver version number.
*/
public static U2CVersionInfo getDriverVersion(U2CDevice device) throws U2CDeviceException {
if (!dllLoaded) {
System.loadLibrary("U2CWrapper");
dllLoaded=true;
}
U2CVersionInfo ver=new U2CVersionInfo();
int status=getDriverVersionN(device,ver);
U2CException.checkDeviceStatus(status);
return ver;
}
private static native int getDriverVersionN(U2CDevice device,U2CVersionInfo ver);
/**
* Retrieves the version of the I2CBrdg.dll dynamic link library or shared
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -