EEPROM Demo

EEPROMActivity.java

  1. packagecom.mini6410.EEPROM;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.os.Message;
  6. importandroid.text.Editable;
  7. importandroid.view.View;
  8. importandroid.view.Window;
  9. importandroid.widget.Button;
  10. importandroid.widget.EditText;
  11. importcom.mini6410.R;
  12. /**
  13. *
  14. *ClassName:EEPROMActivity
  15. *Reason:EEPROMDemo
  16. *
  17. *@authorsnowdream
  18. *@version
  19. *@sinceVer1.1
  20. *@Date20112012-03-1617:04
  21. *
  22. *@see
  23. */
  24. publicclassEEPROMActivityextendsActivity{
  25. publicstaticfinalintMSG_UPDATE_UI=0;
  26. publicstaticfinalintMSG_GET_DATA=1;
  27. /*读写按钮和读写输入框*/
  28. privateButtonmButtonWrite=null;
  29. privateButtonmButtonRead=null;
  30. privateEditTextmEditTextWrite=null;
  31. privateEditTextmEditTextRead=null;
  32. privateEditablemEditable=null;
  33. /*读写模块*/
  34. privateWriteEEPROMmWriteEEPROM=null;
  35. privateReadEEPROMmReadEEPROM=null;
  36. @Override
  37. protectedvoidonCreate(BundlesavedInstanceState){
  38. super.onCreate(savedInstanceState);
  39. requestWindowFeature(Window.FEATURE_PROGRESS);
  40. setContentView(R.layout.eepromdemo);
  41. setProgressBarVisibility(true);
  42. initUI();
  43. initData();
  44. }
  45. /**
  46. *
  47. *initUI:初始化UI
  48. *
  49. *@param
  50. *@return
  51. *@throws
  52. */
  53. publicvoidinitUI(){
  54. mButtonWrite=(Button)findViewById(R.id.Button_write);
  55. mButtonRead=(Button)findViewById(R.id.Button_read);
  56. mButtonWrite.setOnClickListener(mClickListener);
  57. mButtonRead.setOnClickListener(mClickListener);
  58. mEditTextWrite=(EditText)findViewById(R.id.EditText_write);
  59. mEditTextRead=(EditText)findViewById(R.id.EditText_read);
  60. mEditable=mEditTextRead.getText();
  61. }
  62. /**
  63. *
  64. *initData:新建读写模块,准备读写数据
  65. *
  66. *@param
  67. *@return
  68. *@throws
  69. */
  70. publicvoidinitData(){
  71. mWriteEEPROM=newWriteEEPROM(mHandler);
  72. mReadEEPROM=newReadEEPROM(mHandler);
  73. }
  74. privateHandlermHandler=newHandler(){
  75. @Override
  76. publicvoidhandleMessage(Messagemsg){
  77. switch(msg.what){
  78. caseMSG_UPDATE_UI:
  79. intpos=(int)msg.arg1;
  80. intlength=(int)msg.arg2;
  81. setProgress(pos*10000/(length-1));
  82. break;
  83. caseMSG_GET_DATA:
  84. BytedataByte=(Byte)msg.obj;
  85. mEditable.append((char)dataByte.byteValue());
  86. mEditTextRead.setText(mEditable);
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. };
  93. privateButton.OnClickListenermClickListener=newButton.OnClickListener(){
  94. publicvoidonClick(Viewv){
  95. ButtonmButton=(Button)v;
  96. switch(mButton.getId()){
  97. caseR.id.Button_read:
  98. ReadDataIntoEEPROM();
  99. break;
  100. caseR.id.Button_write:
  101. WriteDataIntoEEPROM();
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. };
  108. /**
  109. *
  110. *WriteDataIntoEEPROM:取出mEditTextWrite输入框中的数据,转换成byte数组,启用写模块写入EEPROM
  111. *
  112. *@param
  113. *@return
  114. *@throws
  115. */
  116. publicvoidWriteDataIntoEEPROM(){
  117. byte[]data=mEditTextWrite.getText().toString().getBytes();
  118. if(mWriteEEPROM!=null)
  119. mWriteEEPROM.WriteData(data);
  120. }
  121. /**
  122. *
  123. *ReadDataIntoEEPROM:启用读模块从EEPROM读取数据
  124. *
  125. *@param
  126. *@return
  127. *@throws
  128. */
  129. publicvoidReadDataIntoEEPROM(){
  130. mEditable.clear();
  131. if(mReadEEPROM!=null)
  132. mReadEEPROM.ReadData();
  133. }
  134. @Override
  135. protectedvoidonDestroy(){
  136. super.onDestroy();
  137. }
  138. }

WriteEEPROM.java

  1. packagecom.mini6410.EEPROM;
  2. importandroid.os.Handler;
  3. importandroid.util.Log;
  4. importcom.friendlyarm.AndroidSDK.HardwareControler;
  5. publicclassWriteEEPROM{
  6. privatestaticfinalStringTAG="WriteEEPROM";
  7. privatestaticfinalintMAX_LENGTH=256;//EEPROM最多可存储256个字节数据
  8. HandlermHandler=null;
  9. byte[]mData=null;
  10. privateWriteEEPROMThreadmWriteEEPROMThread=null;
  11. publicWriteEEPROM(HandlermHandler){
  12. this.mHandler=mHandler;
  13. }
  14. /**
  15. *
  16. *WriteData:新建并启动写线程将数据逐个字节写入EEPROM
  17. *
  18. *@paramdatabyte数组
  19. *@return
  20. *@throws
  21. */
  22. publicvoidWriteData(byte[]data){
  23. mData=data;
  24. safeStop();
  25. mWriteEEPROMThread=newWriteEEPROMThread();
  26. mWriteEEPROMThread.start();
  27. }
  28. /**
  29. *
  30. *safeStop:安全停止线程
  31. *
  32. *@param
  33. *@return
  34. *@throws
  35. */
  36. publicvoidsafeStop(){
  37. if(mWriteEEPROMThread!=null&&mWriteEEPROMThread.isAlive()){
  38. mWriteEEPROMThread.interrupt();
  39. mWriteEEPROMThread.stop=true;
  40. try{
  41. mWriteEEPROMThread.join();
  42. }catch(InterruptedExceptione){
  43. e.printStackTrace();
  44. }
  45. }
  46. mWriteEEPROMThread=null;
  47. }
  48. publicvoidsendMessage(intwhat){
  49. if(mHandler!=null){
  50. mHandler.sendMessage(mHandler.obtainMessage(what));
  51. }
  52. }
  53. publicvoidsendMessage(intwhat,Objectobj){
  54. if(mHandler!=null){
  55. mHandler.sendMessage(mHandler.obtainMessage(what,obj));
  56. }
  57. }
  58. publicvoidsendMessage(intwhat,intarg1,intarg2,Objectobj){
  59. if(mHandler!=null){
  60. mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));
  61. }
  62. }
  63. publicvoidsendMessage(intwhat,intarg1,intarg2){
  64. if(mHandler!=null){
  65. mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));
  66. }
  67. }
  68. /**
  69. *
  70. *WriteEEPROMThread:数据写入线程
  71. *
  72. *@param
  73. *@return
  74. *@throws
  75. */
  76. privateclassWriteEEPROMThreadextendsThread{
  77. volatilebooleanstop=false;
  78. intfd=0;
  79. intlength=0;
  80. intpos=0;
  81. @Override
  82. publicvoidrun(){
  83. if(mData==null){
  84. Log.e(TAG,"ThereisNoData!");
  85. stop=true;
  86. }
  87. /*打开设备*/
  88. fd=HardwareControler.openI2CDevice();
  89. if(fd==-1)
  90. {
  91. Log.e(TAG,"FailedtoopentheI2CDevice!");
  92. stop=true;
  93. }
  94. length=mData.length;
  95. if(length>MAX_LENGTH){
  96. length=MAX_LENGTH;
  97. }
  98. //擦除并初始化EEPROM
  99. for(inti=0;i<MAX_LENGTH;i++){
  100. HardwareControler.writeByteDataToI2C(fd,i,(byte)'\0');
  101. }
  102. while(!stop){
  103. if(pos>=length){
  104. break;
  105. }
  106. /*写入数据,每次只能读取一个字节。*/
  107. HardwareControler.writeByteDataToI2C(fd,pos,mData[pos]);
  108. sendMessage(EEPROMActivity.MSG_UPDATE_UI,pos,length);
  109. Log.i(TAG,"writeByteDataToI2Cpos:"+pos);
  110. pos++;
  111. //try{
  112. //Thread.sleep(10);
  113. //}catch(InterruptedExceptione){
  114. //e.printStackTrace();
  115. //}
  116. }
  117. if(fd!=-1)
  118. {
  119. /*关闭设备*/
  120. HardwareControler.close(fd);
  121. }
  122. }
  123. }
  124. }

ReadEEPROM.java

  1. packagecom.mini6410.EEPROM;
  2. importandroid.os.Handler;
  3. importandroid.util.Log;
  4. importcom.friendlyarm.AndroidSDK.HardwareControler;
  5. publicclassReadEEPROM{
  6. privatestaticfinalStringTAG="ReadEEPROM";
  7. privatestaticfinalintMAX_LENGTH=256;//EEPROM最多可存储256个字节数据
  8. HandlermHandler=null;
  9. privateReadEEPROMThreadmReadEEPROMThread=null;
  10. publicReadEEPROM(HandlermHandler){
  11. this.mHandler=mHandler;
  12. }
  13. /**
  14. *
  15. *ReadData:新建并启动读线程从EEPROM中逐个读取数据
  16. *
  17. *@param
  18. *@return
  19. *@throws
  20. */
  21. publicvoidReadData(){
  22. safeStop();
  23. mReadEEPROMThread=newReadEEPROMThread();
  24. mReadEEPROMThread.start();
  25. }
  26. /**
  27. *
  28. *safeStop:安全停止线程
  29. *
  30. *@param
  31. *@return
  32. *@throws
  33. */
  34. publicvoidsafeStop(){
  35. if(mReadEEPROMThread!=null&&mReadEEPROMThread.isAlive()){
  36. mReadEEPROMThread.interrupt();
  37. mReadEEPROMThread.stop=true;
  38. try{
  39. mReadEEPROMThread.join();
  40. }catch(InterruptedExceptione){
  41. e.printStackTrace();
  42. }
  43. }
  44. mReadEEPROMThread=null;
  45. }
  46. publicvoidsendMessage(intwhat){
  47. if(mHandler!=null){
  48. mHandler.sendMessage(mHandler.obtainMessage(what));
  49. }
  50. }
  51. publicvoidsendMessage(intwhat,Objectobj){
  52. if(mHandler!=null){
  53. mHandler.sendMessage(mHandler.obtainMessage(what,obj));
  54. }
  55. }
  56. publicvoidsendMessage(intwhat,intarg1,intarg2,Objectobj){
  57. if(mHandler!=null){
  58. mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));
  59. }
  60. }
  61. publicvoidsendMessage(intwhat,intarg1,intarg2){
  62. if(mHandler!=null){
  63. mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));
  64. }
  65. }
  66. /**
  67. *
  68. *ReadEEPROMThread:数据读取线程
  69. *
  70. *@param
  71. *@return
  72. *@throws
  73. */
  74. privateclassReadEEPROMThreadextendsThread{
  75. volatilebooleanstop=false;
  76. intfd=0;
  77. intlength=0;
  78. intpos=0;
  79. bytedata=0;
  80. @Override
  81. publicvoidrun(){
  82. /*打开设备*/
  83. fd=HardwareControler.openI2CDevice();
  84. if(fd==-1)
  85. {
  86. Log.e(TAG,"FailedtoopentheI2CDevice!");
  87. stop=true;
  88. }
  89. length=MAX_LENGTH;
  90. while(!stop){
  91. if(pos>=length){
  92. break;
  93. }
  94. /*读取数据,每次只能读取一个字节。*/
  95. data=(byte)HardwareControler.readByteDataFromI2C(fd,pos);
  96. if(data!=-1)
  97. {
  98. sendMessage(EEPROMActivity.MSG_GET_DATA,data);
  99. sendMessage(EEPROMActivity.MSG_UPDATE_UI,pos,length);
  100. }
  101. Log.i(TAG,"readByteDataFromI2Cpos:"+pos);
  102. pos++;
  103. //try{
  104. //Thread.sleep(10);
  105. //}catch(InterruptedExceptione){
  106. //e.printStackTrace();
  107. //}
  108. }
  109. if(fd!=-1)
  110. {
  111. /*关闭设备*/
  112. HardwareControler.close(fd);
  113. }
  114. }
  115. }
  116. }

eepromdemo.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <LinearLayout
  7. android:id="@+id/eeprom"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="horizontal">
  11. <LinearLayout
  12. android:id="@+id/writemodel"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_weight="1"
  16. android:orientation="vertical">
  17. <Button
  18. android:id="@+id/Button_write"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="@string/writeeeprom"/>
  22. <EditText
  23. android:id="@+id/EditText_write"
  24. android:layout_width="fill_parent"
  25. android:layout_height="100dip"
  26. android:text="@string/dataeeprom"/>
  27. </LinearLayout>
  28. <LinearLayout
  29. android:id="@+id/readmodel"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:layout_weight="1"
  33. android:orientation="vertical">
  34. <Button
  35. android:id="@+id/Button_read"
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:text="@string/readeeprom"/>
  39. <EditText
  40. android:id="@+id/EditText_read"
  41. android:layout_width="fill_parent"
  42. android:layout_height="100dip"/>
  43. </LinearLayout>
  44. </LinearLayout>
  45. </LinearLayout>

预览效果:

EEPROM Demo