1 package lejos.nxt.remote;
\r
4 import lejos.nxt.comm.*;
\r
8 * Emulates a Sensor Port using LCP
\r
10 public class RemoteSensorPort implements NXTProtocol, ADSensorPort {
\r
13 private int type, mode;
\r
14 private NXTCommand nxtCommand;
\r
16 public RemoteSensorPort(NXTCommand nxtCommand, int id) {
\r
17 this.nxtCommand = nxtCommand;
\r
21 public int getId() {
\r
25 public int getType() {
\r
29 public int getMode() {
\r
33 public void setTypeAndMode(int type, int mode) {
\r
37 nxtCommand.setInputMode(id, type, mode);
\r
38 } catch (IOException ioe) {}
\r
41 public void setType(int type) {
\r
43 setTypeAndMode(type, mode);
\r
46 public void setMode(int mode) {
\r
48 setTypeAndMode(type, mode);
\r
52 * Reads the boolean value of the sensor.
\r
53 * @return Boolean value of sensor.
\r
55 public boolean readBooleanValue() {
\r
57 InputValues vals = nxtCommand.getInputValues(id);
\r
58 return (vals.rawADValue<600);
\r
59 } catch (IOException ioe) {
\r
65 * Reads the raw value of the sensor.
\r
66 * @return Raw sensor value. Range is device dependent.
\r
68 public int readRawValue() {
\r
70 InputValues vals = nxtCommand.getInputValues(id);
\r
71 return vals.rawADValue;
\r
72 } catch (IOException ioe) {
\r
78 * Returns value compatible with Lego firmware.
\r
80 public int readValue()
\r
82 int rawValue = readRawValue();
\r
84 if (mode == MODE_BOOLEAN)
\r
86 return (rawValue < 600 ? 1 : 0);
\r
89 if (mode == MODE_PCTFULLSCALE)
\r
91 return ((1023 - rawValue) * 100/ 1023);
\r