| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
package org.sunspotworld; |
|---|
| 14 |
|
|---|
| 15 |
import com.sun.spot.sensorboard.EDemoBoard; |
|---|
| 16 |
import com.sun.spot.sensorboard.peripheral.ITriColorLED; |
|---|
| 17 |
|
|---|
| 18 |
import com.sun.spot.sensorboard.peripheral.LEDColor; |
|---|
| 19 |
import com.sun.spot.util.Utils; |
|---|
| 20 |
import java.io.*; |
|---|
| 21 |
import java.util.Random; |
|---|
| 22 |
import javax.microedition.io.Datagram; |
|---|
| 23 |
import javax.microedition.midlet.MIDlet; |
|---|
| 24 |
import javax.microedition.midlet.MIDletStateChangeException; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
public class TrafficLightSender extends MIDlet { |
|---|
| 33 |
|
|---|
| 34 |
private static final int PACKETS_PER_SECOND = 5; |
|---|
| 35 |
private static final int PACKET_INTERVAL = 1000 / PACKETS_PER_SECOND; |
|---|
| 36 |
private static final int TOTAL_NUMBER_OF_STATES = 2; |
|---|
| 37 |
private static final int THROW_OFF = 3000; |
|---|
| 38 |
private static int randomDigit = 1; |
|---|
| 39 |
private static Datagram data = null; |
|---|
| 40 |
private static ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs(); |
|---|
| 41 |
private static final int PORT = 41; |
|---|
| 42 |
private static final int DELAY = 1000; |
|---|
| 43 |
private static final int NUM_CYCLES = 20; |
|---|
| 44 |
private static Beacon sender = null; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
public static void redLight() { |
|---|
| 50 |
for (int i = 0; i < 8; i++) { |
|---|
| 51 |
leds[i].setOff(); |
|---|
| 52 |
leds[i].setColor(LEDColor.RED); |
|---|
| 53 |
leds[i].setOn(); |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
public static void yellowLight() { |
|---|
| 61 |
for (int i = 0; i < 8; i++) { |
|---|
| 62 |
leds[i].setOff(); |
|---|
| 63 |
leds[i].setColor(LEDColor.YELLOW); |
|---|
| 64 |
leds[i].setOn(); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
public static void greenLight() { |
|---|
| 72 |
for (int i = 0; i < 8; i++) { |
|---|
| 73 |
leds[i].setOff(); |
|---|
| 74 |
leds[i].setColor(LEDColor.GREEN); |
|---|
| 75 |
leds[i].setOn(); |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
public static void countdown() { |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
for (int i = 0; i < 8; i++) { |
|---|
| 87 |
leds[i].setOff(); |
|---|
| 88 |
leds[i].setColor(LEDColor.WHITE); |
|---|
| 89 |
leds[i].setOn(); |
|---|
| 90 |
} |
|---|
| 91 |
System.out.println("Three"); |
|---|
| 92 |
Utils.sleep(1000); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
for (int i = 0; i < 8; i++) { |
|---|
| 96 |
leds[i].setOff(); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
for (int i = 0; i < 6; i++) { |
|---|
| 100 |
leds[i].setOn(); |
|---|
| 101 |
} |
|---|
| 102 |
System.out.println("Two"); |
|---|
| 103 |
Utils.sleep(1000); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
for (int i = 0; i < 8; i++) { |
|---|
| 107 |
leds[i].setOff(); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
for (int i = 0; i < 4; i++) { |
|---|
| 111 |
leds[i].setOn(); |
|---|
| 112 |
} |
|---|
| 113 |
System.out.println("One"); |
|---|
| 114 |
Utils.sleep(1000); |
|---|
| 115 |
|
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
private static void beaconSender() { |
|---|
| 120 |
|
|---|
| 121 |
try { |
|---|
| 122 |
sender = new Beacon(NUM_CYCLES, DELAY); |
|---|
| 123 |
|
|---|
| 124 |
} catch (IOException ex) { |
|---|
| 125 |
System.out.println("Error in creating setting for the Beacon"); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
try { |
|---|
| 129 |
sender.setCycles(1); |
|---|
| 130 |
sender.dynamicSend(); |
|---|
| 131 |
System.out.println("So far it is working"); |
|---|
| 132 |
} catch (IOException ex) { |
|---|
| 133 |
System.out.println("Error in sending"); |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
public static int randomInt(int rangeOfDigits) { |
|---|
| 144 |
|
|---|
| 145 |
Random r = new Random(); |
|---|
| 146 |
int randomNumber = r.nextInt(rangeOfDigits); |
|---|
| 147 |
randomNumber++; |
|---|
| 148 |
return randomNumber; |
|---|
| 149 |
|
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
public static int randomInt(int lowerRange, int upperRange) { |
|---|
| 161 |
|
|---|
| 162 |
Random r = new Random(); |
|---|
| 163 |
int randomDigit = r.nextInt(upperRange - lowerRange + 1); |
|---|
| 164 |
randomDigit = randomDigit + lowerRange; |
|---|
| 165 |
return randomDigit; |
|---|
| 166 |
|
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
protected void startApp() throws MIDletStateChangeException { |
|---|
| 170 |
|
|---|
| 171 |
countdown(); |
|---|
| 172 |
|
|---|
| 173 |
while (true) { |
|---|
| 174 |
|
|---|
| 175 |
if (randomDigit == 1) { |
|---|
| 176 |
|
|---|
| 177 |
for (int i = 0; i < 1; i++) { |
|---|
| 178 |
|
|---|
| 179 |
System.out.println("GO"); |
|---|
| 180 |
greenLight(); |
|---|
| 181 |
Utils.sleep(50); |
|---|
| 182 |
beaconSender(); |
|---|
| 183 |
|
|---|
| 184 |
System.out.println("greenLight pass"); |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
} |
|---|
| 189 |
randomDigit = randomInt(TOTAL_NUMBER_OF_STATES); |
|---|
| 190 |
|
|---|
| 191 |
} else { |
|---|
| 192 |
|
|---|
| 193 |
System.out.println("Slow"); |
|---|
| 194 |
yellowLight(); |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
Utils.sleep(THROW_OFF); |
|---|
| 198 |
|
|---|
| 199 |
System.out.println("Stop"); |
|---|
| 200 |
|
|---|
| 201 |
redLight(); |
|---|
| 202 |
Utils.sleep(4000); |
|---|
| 203 |
|
|---|
| 204 |
randomDigit = 1; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
long nextTime = System.currentTimeMillis() + PACKET_INTERVAL; |
|---|
| 208 |
long delay = (nextTime - System.currentTimeMillis()) - 2; |
|---|
| 209 |
|
|---|
| 210 |
if (delay > 0) { |
|---|
| 211 |
Utils.sleep(delay); |
|---|
| 212 |
} else { |
|---|
| 213 |
Utils.sleep(PACKET_INTERVAL); |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
protected void pauseApp() { |
|---|
| 222 |
|
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { |
|---|
| 239 |
} |
|---|
| 240 |
} |
|---|