2 Star 1 Fork 1

刘煜/smartspeaker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
button.c 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
刘煜 提交于 2024-03-11 22:44 +08:00 . 消除编译告警
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
// 通过按键控制音量
// key2 音量+
// key3 音量-
int main(int argc, char **argv)
{
struct gpiod_line_bulk keys;
struct gpiod_chip *gpiof = gpiod_chip_open_by_label("GPIOF");
if (!gpiof)
{
perror("gpiod_chip_open");
return EXIT_FAILURE;
}
unsigned int lines[] = {7, 8};
if (gpiod_chip_get_lines(gpiof, lines, 2, &keys) < 0)
{
perror("gpiod_chip_get_lines");
gpiod_chip_close(gpiof);
return EXIT_FAILURE;
}
if (gpiod_line_request_bulk_falling_edge_events(&keys, "keys") < 0)
{
perror("gpiod_line_request_bulk_falling_edges_events");
gpiod_chip_close(gpiof);
return EXIT_FAILURE;
}
while (1)
{
// 等待按键状态改变
struct gpiod_line_bulk pressed;
if (gpiod_line_event_wait_bulk(&keys, NULL, &pressed) < 0)
{
perror("gpiod_line_event_wait_bulk");
}
// 检测按下的按键
struct gpiod_line *key;
int offset;
gpiod_line_bulk_foreach_line_off(&pressed, key, offset)
{
// 读取按键事件
struct gpiod_line_event event;
gpiod_line_event_read(key, &event);
// 根据引脚编号判断按键位置
if (gpiod_line_offset(key) == 7)
{
printf("key2 pressed\n");
system("amixer set Analog 10%+");
}
if (gpiod_line_offset(key) == 8)
{
printf("key3 pressed\n");
system("amixer set Analog 10%-");
}
}
}
gpiod_line_release_bulk(&keys);
gpiod_chip_close(gpiof);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tinytaro/smartspeaker.git
git@gitee.com:tinytaro/smartspeaker.git
tinytaro
smartspeaker
smartspeaker
main

搜索帮助