This commit is contained in:
nic
2011-05-11 11:34:47 +02:00
parent c95dc12a96
commit fd5a1c5b4b
2 changed files with 27 additions and 16 deletions
@@ -30,7 +30,7 @@ public class SensorUtils {
private SensorUtils() {
}
/**
* Extract one unsigned short from a big endian byte array.
*
@@ -66,10 +66,10 @@ public class SensorUtils {
* @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;
public static byte getCrc8(byte[] buffer, int start, int length) {
byte crc = 0x0;
for (int i = start; i < (end + 1); i++) {
for (int i = start; i < (start + length); i++) {
crc = crc8PushByte(crc, buffer[i]);
}
return crc;
@@ -81,14 +81,18 @@ public class SensorUtils {
* @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;
private static byte crc8PushByte(byte crc, byte add) {
crc = (byte) (crc ^ add);
for (int i = 0; i < 8; i++) {
if ((crc & 0x00000001) != 0x00000000) {
crc = (crc >> 1) ^ 0x0000008C;
if ((crc & 0x1) != 0x0) {
// we have to do the OxFF mask because the right shift is
// supposed to shift in a 0
// as crc gets implicitly casted to a
// signed int by >> it may be filled with 1s
crc = (byte) (((crc & 0xFF) >> 1) ^ 0x8C);
} else {
crc = (crc >> 1);
crc = (byte) ((crc & 0xFF) >> 1);
}
}
return crc;
@@ -27,6 +27,10 @@ import com.google.android.apps.mytracks.content.Sensor;
*/
public class ZephyrMessageParser implements MessageParser {
public static final int ZEPHYR_HXM_BYTE_STX = 0;
public static final int ZEPHYR_HXM_BYTE_CRC = 58;
public static final int ZEPHYR_HXM_BYTE_ETX = 59;
@Override
public Sensor.SensorDataSet parseBuffer(byte[] buffer) {
StringBuilder sb = new StringBuilder();
@@ -36,11 +40,11 @@ public class ZephyrMessageParser implements MessageParser {
Log.w(Constants.TAG, "Got zephyr data: " + sb);
// Heart Rate
Sensor.SensorData.Builder heartrate = Sensor.SensorData.newBuilder()
.setValue(buffer[12] & 0xFF)
.setValue(buffer[12])
.setState(Sensor.SensorState.SENDING);
// Changes Nico Laum (Power and Cadence)
Sensor.SensorData.Builder power = Sensor.SensorData.newBuilder()
.setValue(buffer[11] & 0xFF)
.setValue(buffer[11])
.setState(Sensor.SensorState.SENDING);
Sensor.SensorData.Builder cadence = Sensor.SensorData.newBuilder()
.setValue(SensorUtils.unsignedShortToIntLittleEndian(buffer, 56) / 16)
@@ -59,10 +63,13 @@ public class ZephyrMessageParser implements MessageParser {
@Override
public boolean isValid(byte[] buffer) {
// Check STX, ETX and CRC Checksum
return buffer[0] == 0x02
&& buffer[59] == 0x03
&& SensorUtils.getCrc8(buffer, 3, 57) == (int) (buffer[58] & 0xFF);
if (buffer.length > ZEPHYR_HXM_BYTE_ETX) {
// Check STX (Start of Text), ETX (End of Text) and CRC Checksum
return buffer[ZEPHYR_HXM_BYTE_STX] == 0x02
&& buffer[ZEPHYR_HXM_BYTE_ETX] == 0x03
&& SensorUtils.getCrc8(buffer, 3, 55) == buffer[ZEPHYR_HXM_BYTE_CRC];
} else
return false;
}
@Override