Demo目的
该demo是HUST OpenSourceClub SunSpot组第一个demo,由张凯同学负责开发完成,目的是熟悉SunSpot的机制,练习控制SunSpot的switch和LED灯以及无线数据传输,为以后实现idea做基础练习。
Demo思路
该demo分为host application和on_sunspot application两部分,host application接受一串字符串,并对其编码(在这里我们直接使用了ASCII码),然后传输给SunSpot。SunSpot接受到这些编码后,用LED灯将每个字母及符号的二进制码表示出来。当用户记录下一串二进制码,或将其翻译成字母之后,按SunSpot上的switch1可以获取下一个编码。
Demo代码
on_sunspot application:
/*
* SwitchDemo.java
*
* Copyright (c) 2009 HUST OpenSourceClub
*
*/
package org.hustopensourceclub.demo;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ISwitchListener;
import com.sun.spot.util.Utils;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.spot.io.j2me.radiostream.* ;
import com.sun.spot.peripheral.*;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import java.io.*;
import javax.microedition.io.*;
/**
* @author: Zhang Kai
*/
public class SwitchDemo extends MIDlet implements ISwitchListener {
private static final String ADD = "0014.4F01.0000.120e";
private static final String PORT = "100";
private static final int LED_NUM = 7 , DELAY = 1000;
private int order ;
private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
private ISwitch sw ;
protected void startApp() throws MIDletStateChangeException {
try{
run() ;
} catch (IOException e)
{}
}
protected void run() throws IOException {
RadiostreamConnection conn = (RadiostreamConnection)Connector.open("radiostream://"+ADD+":"+PORT);
DataInputStream dis = conn.openDataInputStream();
DataOutputStream dos = conn.openDataOutputStream();
sw = EDemoBoard.getInstance().getSwitches()[EDemoBoard.SW1] ;
sw.addISwitchListener(this);
for (int i = 0; i < LED_NUM; i++)
{
leds[i].setColor(LEDColor.BLUE);
leds[i].setOn();
}
while (true) {
for (int i = 0; i < LED_NUM; i++)
leds[i].setOff() ;
try {
order = dis.readInt() ;
for (int i = 0; i < LED_NUM ; i++) {
if ((order % 2) == 1)
leds[i].setOn() ;
else
leds[i].setOff() ;
order /= 2;
}
Utils.sleep(DELAY);
sw.waitForChange() ;
if (sw.isClosed())
{
dos.writeBoolean(true);
dos.flush();
}
} catch (NoRouteException e) {
System.out.println ("No route to 0014.4F01.0000.0007");
}
}
}
public void switchPressed(ISwitch sw) {
}
public void switchReleased(ISwitch sw) {
}
protected void pauseApp() {
// This will never be called by the Squawk VM
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// Only called if startApp throws any exception other than MIDletStateChangeException
}
}
Host application:
/*
* SwitchDemo.java
*
* Copyright (c) 2009 HUST OpenSourceClub
*
*/
package org.opensourceclub.demo;
import com.sun.spot.io.j2me.radiostream.* ;
import com.sun.spot.peripheral.*;
import java.io.*;
import javax.microedition.io.*;
/**
* @author: Zhang Kai
*/
public class SwitchDemo_onDesktop {
// Broadcast port on which we listen for sensor samples
private static final String ADD = "0014.4F01.0000.13e3";
private static final String PORT = "100";
private int order ;
private void run() throws Exception {
RadiostreamConnection conn = (RadiostreamConnection)Connector.open("radiostream://"+ADD+":"+PORT);
DataInputStream dis = conn.openDataInputStream();
DataOutputStream dos = conn.openDataOutputStream();
while (true) {
order = System.in.read() ;
try {
while ((order) =='\n')
order = System.in.read() ;
dos.writeInt(order) ;
System.out.println("ok") ;
dos.flush();
if (dis.readBoolean()) ;
} catch (NoRouteException e) {
System.out.println ("No route to"+ADD);
}
}
}
/**
* Start up the host application.
*
* @param args any command line arguments
*/
public static void main(String[] args) throws Exception {
SwitchDemo_onDesktop app = new SwitchDemo_onDesktop();
app.run();
}
}