1
0

feat(lab03): add buttons case

This commit is contained in:
2026-04-24 16:10:05 +02:00
parent 1221757fae
commit 110ebe0d70

View File

@@ -42,7 +42,6 @@ void* btn_thread(void* arg) {
btn[i] = open_btn(GPIO_BTN[i], BTN[i]); btn[i] = open_btn(GPIO_BTN[i], BTN[i]);
if (btn[i] < 0) { if (btn[i] < 0) {
perror("Failed to open button"); perror("Failed to open button");
return 1;
} }
} }
@@ -50,7 +49,6 @@ void* btn_thread(void* arg) {
int epfd = epoll_create1(0); int epfd = epoll_create1(0);
if (epfd < 0) { if (epfd < 0) {
perror("Failed to create epoll"); perror("Failed to create epoll");
return 1;
} }
// Add buttons to epoll // Add buttons to epoll
@@ -63,7 +61,6 @@ void* btn_thread(void* arg) {
ev[i].data.fd = btn[i]; ev[i].data.fd = btn[i];
if (epoll_ctl(epfd, EPOLL_CTL_ADD, btn[i], &ev[i]) < 0) { if (epoll_ctl(epfd, EPOLL_CTL_ADD, btn[i], &ev[i]) < 0) {
perror("Failed to add button to epoll"); perror("Failed to add button to epoll");
return 1;
} }
} }
@@ -88,20 +85,22 @@ void* btn_thread(void* arg) {
} }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < NBR_BTN; j++) { // read btn file
if (events[i].data.fd == btn[j]) { pread(btn[i], buf, sizeof(buf), 0);
if (events[i].data.fd == btn[0]) {
// Read the new value. pread uses offset 0 so we don't need lseek()
pread(btn[j], buf, sizeof(buf), 0);
// Print the result. '1' or '0' depends on your hardware pull-up/down resistors
if (buf[0] == '1') { if (buf[0] == '1') {
printf("Button %d State: HIGH (1)\n", j+1); printf("Decrease led frequency");
} else {
printf("Button %d State: LOW (0)\n", j+1);
}
} }
} else if (events[i].data.fd == btn[1]) {
if (buf[0] == '1') {
printf("Reset led frequency");
}
} else if (events[i].data.fd == btn[2]) {
if (buf[0] == '1') {
printf("Increase led frequency");
}
} }
} }
} }