硬件访问服务3之Android程序操作LED_P

来源:互联网 发布:html的sql注入 编辑:程序博客网 时间:2024/05/17 22:27

硬件驱动LED.c:

#include <linux/kernel.h>#include <linux/module.h>#include <linux/miscdevice.h>#include <linux/device.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/moduleparam.h>#include <linux/slab.h>#include <linux/ioctl.h>#include <linux/cdev.h>#include <linux/delay.h>#include <linux/gpio.h>#include <mach/gpio.h>#include <plat/gpio-cfg.h>static int led_gpios[] = {    EXYNOS4212_GPM4(0),    EXYNOS4212_GPM4(1),    EXYNOS4212_GPM4(2),    EXYNOS4212_GPM4(3),};static int led_open(struct inode *inode, struct file *file){    /* 配置GPIO为输出引脚 */    int i;    for (i = 0; i < 4; i++)        s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);    return 0;}/* app : ioctl(fd, cmd, arg) */static long led_ioctl(struct file *filp, unsigned int cmd,        unsigned long arg){    /* 根据传入的参数设置GPIO */    /* cmd : 0-off, 1-on */    /* arg : 0-3, which led */    if ((cmd != 0) && (cmd != 1))        return -EINVAL;    if (arg > 4)        return -EINVAL;    gpio_set_value(led_gpios[arg], !cmd);    return 0;}static struct file_operations leds_ops = {    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open   =   led_open,         .unlocked_ioctl    = led_ioctl,};static int major;static struct class *cls;int leds_init(void){    major = register_chrdev(0, "leds", &leds_ops);    /* 为了让系统udev,mdev给我们创建设备节点 */    /* 创建类, 在类下创建设备 : /sys */    cls = class_create(THIS_MODULE, "leds");    device_create(cls, NULL, MKDEV(major, 0), NULL, "leds"); /* /dev/leds */    return 0;}void leds_exit(void){    device_destroy(cls, MKDEV(major, 0));    class_destroy(cls);    unregister_chrdev(major, "leds");}module_init(leds_init);module_exit(leds_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("www.100ask.net");

c库文件:
hardcontrol.c:

#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <android/log.h>  /* liblog *///__android_log_print(ANDROID_LOG_DEBUG, "JNIDemo", "native add ...");#if 0typedef struct {    char *name;          /* Java里调用的函数名 */    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */    void *fnPtr;          /* C语言实现的本地函数 */} JNINativeMethod;#endifstatic jint fd;jint ledOpen(JNIEnv *env, jobject cls){    fd = open("/dev/leds", O_RDWR);    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen : %d", fd);    if (fd >= 0)        return 0;    else        return -1;}void ledClose(JNIEnv *env, jobject cls){    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ...");    close(fd);}jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status){    int ret = ioctl(fd, status, which);    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d, %d", which, status, ret);    return ret;}static const JNINativeMethod methods[] = {    {"ledOpen", "()I", (void *)ledOpen},    {"ledClose", "()V", (void *)ledClose},    {"ledCtrl", "(II)I", (void *)ledCtrl},};/* System.loadLibrary */JNIEXPORT jint JNICALLJNI_OnLoad(JavaVM *jvm, void *reserved){    JNIEnv *env;    jclass cls;    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {        return JNI_ERR; /* JNI version not supported */    }    cls = (*env)->FindClass(env, "com/thisway/hardlibrary/HardControl");    if (cls == NULL) {        return JNI_ERR;    }    /* 2. map java hello <-->c c_hello */    if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)        return JNI_ERR;    return JNI_VERSION_1_4;}

Android代码:
HardControl.java:

package com.thisway.hardlibrary;**public class HardControl** {    public static native int ledCtrl(int which, int status);    public static native int ledOpen();    public static native void ledClose();    static {        try {            System.loadLibrary("hardcontrol");        } catch (Exception e) {            e.printStackTrace();        }    }}

MainActivity.java

package com.thisway.app_0001_leddemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.view.View;import android.widget.CheckBox;import android.widget.Toast;import com.thisway.hardlibrary.*;public class MainActivity extends AppCompatActivity {    private boolean ledon = false;    private Button button = null;    private CheckBox checkBoxLed1 = null;    private CheckBox checkBoxLed2 = null;    private CheckBox checkBoxLed3 = null;    private CheckBox checkBoxLed4 = null;    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            ledon = !ledon;            if (ledon) {                button.setText("ALL OFF");                checkBoxLed1.setChecked(true);                checkBoxLed2.setChecked(true);                checkBoxLed3.setChecked(true);                checkBoxLed4.setChecked(true);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 1);            }            else {                button.setText("ALL ON");                checkBoxLed1.setChecked(false);                checkBoxLed2.setChecked(false);                checkBoxLed3.setChecked(false);                checkBoxLed4.setChecked(false);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 0);            }        }    }    public void onCheckboxClicked(View view) {        // Is the view now checked?        boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked        switch(view.getId()) {            case R.id.LED1:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED1 on", Toast.LENGTH_SHORT).show();                    **HardControl.ledCtrl(0, 1);**                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED1 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(0, 0);                }                break;            case R.id.LED2:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED2 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(1, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED2 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(1, 0);                }                break;            case R.id.LED3:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED3 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(2, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED3 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(2, 0);                }                break;            case R.id.LED4:                if (checked) {                    // Put some meat on the sandwich                    Toast.makeText(getApplicationContext(), "LED4 on", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(3, 1);                }                else {                    // Remove the meat                    Toast.makeText(getApplicationContext(), "LED4 off", Toast.LENGTH_SHORT).show();                    HardControl.ledCtrl(3, 0);                }                break;            // TODO: Veggie sandwich        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.BUTTON);        HardControl.ledOpen();        checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);        checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);        checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);        checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);        button.setOnClickListener(new MyButtonListener());/*        button.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                // Perform action on click                ledon = !ledon;                if (ledon)                    button.setText("ALL OFF");                else                    button.setText("ALL ON");            }        });*/    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}