如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2008年4月13日星期日

Digest: Starting with Sun SPOT using NetBeans 6.1

Digested from http://weblogs.java.net/blog/brunogh/archive/2008/04/starting_with_s.html from  Bruno Ghisi's Blog.

 

If you do not have a Sun SPOT (Figure 1), do not be bored, you can still have a lot of fun! In this entry, I gonna explain how to start programming the world using NetBeans 6.1!

sun_spot2.jpg
Figure 1: Sun SPOT rulez!!!

Downloading NetBeans IDE 6.1


1. Download NetBeans IDE 6.1 (currently in the RC 1). A lot of new nice features, take a look in the New and Noteworthy and in the release page for a detailed list.

Installing Sun SPOT plugin


2. Install Sun SPOT plugin into NetBeans. Download com-sun-sunspot-updatecenter.nbm. Now, in NetBeans, go to:
2.1 Tools menu -> Plugin.
2.2 Click on Downloaded tab.
2.3 Click on Add Plugins button and select the file. Then, confirm the installation clicking on Install button and accept the license agreement.
2.4 Finally, go to the Available Plugins tab, mark SunSPOTApplicationTemplate, SunSPOTHostApplicationTemplate and Sun SPOT Info, click on Install button and accept the license agreement.
2.5 You can check that everything is installed going to Installed tab (Figure 2).
netbeans_spot1.png
Figure 2: Installed tab.

PS: David Simmons has blogged about these steps before.

Getting Sun SPOT SDK 3.0 Beta


3. Get the Sun SPOT SDK 3.0 Beta emailing David Simmons, more details here. You will receive a link to download SPOTManager tool, that is a jar that installs the SDK and can run the emulator. Be sure you have at least JDK 1.5 and at least Ant 1.6.5 installed and configured in your system.

Creating the Demo Application


4. Let's make the default demo application work. Go to:
4.1 File menu -> New Project
4.2 Select Java category, select Sun SPOT project and then click Next.
4.3 Leave the default project name and package and click Finish.
4.4 Open org.sunspotworld.StartApplication.java. You can see it extends MIDlet and already has some code. If you are familiar with Java ME, you will not have big problems. But, basically, Sun SPOT arquitecture is CLDC (Connected Limited Device Profile) 1.1 and has IMP (Information Module Profile) in the top - which can be defined as a MIDP (Mobile Information Device Profile) without UI stuff. Also, it has some additional libraries and all this runs in a VM called Squawk, that is characterized by being most written in Java. So, StartApplication implements the abstract methods inherited from the MIDlet (startApp,pauseApp that Squawk never calls and destroyApp). It works in a sand box enviromnent.
The startApp code, in List 1, basically gets an object reference to the singleton eDemo Board and then make its LED blink red for a quarter of second each second. As you, can see, the code is pretty high level and easy to understand.

    protected void startApp() throws MIDletStateChangeException {
System.out.println("Hello, world");
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host

long ourAddr = Spot.getInstance().getRadioPolicyManager().getIEEEAddress();
System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));

ISwitch sw1 = EDemoBoard.getInstance().getSwitches()[EDemoBoard.SW1];
leds[0].setRGB(100,0,0); // set color to moderate red
while (sw1.isOpen()) { // done when switch is pressed
leds[0].setOn(); // Blink LED
Utils.sleep(250); // wait 1/4 seconds
leds[0].setOff();
Utils.sleep(1000); // wait 1 second
}
notifyDestroyed(); // cause the MIDlet to exit
}
List 1: startApp code.


Running the Demo Application in the emulator




5. Put this application to run in the emulator.


5.1 Right click the project and select Build. It will generate the jar in the /$PROJECT_HOME/suite/. If you have a spot device, you could send it directly by right clicking the project and selecting Build Project + Deploy to Sun SPOT , forgetting the next steps.


5.2 Open the emulator (in the ToolManager, go to Solarium tab and then click in the Solarium button). Then, in the emulator, click on the Emulator menu -> New virtual SPOT, you will notice that a Sun SPOT will appear in the squared right area.


5.3 Right click on the Sun SPOT picture, then click Specify application jar file... and select the application jar in /$PROJECT_HOME/suite/, which is the place that the application was built in step 5.1.


5.4 After that, click again on it, then Run MIDlet and StartApplication. Finally, you will see a red LED blinking!!! Wow!

netbeans_spot2.png



Figure 3: NetBeans 6.1, ToolManager, Emulator and the red LED blinking!



Where I go next?




In NetBeans, check the Sun SPOT Info, that you have installed with the plugin, by clicking in the Window menu.


David Simmons' Blog - http://blogs.sun.com/davidgs


Roger Meike's Blog - http://blogs.sun.com/roger


Sun SPOT Feeds (really nice aggregator with more cool blogs) - http://planets.sun.com/SunSPOT/group/SunSPOT


Project Sun SPOT - https://www.sunspotworld.com


Open source Sun SPOT - https://spots.dev.java.net (take a look in its subprojects as well).


Search for spaughts in youtube.com or subscribe the tag feed directly.


Recently, Aaron Houston has organized a conference call about Sun SPOT with Roger Meike and published the nice links we get during it in an email he sent. The links are available here too. Slides here.

没有评论: