From f41980c27631dfd8cbfa3d3b9c759195a10ad1f7 Mon Sep 17 00:00:00 2001 From: James Cotton Date: Sun, 6 Mar 2011 00:56:56 -0600 Subject: [PATCH] Remove some debugging lines, also use Number interface instead of explicit Byte, Integer, Short etc in setValue/getValue --- .../src/org/openpilot/uavtalk/UAVObject.java | 5 +- .../org/openpilot/uavtalk/UAVObjectField.java | 118 +++++++++++------- 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/androidgcs/src/org/openpilot/uavtalk/UAVObject.java b/androidgcs/src/org/openpilot/uavtalk/UAVObject.java index 926440805..fe478cf2e 100644 --- a/androidgcs/src/org/openpilot/uavtalk/UAVObject.java +++ b/androidgcs/src/org/openpilot/uavtalk/UAVObject.java @@ -114,7 +114,7 @@ public abstract class UAVObject { for (int n = 0; n < fields.size(); ++n) { fields.get(n).initialize(this); } -// unpack(data); + unpack(data); } /** @@ -291,13 +291,12 @@ public abstract class UAVObject { public int unpack(ByteBuffer dataIn) throws Exception { if( dataIn == null ) return 0; - System.out.println( dataIn.toString() ); + // QMutexLocker locker(mutex); int numBytes = 0; ListIterator li = fields.listIterator(); while (li.hasNext()) { UAVObjectField field = li.next(); - System.out.println(field.toString()); numBytes += field.unpack(dataIn); } return numBytes; diff --git a/androidgcs/src/org/openpilot/uavtalk/UAVObjectField.java b/androidgcs/src/org/openpilot/uavtalk/UAVObjectField.java index b37c992fb..a9006e02f 100644 --- a/androidgcs/src/org/openpilot/uavtalk/UAVObjectField.java +++ b/androidgcs/src/org/openpilot/uavtalk/UAVObjectField.java @@ -99,31 +99,43 @@ public class UAVObjectField { switch (type) { case INT8: - for (int index = 0; index < numElements; ++index) - dataOut.put((Byte) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.put(val.byteValue()); + } break; case INT16: - for (int index = 0; index < numElements; ++index) - dataOut.putShort((Short) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.putShort(val.shortValue()); + } break; case INT32: - for (int index = 0; index < numElements; ++index) - dataOut.putInt((Integer) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.putInt(val); + } break; case UINT8: // TODO: Deal properly with unsigned - for (int index = 0; index < numElements; ++index) - dataOut.put((Byte) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.put(val.byteValue()); + } break; case UINT16: // TODO: Deal properly with unsigned - for (int index = 0; index < numElements; ++index) - dataOut.putShort((Short) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.putShort(val.shortValue()); + } break; case UINT32: // TODO: Deal properly with unsigned - for (int index = 0; index < numElements; ++index) - dataOut.putInt((Integer) getValue(index)); + for (int index = 0; index < numElements; ++index) { + Integer val = (Integer) getValue(index); + dataOut.putInt(val); + } break; case FLOAT32: for (int index = 0; index < numElements; ++index) @@ -142,64 +154,93 @@ public class UAVObjectField { return getNumBytes(); } - public int unpack(ByteBuffer dataIn) throws Exception { + public int unpack(ByteBuffer dataIn) { // Unpack each element from input buffer dataIn.order(ByteOrder.LITTLE_ENDIAN); switch (type) { case INT8: + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Byte) dataIn.get(), index); + Byte val = dataIn.get(); + l.set(index, val.intValue()); } break; + } case INT16: + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Short) dataIn.getShort(), index); + Short val = dataIn.getShort(); + l.set(index, val.intValue()); } break; + } case INT32: + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Integer) dataIn.getInt(), index); + Integer val = dataIn.getInt(); + l.set(index, val.intValue()); } break; + } case UINT8: - // TODO: Deal with unsigned + // TOOD: Deal with unsigned + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Byte) dataIn.get(), index); + Byte val = dataIn.get(); + l.set(index, val.intValue()); } break; + } case UINT16: - // TODO: Deal with unsigned + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Short) dataIn.getShort(), index); + Short val = dataIn.getShort(); + l.set(index, val.intValue()); } break; + } case UINT32: - // TODO: Deal with unsigned + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Integer) dataIn.getInt(), index); + Integer val = dataIn.getInt(); + l.set(index, val.intValue()); } break; + } case FLOAT32: + { + List l = (List) this.data; for (int index = 0 ; index < numElements; ++index) { - setValue((Float) dataIn.getFloat(), index); + Float val = dataIn.getFloat(); + l.set(index, val); } break; + } case ENUM: + { List l = (List) data; for (int index = 0 ; index < numElements; ++index) { l.set(index, dataIn.get(index)); } break; + } case STRING: - throw new Exception("Strings not handled"); + // TODO: implement strings + //throw new Exception("Strings not handled"); } // Done return getNumBytes(); } - Object getValue() throws Exception { return getValue(0); }; - Object getValue(int index) throws Exception { + Object getValue() { return getValue(0); }; + Object getValue(int index) { // QMutexLocker locker(obj->getMutex()); // Check that index is not out of bounds if ( index >= numElements ) @@ -234,14 +275,14 @@ public class UAVObjectField { List l = (List) data; Byte val = l.get(index); - if(val >= options.size() || val < 0) - throw new Exception("Invalid value for" + name); + //if(val >= options.size() || val < 0) + // throw new Exception("Invalid value for" + name); return options.get(val); } case STRING: { - throw new Exception("Shit I should do this"); + //throw new Exception("Shit I should do this"); } } // If this point is reached then we got an invalid type @@ -252,12 +293,9 @@ public class UAVObjectField { public void setValue(Object data, int index) { // QMutexLocker locker(obj->getMutex()); // Check that index is not out of bounds - if ( index >= numElements ) + if ( index >= numElements ); //throw new Exception("Index out of bounds"); - System.out.println(data.toString()); - System.out.println(this.data.toString()); - // Get metadata UAVObject.Metadata mdata = obj.getMetadata(); // Update value if the access mode permits @@ -266,23 +304,19 @@ public class UAVObjectField { switch (type) { case INT8: - data = new Integer((Byte) data); case INT16: - data = new Integer((Short) data); case INT32: { List l = (List) this.data; - l.set(index,(Integer) data); + l.set(index,((Number) data).intValue()); break; } case UINT8: - data = new Integer((Byte) data); case UINT16: - data = new Integer((Short) data); case UINT32: { List l = (List) this.data; - l.set(index, (Integer) data); + l.set(index,((Number) data).intValue()); break; } case FLOAT32: @@ -386,7 +420,7 @@ public class UAVObjectField { sout += name + ": [ "; for (int n = 0; n < numElements; ++n) { - sout += String.valueOf(n) + " "; + sout += String.valueOf(n) + "(" + getValue(n) + ") "; } sout += "] " + units + "\n"; @@ -442,9 +476,8 @@ public class UAVObjectField { this.data = null; this.obj = null; this.elementNames = elementNames; - // Set field size - System.out.println("Initializing: type " + type + this.numElements); + // Set field size switch (type) { case INT8: @@ -487,7 +520,6 @@ public class UAVObjectField { numBytesPerElement = 0; } clear(); - System.out.println("Initialized: " + this.data.toString()); }