用linux的input架构
先产生一个input设备并注册,再用platform_device的/sys当做程序入口
最后用一个用户程序利用随机数产生相对坐标,写入入口
驱动:
-
#include <linux/module.h>
-
#include <linux/platform_device.h>
-
-
#include <linux/input.h>
-
-
-
-
-
struct platform_device *vms_dev;
-
-
struct input_dev *vms_input_dev;
-
-
static ssize_t write_vms(struct device *dev,struct device_attribute *attr,
-
const char *buffer,size_t count)
-
{
-
int x,y;
-
sscanf(buffer,"%d%d",&x,&y);
-
-
input_report_rel(vms_input_dev,REL_X,x);
-
input_report_rel(vms_input_dev,REL_Y,y);
-
input_sync(vms_input_dev);
-
-
return count;
-
}
-
-
DEVICE_ATTR(coordinates,0644,NULL,write_vms);
-
-
static struct attribute *vms_attrs[] = {
-
&dev_attr_coordinates.attr,
-
NULL,
-
};
-
-
static struct attribute_group vms_attr_group = {
-
.attrs = vms_attrs,
-
};
-
-
static int __init vms_init(void)
-
{
-
-
vms_input_dev = input_allocate_device();
-
set_bit(EV_REL,vms_input_dev->evbit);
-
set_bit(REL_X,vms_input_dev->relbit);
-
set_bit(REL_Y,vms_input_dev->relbit);
-
-
input_register_device(vms_input_dev);
-
-
printk("input_register_device ok\n");
-
-
//use cdev or platform as hardware ISR input
-
vms_dev = platform_device_register_simple("vms",-1,NULL,0);
-
-
sysfs_create_group(&vms_dev->dev.kobj,&vms_attr_group);
-
-
-
return 0;
-
}
-
-
static void __exit vms_exit(void)
-
{
-
input_unregister_device(vms_input_dev);
-
-
printk("input_unregister_device ok\n");
-
-
sysfs_remove_group(&vms_dev->dev.kobj,&vms_attr_group);
-
-
platform_device_unregister(vms_dev);
-
}
-
-
-
module_init(vms_init);
-
module_exit(vms_exit);
-
-
MODULE_LICENSE("GPL");
-
MODULE_AUTHOR("archer");
用户虚拟坐标程序:
-
#include <stdio.h>
-
#include <string.h>
-
#include <fcntl.h>
-
-
-
-
-
int main(int argc,char *argv[])
-
{
-
int sim_fd;
-
-
int x,y;
-
char buffer[10];
-
-
sim_fd = open("/sys/devices/platform/vms/coordinates",O_RDWR);
-
if(sim_fd < 0) {
-
printf("open fail\n");
-
return -1;
-
}
-
-
while(1) {
-
x = random()%20;
-
y = random()%20;
-
-
if(x % 2)
-
x = -x;
-
if(y % 2)
-
y = -y;
-
-
sprintf(buffer,"%d %d %d",x,y,0);
-
-
write(sim_fd,buffer,strlen(buffer));
-
fsync(sim_fd);
-
-
sleep(1);
-
}
-
-
close(sim_fd);
-
}
最后用一个 helper工具读取evdev事件包就可以了
gpm -m /dev/input/eventX -t evdev
阅读(2552) | 评论(0) | 转发(0) |