概述
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)'