Merge with 3dfdbcf78de082edb66a8994ffb604c84945df0d

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