[openintents commit] r268 - in branches/developers/peli/shoppinglist01a: openintents openintents/res/layout openintent...

1 view
Skip to first unread message

codesite...@google.com

unread,
Jan 14, 2008, 6:16:10 PM1/14/08
to openintents...@googlegroups.com
Author: peli0101
Date: Mon Jan 14 15:15:45 2008
New Revision: 268

Added:
branches/developers/peli/shoppinglist01a/openintents/res/layout/sensorsimulator.xml
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorClient.java
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorView.java
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/Sensors.java
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/MobilePanel.java
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorServer.java
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/Vector.java
Modified:
branches/developers/peli/shoppinglist01a/openintents/AndroidManifest.xml
branches/developers/peli/shoppinglist01a/openintents/res/values/strings.xml
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/R.java
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/main/OpenIntents.java
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorSimulator.java

Log:
Basic connection between SensorSimulatorClient and
SensorSimulatorServer works.

Modified: branches/developers/peli/shoppinglist01a/openintents/AndroidManifest.xml
==============================================================================
---
branches/developers/peli/shoppinglist01a/openintents/AndroidManifest.xml (original)
+++
branches/developers/peli/shoppinglist01a/openintents/AndroidManifest.xml
Mon Jan 14 15:15:45 2008
@@ -83,5 +83,14 @@
</intent-filter>
</activity>

+ <!-- SensorSimulator -->
+ <activity class=".sensorsimulator.SensorSimulatorView"
+ android:label="@string/sensorsimulator">
+ <intent-filter>
+ <action android:value="android.intent.action.MAIN" />
+ <category android:value="android.intent.category.DEFAULT" />

+ </intent-filter>
+ </activity>
+
</application>
</manifest>

Added: branches/developers/peli/shoppinglist01a/openintents/res/layout/sensorsimulator.xml
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/openintents/res/layout/sensorsimulator.xml
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2007-2008 OpenIntents.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ >
+<TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="OpenIntents SensorSimulator"
+ />
+<EditText id="@+id/edittext"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ />
+<Button id="@+id/button"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/connect" />
+</LinearLayout>

Modified: branches/developers/peli/shoppinglist01a/openintents/res/values/strings.xml
==============================================================================
---
branches/developers/peli/shoppinglist01a/openintents/res/values/strings.xml (original)
+++
branches/developers/peli/shoppinglist01a/openintents/res/values/strings.xml
Mon Jan 14 15:15:45 2008
@@ -56,6 +56,10 @@
<string name="cancel">Cancel</string>
<string name="ok">OK</string>

+ <!-- Sensor Simulator -->
+ <string name="sensorsimulator">Sensor Simulator</string>
+ <string name="connect">Connect</string>
+
<!-- Key codes -->
<string name="key_return">64</string>
<string name="test">test</string>

Modified: branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/R.java
==============================================================================
---
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/R.java (original)
+++
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/R.java
Mon Jan 14 15:15:45 2008
@@ -18,31 +18,32 @@
public static final class id {
public static final int about=0x7f050009;
public static final int activities=0x7f050008;
- public static final int add_tag_button=0x7f050018;
- public static final int background=0x7f05000a;
- public static final int button_add_item=0x7f05000e;
+ public static final int add_tag_button=0x7f050019;
+ public static final int background=0x7f05000b;
+ public static final int button=0x7f05000a;
+ public static final int button_add_item=0x7f05000f;
public static final int cancel=0x7f050002;
- public static final int contents=0x7f05001b;
+ public static final int contents=0x7f05001c;
public static final int edittext=0x7f050001;
- public static final int edittext_add_item=0x7f05000d;
- public static final int image=0x7f05000f;
- public static final int image_URI=0x7f050010;
+ public static final int edittext_add_item=0x7f05000e;
+ public static final int image=0x7f050010;
+ public static final int image_URI=0x7f050011;
public static final int latitude=0x7f050004;
- public static final int list_items=0x7f05000c;
+ public static final int list_items=0x7f05000d;
public static final int listempty=0x7f050007;
public static final int locations=0x7f050006;
public static final int longitude=0x7f050005;
- public static final int name=0x7f050011;
- public static final int new_content=0x7f050017;
- public static final int new_tag=0x7f050016;
+ public static final int name=0x7f050012;
+ public static final int new_content=0x7f050018;
+ public static final int new_tag=0x7f050017;
public static final int ok=0x7f050003;
- public static final int spinner_listfilter=0x7f05000b;
- public static final int tag_content=0x7f050013;
- public static final int tag_filter=0x7f050019;
- public static final int tag_tag=0x7f050012;
- public static final int tag_uri_1=0x7f050014;
- public static final int tag_uri_2=0x7f050015;
- public static final int tags=0x7f05001a;
+ public static final int spinner_listfilter=0x7f05000c;
+ public static final int tag_content=0x7f050014;
+ public static final int tag_filter=0x7f05001a;
+ public static final int tag_tag=0x7f050013;
+ public static final int tag_uri_1=0x7f050015;
+ public static final int tag_uri_2=0x7f050016;
+ public static final int tags=0x7f05001b;
public static final int text=0x7f050000;
}
public static final class layout {
@@ -51,10 +52,11 @@
public static final int location_row=0x7f030002;
public static final int locations=0x7f030003;
public static final int main=0x7f030004;
- public static final int shopping=0x7f030005;
- public static final int shopping_item_row=0x7f030006;
- public static final int tag_row=0x7f030007;
- public static final int tags=0x7f030008;
+ public static final int sensorsimulator=0x7f030005;
+ public static final int shopping=0x7f030006;
+ public static final int shopping_item_row=0x7f030007;
+ public static final int tag_row=0x7f030008;
+ public static final int tags=0x7f030009;
}
public static final class string {
public static final int about_openintents=0x7f040002;
@@ -67,9 +69,10 @@
public static final int cancel=0x7f040016;
public static final int clean_up_list=0x7f040010;
public static final int confirm_delete_list=0x7f040014;
+ public static final int connect=0x7f040019;
public static final int default_tag=0x7f040001;
public static final int delete_list=0x7f040011;
- public static final int key_return=0x7f040018;
+ public static final int key_return=0x7f04001a;
public static final int list_is_empty=0x7f040007;
public static final int list_of_contents=0x7f040009;
public static final int list_of_locations=0x7f040004;
@@ -80,8 +83,9 @@
public static final int no_shopping_provider=0x7f040015;
public static final int ok=0x7f040017;
public static final int select=0x7f04000a;
+ public static final int sensorsimulator=0x7f040018;
public static final int shopping_list=0x7f04000d;
- public static final int test=0x7f040019;
+ public static final int test=0x7f04001b;
public static final int view_location=0x7f040006;
}
}

Modified: branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/main/OpenIntents.java
==============================================================================
---
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/main/OpenIntents.java (original)
+++
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/main/OpenIntents.java
Mon Jan 14 15:15:45 2008
@@ -18,6 +18,7 @@

import org.openintents.R;
import org.openintents.locations.LocationsView;
+import org.openintents.sensorsimulator.SensorSimulatorView;
import org.openintents.shopping.ShoppingView;
import org.openintents.tags.TagsView;

@@ -43,7 +44,12 @@
*/
public class OpenIntents extends Activity implements
OnItemClickListener {

- private String[] activitylist = { "Show locations", "Show
tags", "Shopping list"};
+ private String[] activitylist = {
+ "Show locations",
+ "Show tags",
+ "Shopping list",
+ "SensorSimulator"
+ };

/** Called when the activity is first created. */
@Override
@@ -78,7 +84,12 @@
intent = new Intent(this, ShoppingView.class);
startActivity(intent);
break;
-
+
+ case 3:
+ intent = new Intent(this, SensorSimulatorView.class);
+ startActivity(intent);
+ break;
+
default:
throw new IllegalArgumentException("Unknown position " + position);
}

Added: branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorClient.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorClient.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,130 @@
+package org.openintents.sensorsimulator;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+public class SensorSimulatorClient {
+
+ public boolean connected;
+
+ Socket mSocket;
+ PrintWriter mOut;
+ BufferedReader mIn;
+
+ public SensorSimulatorClient() {
+ connected = false;
+ }
+
+ public void connect() {
+
+ // if (connected) return;
+
+ mSocket = null;
+ mOut = null;
+ mIn = null;
+
+ try {
+ mSocket = new Socket("192.168.178.22", 4444);
+ mOut = new PrintWriter(mSocket.getOutputStream(), true);
+ mIn = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
+ } catch (UnknownHostException e) {
+ System.err.println("Don't know about host: x.x.x.x.");
+ System.exit(1);
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection
to: x.x.x.x.");
+ System.exit(1);
+ }
+
+ String fromServer = "";
+ try {
+ fromServer = mIn.readLine();
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: x.x.x.x.");
+ System.exit(1);
+ }
+
+ if (fromServer.equals("SensorSimulator")) {
+ // OK
+ connected = true;
+ } else {
+ // Who is that???
+ disconnect();
+ }
+
+ }
+
+ public void disconnect() {
+ try {
+ mOut.close();
+ mIn.close();
+ mSocket.close();
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: x.x.x.x.");
+ System.exit(1);
+ }
+
+ connected = false;
+ }
+
+ public String[] getSupportedSensors() {
+
+ mOut.println("getSupportedSensors()");
+ String[] sensors = {""};
+ int num = 0;
+
+ try {
+ String numstr = mIn.readLine();
+ num = Integer.parseInt(numstr);
+
+ sensors = new String[num];
+ for (int i=0; i<num; i++) {
+ sensors[i] = mIn.readLine();
+ }
+
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: x.x.x.x.");
+ System.exit(1);
+ }
+
+ return sensors;
+ }
+
+ public int getNumSensorValues(String sensor) {
+ mOut.println("getNumSensorValues()");
+ mOut.println(sensor);
+ int num = 0;
+
+ try {
+ String numstr = mIn.readLine();
+ num = Integer.parseInt(numstr);
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: x.x.x.x.");
+ System.exit(1);
+ }
+
+ return num;
+ }
+
+ public void readSensor(String sensor, float[] sensorValues) {
+ mOut.println("readSensor()");
+ mOut.println(sensor);
+ int num = 0;
+
+ try {
+ String numstr = mIn.readLine();
+ num = Integer.parseInt(numstr);
+
+ //sensorValues = new float[num];
+ for (int i=0; i<num; i++) {
+ sensorValues[i] = Float.parseFloat(mIn.readLine());
+ }
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: x.x.x.x.");
+ System.exit(1);
+ }
+ }
+}

Added: branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorView.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/SensorSimulatorView.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,68 @@
+package org.openintents.sensorsimulator;
+
+import org.openintents.R;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.EditText;
+
+public class SensorSimulatorView extends Activity {
+ /**
+ * TAG for logging.
+ */
+ private static final String TAG = "SensorSimulator";
+
+ EditText mEditText;
+
+ /* (non-Javadoc)
+ * @see android.app.Activity#onCreate(android.os.Bundle)
+ */
+ @Override
+ protected void onCreate(Bundle icicle) {
+ // TODO Auto-generated method stub
+ super.onCreate(icicle);
+
+ setContentView(R.layout.sensorsimulator);
+
+ mEditText = (EditText) findViewById(R.id.edittext);
+
+ Button button = (Button) findViewById(R.id.button);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ connect();
+ }
+ });
+
+ }
+
+ public void connect() {
+
+ Sensors.mClient.connect();
+
+ readAllSensors();
+ }
+
+ public void readAllSensors() {
+ String data = "";
+ String[] mSupportedSensors = Sensors.getSupportedSensors();
+ for (int i=0; i<mSupportedSensors.length; i++) {
+ data += mSupportedSensors[i] + ":";
+
+ Sensors.enableSensor(mSupportedSensors[i]);
+ //Sensors.disableSensor(mSupportedSensors[i]);
+ int num = Sensors.getNumSensorValues(mSupportedSensors[i]);
+ float[] val = new float[num];
+ Sensors.readSensor(mSupportedSensors[i], val);
+ for (int j=0; j<num; j++) {
+ data += " " + val[j];
+ }
+ data += "\n";
+ }
+ mEditText.setText(data);
+ }
+
+
+}

Added: branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/Sensors.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/openintents/src/org/openintents/sensorsimulator/Sensors.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,53 @@
+package org.openintents.sensorsimulator;
+
+public class Sensors extends android.hardware.Sensors {
+
+ public static final String SENSOR_ACCELEROMETER
+ = android.hardware.Sensors.SENSOR_ACCELEROMETER;
+ public static final String SENSOR_COMPASS
+ = android.hardware.Sensors.SENSOR_COMPASS;
+ public static final String SENSOR_ORIENTATION
+ = android.hardware.Sensors.SENSOR_ORIENTATION;
+
+ // Extensions
+ public static SensorSimulatorClient mClient = new SensorSimulatorClient();
+
+ public Sensors() {
+ super();
+ }
+
+ public static void disableSensor(String sensor) {
+ android.hardware.Sensors.disableSensor(sensor);
+ }
+
+ public static void enableSensor(String sensor) {
+ android.hardware.Sensors.enableSensor(sensor);
+ }
+
+ public static int getNumSensorValues(String sensor) {
+ if (mClient.connected) {
+ return mClient.getNumSensorValues(sensor);
+ } else {
+ return android.hardware.Sensors.getNumSensorValues(sensor);
+ }
+ }
+
+ public static String[] getSupportedSensors() {
+ if (mClient.connected) {
+ return mClient.getSupportedSensors();
+ } else {
+ return android.hardware.Sensors.getSupportedSensors();
+ }
+ }
+
+ public static void readSensor(String sensor, float[] sensorValues) {
+ if (mClient.connected) {
+ mClient.readSensor(sensor, sensorValues);
+ } else {
+ android.hardware.Sensors.readSensor(sensor, sensorValues);
+ }
+
+ }
+
+
+}

Added: branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/MobilePanel.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/MobilePanel.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,148 @@
+package org.openintents.tools.sensorsimulator;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.geom.Line2D;
+
+import javax.swing.BorderFactory;
+import javax.swing.JPanel;
+import javax.swing.JSlider;
+
+class MobilePanel2 extends JPanel {
+
+ double yaw;
+ double pitch;
+ double roll;
+
+ int mousedownx;
+ int mousedowny;
+ int mousedownyaw;
+ int mousedownpitch;
+ int mousedownroll;
+
+ JSlider yawSlider;
+ JSlider pitchSlider;
+ JSlider rollSlider;
+
+ /*
+ * http://code.google.com/android/reference/android/hardware/Sensors.html
+ *
+ * With the device lying flat on a horizontal surface in front of the user,
+ * oriented so the screen is readable by the user in the normal fashion,
+ * the X axis goes from left to right, the Y axis goes from the user
+ * toward the device, and the Z axis goes upwards perpendicular to the
+ * surface.
+ */
+ // Mobile size
+ final double sx = 15; // size x
+ final double sy = 40; // size y
+ final double sz = 5; // size z
+
+ // Display size
+ final double dx = 12; // size x
+ final double dy1 = 37; // size y
+ final double dy2 = -15;
+
+ double[][] phone = {
+ // bottom shape
+ { sx, sy, -sz}, {-sx, sy, -sz},
+ {-sx, sy, -sz}, {-sx,-sy, -sz},
+ {-sx,-sy, -sz}, { sx,-sy, -sz},
+ { sx,-sy, -sz}, { sx, sy, -sz},
+ // top shape
+ { sx, sy, sz}, {-sx, sy, sz},
+ {-sx, sy, sz}, {-sx,-sy, sz},
+ {-sx,-sy, sz}, { sx,-sy, sz},
+ { sx,-sy, sz}, { sx, sy, sz},
+ // connectint top and bottom
+ { sx, sy, -sz}, { sx, sy, sz},
+ {-sx, sy, -sz}, {-sx, sy, sz},
+ {-sx,-sy, -sz}, {-sx,-sy, sz},
+ { sx,-sy, -sz}, { sx,-sy, sz},
+ // display
+ { dx, dy1, sz}, {-dx, dy1, sz},
+ {-dx, dy1, sz}, {-dx, dy2, sz},
+ {-dx, dy2, sz}, { dx, dy2, sz},
+ { dx, dy2, sz}, { dx, dy1, sz},
+ };
+
+ public MobilePanel2() {
+ setBorder(BorderFactory.createLineBorder(Color.black));
+
+ yaw = 0;
+ pitch = 0;
+ roll = 0;
+
+ addMouseListener(new MouseAdapter() {
+ public void mousePressed(MouseEvent e) {
+ //moveSquare(e.getX(),e.getY());
+ mousedownx = e.getX();
+ mousedowny = e.getY();
+ mousedownyaw = yawSlider.getValue();
+ mousedownpitch = pitchSlider.getValue();
+ mousedownroll = rollSlider.getValue();
+ }
+ });
+
+ addMouseMotionListener(new MouseAdapter() {
+ public void mouseDragged(MouseEvent e) {
+ //moveSquare(e.getX(),e.getY());
+ // Control yaw
+ int newyaw = mousedownyaw - (e.getX() - mousedownx);
+ while (newyaw > 180) newyaw -= 360;
+ while (newyaw < -180) newyaw += 360;
+ yawSlider.setValue((int) newyaw);
+ yaw = newyaw;
+
+ // Control pitch
+ int newpitch = mousedownpitch + (e.getY() - mousedowny);
+ while (newpitch > 180) newpitch -= 360;
+ while (newpitch < -180) newpitch += 360;
+ pitchSlider.setValue((int) newpitch);
+ pitch = newpitch;
+
+ repaint();
+ }
+ });
+ }
+
+ public Dimension getPreferredSize() {
+ return new Dimension(250,200);
+ }
+
+ /**
+ * yaws a vector (rotate around z-axis)
+ */
+
+ protected void paintComponent(Graphics g) {
+ super.paintComponent(g);
+ // g.drawString("This is my custom Panel!",(int)yaw,(int)pitch);
+
+ Graphics2D g2 = (Graphics2D) g;
+ // draw Line2D.Double
+
+ double centerx = 100;
+ double centery = 100;
+ double centerz = -150;
+ for (int i=0; i<phone.length; i+=2) {
+ if (i==0) g2.setColor(Color.RED);
+ if (i==24) g2.setColor(Color.BLUE);
+
+ Vector v1 = new Vector(phone[i]);
+ Vector v2 = new Vector(phone[i+1]);
+ v1.rollpitchyaw(roll, pitch, yaw);
+ v2.rollpitchyaw(roll, pitch, yaw);
+ g2.draw(new Line2D.Double(
+ centerx + v1.x * centerz / (centerz - v1.y),
+ centery - v1.z * centerz / (centerz - v1.y),
+ centerx + v2.x * centerz / (centerz - v2.y),
+ centery - v2.z * centerz / (centerz - v2.y)));
+ }
+
+ }
+
+}
\ No newline at end of file

Added: branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorServer.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorServer.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,118 @@
+package org.openintents.tools.sensorsimulator;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+public class SensorServer implements Runnable {
+
+ SensorSimulator mSensorSimulator;
+
+ final String ACCELEROMETER = "accelerometer";
+ final String COMPASS = "compass";
+ final String ORIENTATION = "orientation";
+
+ String[] mSupportedSensors = {
+ ACCELEROMETER,
+ //COMPASS,
+ ORIENTATION
+ };
+
+ public SensorServer(SensorSimulator newSensorSimulator) {
+ mSensorSimulator = newSensorSimulator;
+
+ // start ourselves:
+ (new Thread(this)).start();
+ }
+
+ // Called when starting a thread for network connection
+ @Override
+ public void run() {
+ // TODO Auto-generated method stub
+
+ listen();
+ }
+
+ public void listen() {
+
+ ServerSocket serverSocket = null;
+ try {
+ serverSocket = new ServerSocket(4444);
+ } catch (IOException e) {
+ System.err.println("Could not listen on port: 4444.");
+ System.exit(1);
+ }
+
+ Socket clientSocket = null;
+ try {
+ clientSocket = serverSocket.accept();
+ } catch (IOException e) {
+ System.err.println("Accept failed.");
+ System.exit(1);
+ }
+
+ try {
+ PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(
+ clientSocket.getInputStream()));
+ String inputLine, outputLine;
+
+ outputLine = "SensorSimulator";
+ out.println(outputLine);
+
+ while ((inputLine = in.readLine()) != null) {
+ //outputLine = kkp.processInput(inputLine);
+ if (inputLine.equals("getSupportedSensors()")) {
+ out.println("" + mSupportedSensors.length);
+ for (int i=0; i<mSupportedSensors.length; i++) {
+ out.println(mSupportedSensors[i]);
+ }
+
+ } else if (inputLine.equals("getNumSensorValues()")) {
+ inputLine = in.readLine();
+ if (inputLine.equals(ACCELEROMETER)) {
+ out.println("3");
+ } else if (inputLine.equals(COMPASS)) {
+ out.println("3");
+ } else if (inputLine.equals(ORIENTATION)) {
+ out.println("3");
+ }
+ } else if (inputLine.equals("readSensor()")) {
+ inputLine = in.readLine();
+ if (inputLine.equals(ACCELEROMETER)) {
+ out.println("3");
+ out.println("1.0");
+ out.println("2.0");
+ out.println("3.0");
+ } else if (inputLine.equals(COMPASS)) {
+ out.println("3");
+ out.println("4.0");
+ out.println("5.0");
+ out.println("6.0");
+ } else if (inputLine.equals(ORIENTATION)) {
+ out.println("3");
+ out.println("7.0");
+ out.println("8.0");
+ out.println("9.0");
+ }
+ }
+ //outputLine = inputLine;
+ //mSensorSimulator.yawSlider.setValue(Integer.parseInt(inputLine));
+ //out.println(outputLine);
+ //if (outputLine.equals("Bye."))
+ //break;
+ }
+ out.close();
+ in.close();
+ clientSocket.close();
+ serverSocket.close();
+ } catch (IOException e) {
+ System.err.println("IOException.");
+ System.exit(1);
+ }
+ }
+}

Modified: branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorSimulator.java
==============================================================================
---
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorSimulator.java (original)
+++
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/SensorSimulator.java
Mon Jan 14 15:15:45 2008
@@ -1,24 +1,15 @@
package org.openintents.tools.sensorsimulator;
-//package start;
-
-//package components;

import java.awt.BorderLayout;
-import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
-import java.awt.geom.Line2D;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
@@ -31,202 +22,6 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

-class Vector {
- public double x;
- public double y;
- public double z;
-
- public Vector() {
- x = 0;
- y = 0;
- z = 0;
- }
- public Vector(Vector v) {
- x = v.x;
- y = v.y;
- z = v.z;
- }
- public Vector(double x, double y, double z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector(double[] vec) {
- x = vec[0];
- y = vec[1];
- z = vec[2];
- }
-
- public void yaw(double yaw) {
- Vector v = new Vector(this); // temporary vector
- double yawRad = Math.toRadians(yaw);
- double cos = Math.cos(yawRad);
- double sin = Math.sin(yawRad);
- x = cos * v.x + sin * v.y;
- y = -sin * v.x + cos * v.y;
- z = v.z;
- }
- public void pitch(double pitch) {
- Vector v = new Vector(this); // temporary vector
- double pitchRad = -Math.toRadians(pitch); // negative sign!
- double cos = Math.cos(pitchRad);
- double sin = Math.sin(pitchRad);
- x = v.x;
- y = cos * v.y + sin * v.z;
- z = -sin * v.y + cos * v.z;
- }
- public void roll(double roll) {
- Vector v = new Vector(this); // temporary vector
- double rollRad = Math.toRadians(roll);
- double cos = Math.cos(rollRad);
- double sin = Math.sin(rollRad);
- x = cos * v.x + sin * v.z;
- y = v.y;
- z = -sin * v.x + cos * v.z;
- }
-
- public void rollpitchyaw(double roll, double pitch, double yaw) {
- roll(roll);
- pitch(pitch);
- yaw(yaw);
- }
-}
-
-class MobilePanel extends JPanel {
-
- double yaw;
- double pitch;
- double roll;
-
- int mousedownx;
- int mousedowny;
- int mousedownyaw;
- int mousedownpitch;
- int mousedownroll;
-
- JSlider yawSlider;
- JSlider pitchSlider;
- JSlider rollSlider;
-
- /*
- * http://code.google.com/android/reference/android/hardware/Sensors.html
- *
- * With the device lying flat on a horizontal surface in front of the user,
- * oriented so the screen is readable by the user in the normal fashion,
- * the X axis goes from left to right, the Y axis goes from the user
- * toward the device, and the Z axis goes upwards perpendicular to the
- * surface.
- */
- // Mobile size
- final double sx = 15; // size x
- final double sy = 40; // size y
- final double sz = 5; // size z
-
- // Display size
- final double dx = 12; // size x
- final double dy1 = 37; // size y
- final double dy2 = -15;
-
- double[][] phone = {
- // bottom shape
- { sx, sy, -sz}, {-sx, sy, -sz},
- {-sx, sy, -sz}, {-sx,-sy, -sz},
- {-sx,-sy, -sz}, { sx,-sy, -sz},
- { sx,-sy, -sz}, { sx, sy, -sz},
- // top shape
- { sx, sy, sz}, {-sx, sy, sz},
- {-sx, sy, sz}, {-sx,-sy, sz},
- {-sx,-sy, sz}, { sx,-sy, sz},
- { sx,-sy, sz}, { sx, sy, sz},
- // connectint top and bottom
- { sx, sy, -sz}, { sx, sy, sz},
- {-sx, sy, -sz}, {-sx, sy, sz},
- {-sx,-sy, -sz}, {-sx,-sy, sz},
- { sx,-sy, -sz}, { sx,-sy, sz},
- // display
- { dx, dy1, sz}, {-dx, dy1, sz},
- {-dx, dy1, sz}, {-dx, dy2, sz},
- {-dx, dy2, sz}, { dx, dy2, sz},
- { dx, dy2, sz}, { dx, dy1, sz},
- };
-
- public MobilePanel() {
- setBorder(BorderFactory.createLineBorder(Color.black));
-
- yaw = 0;
- pitch = 0;
- roll = 0;
-
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- //moveSquare(e.getX(),e.getY());
- mousedownx = e.getX();
- mousedowny = e.getY();
- mousedownyaw = yawSlider.getValue();
- mousedownpitch = pitchSlider.getValue();
- mousedownroll = rollSlider.getValue();
- }
- });
-
- addMouseMotionListener(new MouseAdapter() {
- public void mouseDragged(MouseEvent e) {
- //moveSquare(e.getX(),e.getY());
- // Control yaw
- int newyaw = mousedownyaw - (e.getX() - mousedownx);
- while (newyaw > 180) newyaw -= 360;
- while (newyaw < -180) newyaw += 180;
- yawSlider.setValue((int) newyaw);
- yaw = newyaw;
-
- // Control pitch
- int newpitch = mousedownpitch + (e.getY() - mousedowny);
- while (newpitch > 180) newpitch -= 360;
- while (newpitch < -180) newpitch += 180;
- pitchSlider.setValue((int) newpitch);
- pitch = newpitch;
-
- repaint();
- }
- });
- }
-
- public Dimension getPreferredSize() {
- return new Dimension(250,200);
- }
-
- /**
- * yaws a vector (rotate around z-axis)
- */
-
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- // g.drawString("This is my custom Panel!",(int)yaw,(int)pitch);
-
- Graphics2D g2 = (Graphics2D) g;
- // draw Line2D.Double
-
- double centerx = 100;
- double centery = 100;
- double centerz = -150;
- for (int i=0; i<phone.length; i+=2) {
- if (i==0) g2.setColor(Color.RED);
- if (i==24) g2.setColor(Color.BLUE);
-
- Vector v1 = new Vector(phone[i]);
- Vector v2 = new Vector(phone[i+1]);
- v1.rollpitchyaw(roll, pitch, yaw);
- v2.rollpitchyaw(roll, pitch, yaw);
- g2.draw(new Line2D.Double(
- centerx + v1.x * centerz / (centerz - v1.y),
- centery - v1.z * centerz / (centerz - v1.y),
- centerx + v2.x * centerz / (centerz - v2.y),
- centery - v2.z * centerz / (centerz - v2.y)));
- }
-
- }
-
-}
-
/* SensorSimulator.java requires no other files. */
public class SensorSimulator extends JPanel
implements ActionListener,
@@ -239,13 +34,16 @@
Timer timer;

// Displays the mobile phone
- MobilePanel mobile;
+ MobilePanel2 mobile;

// Sliders:
JSlider yawSlider;
JSlider pitchSlider;
JSlider rollSlider;

+ // Server for sending out sensor data
+ SensorServer mSensorServer;
+
public SensorSimulator() {
//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setLayout(new BorderLayout());
@@ -254,7 +52,7 @@
JPanel leftPane = new JPanel(new BorderLayout());

// Add the mobile
- mobile = new MobilePanel();
+ mobile = new MobilePanel2();
leftPane.add(mobile);

// Right pane
@@ -344,6 +142,10 @@
timer.setInitialDelay(delay * 7); //We pause animation twice
per cycle
//by restarting the timer
timer.setCoalesce(true);
+
+ // Set up the server:
+ mSensorServer = new SensorServer(this);
+
}


Added: branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/Vector.java
==============================================================================
--- (empty file)
+++
branches/developers/peli/shoppinglist01a/tools/SensorSimulator/src/org/openintents/tools/sensorsimulator/Vector.java
Mon Jan 14 15:15:45 2008
@@ -0,0 +1,63 @@
+package org.openintents.tools.sensorsimulator;
+
+
+class Vector {
+ public double x;
+ public double y;
+ public double z;
+
+ public Vector() {
+ x = 0;
+ y = 0;
+ z = 0;
+ }
+ public Vector(Vector v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+ }
+ public Vector(double x, double y, double z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+ public Vector(double[] vec) {
+ x = vec[0];
+ y = vec[1];
+ z = vec[2];
+ }
+
+ public void yaw(double yaw) {
+ Vector v = new Vector(this); // temporary vector
+ double yawRad = Math.toRadians(yaw);
+ double cos = Math.cos(yawRad);
+ double sin = Math.sin(yawRad);
+ x = cos * v.x + sin * v.y;
+ y = -sin * v.x + cos * v.y;
+ z = v.z;
+ }
+ public void pitch(double pitch) {
+ Vector v = new Vector(this); // temporary vector
+ double pitchRad = -Math.toRadians(pitch); // negative sign!
+ double cos = Math.cos(pitchRad);
+ double sin = Math.sin(pitchRad);
+ x = v.x;
+ y = cos * v.y + sin * v.z;
+ z = -sin * v.y + cos * v.z;
+ }
+ public void roll(double roll) {
+ Vector v = new Vector(this); // temporary vector
+ double rollRad = Math.toRadians(roll);
+ double cos = Math.cos(rollRad);
+ double sin = Math.sin(rollRad);
+ x = cos * v.x + sin * v.z;
+ y = v.y;
+ z = -sin * v.x + cos * v.z;
+ }
+
+ public void rollpitchyaw(double roll, double pitch, double yaw) {
+ roll(roll);
+ pitch(pitch);
+ yaw(yaw);
+ }
+}
\ No newline at end of file

Reply all
Reply to author
Forward
0 new messages