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

手机网站弹出导航菜单线上如何推广自己的产品

手机网站弹出导航菜单,线上如何推广自己的产品,受欢迎的南昌网站建设,武汉专业做网站的公司有哪些需求 驱动中应用层循环读取按键状态耗费太多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/47392.html

相关文章:

  • 网站流量来源查询合肥做网络推广的公司
  • 开放一个网站多少钱湖南seo优化公司
  • 原墨网站建设长春网站建设技术支持
  • 如何在百度里建网站网络推广是做什么工作的
  • 怎么做网站申请广告灰色关键词排名方法
  • 苏州网站seo什么是百度竞价排名服务
  • 网页设计网站哪个公司好推广合作
  • 广东做网站公司有哪些最近国际新闻
  • 肇庆网站优化建设北京软件培训机构前十名
  • 德惠市城乡建设局网站沈阳seo关键词
  • wordpress的开发文档优化建站seo门户
  • 一台服务器怎么做多给网站教育培训机构前十名
  • 专业做网站有哪些百度文库官网登录入口
  • 前端做商城网站需要多久线上营销渠道
  • 企业网站里面的qq咨询怎么做上海百度推广公司排名
  • 大连承揽营销型网站公司成都专业seo公司
  • 石家庄手机网站开发磁力最好用的搜索引擎
  • wordpress美女图片站主题营销推广案例
  • 厦门同安网站建设网站排名搜索
  • 做一件代发的网站东莞疫情最新消息
  • 虫虫 wordpress 群发搜索引擎优化工作
  • 四川住房和城乡建设厅网站主页seo的方式包括
  • 荆州哪里有做网站的seo优化便宜
  • 做火情监控网站需要用什么系统地推的60种方法
  • 林业门户网站建设网络广告营销成功案例
  • 自助网站建设哪家好南京seo优化推广
  • 猎头可以做单的网站地推网app推广平台
  • 网站建设公司制作网站关键词工具
  • 浅谈电子商务网站建设与管理的理解镇江百度关键词优化
  • 南京鼓楼做网站公司网站代搭建维护