1
0

feat(MP/daemon): full implementation for button and led abstraction

This commit is contained in:
2026-06-05 18:27:55 +02:00
parent 52da239f31
commit f42d00a8f8
3 changed files with 83 additions and 120 deletions

View File

@@ -15,14 +15,15 @@
#define GPIO_UNEXPORT "/sys/class/gpio/unexport" #define GPIO_UNEXPORT "/sys/class/gpio/unexport"
#define GPIO_BTN_BASE "/sys/class/gpio/gpio" #define GPIO_BTN_BASE "/sys/class/gpio/gpio"
#define MAX_EVENTS 10 #define MAX_BTN 10
atomic_int ev_tail = 0; btn_t* btn_list[MAX_BTN];
int epoll_fd; int epoll_fd;
struct epoll_event ev[MAX_EVENTS]; struct epoll_event ev[MAX_BTN];
atomic_int btn_tail = 0;
pthread_t epoll_thread_id; pthread_t epoll_thread_id;
int btn_add_epoll_event(btn_t* btn); void btn_add_epoll_event(btn_t* btn, int tail);
void epoll_init(); void epoll_init();
static void* epoll_thread(void* arg); static void* epoll_thread(void* arg);
@@ -31,29 +32,28 @@ btn_t* btn_init(btn_type_t type) {
if (btn == NULL) return NULL; if (btn == NULL) return NULL;
char gpio_path[32] = GPIO_BTN_BASE; char gpio_path[32] = GPIO_BTN_BASE;
char pin[32];
switch (type) { switch (type) {
case BTN_INCREASE: case BTN_INCREASE:
strcpy(pin, GPIO_BTN_INCREASE); strcpy(btn->pin, GPIO_BTN_INCREASE);
break; break;
case BTN_DECREASE: case BTN_DECREASE:
strcpy(pin, GPIO_BTN_DECREASE); strcpy(btn->pin, GPIO_BTN_DECREASE);
break; break;
case BTN_MODE: case BTN_MODE:
strcpy(pin, GPIO_BTN_MODE); strcpy(btn->pin, GPIO_BTN_MODE);
break; break;
default: default:
printf("Invalid button type\n"); printf("Invalid button type\n");
return NULL; return NULL;
} }
strcat(gpio_path, pin); strcat(gpio_path, btn->pin);
int f = open(GPIO_UNEXPORT, O_WRONLY); int f = open(GPIO_UNEXPORT, O_WRONLY);
write(f, pin, strlen(pin)); write(f, btn->pin, strlen(btn->pin));
close(f); close(f);
f = open(GPIO_EXPORT, O_WRONLY); f = open(GPIO_EXPORT, O_WRONLY);
write(f, pin, strlen(pin)); write(f, btn->pin, strlen(btn->pin));
close(f); close(f);
char direction_path[100]; char direction_path[100];
@@ -78,16 +78,26 @@ btn_t* btn_init(btn_type_t type) {
f = open(value_path, O_RDONLY); f = open(value_path, O_RDONLY);
if (f == -1) { if (f == -1) {
printf("Failed to setup button on pin %s\n", pin); printf("Failed to setup button on pin %s\n", btn->pin);
return NULL; return NULL;
} }
btn->gpio = f; btn->fd = f;
// Dummy read to clear initial state before waiting // Dummy read to clear initial state before waiting
char buf[2]; char buf[2];
pread(btn->gpio, buf, sizeof(buf), 0); pread(btn->fd, buf, sizeof(buf), 0);
btn_add_epoll_event(btn); int tail = atomic_fetch_add(&btn_tail, 1);
if (tail >= MAX_BTN) {
perror("Failed to add epoll event");
exit(EXIT_FAILURE);
}
btn_list[tail] = btn;
if (tail == 0) {
epoll_init();
}
btn_add_epoll_event(btn, tail);
return btn; return btn;
} }
@@ -97,38 +107,21 @@ void btn_set_callback(btn_t* btn, btn_callback_t callback) {
} }
// TODO add mutex to protect this function // TODO add mutex to protect this function
int btn_add_epoll_event(btn_t* btn) { void btn_add_epoll_event(btn_t* btn, int tail) {
int tail = atomic_fetch_add(&ev_tail, 1);
if (tail >= MAX_EVENTS-1) {
perror("Failed to add epoll event");
return -1;
}
if (tail == 0) {
epoll_init();
}
// EPOLLIN is working well as EPOLLPRI (which is more used for priority data) // EPOLLIN is working well as EPOLLPRI (which is more used for priority data)
// EPOLLERR is used to detect if there is an error // EPOLLERR is used to detect if there is an error
// EPOLLET is for edge triggered mode (non-blocking) // EPOLLET is for edge triggered mode (non-blocking)
ev[tail].events = EPOLLIN | EPOLLERR | EPOLLET; ev[tail].events = EPOLLIN | EPOLLERR | EPOLLET;
ev[tail].data.fd = btn->gpio; ev[tail].data.fd = btn->fd;
int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, btn->gpio, &ev[tail]);
int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, btn->fd, &ev[tail]);
if (ret < 0) { if (ret < 0) {
perror("Failed to add epoll event"); perror("Failed to add epoll event");
return -1;
} }
return tail;
} }
void epoll_init(){ void epoll_init(){
if (pthread_create(&epoll_thread_id, NULL, epoll_thread, NULL) != 0) {
perror("Failed to create timer thread");
exit(30);
}
}
static void* epoll_thread(void* arg) {
int epfd = epoll_create1(0); int epfd = epoll_create1(0);
if (epfd < 0) { if (epfd < 0) {
perror("Failed to create epoll instance"); perror("Failed to create epoll instance");
@@ -136,15 +129,44 @@ static void* epoll_thread(void* arg) {
} }
epoll_fd = epfd; epoll_fd = epfd;
if (pthread_create(&epoll_thread_id, NULL, epoll_thread, NULL) != 0) {
perror("Failed to create timer thread");
exit(30);
}
}
static void* epoll_thread(void* arg) {
while (1) { while (1) {
struct epoll_event events[MAX_EVENTS]; struct epoll_event events[MAX_BTN];
int n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); int n = epoll_wait(epoll_fd, events, MAX_BTN, -1);
if (n < 0) { if (n < 0) {
perror("epoll_wait"); perror("epoll_wait");
continue; continue;
} }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
char buf[2];
int fd = events[i].data.fd;
int tail = -1;
btn_t* btn = NULL;
for (int j = 0; j < MAX_BTN; j++) {
if (btn_list[j] == NULL) continue;
if (btn_list[j]->fd == fd) {
tail = j;
btn = btn_list[j];
break;
}
}
if (tail == -1) {
printf("No button found for fd %d\n", fd);
continue;
}
pread(btn->fd, buf, sizeof(buf), 0);
if (buf[0] == '1') {
printf("Button %s pressed\n", btn->pin);
if (btn->callback != NULL) {
btn->callback();
}
}
} }
} }
} }

View File

@@ -14,7 +14,8 @@ typedef enum {
typedef void (*btn_callback_t)(); typedef void (*btn_callback_t)();
typedef struct { typedef struct {
int gpio; int fd;
char pin[32];
btn_callback_t callback; btn_callback_t callback;
} btn_t; } btn_t;

View File

@@ -35,83 +35,21 @@ typedef struct {
int epoll_fd; int epoll_fd;
} ThreadData; } ThreadData;
// constant ThreadData data;
const char* GPIO_BTN[NBR_BTN] = {GPIO_BTN1, GPIO_BTN2, GPIO_BTN3};
const char* BTN[NBR_BTN] = {BTN1, BTN2, BTN3};
void period_inc() {
data.flash_period_ms += 100;
printf("period_inc: flash_period_ms=%ld\n", data.flash_period_ms);
}
void* btn_thread(void* arg) { void period_dec() {
ThreadData* data = (ThreadData*)arg; data.flash_period_ms -= 100;
printf("period_dec: flash_period_ms=%ld\n", data.flash_period_ms);
}
btn_init(BTN_INCREASE); void period_reset() {
btn_init(BTN_DECREASE); data.flash_period_ms = DEFAULT_TIME_MS;
btn_init(BTN_MODE); printf("period_reset: flash_period_ms=%ld\n", data.flash_period_ms);
// Dummy read to clear initial state before waiting
char buf[2];
for(int i=0; i<NBR_BTN; i++) {
pread(btn[i], buf, sizeof(buf), 0);
}
printf("Waiting for button presses...\n");
// Event main loop
while (1) {
struct epoll_event events[NBR_BTN];
// Timeout is -1: Block infinitely until an event occurs!
int n = epoll_wait(epfd, events, 1, -1);
if (n < 0) {
perror("epoll_wait error");
break;
}
for (int i = 0; i < n; i++) {
// read btn file
pread(events[i].data.fd, buf, sizeof(buf), 0);
if (events[i].data.fd == btn[0]) {
if (buf[0] == '1') {
data->flash_period_ms += 200;
char* log_msg = malloc(100);
snprintf(log_msg, 100, "Increase flash period to %ld ms", data->flash_period_ms);
syslog(LOG_INFO, "%s", log_msg);
printf("%s\n", log_msg);
free(log_msg);
}
} else if (events[i].data.fd == btn[1]) {
if (buf[0] == '1') {
data->flash_period_ms = DEFAULT_TIME_MS;
char* log_msg = malloc(100);
snprintf(log_msg, 100, "Reset flash period to %ld ms", data->flash_period_ms);
syslog(LOG_INFO, "%s", log_msg);
printf("%s\n", log_msg);
free(log_msg);
}
} else if (events[i].data.fd == btn[2]) {
if (buf[0] == '1') {
data->flash_period_ms -= 200;
if (data->flash_period_ms <= 0) {
data->flash_period_ms = 200; // Minimum period of 200ms
}
char* log_msg = malloc(100);
snprintf(log_msg, 100, "Decrease flash period to %ld ms", data->flash_period_ms);
syslog(LOG_INFO, "%s", log_msg);
printf("%s\n", log_msg);
free(log_msg);
}
}
}
}
for (int i=0; i<NBR_BTN; i++) {
close(btn[i]);
}
close(epfd);
} }
static void* timer_thread(void* arg) { static void* timer_thread(void* arg) {
@@ -162,7 +100,6 @@ static void* timer_thread(void* arg) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
ThreadData data;
pthread_t thread; pthread_t thread;
openlog("CSEL Logs", LOG_PID, LOG_USER); openlog("CSEL Logs", LOG_PID, LOG_USER);
syslog(LOG_INFO, "Start logging silly led-controller"); syslog(LOG_INFO, "Start logging silly led-controller");
@@ -182,17 +119,20 @@ int main(int argc, char* argv[]) {
timer_link_to_epoll(&data.timer_fd, &data.epoll_fd); timer_link_to_epoll(&data.timer_fd, &data.epoll_fd);
btn_t* btn_inc = btn_init(BTN_INCREASE);
btn_t* btn_dec = btn_init(BTN_DECREASE);
btn_t* btn_mode = btn_init(BTN_MODE);
btn_set_callback(btn_inc, period_inc);
btn_set_callback(btn_dec, period_dec);
btn_set_callback(btn_mode, period_reset);
if (pthread_create(&thread, NULL, timer_thread, &data) != 0) { if (pthread_create(&thread, NULL, timer_thread, &data) != 0) {
perror("Failed to create timer thread"); perror("Failed to create timer thread");
exit(30); exit(30);
} }
// Setup button thread
pthread_t btn_thread_inst;
pthread_create(&btn_thread_inst, NULL, btn_thread, &data);
while (1) { while (1) {
sleep(1); sleep(1);
} }