summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/devices/GenericThermalSensor.java
blob: 2d50fd36abf5b69c74dd2fa0598ab972398644db (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
60
package org.reprap.devices;

import java.io.IOException;

import org.reprap.Device;
import org.reprap.ReprapException;
import org.reprap.comms.Address;
import org.reprap.comms.Communicator;
import org.reprap.comms.IncomingContext;
import org.reprap.comms.IncomingMessage;
import org.reprap.comms.OutgoingMessage;

public class GenericThermalSensor extends Device {

	public class RequestTemperature extends OutgoingMessage {
		public static final int MSG_GetTemp = 1;		

		public byte[] getBinary() {
			return new byte [] { MSG_GetTemp };
		}
		
	}
	
	public class TemperatureResponse extends IncomingMessage {

		public TemperatureResponse(IncomingContext incomingContext) throws IOException {
			super(incomingContext);
		}
		
		int GetTemperature() throws InvalidPayloadException {
		    byte [] reply = getPayload();
		    if (reply.length != 3)
		    	throw new InvalidPayloadException();
		    if (reply[0] != 1)
		    	throw new InvalidPayloadException();
		    return reply[1] + reply[2] << 8;
		}

		protected boolean isExpectedPacketType(byte packetType) {
			return packetType == RequestTemperature.MSG_GetTemp;
		}

	}
	
	public GenericThermalSensor(Communicator communicator, Address address) {
		super(communicator, address);
	}

	double getTemperature() throws ReprapException, IOException {
		OutgoingMessage request = new RequestTemperature();
		IncomingContext replyContext = sendMessage(request);
		TemperatureResponse response = new TemperatureResponse(replyContext);
		
		int unscaled = response.GetTemperature();
		
		// TODO scale this according to callibration info 
		return unscaled;
	}
	
}