summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/comms/snap/SNAPChecksum.java
blob: 2240aab1bf998a05ec129475c0b22dfb2d20da5e (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
50
51
52
53
54
55
56
57
58
59
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;
	}
	
	/**
	 * @return
	 */
	byte getResult() {
		return crc;
	}
	
}