Exercise 4 Solution: Sensor Application Code
Bend Sensor Code:
1
24
25
26 package org.sunspotworld;
27
28 import com.sun.spot.sensorboard.EDemoBoard;
29 import com.sun.spot.sensorboard.peripheral.ISwitch;
30 import com.sun.spot.sensorboard.peripheral.ITriColorLED;
31 import com.sun.spot.io.j2me.radiostream.*;
32 import com.sun.spot.io.j2me.radiogram.*;
33 import com.sun.spot.sensorboard.io.IScalarInput;
34 import com.sun.spot.sensorboard.peripheral.ISwitchListener;
35 import com.sun.spot.util.*;
36
37 import java.io.*;
38 import javax.microedition.io.*;
39 import javax.microedition.midlet.MIDlet;
40 import javax.microedition.midlet.MIDletStateChangeException;
41
42
53 public class Bendy extends MIDlet implements ISwitchListener {
54
55 private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();
56 private static final int SAMPLE_SIZE = 150;
57 private int bendMin = 1000;
58 private int bendCenter = 0;
59 private int bendMax = 0;
60 private IScalarInput bend = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
61 private ISwitch sw1 = EDemoBoard.getInstance().getSwitches()[EDemoBoard.SW1];
62 private ISwitch sw2 = EDemoBoard.getInstance().getSwitches()[EDemoBoard.SW2];
63 private BendySender nw;
64
65
66
70 protected void startApp() throws MIDletStateChangeException {
71 new BootloaderListener().start();
72 nw = new BendySender();
73 calibrate();
74 sw2.addISwitchListener(this);
75 readSensor();
76 }
77
78
83 private void readSensor(){
84 int lastVal = 0;
85 int bendInt = 0;
86 leds[7].setRGB(0, 250, 0);
87 while (true) {
88 try {
89 bendInt = bend.getValue();
90 } catch (IOException ex) {
91 ex.printStackTrace();
92 }
93 if ((bendInt > lastVal + 15) || (bendInt < lastVal - 15)) {
94 lastVal = bendInt;
95 String msg = String.valueOf(bendInt);
96 try {
97 nw.send(msg);
98 } catch (IOException ex) {
99 ex.printStackTrace();
100 }
101 }
102 leds[7].setOn(!leds[7].isOn());
103 Utils.sleep(50);
104 }
105 }
106
107
108
109
113 protected void calibrate() {
114 leds[7].setOff();
115 setBendMin(1000);
116 setBendCenter(0);
117 setBendMax(0);
118 System.out.println("Do not touch bend sensor while red light is flashing ...");
119 Utils.sleep(1000);
120 leds[0].setRGB(250, 0, 0);
121 int z = 0;
122
125 while (z < SAMPLE_SIZE) {
126 try {
127 setBendCenter(getBendCenter() + bend.getValue());
128 z++;
129 } catch (IOException ex) {
130 ex.printStackTrace();
131 }
132 leds[0].setOn(!leds[0].isOn());
133 Utils.sleep(50);
134 }
135 setBendCenter(getBendCenter() / SAMPLE_SIZE);
136 System.out.println("Center Average: " + getBendCenter() + "(over " + SAMPLE_SIZE + " readings)");
137 System.out.println("Calibrate bend Sensor. Bend in both directions several times. Click Switch one when done.");
138 leds[0].setRGB(0, 0, 250);
139 while (sw1.isOpen()) {
140 try {
141 int bendVal = bend.getValue();
142 if (bendVal < getBendMin()) {
143 setBendMin(bendVal);
144 }
145 if (bendVal > getBendMax()) {
146 setBendMax(bendVal);
147 }
148 System.out.println("Bend: " + bendVal + " Min: " + getBendMin() + " Max: " + getBendMax());
149 leds[0].setOn(!leds[0].isOn());
150 Utils.sleep(50);
151 } catch (IOException ex) {
152 ex.printStackTrace();
153 }
154 }
155 System.out.println(" Setting Min: " + getBendMin() + " Center: " + getBendCenter() + " Max: " +
» getBendMax());
156 leds[0].setOff();
157 nw.connect();
158
159 }
160
161 protected void pauseApp() {
162
163 }
164
165
178 protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
179 for (int i = 0; i < 8; i++) {
180 leds[i].setOff();
181 }
182 }
183
184
188 public int getBendMin() {
189 return bendMin;
190 }
191
192
196 public void setBendMin(int bendMin) {
197 this.bendMin = bendMin;
198 }
199
200
205 public int getBendCenter() {
206 return bendCenter;
207 }
208
209
214 public void setBendCenter(int bendCenter) {
215 this.bendCenter = bendCenter;
216 }
217
218
223 public int getBendMax() {
224 return bendMax;
225 }
226
227
232 public void setBendMax(int bendMax) {
233 this.bendMax = bendMax;
234 }
235
236
240
247 public void switchPressed(ISwitch sw) {
248 int switchNum = (sw == sw1) ? 1 : 2;
249 System.out.println("Switch " + switchNum + " closed.");
250 calibrate();
251 }
252
253 public void switchReleased(ISwitch arg0) {
254 }
255 }
Go on to Exercise 5