1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

Remove some debugging lines, also use Number interface instead of explicit

Byte, Integer, Short etc in setValue/getValue
This commit is contained in:
James Cotton 2011-03-06 00:56:56 -06:00
parent a9066494c7
commit f41980c276
2 changed files with 77 additions and 46 deletions

View File

@ -114,7 +114,7 @@ public abstract class UAVObject {
for (int n = 0; n < fields.size(); ++n) { for (int n = 0; n < fields.size(); ++n) {
fields.get(n).initialize(this); fields.get(n).initialize(this);
} }
// unpack(data); unpack(data);
} }
/** /**
@ -291,13 +291,12 @@ public abstract class UAVObject {
public int unpack(ByteBuffer dataIn) throws Exception { public int unpack(ByteBuffer dataIn) throws Exception {
if( dataIn == null ) if( dataIn == null )
return 0; return 0;
System.out.println( dataIn.toString() );
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
int numBytes = 0; int numBytes = 0;
ListIterator<UAVObjectField> li = fields.listIterator(); ListIterator<UAVObjectField> li = fields.listIterator();
while (li.hasNext()) { while (li.hasNext()) {
UAVObjectField field = li.next(); UAVObjectField field = li.next();
System.out.println(field.toString());
numBytes += field.unpack(dataIn); numBytes += field.unpack(dataIn);
} }
return numBytes; return numBytes;

View File

@ -99,31 +99,43 @@ public class UAVObjectField {
switch (type) switch (type)
{ {
case INT8: case INT8:
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.put((Byte) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.put(val.byteValue());
}
break; break;
case INT16: case INT16:
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.putShort((Short) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.putShort(val.shortValue());
}
break; break;
case INT32: case INT32:
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.putInt((Integer) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.putInt(val);
}
break; break;
case UINT8: case UINT8:
// TODO: Deal properly with unsigned // TODO: Deal properly with unsigned
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.put((Byte) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.put(val.byteValue());
}
break; break;
case UINT16: case UINT16:
// TODO: Deal properly with unsigned // TODO: Deal properly with unsigned
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.putShort((Short) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.putShort(val.shortValue());
}
break; break;
case UINT32: case UINT32:
// TODO: Deal properly with unsigned // TODO: Deal properly with unsigned
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index) {
dataOut.putInt((Integer) getValue(index)); Integer val = (Integer) getValue(index);
dataOut.putInt(val);
}
break; break;
case FLOAT32: case FLOAT32:
for (int index = 0; index < numElements; ++index) for (int index = 0; index < numElements; ++index)
@ -142,64 +154,93 @@ public class UAVObjectField {
return getNumBytes(); return getNumBytes();
} }
public int unpack(ByteBuffer dataIn) throws Exception { public int unpack(ByteBuffer dataIn) {
// Unpack each element from input buffer // Unpack each element from input buffer
dataIn.order(ByteOrder.LITTLE_ENDIAN); dataIn.order(ByteOrder.LITTLE_ENDIAN);
switch (type) switch (type)
{ {
case INT8: case INT8:
{
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Byte) dataIn.get(), index); Byte val = dataIn.get();
l.set(index, val.intValue());
} }
break; break;
}
case INT16: case INT16:
{
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Short) dataIn.getShort(), index); Short val = dataIn.getShort();
l.set(index, val.intValue());
} }
break; break;
}
case INT32: case INT32:
{
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Integer) dataIn.getInt(), index); Integer val = dataIn.getInt();
l.set(index, val.intValue());
} }
break; break;
}
case UINT8: case UINT8:
// TODO: Deal with unsigned // TOOD: Deal with unsigned
{
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Byte) dataIn.get(), index); Byte val = dataIn.get();
l.set(index, val.intValue());
} }
break; break;
}
case UINT16: case UINT16:
// TODO: Deal with unsigned {
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Short) dataIn.getShort(), index); Short val = dataIn.getShort();
l.set(index, val.intValue());
} }
break; break;
}
case UINT32: case UINT32:
// TODO: Deal with unsigned {
List<Integer> l = (List<Integer>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Integer) dataIn.getInt(), index); Integer val = dataIn.getInt();
l.set(index, val.intValue());
} }
break; break;
}
case FLOAT32: case FLOAT32:
{
List<Float> l = (List<Float>) this.data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
setValue((Float) dataIn.getFloat(), index); Float val = dataIn.getFloat();
l.set(index, val);
} }
break; break;
}
case ENUM: case ENUM:
{
List<Byte> l = (List<Byte>) data; List<Byte> l = (List<Byte>) data;
for (int index = 0 ; index < numElements; ++index) { for (int index = 0 ; index < numElements; ++index) {
l.set(index, dataIn.get(index)); l.set(index, dataIn.get(index));
} }
break; break;
}
case STRING: case STRING:
throw new Exception("Strings not handled"); // TODO: implement strings
//throw new Exception("Strings not handled");
} }
// Done // Done
return getNumBytes(); return getNumBytes();
} }
Object getValue() throws Exception { return getValue(0); }; Object getValue() { return getValue(0); };
Object getValue(int index) throws Exception { Object getValue(int index) {
// QMutexLocker locker(obj->getMutex()); // QMutexLocker locker(obj->getMutex());
// Check that index is not out of bounds // Check that index is not out of bounds
if ( index >= numElements ) if ( index >= numElements )
@ -234,14 +275,14 @@ public class UAVObjectField {
List<Byte> l = (List<Byte>) data; List<Byte> l = (List<Byte>) data;
Byte val = l.get(index); Byte val = l.get(index);
if(val >= options.size() || val < 0) //if(val >= options.size() || val < 0)
throw new Exception("Invalid value for" + name); // throw new Exception("Invalid value for" + name);
return options.get(val); return options.get(val);
} }
case STRING: 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 // 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) { public void setValue(Object data, int index) {
// QMutexLocker locker(obj->getMutex()); // QMutexLocker locker(obj->getMutex());
// Check that index is not out of bounds // Check that index is not out of bounds
if ( index >= numElements ) if ( index >= numElements );
//throw new Exception("Index out of bounds"); //throw new Exception("Index out of bounds");
System.out.println(data.toString());
System.out.println(this.data.toString());
// Get metadata // Get metadata
UAVObject.Metadata mdata = obj.getMetadata(); UAVObject.Metadata mdata = obj.getMetadata();
// Update value if the access mode permits // Update value if the access mode permits
@ -266,23 +304,19 @@ public class UAVObjectField {
switch (type) switch (type)
{ {
case INT8: case INT8:
data = new Integer((Byte) data);
case INT16: case INT16:
data = new Integer((Short) data);
case INT32: case INT32:
{ {
List<Integer> l = (List<Integer>) this.data; List<Integer> l = (List<Integer>) this.data;
l.set(index,(Integer) data); l.set(index,((Number) data).intValue());
break; break;
} }
case UINT8: case UINT8:
data = new Integer((Byte) data);
case UINT16: case UINT16:
data = new Integer((Short) data);
case UINT32: case UINT32:
{ {
List<Integer> l = (List<Integer>) this.data; List<Integer> l = (List<Integer>) this.data;
l.set(index, (Integer) data); l.set(index,((Number) data).intValue());
break; break;
} }
case FLOAT32: case FLOAT32:
@ -386,7 +420,7 @@ public class UAVObjectField {
sout += name + ": [ "; sout += name + ": [ ";
for (int n = 0; n < numElements; ++n) for (int n = 0; n < numElements; ++n)
{ {
sout += String.valueOf(n) + " "; sout += String.valueOf(n) + "(" + getValue(n) + ") ";
} }
sout += "] " + units + "\n"; sout += "] " + units + "\n";
@ -442,9 +476,8 @@ public class UAVObjectField {
this.data = null; this.data = null;
this.obj = null; this.obj = null;
this.elementNames = elementNames; this.elementNames = elementNames;
// Set field size
System.out.println("Initializing: type " + type + this.numElements);
// Set field size
switch (type) switch (type)
{ {
case INT8: case INT8:
@ -487,7 +520,6 @@ public class UAVObjectField {
numBytesPerElement = 0; numBytesPerElement = 0;
} }
clear(); clear();
System.out.println("Initialized: " + this.data.toString());
} }