summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/devices/GenericThermalSensor.java
blob: 42de0f00324cce6c03d6730230f5c51c77860107 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;		

		/* (non-Javadoc)
		 * @see org.reprap.comms.OutgoingMessage#getBinary()
		 */
		public byte[] getBinary() {
			return new byte [] { MSG_GetTemp };
		}
		
	}
	
	/**
	 *
	 */
	public class TemperatureResponse extends IncomingMessage {

		/**
		 * @param incomingContext
		 * @throws IOException
		 */
		public TemperatureResponse(IncomingContext incomingContext) throws IOException {
			super(incomingContext);
		}
		
		/**
		 * @return
		 * @throws InvalidPayloadException
		 */
		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;
		}

		/* (non-Javadoc)
		 * @see org.reprap.comms.IncomingMessage#isExpectedPacketType(byte)
		 */
		protected boolean isExpectedPacketType(byte packetType) {
			return packetType == RequestTemperature.MSG_GetTemp;
		}

	}
	
	/**
	 * @param communicator
	 * @param address
	 */
	public GenericThermalSensor(Communicator communicator, Address address) {
		super(communicator, address);
	}

	/**
	 * @return
	 * @throws ReprapException
	 * @throws IOException
	 */
	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;
	}
	
}