Android-ADC数模转换

Mainactity.java

package com.topeet.adctest;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {

	/* add by cym 20140618 */
	adc adc = new adc();
	
	private Button read;
	//String rxIdCode = "";
	private EditText ET1;
	
	private int[] RfidBuffer=new int[20];
	private int RfidRxCount=0;
	/* end add */
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/* add by cym 20140618 */
		read = (Button)findViewById(R.id.button1);
		ET1 = (EditText)findViewById(R.id.tv1);
		
		adc.Open();
		
		read.setOnClickListener(new manager());
		/* end add */
	}

	class manager implements OnClickListener{
		public void onClick(View v) {
			String rxIdCode = "";
			String str;
			
			int i;
			switch (v.getId()) {
			case R.id.button1:
			
				int[] RX= adc.Read();
				
				if(RX != null)
				{
					System.arraycopy(RX, 0, RfidBuffer, RfidRxCount, RX.length);
				}
				for(i=0; i<5; i++)
				{
					str = Integer.toHexString(RfidBuffer[i]);
					
					rxIdCode += str.substring(1);
				}
				
				ET1.setText(rxIdCode);
				break;
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/* add by cym 20140617 */
	static {
        System.loadLibrary("adc");
	}
	/* end add */
}

JNI

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <termios.h>
#include <android/log.h>
#include <sys/ioctl.h>
#include "com_topeet_adctest_adc.h"

#undef  TCSAFLUSH
#define TCSAFLUSH  TCSETSF
#ifndef _TERMIOS_H_
#define _TERMIOS_H_
#endif

int fd=0;

/*
 * Class:     com_topeet_adctest_adc
 * Method:    Open
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Open
  (JNIEnv *env, jobject obj)
  {
	if(fd<=0)fd = open("/dev/adc", O_RDWR|O_NDELAY|O_NOCTTY);
	if(fd <=0 )__android_log_print(ANDROID_LOG_INFO, "serial", "open /dev/adc Error");
	else __android_log_print(ANDROID_LOG_INFO, "serial", "open /dev/adc Sucess fd=%d",fd);
  }

/*
 * Class:     com_topeet_adctest_adc
 * Method:    Close
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Close
  (JNIEnv *env, jobject obj)
  {
	if(fd > 0)close(fd);
  }

/*
 * Class:     com_topeet_adctest_adc
 * Method:    Ioctl
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Ioctl
  (JNIEnv *env, jobject obj, jint num, jint en)
  {
  	ioctl(fd, en, num);
  }
  
/*
 * Class:     com_topeet_adctest_adc
 * Method:    Read
 * Signature: ()[I
 */
JNIEXPORT jintArray JNICALL Java_com_topeet_adctest_adc_Read
  (JNIEnv *env, jobject obj)
 {
		unsigned char buffer[512];
		int BufToJava[512];
		int len=0, i=0;
		
		memset(buffer,0,sizeof(buffer));
		memset(BufToJava,0,sizeof(BufToJava));
		
		len=read(fd,buffer,512);
	
		if(len<=0)return NULL;	
	
		for(i=0;i<len;i++)
		{
			printf("%x", buffer[i]);
			BufToJava[i] = buffer[i];
		}
		jintArray array = (*env)-> NewIntArray(env, len); 
		(*env)->SetIntArrayRegion(env,array, 0, len, BufToJava);
	
		return array;	
  }
  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIOT技术栈

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值