当前位置: 首页 > wzjs >正文

手机网站弹出导航菜单长沙seo优化

手机网站弹出导航菜单,长沙seo优化,做义工的靠谱网站,网站程序 不能创建文件夹需求 驱动中应用层循环读取按键状态耗费太多cpu资源&#xff0c;期望在按键未被操作时以阻塞形式让程序休眠&#xff0c;待按键被操作时&#xff0c;唤醒读取数据给应用层。驱动代码&#xff1a; #include <linux/types.h> #include <linux/kernel.h> #include <…

需求

驱动中应用层循环读取按键状态耗费太多cpu资源,期望在按键未被操作时以阻塞形式让程序休眠,待按键被操作时,唤醒读取数据给应用层。

在这里插入图片描述

驱动代码:

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
//#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>#define KEY_CNT			1		/* 设备号个数 	*/
#define KEY_NAME		"blockirqkey"	/* 名字 		*/enum key_status{KEY_PRESS = 0,  // 按下KEY_RELEASE,   // 松开KEY_KEEP,     // 保持
};struct key_dev{dev_t devid;                   // 设备号struct cdev cdev;             // cdevstruct class* class;         //  类struct device* device;      // 设备struct device_node* nd;     // 设备节点int key_gpio;             // gpio编号struct timer_list timer;  //int irq_num;             // 中断号atomic_t status;         // 按键状态wait_queue_head_t r_wait; // 读等待队列头
};static struct key_dev key;  // 设备static void key_timer_function(struct timer_list* arg){   // 定时器处理函数static int last_val = 0;int current_val;current_val = gpio_get_value(key.key_gpio); // 读取按键值if(1 == current_val && !last_val){  // 按下atomic_set(&key.status, KEY_PRESS);wake_up_interruptible(&key.r_wait);  // 唤醒r_wait队列头中的所有队列}else if(0 == current_val && last_val){ // 松开atomic_set(&key.status, KEY_RELEASE);wake_up_interruptible(&key.r_wait); // 唤醒}else atomic_set(&key.status, KEY_KEEP); // 状态保持last_val = current_val;}/*解析设备树*/
static int key_parse_dt(void){int ret;const char* str;key.nd = of_find_node_by_path("/key");if(key.nd == NULL){printk("key node not find \n");return -1;}ret = of_property_read_string(key.nd, "status", &str);if(ret < 0)return -1;if(strcmp(str, "okay"))return -1;ret = of_property_read_string(key.nd, "compatible", &str);if(ret < 0){printk("key: failed to get compatible property\n");return -1;}if(strcmp(str, "alientek,key")){printk("key: compatible match failed\n");return -1;}key.key_gpio = of_get_named_gpio(key.nd, "key-gpio", 0);if(key.key_gpio < 0){printk("cant get key-gpio");return -1;}key.irq_num = irq_of_parse_and_map(key.nd, 0); // 获取gpio对应中断号if(!key.irq_num){return -1;}printk("key-gpio num: %d\n", key.key_gpio);return 0;
}static irqreturn_t key_interrupt(int irq, void* dev_id){  // 中断处理函数mod_timer(&key.timer, jiffies + msecs_to_jiffies(15));return IRQ_HANDLED;
}static int key_gpio_init(void){int ret;unsigned long irq_flags;ret = gpio_request(key.key_gpio, "KEY0");if(ret){printk("failed to request key-gpio\n");return ret;}gpio_direction_input(key.key_gpio); // 输入模式irq_flags = irq_get_trigger_type(key.irq_num);if(irq_flags == IRQF_TRIGGER_NONE)irq_flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;ret = request_irq(key.irq_num, key_interrupt, irq_flags, "key0_IRQ", NULL); // 申请中断if(ret){gpio_free(key.key_gpio);return ret;}return 0;
}static int key_open(struct inode* inode, struct file* filp){return 0;
}static ssize_t key_read(struct file* filp, char __user* buf, size_t cnt, loff_t* offt){int ret;// 加入队列,当有按键按下或松开动作发生才会被唤醒printk("read wait\n");ret = wait_event_interruptible(key.r_wait, KEY_KEEP != atomic_read(&key.status));printk("read release\n");if(ret) return ret;// 发送数据给应用层序ret = copy_to_user(buf, &key.status, sizeof(int));atomic_set(&key.status, KEY_KEEP);return ret; 
}static ssize_t key_write(struct file* filp, const char __user* buf, size_t cnt, loff_t* offt){return 0;
}static int key_release(struct inode* inode, struct file* filp){return 0;
}static struct file_operations key_fops = {.owner = THIS_MODULE,.open = key_open,.read = key_read,.write = key_write,.release = key_release,
};static int __init mykey_init(void){int ret;// 初始化等待队列头init_waitqueue_head(&key.r_wait);timer_setup(&key.timer, key_timer_function, 0); // 初始化timer// 初始化按键状态atomic_set(&key.status, KEY_KEEP);ret = key_parse_dt(); // 设备树解析if(ret) return ret;ret = key_gpio_init(); // 中断初始化if(ret) return ret; /*注册字符设备驱动*/// 1、创建设备号  ret = alloc_chrdev_region(&key.devid, 0, KEY_CNT, KEY_NAME);  // 申请设备号if(ret < 0){printk("alloc_chrdev_region failed");goto free_gpio;}// 2.初始化cdevkey.cdev.owner = THIS_MODULE;cdev_init(&key.cdev, &key_fops);ret = cdev_add(&key.cdev, key.devid, KEY_CNT);if(ret < 0) goto del_unregister;// 创建类key.class = class_create(THIS_MODULE, KEY_NAME);if(IS_ERR(key.class)){goto del_cdev;}// 创建设备key.device = device_create(key.class, NULL, key.devid, NULL, KEY_NAME);if(IS_ERR(key.device)){goto destory_class;}return 0;destory_class:class_destroy(key.class);
del_cdev:cdev_del(&key.cdev);
del_unregister:unregister_chrdev_region(key.devid, KEY_CNT);
free_gpio:  free_irq(key.irq_num, NULL);gpio_free(key.key_gpio);return -1;
}static void __exit mykey_exit(void){// 注销字符设备驱动cdev_del(&key.cdev);  // 删除cdevunregister_chrdev_region(key.devid, KEY_CNT); // 注销设备号del_timer_sync(&key.timer); // 删除timerdevice_destroy(key.class, key.devid); // 注销设备class_destroy(key.class); // 注销类free_irq(key.irq_num, NULL); // 释放中断gpio_free(key.key_gpio); // 释放io
}module_init(mykey_init);
module_exit(mykey_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Narnat");

按键未被操作ret = wait_event_interruptible(key.r_wait, KEY_KEEP != atomic_read(&key.status));会休眠程序,按键被操作后触发上升沿或下降沿,进入定时器、进入中断后唤醒程序,将键值发送给用户

应用层:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char* argv[]){int fd, ret;int key_val;if(argc != 2){printf("input number error!\n");return -1;}fd = open(argv[1], O_RDONLY);if(fd < 0){printf("open file error\n");return -1;}printf("fd: %d \n", fd);while(1){read(fd, &key_val, sizeof(int));printf("keyApp key_value: %d\n", key_val);if(key_val == 0){printf("key press\n");}else if(key_val == 1){printf("key release\n");}}close(fd);return 0;
}

效果:

在这里插入图片描述

http://www.dtcms.com/wzjs/464065.html

相关文章:

  • 四川平台网站建设哪里有简易的旅游网页制作
  • 太原建设网站淘宝关键词搜索工具
  • 免费门户网站搭建如何seo网站推广
  • 政府网站上怎么做电子签名浏览器大全
  • 自己做彩票网站犯法吗佛山seo培训机构
  • 做网站的版式会侵权吗足球世界积分榜
  • 做自媒体用到的网站舆情优化公司
  • 崇明做网站友情链接交换的意义是什么
  • 宝塔建设网站域名进不去网络营销的重要性
  • 电子商务网站优点怎么做神马搜索排名seo
  • 湖南省交通建设质监局网站宁波网站推广大全
  • 怎样在网站上做友情链接高端网站建设的公司
  • 鹤岗做网站北京seo优化
  • 西宁做网站seo网络开发
  • 网页网站动作效果做的比较棒灯塔seo
  • wordpress手机端怎么用旺道网站排名优化
  • 建设网站呼叫中心有什么好处推广引流网站
  • 机械做网站长沙网站seo收费标准
  • 南京建设网站首页电子商务与网络营销教案
  • wordpress萌主题下载黑河seo
  • 网站开发项目合同今日国际新闻摘抄十条
  • 网站做流量推广的方式网站运营seo实训总结
  • 西安市政道桥建设公司网站手机搭建网站
  • 公司做了网站怎么做推广搭建网站需要什么技术
  • 商丘集团网站建设百度点击软件找名风
  • 个人网站做淘宝客容易封吗淘宝seo搜索引擎优化
  • 芜湖做公司网站的如何屏蔽百度广告推广
  • app制作永久免费网站排名优化服务
  • 开一个做网站的工作室网络服务公司经营范围
  • 有没有可以做物理实验的网站西安百度搜索排名