Adjusted short and byte scaling and reversal in EvdevAbsolute and accounting for signed and unsigned input

This commit is contained in:
admiralmachado
2014-03-11 21:26:32 +01:00
committed by Iwan Timmer
parent 46a7c90a72
commit 12efe3b5a3

View File

@@ -11,14 +11,18 @@ public class EvdevAbsolute {
public final static int UP = 1, DOWN = -1, NONE = 0; public final static int UP = 1, DOWN = -1, NONE = 0;
private static final int SHORT_MAX_UNSIGNED = 0xFFFF;
private static final int BYTE_MAX_UNSIGNED = 0xFF;
private final static int ABS_OFFSET = 0x40; private final static int ABS_OFFSET = 0x40;
private final static int READ_ONLY = 2; private final static int READ_ONLY = 2;
private final static char EVDEV_TYPE = 'E'; private final static char EVDEV_TYPE = 'E';
private int max;
private int avg; private int avg;
private int range; private int range;
private boolean reverse; private boolean reverse;
private boolean signed;
public EvdevAbsolute(String filename, int axis, boolean reverse) { public EvdevAbsolute(String filename, int axis, boolean reverse) {
ByteBuffer buffer = ByteBuffer.allocate(6*4); ByteBuffer buffer = ByteBuffer.allocate(6*4);
@@ -29,10 +33,10 @@ public class EvdevAbsolute {
buffer.getInt(); //Skip current value buffer.getInt(); //Skip current value
int min = buffer.getInt(); int min = buffer.getInt();
int max = buffer.getInt(); max = buffer.getInt();
avg = (min+max)/2; avg = (min+max)/2;
range = max-avg; range = max-avg;
signed = min < 0;
this.reverse = reverse; this.reverse = reverse;
} }
@@ -46,7 +50,10 @@ public class EvdevAbsolute {
* @return input value as short * @return input value as short
*/ */
public short getShort(int value) { public short getShort(int value) {
return (short) ((value-avg) * (reverse?-range:range) / Short.MAX_VALUE); if (signed)
return (short) ((reverse ? -1 - value : value) * (Short.MAX_VALUE + 1) / (max + 1));
else
return (short) ((reverse ? max - value : value) * SHORT_MAX_UNSIGNED / max);
} }
/** /**
@@ -55,7 +62,11 @@ public class EvdevAbsolute {
* @return input value as byte * @return input value as byte
*/ */
public byte getByte(int value) { public byte getByte(int value) {
return (byte) ((value-avg) * (reverse?-range:range) / Byte.MAX_VALUE); if (signed) {
return (byte) ((reverse ? -1 - value : value) * (Byte.MAX_VALUE + 1) / (max + 1));
} else {
return (byte) ((reverse ? max - value : value) * BYTE_MAX_UNSIGNED / max);
}
} }
/** /**