Skip to content

Commit

Permalink
[wpiutil] Add last value and change detection
Browse files Browse the repository at this point in the history
Update() checks/updates the last value and appends only if changed.
GetLastValue() gets the last value.
  • Loading branch information
PeterJohnson committed Jun 22, 2024
1 parent 2ff7033 commit 1227b7b
Show file tree
Hide file tree
Showing 18 changed files with 1,722 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.util.datalog;

import java.util.Arrays;

/** Log array of boolean values. */
public class BooleanArrayLogEntry extends DataLogEntry {
/** The data type for boolean array values. */
Expand Down Expand Up @@ -71,4 +73,58 @@ public void append(boolean[] value, long timestamp) {
public void append(boolean[] value) {
m_log.appendBooleanArray(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(boolean[] value, long timestamp) {
if (!equalsLast(value)) {
copyToLast(value);
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(boolean[] value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or null if none.
*/
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
public synchronized boolean[] getLastValue() {
if (m_lastValue == null) {
return null;
}
return Arrays.copyOf(m_lastValue, m_lastValueLen);
}

private boolean equalsLast(boolean[] value) {
if (m_lastValue == null || m_lastValueLen != value.length) {
return false;
}
return Arrays.equals(m_lastValue, 0, value.length, value, 0, value.length);
}

private void copyToLast(boolean[] value) {
if (m_lastValue == null || m_lastValue.length < value.length) {
m_lastValue = Arrays.copyOf(value, value.length);
} else {
System.arraycopy(value, 0, m_lastValue, 0, value.length);
}
m_lastValueLen = value.length;
}

private boolean[] m_lastValue;
private int m_lastValueLen;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,39 @@ public void append(boolean value, long timestamp) {
public void append(boolean value) {
m_log.appendBoolean(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(boolean value, long timestamp) {
if (!m_hasLastValue || m_lastValue != value) {
m_lastValue = value;
m_hasLastValue = true;
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(boolean value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or false if none.
*/
public synchronized boolean getLastValue() {
return m_lastValue;
}

private boolean m_hasLastValue;
private boolean m_lastValue;
}
12 changes: 6 additions & 6 deletions wpiutil/src/main/java/edu/wpi/first/util/datalog/DataLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ public void setMetadata(int entry, String metadata) {
setMetadata(entry, metadata, 0);
}

@Override
public void close() {
DataLogJNI.close(m_impl);
m_impl = 0;
}

/**
* Appends a raw record to the log.
*
Expand Down Expand Up @@ -318,12 +324,6 @@ public void appendRaw(int entry, ByteBuffer data, int start, int len, long times
DataLogJNI.appendRaw(m_impl, entry, data, start, len, timestamp);
}

@Override
public void close() {
DataLogJNI.close(m_impl);
m_impl = 0;
}

/**
* Appends a boolean record to the log.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.util.datalog;

import java.util.Arrays;

/** Log array of double values. */
public class DoubleArrayLogEntry extends DataLogEntry {
/** The data type for double array values. */
Expand Down Expand Up @@ -71,4 +73,58 @@ public void append(double[] value, long timestamp) {
public void append(double[] value) {
m_log.appendDoubleArray(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(double[] value, long timestamp) {
if (!equalsLast(value)) {
copyToLast(value);
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(double[] value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or false if none.
*/
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
public synchronized double[] getLastValue() {
if (m_lastValue == null) {
return null;
}
return Arrays.copyOf(m_lastValue, m_lastValueLen);
}

private boolean equalsLast(double[] value) {
if (m_lastValue == null || m_lastValueLen != value.length) {
return false;
}
return Arrays.equals(m_lastValue, 0, value.length, value, 0, value.length);
}

private void copyToLast(double[] value) {
if (m_lastValue == null || m_lastValue.length < value.length) {
m_lastValue = Arrays.copyOf(value, value.length);
} else {
System.arraycopy(value, 0, m_lastValue, 0, value.length);
}
m_lastValueLen = value.length;
}

private double[] m_lastValue;
private int m_lastValueLen;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,39 @@ public void append(double value, long timestamp) {
public void append(double value) {
m_log.appendDouble(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(double value, long timestamp) {
if (!m_hasLastValue || m_lastValue != value) {
m_lastValue = value;
m_hasLastValue = true;
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(double value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or 0 if none.
*/
public synchronized double getLastValue() {
return m_lastValue;
}

boolean m_hasLastValue;
double m_lastValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.util.datalog;

import java.util.Arrays;

/** Log array of float values. */
public class FloatArrayLogEntry extends DataLogEntry {
/** The data type for float array values. */
Expand Down Expand Up @@ -71,4 +73,58 @@ public void append(float[] value, long timestamp) {
public void append(float[] value) {
m_log.appendFloatArray(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(float[] value, long timestamp) {
if (!equalsLast(value)) {
copyToLast(value);
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(float[] value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or false if none.
*/
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
public synchronized float[] getLastValue() {
if (m_lastValue == null) {
return null;
}
return Arrays.copyOf(m_lastValue, m_lastValueLen);
}

private boolean equalsLast(float[] value) {
if (m_lastValue == null || m_lastValueLen != value.length) {
return false;
}
return Arrays.equals(m_lastValue, 0, value.length, value, 0, value.length);
}

private void copyToLast(float[] value) {
if (m_lastValue == null || m_lastValue.length < value.length) {
m_lastValue = Arrays.copyOf(value, value.length);
} else {
System.arraycopy(value, 0, m_lastValue, 0, value.length);
}
m_lastValueLen = value.length;
}

private float[] m_lastValue;
private int m_lastValueLen;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,39 @@ public void append(float value, long timestamp) {
public void append(float value) {
m_log.appendFloat(m_entry, value, 0);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update(float value, long timestamp) {
if (!m_hasLastValue || m_lastValue != value) {
m_lastValue = value;
m_hasLastValue = true;
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update(float value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or 0 if none.
*/
public synchronized float getLastValue() {
return m_lastValue;
}

boolean m_hasLastValue;
float m_lastValue;
}
Loading

0 comments on commit 1227b7b

Please sign in to comment.