Android之在GEC210板上点灯
要用Android控制自定义的硬件,如何实现呢?用JNI即可。1、准备工作
好了,先做些准备工作。准备工作无非就是搭建下环境,下载些东西。请看些链接。点我点我!
2、led驱动
照理说,点灯的程序,我不应该贴出来的,但是,考虑到有同学做Android没学过驱动,我就贴出来,仅供参考哈:
[cpp]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "leds"
static int led_gpios[] = {
S5PV210_GPJ2(0),
S5PV210_GPJ2(1),
S5PV210_GPJ2(2),
S5PV210_GPJ2(3),
};
#define LED_NUM ARRAY_SIZE(led_gpios)
static long gec210_leds_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > LED_NUM) {
return -EINVAL;
}
gpio_set_value(led_gpios[arg], !cmd);
//printk(DEVICE_NAME": %d %d\n", arg, cmd);
break;
default:
return -EINVAL;
}
return 0;
}
static struct file_operations gec210_led_dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = gec210_leds_ioctl,
};
static struct miscdevice gec210_led_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gec210_led_dev_fops,
};
static int __init gec210_led_dev_init(void) {
int ret;
int i;
for (i = 0; i < LED_NUM; i++) {
ret = gpio_request(led_gpios[i], "LED");
if (ret) {
printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,
led_gpios[i], ret);
return ret;
}
s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);
gpio_set_value(led_gpios[i], 1);
}
ret = misc_register(&gec210_led_dev);
printk(DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit gec210_led_dev_exit(void) {
int i;
for (i = 0; i < LED_NUM; i++) {
gpio_free(led_gpios[i]);
}
misc_deregister(&gec210_led_dev);
}
module_init(gec210_led_dev_init);
module_exit(gec210_led_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("GEC Inc.");
Makefile也附出来了:
[plain]
ifneq ($(KERNELRELEASE),)
obj-m :=led_drv.o
else
module-objs :=led_drv.o
KERNELDIR :=/home/gec/linux_kernel/linux-2.6.35.7/
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
$(RM) *.ko *.mod.c *.mod.o *.o *.order *.symvers *.cmd
那个linux-2.6.35.7是指GEC210板上Android系统的Linux内核版本,而且在PC机的Linux下也要有这个内核的源码,路径按Makefile里面放,也可以改Makefile需要的同学可以自行下载哈。
好了。执行make,之后,就能得到一个.ko结尾的文件,把这个.ko文件放进GEC210板的文件系统里,怎么放进去?SD卡也可以,网线nfs也可以,串口线也可以。这里不详说。
3、编写Android应用程序
不多说,看代码吧,都是最基本的。2.2版的
LedDemoTestActivity.java
[java]
package com.gec.leddemotest.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class LedDemoTestActivity extends Activity {
private RadioGroup radioGroupLed01;
private RadioGroup radioGroupLed02;
private RadioGroup radioGroupLed03;
private RadioGroup radioGroupLed04;
static{
System.loadLibrary("leddemotest");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ope
补充:移动开发 , Android ,