EEPROM Demo

EEPROMActivity.java

  1. package com.mini6410.EEPROM;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.text.Editable;  
  8. import android.view.View;  
  9. import android.view.Window;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.   
  13. import com.mini6410.R;  
  14.   
  15.   
  16. /** 
  17.  *  
  18.  * ClassName:EEPROMActivity 
  19.  * Reason:   EEPROM Demo 
  20.  * 
  21.  * @author   snowdream 
  22.  * @version   
  23.  * @since    Ver 1.1 
  24.  * @Date     2011   2012-03-16      17:04 
  25.  * 
  26.  * @see       
  27.  */  
  28. public class EEPROMActivity extends Activity {  
  29.     public static final int MSG_UPDATE_UI = 0;  
  30.     public static final int MSG_GET_DATA = 1;  
  31.   
  32.     /*读写按钮和读写输入框*/  
  33.     private Button mButtonWrite = null;  
  34.     private Button mButtonRead = null;  
  35.     private EditText mEditTextWrite = null;  
  36.     private EditText mEditTextRead = null;  
  37.   
  38.     private Editable mEditable = null;  
  39.   
  40.     /*读写模块*/  
  41.     private WriteEEPROM mWriteEEPROM = null;  
  42.     private ReadEEPROM mReadEEPROM = null;  
  43.   
  44.     @Override  
  45.     protected void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         requestWindowFeature(Window.FEATURE_PROGRESS);    
  48.         setContentView(R.layout.eepromdemo);  
  49.         setProgressBarVisibility(true);  
  50.   
  51.         initUI();  
  52.         initData();  
  53.   
  54.     }  
  55.   
  56.   
  57.     /** 
  58.      *  
  59.      * initUI: 初始化UI 
  60.      * 
  61.      * @param    
  62.      * @return      
  63.      * @throws  
  64.      */  
  65.     public void initUI(){  
  66.         mButtonWrite = (Button)findViewById(R.id.Button_write);  
  67.         mButtonRead =  (Button)findViewById(R.id.Button_read);  
  68.   
  69.         mButtonWrite.setOnClickListener(mClickListener);  
  70.         mButtonRead.setOnClickListener(mClickListener);  
  71.   
  72.         mEditTextWrite = (EditText)findViewById(R.id.EditText_write);  
  73.         mEditTextRead = (EditText)findViewById(R.id.EditText_read);  
  74.   
  75.         mEditable = mEditTextRead.getText();  
  76.     }  
  77.   
  78.   
  79.     /** 
  80.      *  
  81.      * initData:新建读写模块,准备读写数据 
  82.      * 
  83.      * @param    
  84.      * @return      
  85.      * @throws  
  86.      */  
  87.     public void initData(){  
  88.         mWriteEEPROM = new WriteEEPROM(mHandler);  
  89.         mReadEEPROM = new ReadEEPROM(mHandler);  
  90.     }  
  91.   
  92.   
  93.     private Handler mHandler = new Handler(){  
  94.   
  95.         @Override  
  96.         public void handleMessage(Message msg) {  
  97.             switch (msg.what) {  
  98.             case MSG_UPDATE_UI:  
  99.                 int pos = (int)msg.arg1;  
  100.                 int length = (int)msg.arg2;  
  101.                 setProgress(pos*10000/(length -1));  
  102.                 break;  
  103.             case MSG_GET_DATA:  
  104.                 Byte dataByte = (Byte)msg.obj;  
  105.                 mEditable.append((char)dataByte.byteValue());  
  106.                 mEditTextRead.setText(mEditable);  
  107.                 break;  
  108.             default:  
  109.                 break;  
  110.             }  
  111.         }  
  112.     };  
  113.   
  114.   
  115.     private Button.OnClickListener mClickListener = new Button.OnClickListener(){  
  116.   
  117.         public void onClick(View v) {  
  118.             Button mButton = (Button)v;  
  119.   
  120.             switch (mButton.getId()) {  
  121.             case R.id.Button_read:  
  122.                 ReadDataIntoEEPROM();  
  123.                 break;  
  124.             case R.id.Button_write:  
  125.                 WriteDataIntoEEPROM();  
  126.                 break;  
  127.             default:  
  128.                 break;  
  129.             }  
  130.   
  131.         }  
  132.   
  133.     };  
  134.   
  135.   
  136.     /** 
  137.      *  
  138.      * WriteDataIntoEEPROM:取出mEditTextWrite输入框中的数据,转换成byte数组,启用写模块写入EEPROM 
  139.      * 
  140.      * @param    
  141.      * @return      
  142.      * @throws  
  143.      */  
  144.     public void WriteDataIntoEEPROM(){  
  145.         byte[] data = mEditTextWrite.getText().toString().getBytes();  
  146.   
  147.         if(mWriteEEPROM != null)  
  148.             mWriteEEPROM.WriteData(data);  
  149.     }  
  150.   
  151.     /** 
  152.      *  
  153.      * ReadDataIntoEEPROM:启用读模块从EEPROM读取数据 
  154.      * 
  155.      * @param    
  156.      * @return      
  157.      * @throws  
  158.      */  
  159.     public void ReadDataIntoEEPROM(){  
  160.         mEditable.clear();  
  161.         if(mReadEEPROM != null)  
  162.             mReadEEPROM.ReadData();  
  163.     }  
  164.   
  165.     @Override  
  166.     protected void onDestroy() {  
  167.         super.onDestroy();  
  168.     }  
  169.   
  170.   
  171. }  

WriteEEPROM.java

  1. package com.mini6410.EEPROM;  
  2.   
  3. import android.os.Handler;  
  4. import android.util.Log;  
  5.   
  6. import com.friendlyarm.AndroidSDK.HardwareControler;  
  7.   
  8. public class WriteEEPROM{  
  9.     private static final String TAG = "WriteEEPROM";  
  10.   
  11.     private static final int MAX_LENGTH = 256//EEPROM最多可存储256个字节数据   
  12.   
  13.     Handler mHandler = null;  
  14.   
  15.     byte[] mData = null;  
  16.   
  17.     private WriteEEPROMThread mWriteEEPROMThread = null;  
  18.   
  19.     public WriteEEPROM(Handler mHandler){  
  20.         this.mHandler = mHandler;  
  21.     }  
  22.   
  23.   
  24.     /** 
  25.      *  
  26.      * WriteData: 新建并启动写线程将数据逐个字节写入EEPROM 
  27.      * 
  28.      * @param   data byte数组 
  29.      * @return      
  30.      * @throws  
  31.      */  
  32.     public void WriteData(byte[] data){  
  33.         mData = data;  
  34.   
  35.         safeStop();  
  36.   
  37.         mWriteEEPROMThread = new WriteEEPROMThread();  
  38.         mWriteEEPROMThread.start();  
  39.     }  
  40.   
  41.     /** 
  42.      *  
  43.      * safeStop: 安全停止线程 
  44.      * 
  45.      * @param    
  46.      * @return      
  47.      * @throws  
  48.      */  
  49.     public void safeStop(){  
  50.         if(mWriteEEPROMThread != null && mWriteEEPROMThread.isAlive()){  
  51.             mWriteEEPROMThread.interrupt();  
  52.             mWriteEEPROMThread.stop = true;  
  53.             try {  
  54.                 mWriteEEPROMThread.join();  
  55.             } catch (InterruptedException e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.         }  
  59.         mWriteEEPROMThread = null;  
  60.     }  
  61.   
  62.     public void sendMessage(int what ){  
  63.         if(mHandler != null){  
  64.             mHandler.sendMessage(mHandler.obtainMessage(what));  
  65.         }     
  66.     }  
  67.   
  68.     public void sendMessage(int what, Object obj ){  
  69.         if(mHandler != null){  
  70.             mHandler.sendMessage(mHandler.obtainMessage(what, obj));  
  71.         }  
  72.     }  
  73.   
  74.     public void sendMessage(int what, int arg1,int arg2,Object obj ){  
  75.         if(mHandler != null){  
  76.             mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));  
  77.         }  
  78.     }  
  79.   
  80.   
  81.     public void sendMessage(int what, int arg1,int arg2 ){  
  82.         if(mHandler != null){  
  83.             mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));  
  84.         }  
  85.     }  
  86.   
  87.     /** 
  88.      *  
  89.      * WriteEEPROMThread: 数据写入线程 
  90.      * 
  91.      * @param    
  92.      * @return      
  93.      * @throws  
  94.      */  
  95.     private class WriteEEPROMThread extends Thread{  
  96.         volatile boolean stop = false;  
  97.   
  98.         int fd = 0;   
  99.         int length = 0;  
  100.         int pos = 0;  
  101.   
  102.         @Override  
  103.         public void run() {  
  104.   
  105.             if(mData == null){  
  106.                 Log.e(TAG, "There is No Data!");  
  107.   
  108.                 stop = true;  
  109.             }  
  110.   
  111.             /*打开设备*/  
  112.             fd = HardwareControler.openI2CDevice();  
  113.   
  114.             if(fd == -1)  
  115.             {  
  116.                 Log.e(TAG, "Failed to open the I2CDevice !");  
  117.   
  118.                 stop = true;  
  119.             }  
  120.   
  121.   
  122.             length = mData.length;  
  123.   
  124.             if (length > MAX_LENGTH) {  
  125.                 length = MAX_LENGTH;  
  126.             }  
  127.   
  128.             //擦除并初始化EEPROM   
  129.             for(int i = 0 ; i < MAX_LENGTH; i++){  
  130.                 HardwareControler.writeByteDataToI2C(fd, i, (byte)'\0');  
  131.             }  
  132.   
  133.             while(!stop){  
  134.   
  135.                 if (pos >= length) {  
  136.                     break;  
  137.                 }  
  138.   
  139.                 /*写入数据,每次只能读取一个字节。*/  
  140.                 HardwareControler.writeByteDataToI2C(fd, pos, mData[pos]);  
  141.   
  142.                 sendMessage(EEPROMActivity.MSG_UPDATE_UI, pos,length);  
  143.   
  144.                 Log.i(TAG, "writeByteDataToI2C pos: "+ pos);  
  145.   
  146.                 pos++;  
  147.   
  148.                 //              try {   
  149.                 //                  Thread.sleep(10);   
  150.                 //              } catch ( InterruptedException e ) {   
  151.                 //                  e.printStackTrace();   
  152.                 //              }   
  153.   
  154.             }  
  155.   
  156.             if(fd != -1)  
  157.             {  
  158.                 /*关闭设备*/  
  159.                 HardwareControler.close(fd);  
  160.             }  
  161.   
  162.         }  
  163.     }  
  164. }  

ReadEEPROM.java

  1. package com.mini6410.EEPROM;  
  2.   
  3. import android.os.Handler;  
  4. import android.util.Log;  
  5.   
  6. import com.friendlyarm.AndroidSDK.HardwareControler;  
  7.   
  8. public class ReadEEPROM{  
  9.     private static final String TAG = "ReadEEPROM";  
  10.   
  11.     private static final int MAX_LENGTH = 256//EEPROM最多可存储256个字节数据   
  12.   
  13.     Handler mHandler = null;  
  14.   
  15.     private ReadEEPROMThread mReadEEPROMThread = null;  
  16.   
  17.     public ReadEEPROM(Handler mHandler){  
  18.         this.mHandler = mHandler;  
  19.     }  
  20.   
  21.     /** 
  22.      *  
  23.      * ReadData: 新建并启动读线程从EEPROM中逐个读取数据 
  24.      * 
  25.      * @param    
  26.      * @return      
  27.      * @throws  
  28.      */  
  29.     public void ReadData(){  
  30.         safeStop();  
  31.   
  32.         mReadEEPROMThread = new ReadEEPROMThread();  
  33.         mReadEEPROMThread.start();  
  34.     }  
  35.   
  36.     /** 
  37.      *  
  38.      * safeStop: 安全停止线程 
  39.      * 
  40.      * @param    
  41.      * @return      
  42.      * @throws  
  43.      */  
  44.     public void safeStop(){  
  45.         if(mReadEEPROMThread != null && mReadEEPROMThread.isAlive()){  
  46.             mReadEEPROMThread.interrupt();  
  47.             mReadEEPROMThread.stop = true;  
  48.             try {  
  49.                 mReadEEPROMThread.join();  
  50.             } catch (InterruptedException e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.         }  
  54.         mReadEEPROMThread = null;  
  55.     }  
  56.   
  57.     public void sendMessage(int what ){  
  58.         if(mHandler != null){  
  59.             mHandler.sendMessage(mHandler.obtainMessage(what));  
  60.         }     
  61.     }  
  62.   
  63.     public void sendMessage(int what, Object obj ){  
  64.         if(mHandler != null){  
  65.             mHandler.sendMessage(mHandler.obtainMessage(what, obj));  
  66.         }  
  67.     }  
  68.   
  69.     public void sendMessage(int what, int arg1,int arg2,Object obj ){  
  70.         if(mHandler != null){  
  71.             mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2,obj));  
  72.         }  
  73.     }  
  74.   
  75.       
  76.     public void sendMessage(int what, int arg1,int arg2 ){  
  77.         if(mHandler != null){  
  78.             mHandler.sendMessage(mHandler.obtainMessage(what,arg1,arg2));  
  79.         }  
  80.     }  
  81.       
  82.     /** 
  83.      *  
  84.      * ReadEEPROMThread: 数据读取线程 
  85.      * 
  86.      * @param    
  87.      * @return      
  88.      * @throws  
  89.      */  
  90.     private class ReadEEPROMThread extends Thread{  
  91.         volatile boolean stop = false;  
  92.   
  93.         int fd = 0;   
  94.         int length = 0;  
  95.         int pos = 0;  
  96.         byte data = 0;  
  97.   
  98.         @Override  
  99.         public void run() {  
  100.   
  101.             /*打开设备*/  
  102.             fd = HardwareControler.openI2CDevice();  
  103.   
  104.             if(fd == -1)  
  105.             {  
  106.                 Log.e(TAG, "Failed to open the I2CDevice !");  
  107.   
  108.                 stop = true;  
  109.             }  
  110.   
  111.             length = MAX_LENGTH;  
  112.   
  113.             while(!stop){  
  114.               
  115.                 if (pos >= length) {  
  116.                     break;  
  117.                 }  
  118.                   
  119.                 /*读取数据,每次只能读取一个字节。*/  
  120.                 data = (byte)HardwareControler.readByteDataFromI2C(fd, pos);  
  121.                   
  122.                 if(data != -1)  
  123.                 {  
  124.                     sendMessage(EEPROMActivity.MSG_GET_DATA, data);  
  125.                     sendMessage(EEPROMActivity.MSG_UPDATE_UI, pos,length);  
  126.                 }  
  127.           
  128.                 Log.i(TAG, "readByteDataFromI2C pos: "+ pos);  
  129.                   
  130.                 pos++;  
  131.   
  132. //              try {   
  133. //                  Thread.sleep(10);   
  134. //              } catch ( InterruptedException e ) {   
  135. //                  e.printStackTrace();   
  136. //              }   
  137.   
  138.             }  
  139.   
  140.             if(fd != -1)  
  141.             {  
  142.                 /*关闭设备*/  
  143.                 HardwareControler.close(fd);  
  144.             }  
  145.         }  
  146.     }  
  147. }  

eepromdemo.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.   
  7.     <LinearLayout  
  8.         android:id="@+id/eeprom"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <LinearLayout  
  14.             android:id="@+id/writemodel"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_weight="1"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <Button  
  21.                 android:id="@+id/Button_write"  
  22.                 android:layout_width="fill_parent"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:text="@string/writeeeprom" />  
  25.   
  26.             <EditText  
  27.                 android:id="@+id/EditText_write"  
  28.                 android:layout_width="fill_parent"  
  29.                 android:layout_height="100dip"  
  30.                 android:text="@string/dataeeprom" />  
  31.         </LinearLayout>  
  32.   
  33.         <LinearLayout  
  34.             android:id="@+id/readmodel"  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="fill_parent"  
  37.             android:layout_weight="1"  
  38.             android:orientation="vertical" >  
  39.   
  40.             <Button  
  41.                 android:id="@+id/Button_read"  
  42.                 android:layout_width="fill_parent"  
  43.                 android:layout_height="wrap_content"  
  44.                 android:text="@string/readeeprom" />  
  45.   
  46.             <EditText  
  47.                 android:id="@+id/EditText_read"  
  48.                 android:layout_width="fill_parent"  
  49.                 android:layout_height="100dip" />  
  50.         </LinearLayout>  
  51.     </LinearLayout>  
  52.   
  53. </LinearLayout>  

预览效果:


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值