2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-07-16 16:24:44 -04:00
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
2016-05-20 12:07:40 -04:00
|
|
|
import java.nio.ByteOrder;
|
2014-07-16 16:24:44 -04:00
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
|
|
|
|
import edu.wpi.first.wpilibj.hal.HAL;
|
2014-07-16 16:24:44 -04:00
|
|
|
import edu.wpi.first.wpilibj.hal.SPIJNI;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Represents a SPI bus port.
|
2014-07-16 16:24:44 -04:00
|
|
|
*/
|
|
|
|
|
public class SPI extends SensorBase {
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
public enum Port {
|
|
|
|
|
kOnboardCS0(0), kOnboardCS1(1), kOnboardCS2(2), kOnboardCS3(3), kMXP(4);
|
|
|
|
|
|
2016-07-13 23:39:58 -07:00
|
|
|
@SuppressWarnings("MemberName")
|
|
|
|
|
public int value;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-13 23:39:58 -07:00
|
|
|
private Port(int value) {
|
|
|
|
|
this.value = value;
|
2014-07-16 16:24:44 -04:00
|
|
|
}
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
private static int devices = 0;
|
|
|
|
|
|
2017-05-09 12:12:46 -07:00
|
|
|
private int m_port;
|
2016-05-20 12:07:40 -04:00
|
|
|
private int m_bitOrder;
|
|
|
|
|
private int m_clockPolarity;
|
|
|
|
|
private int m_dataOnTrailing;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Constructor.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
|
|
|
|
* @param port the physical SPI port
|
|
|
|
|
*/
|
|
|
|
|
public SPI(Port port) {
|
2016-07-13 23:39:58 -07:00
|
|
|
m_port = (byte) port.value;
|
2015-06-25 15:07:55 -04:00
|
|
|
devices++;
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
SPIJNI.spiInitialize(m_port);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
HAL.report(tResourceType.kResourceType_SPI, devices);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Free the resources used by this object.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public void free() {
|
|
|
|
|
SPIJNI.spiClose(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure the rate of the generated clock signal. The default value is 500,000 Hz. The maximum
|
|
|
|
|
* value is 4,000,000 Hz.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
|
|
|
|
* @param hz The clock rate in Hertz.
|
|
|
|
|
*/
|
|
|
|
|
public final void setClockRate(int hz) {
|
|
|
|
|
SPIJNI.spiSetSpeed(m_port, hz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure the order that bits are sent and received on the wire to be most significant bit
|
|
|
|
|
* first.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setMSBFirst() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_bitOrder = 1;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure the order that bits are sent and received on the wire to be least significant bit
|
|
|
|
|
* first.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setLSBFirst() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_bitOrder = 0;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure the clock output line to be active low. This is sometimes called clock polarity high
|
|
|
|
|
* or clock idle high.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setClockActiveLow() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_clockPolarity = 1;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure the clock output line to be active high. This is sometimes called clock polarity low
|
|
|
|
|
* or clock idle low.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setClockActiveHigh() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_clockPolarity = 0;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure that the data is stable on the falling edge and the data changes on the rising edge.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setSampleDataOnFalling() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_dataOnTrailing = 1;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Configure that the data is stable on the rising edge and the data changes on the falling edge.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public final void setSampleDataOnRising() {
|
2016-05-20 12:07:40 -04:00
|
|
|
m_dataOnTrailing = 0;
|
|
|
|
|
SPIJNI.spiSetOpts(m_port, m_bitOrder, m_dataOnTrailing, m_clockPolarity);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the chip select line to be active high.
|
|
|
|
|
*/
|
|
|
|
|
public final void setChipSelectActiveHigh() {
|
2015-11-01 09:11:52 -08:00
|
|
|
SPIJNI.spiSetChipSelectActiveHigh(m_port);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the chip select line to be active low.
|
|
|
|
|
*/
|
|
|
|
|
public final void setChipSelectActiveLow() {
|
2015-11-01 09:11:52 -08:00
|
|
|
SPIJNI.spiSetChipSelectActiveLow(m_port);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Write data to the slave device. Blocks until there is space in the output FIFO.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>If not running in output only mode, also saves the data received on the MISO input during
|
|
|
|
|
* the transfer into the receive FIFO.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public int write(byte[] dataToSend, int size) {
|
|
|
|
|
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(size);
|
|
|
|
|
dataToSendBuffer.put(dataToSend);
|
2015-11-06 11:12:00 -08:00
|
|
|
return SPIJNI.spiWrite(m_port, dataToSendBuffer, (byte) size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Write data to the slave device. Blocks until there is space in the output FIFO.
|
2015-11-06 11:12:00 -08:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>If not running in output only mode, also saves the data received on the MISO input during
|
|
|
|
|
* the transfer into the receive FIFO.
|
2015-11-06 11:12:00 -08:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param dataToSend The buffer containing the data to send. Must be created using
|
|
|
|
|
* ByteBuffer.allocateDirect().
|
2015-11-06 11:12:00 -08:00
|
|
|
*/
|
|
|
|
|
public int write(ByteBuffer dataToSend, int size) {
|
2016-05-20 12:07:40 -04:00
|
|
|
if (!dataToSend.isDirect()) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("must be a direct buffer");
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
if (dataToSend.capacity() < size) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("buffer is too small, must be at least " + size);
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-11-06 11:12:00 -08:00
|
|
|
return SPIJNI.spiWrite(m_port, dataToSend, (byte) size);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read a word from the receive FIFO.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>Waits for the current transfer to complete if the receive FIFO is empty.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>If the receive FIFO is empty, there is no active transfer, and initiate is false, errors.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param initiate If true, this function pushes "0" into the transmit buffer and initiates a
|
|
|
|
|
* transfer. If false, this function assumes that data is already in the receive
|
|
|
|
|
* FIFO from a previous write.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public int read(boolean initiate, byte[] dataReceived, int size) {
|
2016-05-20 12:07:40 -04:00
|
|
|
final int retVal;
|
2015-06-25 15:07:55 -04:00
|
|
|
ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(size);
|
|
|
|
|
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(size);
|
2016-05-20 12:07:40 -04:00
|
|
|
if (initiate) {
|
2015-06-25 15:07:55 -04:00
|
|
|
retVal = SPIJNI.spiTransaction(m_port, dataToSendBuffer, dataReceivedBuffer, (byte) size);
|
2016-05-20 12:07:40 -04:00
|
|
|
} else {
|
2015-06-25 15:07:55 -04:00
|
|
|
retVal = SPIJNI.spiRead(m_port, dataReceivedBuffer, (byte) size);
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
dataReceivedBuffer.get(dataReceived);
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 11:12:00 -08:00
|
|
|
/**
|
|
|
|
|
* Read a word from the receive FIFO.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>Waits for the current transfer to complete if the receive FIFO is empty.
|
2015-11-06 11:12:00 -08:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>If the receive FIFO is empty, there is no active transfer, and initiate is false, errors.
|
2015-11-06 11:12:00 -08:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param initiate If true, this function pushes "0" into the transmit buffer and initiates
|
|
|
|
|
* a transfer. If false, this function assumes that data is already in the
|
|
|
|
|
* receive FIFO from a previous write.
|
|
|
|
|
* @param dataReceived The buffer to be filled with the received data. Must be created using
|
|
|
|
|
* ByteBuffer.allocateDirect().
|
|
|
|
|
* @param size The length of the transaction, in bytes
|
2015-11-06 11:12:00 -08:00
|
|
|
*/
|
|
|
|
|
public int read(boolean initiate, ByteBuffer dataReceived, int size) {
|
2016-05-20 12:07:40 -04:00
|
|
|
if (!dataReceived.isDirect()) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("must be a direct buffer");
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
if (dataReceived.capacity() < size) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("buffer is too small, must be at least " + size);
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-11-06 11:12:00 -08:00
|
|
|
if (initiate) {
|
|
|
|
|
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(size);
|
|
|
|
|
return SPIJNI.spiTransaction(m_port, dataToSendBuffer, dataReceived, (byte) size);
|
|
|
|
|
}
|
|
|
|
|
return SPIJNI.spiRead(m_port, dataReceived, (byte) size);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Perform a simultaneous read/write transaction with the device.
|
2015-06-25 15:07:55 -04:00
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param dataToSend The data to be written out to the device
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param dataReceived Buffer to receive data from the device
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param size The length of the transaction, in bytes
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
|
|
|
|
public int transaction(byte[] dataToSend, byte[] dataReceived, int size) {
|
|
|
|
|
ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(size);
|
|
|
|
|
dataToSendBuffer.put(dataToSend);
|
|
|
|
|
ByteBuffer dataReceivedBuffer = ByteBuffer.allocateDirect(size);
|
2015-11-06 11:12:00 -08:00
|
|
|
int retVal = SPIJNI.spiTransaction(m_port, dataToSendBuffer, dataReceivedBuffer, (byte) size);
|
2015-06-25 15:07:55 -04:00
|
|
|
dataReceivedBuffer.get(dataReceived);
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
2015-11-06 11:12:00 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform a simultaneous read/write transaction with the device
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param dataToSend The data to be written out to the device. Must be created using
|
|
|
|
|
* ByteBuffer.allocateDirect().
|
|
|
|
|
* @param dataReceived Buffer to receive data from the device. Must be created using
|
|
|
|
|
* ByteBuffer.allocateDirect().
|
|
|
|
|
* @param size The length of the transaction, in bytes
|
2015-11-06 11:12:00 -08:00
|
|
|
*/
|
|
|
|
|
public int transaction(ByteBuffer dataToSend, ByteBuffer dataReceived, int size) {
|
2016-05-20 12:07:40 -04:00
|
|
|
if (!dataToSend.isDirect()) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("dataToSend must be a direct buffer");
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
if (dataToSend.capacity() < size) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("dataToSend is too small, must be at least " + size);
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
if (!dataReceived.isDirect()) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("dataReceived must be a direct buffer");
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
|
|
|
|
if (dataReceived.capacity() < size) {
|
2015-11-06 11:12:00 -08:00
|
|
|
throw new IllegalArgumentException("dataReceived is too small, must be at least " + size);
|
2016-05-20 12:07:40 -04:00
|
|
|
}
|
2015-11-06 11:12:00 -08:00
|
|
|
return SPIJNI.spiTransaction(m_port, dataToSend, dataReceived, (byte) size);
|
|
|
|
|
}
|
2015-11-22 11:50:49 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the accumulator.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* @param period Time between reads
|
|
|
|
|
* @param cmd SPI command to send to request data
|
|
|
|
|
* @param xferSize SPI transfer size, in bytes
|
|
|
|
|
* @param validMask Mask to apply to received data for validity checking
|
|
|
|
|
* @param validValue After validMask is applied, required matching value for validity checking
|
|
|
|
|
* @param dataShift Bit shift to apply to received data to get actual data value
|
|
|
|
|
* @param dataSize Size (in bits) of data field
|
|
|
|
|
* @param isSigned Is data field signed?
|
|
|
|
|
* @param bigEndian Is device big endian?
|
2015-11-22 11:50:49 -08:00
|
|
|
*/
|
2016-05-20 12:07:40 -04:00
|
|
|
public void initAccumulator(double period, int cmd, int xferSize,
|
|
|
|
|
int validMask, int validValue,
|
|
|
|
|
int dataShift, int dataSize,
|
|
|
|
|
boolean isSigned, boolean bigEndian) {
|
|
|
|
|
SPIJNI.spiInitAccumulator(m_port, (int) (period * 1.0e6), cmd,
|
|
|
|
|
(byte) xferSize, validMask, validValue, (byte) dataShift,
|
|
|
|
|
(byte) dataSize, isSigned, bigEndian);
|
2015-11-22 11:50:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Frees the accumulator.
|
|
|
|
|
*/
|
|
|
|
|
public void freeAccumulator() {
|
|
|
|
|
SPIJNI.spiFreeAccumulator(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the accumulator to zero.
|
|
|
|
|
*/
|
|
|
|
|
public void resetAccumulator() {
|
|
|
|
|
SPIJNI.spiResetAccumulator(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the center value of the accumulator.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>The center value is subtracted from each value before it is added to the accumulator. This
|
2015-11-22 11:50:49 -08:00
|
|
|
* is used for the center value of devices like gyros and accelerometers to make integration work
|
|
|
|
|
* and to take the device offset into account when integrating.
|
|
|
|
|
*/
|
|
|
|
|
public void setAccumulatorCenter(int center) {
|
|
|
|
|
SPIJNI.spiSetAccumulatorCenter(m_port, center);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the accumulator's deadband.
|
|
|
|
|
*/
|
|
|
|
|
public void setAccumulatorDeadband(int deadband) {
|
|
|
|
|
SPIJNI.spiSetAccumulatorDeadband(m_port, deadband);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the last value read by the accumulator engine.
|
|
|
|
|
*/
|
|
|
|
|
public int getAccumulatorLastValue() {
|
|
|
|
|
return SPIJNI.spiGetAccumulatorLastValue(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the accumulated value.
|
|
|
|
|
*
|
|
|
|
|
* @return The 64-bit value accumulated since the last Reset().
|
|
|
|
|
*/
|
|
|
|
|
public long getAccumulatorValue() {
|
|
|
|
|
return SPIJNI.spiGetAccumulatorValue(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the number of accumulated values.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>Read the count of the accumulated values since the accumulator was last Reset().
|
2015-11-22 11:50:49 -08:00
|
|
|
*
|
|
|
|
|
* @return The number of times samples from the channel were accumulated.
|
|
|
|
|
*/
|
|
|
|
|
public int getAccumulatorCount() {
|
|
|
|
|
return SPIJNI.spiGetAccumulatorCount(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the average of the accumulated value.
|
|
|
|
|
*
|
|
|
|
|
* @return The accumulated average value (value / count).
|
|
|
|
|
*/
|
|
|
|
|
public double getAccumulatorAverage() {
|
|
|
|
|
return SPIJNI.spiGetAccumulatorAverage(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the accumulated value and the number of accumulated values atomically.
|
|
|
|
|
*
|
2016-05-20 12:07:40 -04:00
|
|
|
* <p>This function reads the value and count atomically. This can be used for averaging.
|
2015-11-22 11:50:49 -08:00
|
|
|
*
|
|
|
|
|
* @param result AccumulatorResult object to store the results in.
|
|
|
|
|
*/
|
|
|
|
|
public void getAccumulatorOutput(AccumulatorResult result) {
|
|
|
|
|
if (result == null) {
|
|
|
|
|
throw new IllegalArgumentException("Null parameter `result'");
|
|
|
|
|
}
|
|
|
|
|
ByteBuffer value = ByteBuffer.allocateDirect(8);
|
|
|
|
|
// set the byte order
|
|
|
|
|
value.order(ByteOrder.LITTLE_ENDIAN);
|
2016-07-12 10:45:14 -07:00
|
|
|
ByteBuffer count = ByteBuffer.allocateDirect(8);
|
2015-11-22 11:50:49 -08:00
|
|
|
// set the byte order
|
|
|
|
|
count.order(ByteOrder.LITTLE_ENDIAN);
|
2016-07-12 10:45:14 -07:00
|
|
|
SPIJNI.spiGetAccumulatorOutput(m_port, value.asLongBuffer(), count.asLongBuffer());
|
2015-11-22 11:50:49 -08:00
|
|
|
result.value = value.asLongBuffer().get(0);
|
2016-07-12 10:45:14 -07:00
|
|
|
result.count = count.asLongBuffer().get(0);
|
2015-11-22 11:50:49 -08:00
|
|
|
}
|
2014-07-16 16:24:44 -04:00
|
|
|
}
|