diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java index 96704196d..45c2364a9 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java @@ -33,6 +33,7 @@ public class SensorUtils { /** * Extract one unsigned short from a big endian byte array. + * * @param buffer the buffer to extract the short from * @param index the first byte to be interpreted as part of the short * @return The unsigned short at the given index in the buffer @@ -42,51 +43,56 @@ public class SensorUtils { r |= buffer[index + 1] & 0xFF; return r; } - + /** * Extract one unsigned short from a little endian byte array. + * * @param buffer the buffer to extract the short from * @param index the first byte to be interpreted as part of the short * @return The unsigned short at the given index in the buffer */ - public static int unsignedShortToIntLittleEndian(byte[] buffer, int index) { - int r = buffer[index] & 0xFF; - r |= (buffer[index + 1] & 0xFF) << 8; - return r; - } - - /** - * Returns CRC8 (polynomial 0x8C) from byte array buffer[start] until (including) buffer[end] - * @param buffer the byte array of data (payload) - * @param start the position in the byte array where the payload begins - * @param end the position in the byte array where the payload ends - * @return CRC8 value - */ - public static int getCrc8(byte[] buffer, int start, int end) { - int[] crc = { 0x0 }; - - for (int i = start; i < (end+1); i++) { - crc8PushByte(crc, buffer[i]); - } - return crc[0]; - } - - /** - * Updates a CRC8 value by using the next byte passed to this method - * @param crc int of crc value (as an array to pass it as reference) - * @param add the next byte to add to the CRC8 calculation - */ - private static void crc8PushByte(int[] crc, byte add) { - int addInt = (add & 0x000000FF); - crc[0] = crc[0] ^ addInt; - for (int i = 0; i < 8; i++) { - if ((crc[0] & 0x00000001) != 0x00000000) { - crc[0] = (crc[0] >> 1) ^ 0x0000008C; - } else { - crc[0] = (crc[0] >> 1); - } - } - } + public static int unsignedShortToIntLittleEndian(byte[] buffer, int index) { + int r = buffer[index] & 0xFF; + r |= (buffer[index + 1] & 0xFF) << 8; + return r; + } + + /** + * Returns CRC8 (polynomial 0x8C) from byte array buffer[start] until + * (including) buffer[end] + * + * @param buffer the byte array of data (payload) + * @param start the position in the byte array where the payload begins + * @param end the position in the byte array where the payload ends + * @return CRC8 value + */ + public static int getCrc8(byte[] buffer, int start, int end) { + int crc = 0x0; + + for (int i = start; i < (end + 1); i++) { + crc = crc8PushByte(crc, buffer[i]); + } + return crc; + } + + /** + * Updates a CRC8 value by using the next byte passed to this method + * + * @param crc int of crc value + * @param add the next byte to add to the CRC8 calculation + */ + private static int crc8PushByte(int crc, byte add) { + int addInt = (add & 0x000000FF); + crc = crc ^ addInt; + for (int i = 0; i < 8; i++) { + if ((crc & 0x00000001) != 0x00000000) { + crc = (crc >> 1) ^ 0x0000008C; + } else { + crc = (crc >> 1); + } + } + return crc; + } public static String getStateAsString(Sensor.SensorState state, Context c) { switch (state) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java index 861d047ef..0808c73ca 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java @@ -21,34 +21,31 @@ import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.content.Sensor; /** - * An implementation of a SensorData parser for Zephyr HRM. + * An implementation of a Sensor MessageParser for Zephyr. * * @author Sandor Dornbush */ public class ZephyrMessageParser implements MessageParser { + @Override public Sensor.SensorDataSet parseBuffer(byte[] buffer) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buffer.length; i++) { sb.append(String.format("%02X", buffer[i])); } Log.w(Constants.TAG, "Got zephyr data: " + sb); - // The provided units are 1/16 strides per minute. // Heart Rate Sensor.SensorData.Builder heartrate = Sensor.SensorData.newBuilder() - .setValue(buffer[12] & 0xFF) - .setState(Sensor.SensorState.SENDING); + .setValue(buffer[12] & 0xFF) + .setState(Sensor.SensorState.SENDING); // Changes Nico Laum (Power and Cadence) Sensor.SensorData.Builder power = Sensor.SensorData.newBuilder() .setValue(buffer[11] & 0xFF) .setState(Sensor.SensorState.SENDING); Sensor.SensorData.Builder cadence = Sensor.SensorData.newBuilder() - .setValue(SensorUtils.unsignedShortToIntLittleEndian(buffer, 56)/16) + .setValue(SensorUtils.unsignedShortToIntLittleEndian(buffer, 56) / 16) .setState(Sensor.SensorState.SENDING); - // Cadence - //.setCadence(cadence / 16) - //.build(); Sensor.SensorDataSet sds = Sensor.SensorDataSet.newBuilder() .setCreationTime(System.currentTimeMillis()) @@ -60,16 +57,20 @@ public class ZephyrMessageParser implements MessageParser { return sds; } + @Override public boolean isValid(byte[] buffer) { - // Changes Nico Laum - // Check STX, ETX and CRC Checksum - return (buffer[0] == 0x02 && buffer[59] == 0x03 && (SensorUtils.getCrc8(buffer, 3, 57) == (int) (buffer[58] & 0xFF))); + // Check STX, ETX and CRC Checksum + return buffer[0] == 0x02 + && buffer[59] == 0x03 + && SensorUtils.getCrc8(buffer, 3, 57) == (int) (buffer[58] & 0xFF); } + @Override public int getFrameSize() { return 60; } + @Override public int findNextAlignment(byte[] buffer) { // TODO test or understand this code. for (int i = 0; i < buffer.length - 1; i++) {