root/Projects/TrafficLightSender/src/org/sunspotworld/TrafficLightSender.java

Revision 67, 6.9 kB (checked in by mcintoke, 1 year ago)

Added copywrite

Line 
1 /*
2  * Title: TrafficLightSender
3  * Author: Kevin McIntosh
4  * Description:
5  * SunSpot's LEDs will periodically switch between green, yellow, and red in order to simulate a traffic light.   
6  * Also trasmits radio signals at different strengths using the dynamicSend() method
7  * from the Beacon class. 
8  *
9  * Pay close attention in keeping settings (port, channel, etc.) in synch with
10  * TrafficLightFinder SPOTBot.  Currently the Beacon class sets the default
11  * setting for this class.
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  * A class intended to simulate a traffic light while simultaneously tranmiting
28  * radio strength signals.
29  *   
30  * @author Kevin McIntosh
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;//Red light and green light.  Yellow light acts as a stall and is not a true state in this sense
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      * Displays red in the LEDs
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      * Displays green in the LEDs
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      * Displays green in the LEDs
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      * A method purely for show-off purposes.  Uses the LEDs to count-down from
81      * 3 to 0.
82      */
83     public static void countdown() {
84
85         // 3
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         // 2
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         // 1
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      * Input a range of numbers.  The "range" will start from one and end with your number.
139      *
140      * @param rangeOfDigits
141      * @return A random digit within the givin range
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      * Input a minimum and a maximum of a range of numbers. 
154      * Returns a random digit within that range
155      *
156      * @param lowerRange
157      * @param upperRange
158      * @return randomDigit
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) { //begin the search!!
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                     //Utils.sleep(randomInt(200, 500));
187
188                 }
189                 randomDigit = randomInt(TOTAL_NUMBER_OF_STATES);
190
191             } else {
192
193                 System.out.println("Slow");
194                 yellowLight();
195
196                 //sendNewInt(SLOW);
197                 Utils.sleep(THROW_OFF); //the intention of yellowLight() is to throw off the searching algorythm.  Bassically, makes the signal harder to find
198
199                 System.out.println("Stop");
200                 //sendNewChar(STOP);
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         // This is not currently called by the Squawk VM
223     }
224
225    
226     protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
227     }
228 }
229
230 /*
231  
232 Copyright 2008 The SPOTBot Team
233
234 Licensed under the Apache License, Version 2.0 (the "License");
235 you may not use this file except in compliance with the License.
236 You may obtain a copy of the License at
237
238     http://www.apache.org/licenses/LICENSE-2.0
239
240 Unless required by applicable law or agreed to in writing, software
241 distributed under the License is distributed on an "AS IS" BASIS,
242 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
243 See the License for the specific language governing permissions and
244 limitations under the License.
245
246  */
247
Note: See TracBrowser for help on using the browser.