友善之臂Mini6410之Android开发学习笔记源码同步更新,请使用git工具进行同步。关于Git工具更多信息,请参考:http://progit.org/book/zh/
git clone https://code.google.com/p/androiddemoformini6410/
EEPROMActivity.java
package com.mini6410.EEPROM; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.Editable; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import com.mini6410.R; /** * * ClassName:EEPROMActivity * Reason: EEPROM Demo * * @author snowdream * @version * @since Ver 1.1 * @Date 2011 2012-03-16 17:04 * * @see */ public class EEPROMActivity extends Activity { public static final int MSG_UPDATE_UI = 0; public static final int MSG_GET_DATA = 1; /*读写按钮和读写输入框*/ private Button mButtonWrite = null; private Button mButtonRead = null; private EditText mEditTextWrite = null; private EditText mEditTextRead = null; private Editable mEditable = null; /*读写模块*/ private WriteEEPROM mWriteEEPROM = null; private ReadEEPROM mReadEEPROM = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.eepromdemo); setProgressBarVisibility(true); initUI(); initData(); } /** * * initUI: 初始化UI * * @param * @return * @throws */ public void initUI(){ mButtonWrite = (Button)findViewById(R.id.Button_write); mButtonRead = (Button)findViewById(R.id.Button_read); mButtonWrite.setOnClickListener(mClickListener); mButtonRead.setOnClickListener(mClickListener); mEditTextWrite = (EditText)findViewById(R.id.EditText_write); mEditTextRead = (EditText)findViewById(R.id.EditText_read); mEditable = mEditTextRead.getText(); } /** * * initData:新建读写模块,准备读写数据 * * @param * @return * @throws */ public void initData(){ mWriteEEPROM = new WriteEEPROM(mHandler); mReadEEPROM = new ReadEEPROM(mHandler); } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE_UI: int pos = (int)msg.arg1; int length = (int)msg.arg2; setProgress(pos*10000/(length -1)); break; case MSG_GET_DATA: Byte dataByte = (Byte)msg.obj; mEditable.append((char)dataByte.byteValue()); mEditTextRead.setText(mEditable); break; default: break; } } }; private Button.OnClickListener mClickListener = new Button.OnClickListener(){ public void onClick(View v) { Button mButton = (Button)v; switch (mButton.getId()) { case R.id.Button_read: ReadDataIntoEEPROM(); break; case R.id.Button_write: WriteDataIntoEEPROM(); break; default: break; } } }; /** * * WriteDataIntoEEPROM:取出mEditTextWrite输入框中的数据,转换成byte数组,启用写模块写入EEPROM * * @param * @return * @throws */ public void WriteDataIntoEEPROM(){ byte[] data = mEditTextWrite.getText().toString().getBytes(); if(mWriteEEPROM != null) mWriteEEPROM.WriteData(data); } /** * * ReadDataIntoEEPROM:启用读模块从EEPROM读取数据 * * @param * @return * @throws */ public void ReadDataIntoEEPROM(){ mEditable.clear(); if(mReadEEPROM != null) mReadEEPROM.ReadData(); } @Override protected void onDestroy() { super.onDestroy(); } }
WriteEEPROM.java
package com.mini6410.EEPROM; import android.os.Handler; import android.util.Log; import com.friendlyarm.AndroidSDK.HardwareControler; public class WriteEEPROM{ private static final String TAG = "WriteEEPROM"; private static final int MAX_LENGTH = 256; //EEPROM最多可存储256个字节数据 Handler mHandler = null; byte[] mData = null; private WriteEEPROMThread mWriteEEPROMThread = null; public WriteEEPROM(Handler mHandler){ this.mHandler = mHandler; } /** * * WriteData: 新建并启动写线程将数据逐个字节写入EEPROM * * @param data byte数组 * @return * @throws */ public void WriteData(byte[] data){ mData = data; safeStop(); mWriteEEPROMThread = new WriteEEPROMThread(); mWriteEEPROMThread.start(); } /** * * safeStop: 安全停止线程 * * @param * @return * @throws */ public void safeStop(){ if(mWriteEEPROMThread != null && mWriteEEPROMThread.isAlive()){ mWriteEEPROMThread.interrupt(); mWriteEEPROMThread.stop = true; try { mWriteEEPROMThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } mWriteEEPROMThread = null; } public void sendMessage(int what ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what)); } } public void sendMessage(int what, Object obj ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what, obj)); } } public void sendMessage(int what, int arg1,int arg2,Object obj ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj)); } } public void sendMessage(int what, int arg1,int arg2 ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2)); } } /** * * WriteEEPROMThread: 数据写入线程 * * @param * @return * @throws */ private class WriteEEPROMThread extends Thread{ volatile boolean stop = false; int fd = 0; int length = 0; int pos = 0; @Override public void run() { if(mData == null){ Log.e(TAG, "There is No Data!"); stop = true; } /*打开设备*/ fd = HardwareControler.openI2CDevice(); if(fd == -1) { Log.e(TAG, "Failed to open the I2CDevice !"); stop = true; } length = mData.length; if (length > MAX_LENGTH) { length = MAX_LENGTH; } //擦除并初始化EEPROM for(int i = 0 ; i < MAX_LENGTH; i++){ HardwareControler.writeByteDataToI2C(fd, i, (byte)'\0'); } while(!stop){ if (pos >= length) { break; } /*写入数据,每次只能读取一个字节。*/ HardwareControler.writeByteDataToI2C(fd, pos, mData[pos]); sendMessage(EEPROMActivity.MSG_UPDATE_UI, pos,length); Log.i(TAG, "writeByteDataToI2C pos: "+ pos); pos++; // try { // Thread.sleep(10); // } catch ( InterruptedException e ) { // e.printStackTrace(); // } } if(fd != -1) { /*关闭设备*/ HardwareControler.close(fd); } } } }
ReadEEPROM.java
package com.mini6410.EEPROM; import android.os.Handler; import android.util.Log; import com.friendlyarm.AndroidSDK.HardwareControler; public class ReadEEPROM{ private static final String TAG = "ReadEEPROM"; private static final int MAX_LENGTH = 256; //EEPROM最多可存储256个字节数据 Handler mHandler = null; private ReadEEPROMThread mReadEEPROMThread = null; public ReadEEPROM(Handler mHandler){ this.mHandler = mHandler; } /** * * ReadData: 新建并启动读线程从EEPROM中逐个读取数据 * * @param * @return * @throws */ public void ReadData(){ safeStop(); mReadEEPROMThread = new ReadEEPROMThread(); mReadEEPROMThread.start(); } /** * * safeStop: 安全停止线程 * * @param * @return * @throws */ public void safeStop(){ if(mReadEEPROMThread != null && mReadEEPROMThread.isAlive()){ mReadEEPROMThread.interrupt(); mReadEEPROMThread.stop = true; try { mReadEEPROMThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } mReadEEPROMThread = null; } public void sendMessage(int what ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what)); } } public void sendMessage(int what, Object obj ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what, obj)); } } public void sendMessage(int what, int arg1,int arg2,Object obj ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj)); } } public void sendMessage(int what, int arg1,int arg2 ){ if(mHandler != null){ mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2)); } } /** * * ReadEEPROMThread: 数据读取线程 * * @param * @return * @throws */ private class ReadEEPROMThread extends Thread{ volatile boolean stop = false; int fd = 0; int length = 0; int pos = 0; byte data = 0; @Override public void run() { /*打开设备*/ fd = HardwareControler.openI2CDevice(); if(fd == -1) { Log.e(TAG, "Failed to open the I2CDevice !"); stop = true; } length = MAX_LENGTH; while(!stop){ if (pos >= length) { break; } /*读取数据,每次只能读取一个字节。*/ data = (byte)HardwareControler.readByteDataFromI2C(fd, pos); if(data != -1) { sendMessage(EEPROMActivity.MSG_GET_DATA, data); sendMessage(EEPROMActivity.MSG_UPDATE_UI, pos,length); } Log.i(TAG, "readByteDataFromI2C pos: "+ pos); pos++; // try { // Thread.sleep(10); // } catch ( InterruptedException e ) { // e.printStackTrace(); // } } if(fd != -1) { /*关闭设备*/ HardwareControler.close(fd); } } } }
eepromdemo.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/eeprom" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/writemodel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <Button android:id="@+id/Button_write" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/writeeeprom" /> <EditText android:id="@+id/EditText_write" android:layout_width="fill_parent" android:layout_height="100dip" android:text="@string/dataeeprom" /> </LinearLayout> <LinearLayout android:id="@+id/readmodel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <Button android:id="@+id/Button_read" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/readeeprom" /> <EditText android:id="@+id/EditText_read" android:layout_width="fill_parent" android:layout_height="100dip" /> </LinearLayout> </LinearLayout> </LinearLayout>
预览效果: