📄 sensor.java
字号:
/*****************************************************************************
*
* Description: Opens a connection to the accelerometer in order to display data
* received from it on a canvas.
*
* @author Gustav Stenlund 2007-08-01
* @file Sensor.java
* @version 1.0
*
* COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004.
* The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
* The use of the software is subject to the terms of the end-user license
* agreement which accompanies or is included with the software. The software is
* provided "as is" and Sony Ericsson specifically disclaim any warranty or
* condition whatsoever regarding merchantability or fitness for a specific
* purpose, title or non-infringement. No warranty of any kind is made in
* relation to the condition, suitability, availability, accuracy, reliability,
* merchantability and/or non-infringement of the software provided herein.
*
*****************************************************************************/
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.sensor.*;
import com.nokia.mid.ui.*;
public class Sensor extends MIDlet implements Runnable, DataListener{
private C canvas;
private int x=0;
private int y=0;
private int z=0;
private SensorConnection sensor;
private final String URL = "sensor:acceleration;contextType=user;model=ST_LIS302DL;location=inside";
public void startApp() {
canvas = new C();
Display.getDisplay(this).setCurrent(canvas);
printInfo();
new Thread(this).start();
DeviceControl.setLights(0, 100);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
try {
sensor.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
try {
sensor = (SensorConnection)Connector.open(URL);
sensor.setDataListener(this, 10);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void printInfo(){
String sensorVersion = System.getProperty("microedition.sensor.version");
System.out.println("Sensor version: "+sensorVersion);
//To query for a specific sensor:
//SensorInfo[] si = SensorManager.findSensors("acceleration", SensorInfo.CONTEXT_TYPE_USER);
//To query for all available sensors:
SensorInfo[] si = SensorManager.findSensors(null, null);
for(int i=0; i<si.length; i++){
SensorInfo s = si[i];
System.out.println("getDescription: "+s.getDescription());
System.out.println("isAvailable: "+s.isAvailable());
System.out.println("isAvailabilityPushSupported: "+s.isAvailabilityPushSupported());
System.out.println("isConditionPushSupported: "+s.isConditionPushSupported());
System.out.println("getConnectionType: "+s.getConnectionType());
System.out.println("getContextType: "+s.getContextType());
System.out.println("getMaxBufferSize: "+s.getMaxBufferSize());
System.out.println("getModel: "+s.getModel());
System.out.println("getQuantity: "+s.getQuantity());
System.out.println("getUrl: "+s.getUrl());
System.out.println("Properties:");
String[] propNames = s.getPropertyNames();
for(int r=0; r<propNames.length; r++){
System.out.println(" "+propNames[r]+": "+s.getProperty(propNames[r]));
}
System.out.println("Channels:");
ChannelInfo[] cInfos = s.getChannelInfos();
for(int r=0; r<cInfos.length; r++){
ChannelInfo c = cInfos[r];
System.out.println(" getName: "+c.getName());
int type = c.getDataType();
switch(type){
case ChannelInfo.TYPE_INT:
System.out.println(" Data type: TYPE_INT");
break;
case ChannelInfo.TYPE_DOUBLE:
System.out.println(" Data type: TYPE_DOUBLE");
break;
case ChannelInfo.TYPE_OBJECT:
System.out.println(" Data type: TYPE_OBJECT");
break;
}
System.out.println(" getScale: "+c.getScale());
System.out.println(" getUnit: "+c.getUnit().toString());
System.out.println(" getAccuracy: "+c.getAccuracy());
MeasurementRange[] ranges = c.getMeasurementRanges();
System.out.println(" Measurement range:");
for(int p=0; p<ranges.length; p++){
MeasurementRange m = ranges[p];
System.out.println(" getSmallestValue:"+m.getSmallestValue());
System.out.println(" getLargestValue: "+m.getLargestValue());
System.out.println(" getResolution: "+m.getResolution());
}
}
}
}
public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost){
//Calculate the maximum absolute value
for(int i=0; i<data.length; i++){
if(data[i].getChannelInfo().getName().compareTo("X") == 0){
x = getMax(data[i].getIntValues());
} else if(data[i].getChannelInfo().getName().compareTo("Y") == 0){
y = getMax(data[i].getIntValues());
} else if(data[i].getChannelInfo().getName().compareTo("Z") == 0){
z = getMax(data[i].getIntValues());
}
}
canvas.repaint();
}
private int getMax(int[] values){
int max = 0;
for(int i=0; i<values.length; i++)
if(Math.abs(max) < Math.abs(values[i]))
max = values[i];
return max;
}
class C extends Canvas{
public C(){
setFullScreenMode(true);
}
protected void paint(Graphics g) {
Font textFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
Font titleFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
int textHeight = textFont.getHeight();
int textWidth = textFont.stringWidth("X: -2300");
int titleHeight = titleFont.getHeight();
int titleWidth = titleFont.stringWidth("Accelerometer");
g.setColor(0xFF0000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0);
g.setFont(titleFont);
g.drawString("Accelerometer", (getWidth()/2)-(titleWidth/2), (getHeight()/2)-((textHeight/2)+textHeight+textHeight+20), Graphics.TOP|Graphics.LEFT);
g.setFont(textFont);
g.drawString("X: "+x, (getWidth()/2)-(textWidth/2), (getHeight()/2)-((textHeight/2)+textHeight+10), Graphics.LEFT|Graphics.TOP);
g.drawString("Y: "+y, (getWidth()/2)-(textWidth/2), (getHeight()/2)-(textHeight/2), Graphics.LEFT|Graphics.TOP);
g.drawString("Z: "+z, (getWidth()/2)-(textWidth/2), (getHeight()/2)+((textHeight/2)+10), Graphics.LEFT|Graphics.TOP);
}
protected void keyPressed(int keyCode) {
if(keyCode == -8){ //Clear button
destroyApp(true);
notifyDestroyed();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -