package se.kth.csc; import com.sun.spot.sensorboard.EDemoBoard; import com.sun.spot.sensorboard.io.PinDescriptor; import com.sun.spot.sensorboard.peripheral.LEDColor; import com.sun.spot.util.Utils; import com.sun.spot.util.BootloaderListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Random; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.io.StreamConnection; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** This is the interak08 communication example **/ public class CommLecture extends MIDlet { /** * Main application loop. */ private void run()throws IOException { byteUartCommunication(); //httpCommunication(); //radiostreamCommunication(); } private void byteUartCommunication(){ EDemoBoard.getInstance().initUART(9600, false); while(true){ try{ System.out.print((char)EDemoBoard.getInstance().receiveUART()); }catch(IOException e){ if(e.getMessage().equals("empty uart buffer")) Utils.sleep(100); else System.out.println(e); } } } // this is untested, should work with the Purple API private void streamUartCommunication() throws IOException { InputStream is= Connector.openInputStream("edemoserial://usart?baudrate=9600"); byte[]buffer= new byte[256]; int length=-1; while((length=is.read(buffer))!=-1){ System.out.write(buffer, 0, length); } } private void radiostreamCommunication() throws IOException{ EDemoBoard.getInstance().getLED(EDemoBoard.LED1).setColor(LEDColor.BLUE); EDemoBoard.getInstance().getLED(EDemoBoard.LED2).setColor(LEDColor.RED); String addr= System.getProperty("IEEE_ADDRESS"); String suffix=addr.endsWith("190C")?"106C":"190C"; StreamConnection conn = (StreamConnection)Connector.open("radiostream://"+addr.substring(0, 15)+suffix+":100"); final DataInputStream dis = conn.openDataInputStream(); DataOutputStream dos = conn.openDataOutputStream(); Random r= new Random(Integer.valueOf(suffix, 16).intValue()); try { new Thread(new Runnable(){ public void run(){ try{ while(true){ dis.readUTF(); blinkLED(EDemoBoard.LED2); } }catch(IOException ioe){ System.out.println(ioe); } } }).start(); while(true){ Utils.sleep(Math.abs((r.nextInt())%5)*1000); dos.writeUTF("dummy"); dos.flush(); blinkLED(EDemoBoard.LED1); } } finally { dis.close(); dos.close(); conn.close(); } } private void blinkLED(PinDescriptor pd){ EDemoBoard.getInstance().getLED(pd).setOn(); Utils.sleep(100); EDemoBoard.getInstance().getLED(pd).setOff(); } private void httpCommunication() throws IOException{ EDemoBoard.getInstance().getLED(EDemoBoard.LED1).setColor(LEDColor.BLUE); EDemoBoard.getInstance().getLED(EDemoBoard.LED2).setColor(LEDColor.RED); EDemoBoard.getInstance().getLED(EDemoBoard.LED3).setColor(LEDColor.GREEN); try{ HttpConnection conn= (HttpConnection)Connector.open("http://www.kth.se/index.html"); conn.setRequestProperty("Connection", "close"); InputStream is = conn.openInputStream(); byte[]buffer= new byte[256]; int length=-1; EDemoBoard.getInstance().getLED(EDemoBoard.LED1).setOn(); while((length=is.read(buffer))!=-1){ EDemoBoard.getInstance().getLED(EDemoBoard.LED3).setOff(); System.out.write(buffer, 0, length); EDemoBoard.getInstance().getLED(EDemoBoard.LED1).setOn(); } System.out.flush(); EDemoBoard.getInstance().getLED(EDemoBoard.LED1).setOff(); EDemoBoard.getInstance().getLED(EDemoBoard.LED3).setOn(); }catch(IOException e){ // we turn on RED to show error EDemoBoard.getInstance().getLED(EDemoBoard.LED2).setOn(); } } /** * The rest is boiler plate code, for Java ME compliance * * startApp() is the MIDlet call that starts the application. */ protected void startApp() throws MIDletStateChangeException { new BootloaderListener().start(); // Listen for downloads/commands over USB connection try { run(); } catch (IOException ex) { //A problem in reading the sensors. ex.printStackTrace(); } } /** * This will never be called by the Squawk VM. */ protected void pauseApp() { } /** * Only called if startApp throws any exception other than MIDletStateChangeException. */ protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } }