日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

gpio操作实现人体感应灯(一)

發布時間:2023/12/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gpio操作实现人体感应灯(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <poll.h>#define MSG(args...) printf(args) //函數聲明 static int gpio_export(int pin); static int gpio_unexport(int pin); static int gpio_direction(int pin, int dir); static int gpio_write(int pin, int value); static int gpio_read(int pin);static int gpio_export(int pin) { char buffer[64]; int len; int fd; fd = open("/sys/class/gpio/export", O_WRONLY); if (fd < 0) { MSG("Failed to open export for writing!\n"); return(-1); } len = snprintf(buffer, sizeof(buffer), "%d", pin); if (write(fd, buffer, len) < 0) { MSG("Failed to export gpio!"); return -1; } close(fd); return 0; } static int gpio_unexport(int pin) { char buffer[64]; int len; int fd; fd = open("/sys/class/gpio/unexport", O_WRONLY); if (fd < 0) { MSG("Failed to open unexport for writing!\n"); return -1; } len = snprintf(buffer, sizeof(buffer), "%d", pin); if (write(fd, buffer, len) < 0) { MSG("Failed to unexport gpio!"); return -1; } close(fd); return 0; } //dir: 0-->IN, 1-->OUT static int gpio_direction(int pin, int dir) { static const char dir_str[] = "in\0out"; char path[64]; int fd; snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin); fd = open(path, O_WRONLY); if (fd < 0) { MSG("Failed to open gpio direction for writing!\n"); return -1; } if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) { MSG("Failed to set direction!\n"); return -1; } close(fd); return 0; } //value: 0-->LOW, 1-->HIGH static int gpio_write(int pin, int value) { static const char values_str[] = "01"; char path[64]; int fd; snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin); fd = open(path, O_WRONLY); if (fd < 0) { MSG("Failed to open gpio value for writing!\n"); return -1; } if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) { MSG("Failed to write value!\n"); return -1; } close(fd); return 0; }static int gpio_read(int pin) { char path[64]; char value_str[3]; int fd; snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin); fd = open(path, O_RDONLY); if (fd < 0) { MSG("Failed to open gpio value for reading!\n"); return -1; } if (read(fd, value_str, 3) < 0) { MSG("Failed to read value!\n"); return -1; } close(fd); return (atoi(value_str)); } // none表示引腳為輸入,不是中斷引腳 // rising表示引腳為中斷輸入,上升沿觸發 // falling表示引腳為中斷輸入,下降沿觸發 // both表示引腳為中斷輸入,邊沿觸發 // 0-->none, 1-->rising, 2-->falling, 3-->both static int gpio_edge(int pin, int edge) { const char dir_str[] = "none\0rising\0falling\0both"; char ptr; char path[64]; int fd; switch(edge){ case 0: ptr = 0; break; case 1: ptr = 5; break; case 2: ptr = 12; break; case 3: ptr = 20; break; default: ptr = 0; } snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin); fd = open(path, O_WRONLY); if (fd < 0) { MSG("Failed to open gpio edge for writing!\n"); return -1; } if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0) { MSG("Failed to set edge!\n"); return -1; } close(fd); return 0; }//GPIO1_17 int main() { int gpio_fd, ret; struct pollfd fds[1]; char buff[10]; unsigned char cnt = 0; //LED引腳初始化 gpio_export(115); gpio_direction(115, 1); gpio_write(115, 0); //按鍵引腳初始化 gpio_export(49); gpio_direction(49, 0); gpio_edge(49,1); gpio_fd = open("/sys/class/gpio/gpio49/value",O_RDONLY); if(gpio_fd < 0){ MSG("Failed to open value!\n"); return -1; } fds[0].fd = gpio_fd; fds[0].events = POLLPRI; ret = read(gpio_fd,buff,10); if( ret == -1 ) MSG("read\n"); while(1){ ret = poll(fds,1,0); if( ret == -1 ) MSG("poll\n"); if( fds[0].revents & POLLPRI){ ret = lseek(gpio_fd,0,SEEK_SET); if( ret == -1 ) MSG("lseek\n"); ret = read(gpio_fd,buff,10); if( ret == -1 ) MSG("read\n"); gpio_write(115, cnt++%2); } usleep(100000); } return 0; }

總結

以上是生活随笔為你收集整理的gpio操作实现人体感应灯(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。