summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/comms/snap/SNAPChecksum.java
blob: 1df8eb8da966b0b3c617d7415e7fd160a1e0b683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.reprap.comms.snap;

/**
 * 
 * Represents the checksum calculation process used in SNAP which
 * is an 8 bit CRC.
 * 
 */
public class SNAPChecksum {
	byte crc;
	
	SNAPChecksum() {
		crc = 0;
	}
	
	/**
	 * Add data to the CRC computation
	 * @param data
	 * @return The data passed for convenience
	 */
	byte addData(byte data) {
		byte i = (byte)(data ^ crc);
		
		crc = 0;
		
		if((i & 1) != 0)
			crc ^= 0x5e;
		if((i & 2) != 0)
			crc ^= 0xbc;
		if((i & 4) != 0)
			crc ^= 0x61;
		if((i & 8) != 0)
			crc ^= 0xc2;
		if((i & 0x10) != 0)
			crc ^= 0x9d;
		if((i & 0x20) != 0)
			crc ^= 0x23;
		if((i & 0x40) != 0)
			crc ^= 0x46;
		if((i & 0x80) != 0)
			crc ^= 0x8c;
		return data;
	}
	
	byte getResult() {
		return crc;
	}
	
}