Permalink
Browse files

Monitor NFC mode was implemented.

  • Loading branch information...
1 parent 594cd85 commit 720a1370783999160ddd8ce989a7ebd794b805bd @cattaka committed Aug 29, 2012
@@ -18,10 +18,10 @@ public int getIntValue() {
}
public static MotorDir parse(int intValue) {
- if (intValue < 0) {
- return REVERSE;
- } else if (intValue > 0) {
- return FORWARD;
+ for (MotorDir item : values()) {
+ if (item.getIntValue() == intValue) {
+ return item;
+ }
}
return STOP;
}
@@ -7,11 +7,13 @@
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.hardware.camera" />
+ <uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
+ <uses-permission android:name="android.permission.NFC" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:name=".DroidRobo01Application">
@@ -32,7 +34,8 @@
<activity android:name="net.cattaka.droidrobo01.ControllerActivity"></activity>
<activity android:name="net.cattaka.droidrobo01.AuthTwitterActivity"></activity>
<activity android:name="net.cattaka.droidrobo01.MonitorTwitterActivity"></activity>
-
+ <activity android:name="net.cattaka.droidrobo01.MonitorNfcActivity"></activity>
+
<activity android:name="net.cattaka.droidrobo01.AutoDetectionModeActivity"
android:screenOrientation="landscape"
>
@@ -59,8 +59,8 @@ void setup() {
digitalWrite(PIN_MOTOR_R1, LOW);
digitalWrite(PIN_MOTOR_R2, LOW);
- initMyServo(0, 750, 2250, 0x7FFF, 0xffff); // ZS
- initMyServo(1, 750, 2250, 0x7FFF, 0xffff); // ZS
+ initMyServo(0, 2250, 750, 0x7FFF, 0xffff); // ZS
+ initMyServo(1, 2250, 750, 0x7FFF, 0xffff); // ZS
// digitalWrite (LED_PIN, HIGH);
mMemoryMap.setStreamInterface(&mAndroidAccessoryStream);
@@ -9,4 +9,4 @@
# Project target.
target=Google Inc.:Google APIs:10
-android.library.reference.1=../../andylib/AndyLib
+android.library.reference.1=../../../myfile/mydoc/eclipseAndroid/AndyLib
@@ -9,7 +9,7 @@
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
- android:text="Robot Arm 01"
+ android:text="Droid Robo 01"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
@@ -27,7 +27,7 @@
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Auth Twitter" />
-
+
<Button
android:id="@+id/buttonAdjustment"
android:layout_width="wrap_content"
@@ -43,5 +43,11 @@
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="Monitor Twitter" />
-
+ <Button
+ android:id="@+id/buttonMonitorNfc"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_below="@+id/buttonMonitorTwitter"
+ android:text="Monitor Nfc" />
</RelativeLayout>
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/textView1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Monitor NFC"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/motionCountText"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentRight="true"
+ android:layout_alignParentTop="true"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+</RelativeLayout>
@@ -5,7 +5,6 @@
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
-import net.cattaka.droidrobo01.R;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
@Override
@@ -16,6 +15,7 @@ protected void onCreate(Bundle arg0) {
findViewById(R.id.buttonAuthTwitter).setOnClickListener(this);
findViewById(R.id.buttonAdjustment).setOnClickListener(this);
findViewById(R.id.buttonMonitorTwitter).setOnClickListener(this);
+ findViewById(R.id.buttonMonitorNfc).setOnClickListener(this);
}
@Override
@@ -29,6 +29,9 @@ public void onClick(View v) {
} else if (v.getId() == R.id.buttonMonitorTwitter) {
Intent intent = new Intent(this, MonitorTwitterActivity.class);
startActivity(intent);
+ } else if (v.getId() == R.id.buttonMonitorNfc) {
+ Intent intent = new Intent(this, MonitorNfcActivity.class);
+ startActivity(intent);
} else if (v.getId() == R.id.buttonAdjustment) {
Intent intent = new Intent(this, AdjustmentActivity.class);
startActivity(intent);
@@ -0,0 +1,246 @@
+
+package net.cattaka.droidrobo01;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+
+import net.cattaka.droidrobo01.robo.RoboPauseInfo;
+import net.cattaka.droidrobo01.robo.RoboPauseInfo.MotorDir;
+import net.cattaka.droidrobo01.service.AdkService;
+import net.cattaka.droidrobo01.service.IAdkService;
+import android.app.Activity;
+import android.app.PendingIntent;
+import android.app.Service;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.ServiceConnection;
+import android.content.SharedPreferences;
+import android.nfc.NdefMessage;
+import android.nfc.NdefRecord;
+import android.nfc.NfcAdapter;
+import android.nfc.Tag;
+import android.nfc.tech.Ndef;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Parcelable;
+import android.os.RemoteException;
+import android.preference.PreferenceManager;
+import android.util.Log;
+import android.view.WindowManager;
+import android.widget.TextView;
+
+public class MonitorNfcActivity extends Activity {
+ private static final int EVENT_DRIVE = 2;
+
+ private MonitorNfcActivity me = this;
+
+ private TextView mMotionCountText;
+
+ private IAdkService mAdkService;
+
+ private NfcAdapter mNfcAdapter;
+
+ private IntentFilter[] mNfcFilters = new IntentFilter[] {
+ new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
+ };
+
+ private String[][] mNfcTechLists = new String[][] {
+ new String[] {
+ Ndef.class.getName()
+ }
+ };
+
+ private PendingIntent mPendingIntent;
+
+ private ArmSetting mSetting;
+
+ private Queue<RoboPauseInfo> mPauseQueue = new LinkedList<RoboPauseInfo>();
+
+ private RoboPauseInfo ROBO_PAUSE_STOP = new RoboPauseInfo(false, MotorDir.STOP, MotorDir.STOP,
+ 500, 0.25f, 0.25f);
+
+ private ServiceConnection mServiceConnection = new ServiceConnection() {
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ mAdkService = null;
+ }
+
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ mAdkService = IAdkService.Stub.asInterface(service);
+ try {
+ mAdkService.connectDevice();
+ } catch (RemoteException e) {
+ Log.d("Debug", e.toString(), e);
+ }
+ }
+ };
+
+ private Handler mHandler = new Handler() {
+ public void handleMessage(android.os.Message msg) {
+ if (msg.what == EVENT_DRIVE) {
+ startDriveRobo();
+ }
+ };
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.monitor_nfc);
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+
+ mMotionCountText = (TextView)findViewById(R.id.motionCountText);
+
+ mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
+
+ { // enable
+ Intent intent = new Intent(this, getClass());
+ intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
+ mPendingIntent = PendingIntent.getActivity(this, 1, intent, 0);
+ }
+ }
+
+ @Override
+ protected void onNewIntent(Intent intent) {
+ super.onNewIntent(intent);
+ String action = intent.getAction();
+ if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
+ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+ NdefMessage[] msgs;
+ Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
+ if (rawMsgs != null) {
+ msgs = new NdefMessage[rawMsgs.length];
+ for (int i = 0; i < rawMsgs.length; i++) {
+ msgs[i] = (NdefMessage)rawMsgs[i];
+ }
+
+ List<RoboPauseInfo> infos = readNdefMessages(tag, msgs);
+ if (infos.size() > 0) {
+ mPauseQueue.clear();
+ mPauseQueue.addAll(infos);
+ }
+ }
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ {
+ Intent intent = new Intent(this, AdkService.class);
+ bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
+ }
+
+ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
+ {
+ mSetting = new ArmSetting();
+ mSetting.loadPreference(pref);
+ }
+
+ startDriveRobo();
+
+ mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mNfcFilters, mNfcTechLists);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ stopDriveRobo();
+ mNfcAdapter.disableForegroundDispatch(this);
+ }
+
+ private void addMessageAsync(String str) {
+ if (mPauseQueue.size() == 0) {
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 0.25f, 0.25f));
+ mPauseQueue.add(new RoboPauseInfo(false, MotorDir.STOP, MotorDir.STOP, 500, 0.25f,
+ 0.25f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 0.25f, 0.25f));
+ mPauseQueue.add(new RoboPauseInfo(false, MotorDir.STOP, MotorDir.STOP, 500, 0.25f,
+ 0.25f));
+
+ mPauseQueue.add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 1000, 0.75f,
+ 0.75f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 1.00f, 0.50f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 0.50f, 1.00f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 1.00f, 0.50f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 0.50f, 1.00f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 1.00f, 0.50f));
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 500, 0.50f, 1.00f));
+ mPauseQueue.add(new RoboPauseInfo(false, MotorDir.STOP, MotorDir.STOP, 500, 0.75f,
+ 0.75f));
+
+ mPauseQueue.add(new RoboPauseInfo(true, MotorDir.FORWARD, MotorDir.REVERSE, 200, 0.75f,
+ 0.75f));
+ mPauseQueue.add(new RoboPauseInfo(true, MotorDir.REVERSE, MotorDir.FORWARD, 200, 0.75f,
+ 0.75f));
+
+ mPauseQueue
+ .add(new RoboPauseInfo(true, MotorDir.STOP, MotorDir.STOP, 1000, 1.0f, 1.0f));
+
+ }
+ }
+
+ private List<RoboPauseInfo> readNdefMessages(Tag tag, NdefMessage[] msgs) {
+ List<RoboPauseInfo> infos = new ArrayList<RoboPauseInfo>();
+ for (NdefMessage msg : msgs) {
+ NdefRecord rec = msg.getRecords()[0];
+ byte[] data = rec.getPayload();
+ int repeat = data[0];
+ int size = data[1];
+ int pos = 2;
+ List<RoboPauseInfo> tmpList = new ArrayList<RoboPauseInfo>(infos);
+ for (int i = 0; i < size; i++) {
+ RoboPauseInfo info = new RoboPauseInfo();
+ info.setEyeLight(data[pos++] != 0);
+ info.setArmLeftAngle((float)(0xFF & data[pos++]) / (float)0xFF);
+ info.setArmRightAngle((float)(0xFF & data[pos++]) / (float)0xFF);
+ info.setMotorDirLeft(MotorDir.parse(data[pos++]));
+ info.setMotorDirRight(MotorDir.parse(data[pos++]));
+ info.setDucation(data[pos++] * 100);
+ tmpList.add(info);
+ }
+ for (int i = 0; i < repeat; i++) {
+ infos.addAll(tmpList);
+ }
+ }
+ return infos;
+ }
+
+ private void startDriveRobo() {
+ mMotionCountText.setText(String.valueOf(mPauseQueue.size()));
+
+ RoboPauseInfo rpInfo = mPauseQueue.poll();
+ if (rpInfo == null) {
+ rpInfo = ROBO_PAUSE_STOP;
+ }
+ {
+ if (mAdkService != null) {
+ try {
+ mAdkService.sendCommand((byte)0x03, (byte)(0x0), rpInfo.toUpperBytes());
+ mAdkService.sendCommand((byte)0x03, (byte)(0x5), rpInfo.toLowerBytes());
+ } catch (RemoteException e) {
+ Log.e("Debug", e.getMessage(), e);
+ }
+ }
+ }
+ mHandler.sendEmptyMessageDelayed(EVENT_DRIVE, rpInfo.getDucation());
+ }
+
+ private void stopDriveRobo() {
+ mHandler.removeMessages(EVENT_DRIVE);
+ }
+}
Oops, something went wrong.

0 comments on commit 720a137

Please sign in to comment.