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

2007年7月23日星期一

Sun SPOT Mouse Version 1.0

FreeRange Sun SPOT Side

/*
* StartApplication.java
*
* Created on October 8, 2006, 12:15 AM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.*;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* The startApp method of this class is called by the VM to start the
* application.
*
* The manifest specifies this class as MIDlet-1, which means it will
* be selected for execution.
*/

public class StartApplication extends MIDlet {
private IAccelerometer3D accel = EDemoBoard.getInstance().getAccelerometer();
private ISwitch sw1, sw2;
private int st=0;

protected void startApp() throws MIDletStateChangeException{
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host

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

sw1 = EDemoBoard.getInstance().getSwitches()[0];
sw2 = EDemoBoard.getInstance().getSwitches()[1];

startSW1WatchThread();
startSW2WatchThread();
startAcceWatchThreadd();
}

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
}

private void checkingMovements() {
try {
String msg = String.valueOf((int)Math.toDegrees(accel.getTiltX()))
+";"+String.valueOf((int)Math.toDegrees(accel.getTiltY()));
System.out.println(msg);
sendOut(msg);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

private synchronized void sendOut(String msg) {
System.out.println(msg);
/*
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
*/


try{
RadiogramConnection conn =(RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.1455:100"); //The IP address here is the address of base station
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try{
dg.writeUTF(msg);
conn.send(dg);
}finally{
conn.close();
}
}catch(IOException e){
}
}

public void startSW1WatchThread(){
Runnable r = new Runnable(){
public void run(){
while(true){
sw1.waitForChange();
if(sw1.isClosed()){
sendOut("01");
} else {
sendOut("00");
}
}
}
};
Thread tsw1= new Thread(r);
tsw1.setPriority(Thread.MAX_PRIORITY);
tsw1.start();
}

public void startSW2WatchThread(){
Runnable r = new Runnable(){
public void run(){
while(true){
sw2.waitForChange();
if(sw2.isClosed()){
sendOut("11");
} else {
sendOut("10");
}
}
}
};
Thread tsw2= new Thread(r);
tsw2.setPriority(Thread.MAX_PRIORITY);
tsw2.start();
}

public void startAcceWatchThreadd(){
Runnable r = new Runnable() {
public void run() {
while(true){
checkingMovements();
}
}
};
Thread ta= new Thread(r);
ta.start();
}
}
 

Base Station and Host PC Application

/*
* SunSpotHostApplication.java
*
* Created on December 21, 2006, 16:45 PM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.IEEEAddress;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;

import java.io.*;
import javax.microedition.io.*;


/**
* Sample Sun SPOT host application
*/

public class SunSpotHostApplication {
double screenWidth;
double screenHeight;
private Dimension dim;
int initialSpeed=50;
int gravity;
PointerInfo pt;
Point Cursorlocation;
double initialX;
double initialY;
Robot r;
/**
* Print out basestation address.
*/

public void run() {
dim=Toolkit.getDefaultToolkit().getScreenSize();
screenWidth=dim.getWidth();
screenHeight=dim.getHeight();

IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
System.out.println("Our radio address = " + ourAddr.asDottedHex());
while(true){
try{
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:100");
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try {
conn.receive(dg);
String rawData = dg.readUTF();
String[] splitStr = rawData.split(";");
if(splitStr.length==1){
clickMouseButton(splitStr[0]);
}else if(splitStr.length==2){
moveCursor(splitStr[0],splitStr[1]);
}
} catch (NoAckException e) {
e.printStackTrace();
} finally {
conn.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}

/**
* Start up the host application.
*
* @param args any command line arguments
*/

public static void main(String[] args) {
SunSpotHostApplication app = new SunSpotHostApplication();
app.run();
}

private void moveCursor(String accelX, String accelY) {
pt=MouseInfo.getPointerInfo();
Cursorlocation=pt.getLocation();
initialX=Cursorlocation.getX();
initialY=Cursorlocation.getY();

int intaccX=(int)Double.parseDouble(accelX);
int intaccY=(int)Double.parseDouble(accelY);
int posx=(int)initialX+intaccX/2;
int posy=(int)initialY-intaccY/2;
try {
r = new Robot();
r.mouseMove(posx,posy);
} catch (AWTException ex) {
ex.printStackTrace();
}

}

private void clickMouseButton(String btn) {
try {
r = new Robot();
if(btn.equalsIgnoreCase("01"))
r.mousePress(InputEvent.BUTTON1_MASK);
if(btn.equalsIgnoreCase("00"))
r.mouseRelease(InputEvent.BUTTON1_MASK);
if(btn.equalsIgnoreCase("11"))
r.mousePress(InputEvent.BUTTON3_MASK);
if(btn.equalsIgnoreCase("10"))
r.mouseRelease(InputEvent.BUTTON3_MASK);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
}

5 条评论:

Mythal 说...

你好啊,我也在做sunspot的project,交个朋友吧。。。

Mythal 说...

:——)

Wujiang Er 说...

hi i am interested too
BTW could have u mail, the same with Mythal. For i am 中国人 too。

匿名 说...

araç sorgulama
sorgulama
ehliyet
açıköğretim
bağkur sorgulama
ssk sorgulama
emekli sandığı
cinsellik
radyo dinle
korku
evlilik
hikaye
gazeteler
ilan
ssk
ehliyet sınav sonuçları
iş ilanları
bağkur
gazete oku

Unknown 说...

Ankara'nın en güzel escortlarıyla görüşmek için çankaya escort sayfamızı ziyaret edebilirsiniz.