Flashlight.java
Flashlight.java |
1 /* 2 * Flashlight.java 3 * 4 * Created on Aug 24, 2010 3:01:29 PM; 5 */ 6 7 package org.sunspotworld; 8 9 import com.sun.spot.resources.Resources; 10 import com.sun.spot.resources.transducers.ILightSensor; 11 import com.sun.spot.resources.transducers.ISwitch; 12 import com.sun.spot.resources.transducers.ISwitchListener; 13 import com.sun.spot.resources.transducers.ITriColorLEDArray; 14 import com.sun.spot.resources.transducers.LEDColor; 15 import com.sun.spot.resources.transducers.SwitchEvent; 16 import com.sun.spot.util.*; 17 18 import javax.microedition.midlet.MIDlet; 19 import javax.microedition.midlet.MIDletStateChangeException; 20 21 /** 22 * The manifest specifies this class as MIDlet-1, which means the VM 23 * will call the startApp method of this class to start the application. 24 */ 25 public class Flashlight extends MIDlet { 26 private static int LOW_LIGHT_THRESHOLD = 75; 27 private static LEDColor currentColor = LEDColor.WHITE; 28 private static int currentColorIdx = 0; 29 private static LEDColor[] colorChoices = { 30 LEDColor.WHITE, LEDColor.GREEN, LEDColor.CYAN, LEDColor.YELLOW 31 }; 32 private ILightSensor lightSensor = null; 33 private ITriColorLEDArray myLEDs = null; 34 private ISwitch sw = null; 35 36 37 protected void startApp() throws MIDletStateChangeException { 38 int lightVal = 0; 39 40 System.out.println("Flashlight application starting ...\n" + 41 "To turn on the LEDs, cover the light sensor on the Sun SPOT.\n" + 42 "The light sensor is the tiny white square just underneath \n" + 43 "Switch 1. Press Switch 2 to change LED color.\n"); 44 45 // Get the lightsensor, the LED array and the switch as a resource ... 46 lightSensor = (ILightSensor) Resources.lookup(ILightSensor.class); 47 myLEDs = (ITriColorLEDArray) Resources.lookup(ITriColorLEDArray.class); 48 // The resource lookup API supports tags ... this gets us Switch 2. 49 sw = (ISwitch) Resources.lookup(ISwitch.class, "SW2"); 50 51 if ((lightSensor == null) || (myLEDs == null) || (sw == null)) { 52 System.err.println("Could not obtain necessary resources"); 53 notifyDestroyed(); 54 } 55 56 // Define & bind an anonymous switchlistener to change color on switch press 57 sw.addISwitchListener(new ISwitchListener() { 58 59 public void switchPressed(SwitchEvent evt) { 60 currentColorIdx = (currentColorIdx + 1) % (colorChoices.length); 61 currentColor = colorChoices[currentColorIdx]; 62 System.out.println("New color is " + currentColor); 63 } 64 65 public void switchReleased(SwitchEvent evt) { } 66 }); 67 68 while (true) { // In a loop ... 69 try { 70 // Get light reading ... 71 lightVal = lightSensor.getValue(); 72 System.out.println("Light value is " + lightVal); 73 74 if (lightVal < LOW_LIGHT_THRESHOLD) { 75 // turn on the LEDs if light is below predefined threshold 76 for (int i = 0; i < myLEDs.size(); i++) { 77 myLEDs.getLED(i).setColor(currentColor); 78 myLEDs.getLED(i).setOn(); 79 } 80 } else { 81 // otherwise, turn off the LEDs 82 for (int i = 0; i < myLEDs.size(); i++) { 83 myLEDs.getLED(i).setOff(); 84 } 85 } 86 } catch (Exception e) { 87 System.err.println("Caught " + e); 88 e.printStackTrace(); 89 } 90 91 Utils.sleep(500); 92 } 93 } 94 95 protected void pauseApp() { } 96 97 protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { 98 for (int i = 0; i < myLEDs.size(); i++) { 99 myLEDs.getLED(i).setOff(); 100 } 101 } 102 } 103