EEPROM Demo
EEPROMActivity.java
- packagecom.mini6410.EEPROM;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.text.Editable;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importcom.mini6410.R;
- /**
- *
- *ClassName:EEPROMActivity
- *Reason:EEPROMDemo
- *
- *@authorsnowdream
- *@version
- *@sinceVer1.1
- *@Date20112012-03-1617:04
- *
- *@see
- */
- publicclassEEPROMActivityextendsActivity{
- publicstaticfinalintMSG_UPDATE_UI=0;
- publicstaticfinalintMSG_GET_DATA=1;
- /*读写按钮和读写输入框*/
- privateButtonmButtonWrite=null;
- privateButtonmButtonRead=null;
- privateEditTextmEditTextWrite=null;
- privateEditTextmEditTextRead=null;
- privateEditablemEditable=null;
- /*读写模块*/
- privateWriteEEPROMmWriteEEPROM=null;
- privateReadEEPROMmReadEEPROM=null;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_PROGRESS);
- setContentView(R.layout.eepromdemo);
- setProgressBarVisibility(true);
- initUI();
- initData();
- }
- /**
- *
- *initUI:初始化UI
- *
- *@param
- *@return
- *@throws
- */
- publicvoidinitUI(){
- 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
- */
- publicvoidinitData(){
- mWriteEEPROM=newWriteEEPROM(mHandler);
- mReadEEPROM=newReadEEPROM(mHandler);
- }
- privateHandlermHandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- caseMSG_UPDATE_UI:
- intpos=(int)msg.arg1;
- intlength=(int)msg.arg2;
- setProgress(pos*10000/(length-1));
- break;
- caseMSG_GET_DATA:
- BytedataByte=(Byte)msg.obj;
- mEditable.append((char)dataByte.byteValue());
- mEditTextRead.setText(mEditable);
- break;
- default:
- break;
- }
- }
- };
- privateButton.OnClickListenermClickListener=newButton.OnClickListener(){
- publicvoidonClick(Viewv){
- ButtonmButton=(Button)v;
- switch(mButton.getId()){
- caseR.id.Button_read:
- ReadDataIntoEEPROM();
- break;
- caseR.id.Button_write:
- WriteDataIntoEEPROM();
- break;
- default:
- break;
- }
- }
- };
- /**
- *
- *WriteDataIntoEEPROM:取出mEditTextWrite输入框中的数据,转换成byte数组,启用写模块写入EEPROM
- *
- *@param
- *@return
- *@throws
- */
- publicvoidWriteDataIntoEEPROM(){
- byte[]data=mEditTextWrite.getText().toString().getBytes();
- if(mWriteEEPROM!=null)
- mWriteEEPROM.WriteData(data);
- }
- /**
- *
- *ReadDataIntoEEPROM:启用读模块从EEPROM读取数据
- *
- *@param
- *@return
- *@throws
- */
- publicvoidReadDataIntoEEPROM(){
- mEditable.clear();
- if(mReadEEPROM!=null)
- mReadEEPROM.ReadData();
- }
- @Override
- protectedvoidonDestroy(){
- super.onDestroy();
- }
- }
WriteEEPROM.java
- packagecom.mini6410.EEPROM;
- importandroid.os.Handler;
- importandroid.util.Log;
- importcom.friendlyarm.AndroidSDK.HardwareControler;
- publicclassWriteEEPROM{
- privatestaticfinalStringTAG="WriteEEPROM";
- privatestaticfinalintMAX_LENGTH=256;//EEPROM最多可存储256个字节数据
- HandlermHandler=null;
- byte[]mData=null;
- privateWriteEEPROMThreadmWriteEEPROMThread=null;
- publicWriteEEPROM(HandlermHandler){
- this.mHandler=mHandler;
- }
- /**
- *
- *WriteData:新建并启动写线程将数据逐个字节写入EEPROM
- *
- *@paramdatabyte数组
- *@return
- *@throws
- */
- publicvoidWriteData(byte[]data){
- mData=data;
- safeStop();
- mWriteEEPROMThread=newWriteEEPROMThread();
- mWriteEEPROMThread.start();
- }
- /**
- *
- *safeStop:安全停止线程
- *
- *@param
- *@return
- *@throws
- */
- publicvoidsafeStop(){
- if(mWriteEEPROMThread!=null&&mWriteEEPROMThread.isAlive()){
- mWriteEEPROMThread.interrupt();
- mWriteEEPROMThread.stop=true;
- try{
- mWriteEEPROMThread.join();
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- }
- mWriteEEPROMThread=null;
- }
- publicvoidsendMessage(intwhat){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what));
- }
- }
- publicvoidsendMessage(intwhat,Objectobj){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,obj));
- }
- }
- publicvoidsendMessage(intwhat,intarg1,intarg2,Objectobj){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));
- }
- }
- publicvoidsendMessage(intwhat,intarg1,intarg2){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));
- }
- }
- /**
- *
- *WriteEEPROMThread:数据写入线程
- *
- *@param
- *@return
- *@throws
- */
- privateclassWriteEEPROMThreadextendsThread{
- volatilebooleanstop=false;
- intfd=0;
- intlength=0;
- intpos=0;
- @Override
- publicvoidrun(){
- if(mData==null){
- Log.e(TAG,"ThereisNoData!");
- stop=true;
- }
- /*打开设备*/
- fd=HardwareControler.openI2CDevice();
- if(fd==-1)
- {
- Log.e(TAG,"FailedtoopentheI2CDevice!");
- stop=true;
- }
- length=mData.length;
- if(length>MAX_LENGTH){
- length=MAX_LENGTH;
- }
- //擦除并初始化EEPROM
- for(inti=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,"writeByteDataToI2Cpos:"+pos);
- pos++;
- //try{
- //Thread.sleep(10);
- //}catch(InterruptedExceptione){
- //e.printStackTrace();
- //}
- }
- if(fd!=-1)
- {
- /*关闭设备*/
- HardwareControler.close(fd);
- }
- }
- }
- }
ReadEEPROM.java
- packagecom.mini6410.EEPROM;
- importandroid.os.Handler;
- importandroid.util.Log;
- importcom.friendlyarm.AndroidSDK.HardwareControler;
- publicclassReadEEPROM{
- privatestaticfinalStringTAG="ReadEEPROM";
- privatestaticfinalintMAX_LENGTH=256;//EEPROM最多可存储256个字节数据
- HandlermHandler=null;
- privateReadEEPROMThreadmReadEEPROMThread=null;
- publicReadEEPROM(HandlermHandler){
- this.mHandler=mHandler;
- }
- /**
- *
- *ReadData:新建并启动读线程从EEPROM中逐个读取数据
- *
- *@param
- *@return
- *@throws
- */
- publicvoidReadData(){
- safeStop();
- mReadEEPROMThread=newReadEEPROMThread();
- mReadEEPROMThread.start();
- }
- /**
- *
- *safeStop:安全停止线程
- *
- *@param
- *@return
- *@throws
- */
- publicvoidsafeStop(){
- if(mReadEEPROMThread!=null&&mReadEEPROMThread.isAlive()){
- mReadEEPROMThread.interrupt();
- mReadEEPROMThread.stop=true;
- try{
- mReadEEPROMThread.join();
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- }
- mReadEEPROMThread=null;
- }
- publicvoidsendMessage(intwhat){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what));
- }
- }
- publicvoidsendMessage(intwhat,Objectobj){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,obj));
- }
- }
- publicvoidsendMessage(intwhat,intarg1,intarg2,Objectobj){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));
- }
- }
- publicvoidsendMessage(intwhat,intarg1,intarg2){
- if(mHandler!=null){
- mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));
- }
- }
- /**
- *
- *ReadEEPROMThread:数据读取线程
- *
- *@param
- *@return
- *@throws
- */
- privateclassReadEEPROMThreadextendsThread{
- volatilebooleanstop=false;
- intfd=0;
- intlength=0;
- intpos=0;
- bytedata=0;
- @Override
- publicvoidrun(){
- /*打开设备*/
- fd=HardwareControler.openI2CDevice();
- if(fd==-1)
- {
- Log.e(TAG,"FailedtoopentheI2CDevice!");
- 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,"readByteDataFromI2Cpos:"+pos);
- pos++;
- //try{
- //Thread.sleep(10);
- //}catch(InterruptedExceptione){
- //e.printStackTrace();
- //}
- }
- if(fd!=-1)
- {
- /*关闭设备*/
- HardwareControler.close(fd);
- }
- }
- }
- }
eepromdemo.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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>
预览效果: