I am using the leapmotion library and some values are coming in a PVector.
They print out fine, but when I put them in a string to be sent to Arduino I do get brackes and commas…
This is very hard to parse in values on the Arduino side.
The "handPostion is defined as a Pvector and the rest are just floats.
PVector handStabilized = hand.getStabilizedPosition();
You can simply pick character TAB as the delimiter for all those 5 sent variables: final String tsv = toTsvPVector(hand.getStabilizedPosition()) + TAB + handRoll + TAB + handPitch + TAB + handPitch + TAB + handPitch + ENTER;
yey, that worked.
Thanks a lot. Now I am faced with too many decimals. I could round the lone floats, but what to do about the vals inside the PVector?
You can’t change its 3 float primitive datatype values!
You’re gonna need another container for it; for example an int[] array.
BtW you can grab a float[] outta a PVector via its array() method:
After that you just need another util function to convert that float[] to a rounded int[].
Then use Arrays.toString() to convert the whole array to a String and use some other String methods like substring() & replace(): Arrays (Java SE 11 & JDK 11 ) String (Java SE 11 & JDK 11 )
// Discourse.Processing.org/t/how-to-extract-values-from-a-pvector/25778/4
// GoToLoop (2020/Nov/28)
import java.util.Arrays;
void setup() {
final PVector vec = PVector.random3D(this).mult(100);
final int[] roundedVec = roundedArray(vec.array());
println(vec);
final String tsv = toDsvArr(roundedVec);
println(tsv);
final String dsv = toDsvArr(" ", roundedVec);
println(dsv);
final String csv = toDsvArr(",", roundedVec);
println(csv);
exit();
}
@SafeVarargs static final int[] roundedArray(final float... arr) {
final int[] rounded = new int[arr.length];
for (int i = 0; i < arr.length; rounded[i] = round(arr[i++]));
return rounded;
}
@SafeVarargs static final String toDsvArr(final int... arr) {
return toDsvArr("\t", arr);
}
@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
final String arrStr = Arrays.toString(arr);
return arrStr.substring(1, arrStr.length() - 1).replace(", ", sep);
}
You can use join() in place of Arrays.toString(). That’d be easier now that I’ve checked that out:
@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
return join(str(arr), sep);
}