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
@@ -66,10 +66,10 @@ public class SensorUtils {
* @param end the position in the byte array where the payload ends * @param end the position in the byte array where the payload ends
* @return CRC8 value * @return CRC8 value
*/ */
public static int getCrc8(byte[] buffer, int start, int end) { public static byte getCrc8(byte[] buffer, int start, int length) {
int crc = 0x0; byte crc = 0x0;
for (int i = start; i < (end + 1); i++) { for (int i = start; i < (start + length); i++) {
crc = crc8PushByte(crc, buffer[i]); crc = crc8PushByte(crc, buffer[i]);
} }
return crc; return crc;
@@ -81,14 +81,18 @@ public class SensorUtils {
* @param crc int of crc value * @param crc int of crc value
* @param add the next byte to add to the CRC8 calculation * @param add the next byte to add to the CRC8 calculation
*/ */
private static int crc8PushByte(int crc, byte add) { private static byte crc8PushByte(byte crc, byte add) {
int addInt = (add & 0x000000FF); crc = (byte) (crc ^ add);
crc = crc ^ addInt;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if ((crc & 0x00000001) != 0x00000000) { if ((crc & 0x1) != 0x0) {
crc = (crc >> 1) ^ 0x0000008C; // 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 { } else {
crc = (crc >> 1); crc = (byte) ((crc & 0xFF) >> 1);
} }
} }
return crc; return crc;
@@ -27,6 +27,10 @@ import com.google.android.apps.mytracks.content.Sensor;
*/ */
public class ZephyrMessageParser implements MessageParser { 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 @Override
public Sensor.SensorDataSet parseBuffer(byte[] buffer) { public Sensor.SensorDataSet parseBuffer(byte[] buffer) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@@ -36,11 +40,11 @@ public class ZephyrMessageParser implements MessageParser {
Log.w(Constants.TAG, "Got zephyr data: " + sb); Log.w(Constants.TAG, "Got zephyr data: " + sb);
// 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])
.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])
.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)
@@ -59,10 +63,13 @@ public class ZephyrMessageParser implements MessageParser {
@Override @Override
public boolean isValid(byte[] buffer) { public boolean isValid(byte[] buffer) {
// Check STX, ETX and CRC Checksum if (buffer.length > ZEPHYR_HXM_BYTE_ETX) {
return buffer[0] == 0x02 // Check STX (Start of Text), ETX (End of Text) and CRC Checksum
&& buffer[59] == 0x03 return buffer[ZEPHYR_HXM_BYTE_STX] == 0x02
&& SensorUtils.getCrc8(buffer, 3, 57) == (int) (buffer[58] & 0xFF); && buffer[ZEPHYR_HXM_BYTE_ETX] == 0x03
&& SensorUtils.getCrc8(buffer, 3, 55) == buffer[ZEPHYR_HXM_BYTE_CRC];
} else
return false;
} }
@Override @Override