1
0

refactor(MP/daemon): use standard c class denomination for led and button

This commit is contained in:
2026-06-06 22:00:35 +02:00
parent f99f8882c5
commit bd86f241c2
7 changed files with 39 additions and 40 deletions

View File

@@ -5,7 +5,7 @@
#include <unistd.h>
#include <stdio.h>
static led_t* led;
static LED* led;
/* Threads objects */
static pthread_t anim_thread_id;
@@ -16,7 +16,7 @@ static pthread_cond_t anim_condition = PTHREAD_COND_INITIALIZER; // SHARED RESOU
static int pending_animations = 0; // SHARED RESOURCES
static int keep_running = 1;
void btn_set_led(led_t* l) {
void btn_set_led(LED* l) {
if (l != NULL) {
led = l;
}
@@ -92,9 +92,9 @@ static void* animation_worker(void* arg) {
/* Perform the visual task */
if (led != NULL) {
led_on(led);
LED_on(led);
usleep(150000);
led_off(led);
LED_off(led);
usleep(100000); /* Small delay between consecutive pulses */
}
}

View File

@@ -28,7 +28,7 @@ float get_temperature();
/* --- Button specific function --- */
void btn_set_led(led_t* l);
void btn_set_led(LED* l);
void init_animations(void);
void btn_increase_period();

View File

@@ -16,19 +16,19 @@
#define GPIO_BTN_BASE "/sys/class/gpio/gpio"
#define MAX_BTN 10
btn_t* btn_list[MAX_BTN];
BTN* btn_list[MAX_BTN];
int epoll_fd;
struct epoll_event ev[MAX_BTN];
atomic_int btn_tail = 0;
pthread_t epoll_thread_id;
void btn_add_epoll_event(btn_t* btn, int tail);
void BTN_add_epoll_event(BTN* btn, int tail);
void epoll_init();
static void* epoll_thread(void* arg);
btn_t* btn_init(btn_type_t type) {
btn_t* btn = malloc(sizeof(btn_t));
BTN* BTN_init(BTN_type type) {
BTN* btn = malloc(sizeof(BTN));
if (btn == NULL) return NULL;
char gpio_path[32] = GPIO_BTN_BASE;
@@ -97,17 +97,17 @@ btn_t* btn_init(btn_type_t type) {
if (tail == 0) {
epoll_init();
}
btn_add_epoll_event(btn, tail);
BTN_add_epoll_event(btn, tail);
return btn;
}
void btn_set_callback(btn_t* btn, btn_callback_t callback) {
void BTN_set_callback(BTN* btn, BTN_callback callback) {
btn->callback = callback;
}
// TODO add mutex to protect this function
void btn_add_epoll_event(btn_t* btn, int tail) {
void BTN_add_epoll_event(BTN* btn, int tail) {
// EPOLLIN is working well as EPOLLPRI (which is more used for priority data)
// EPOLLERR is used to detect if there is an error
@@ -148,7 +148,7 @@ static void* epoll_thread(void* arg) {
char buf[2];
int fd = events[i].data.fd;
int tail = -1;
btn_t* btn = NULL;
BTN* btn = NULL;
for (int j = 0; j < MAX_BTN; j++) {
if (btn_list[j] == NULL) continue;
if (btn_list[j]->fd == fd) {

View File

@@ -9,17 +9,17 @@ typedef enum {
BTN_INCREASE,
BTN_DECREASE,
BTN_MODE,
} btn_type_t;
} BTN_type;
typedef void (*btn_callback_t)();
typedef void (*BTN_callback)();
typedef struct {
int fd;
char pin[32];
btn_callback_t callback;
} btn_t;
BTN_callback callback;
} BTN;
btn_t* btn_init(btn_type_t type);
void btn_set_callback(btn_t* btn, btn_callback_t callback);
BTN* BTN_init(BTN_type type);
void BTN_set_callback(BTN* btn, BTN_callback callback);
#endif

View File

@@ -16,8 +16,8 @@
#include <stdlib.h>
led_t* led_init(led_type_t type) {
led_t* led = malloc(sizeof(led_t));
LED* LED_init(LED_type type) {
LED* led = malloc(sizeof(LED));
if (led == NULL) return NULL;
// Concatenate GPIO LED path based on type
@@ -70,29 +70,29 @@ led_t* led_init(led_type_t type) {
return led;
}
void led_on(led_t* led) {
void LED_on(LED* led) {
if (led == NULL) {
return;
}
pwrite(led->gpio, "1", sizeof("1"), 0);
}
void led_off(led_t* led) {
void LED_off(LED* led) {
if (led == NULL) {
return;
}
pwrite(led->gpio, "0", sizeof("0"), 0);
}
void led_toggle(led_t* led) {
void LED_toggle(LED* led) {
if (led == NULL) {
return;
}
char value[2];
pread(led->gpio, value, sizeof(value), 0);
if (value[0] == '0') {
led_on(led);
LED_on(led);
} else {
led_off(led);
LED_off(led);
}
}

View File

@@ -7,15 +7,15 @@
typedef enum {
LED_STATUS, // gpioa.10 --> gpio10
LED_POWER, // gpiol.10 --> gpio362
} led_type_t;
} LED_type;
typedef struct {
int gpio;
} led_t;
} LED;
led_t* led_init(led_type_t type);
void led_on(led_t* led);
void led_off(led_t* led);
void led_toggle(led_t* led);
LED* LED_init(LED_type type);
void LED_on(LED* led);
void LED_off(LED* led);
void LED_toggle(LED* led);
#endif //LED_H

View File

@@ -1,7 +1,6 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@@ -24,15 +23,15 @@
int main(void) {
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* btn_inc = BTN_init(BTN_INCREASE);
BTN* btn_dec = BTN_init(BTN_DECREASE);
BTN* btn_mode = BTN_init(BTN_MODE);
led_t* led_power = led_init(LED_POWER);
LED* led_power = LED_init(LED_POWER);
btn_set_callback(btn_inc, btn_increase_period);
btn_set_callback(btn_dec, btn_decrease_period);
btn_set_callback(btn_mode, mode_toggle);
BTN_set_callback(btn_inc, btn_increase_period);
BTN_set_callback(btn_dec, btn_decrease_period);
BTN_set_callback(btn_mode, mode_toggle);
struct ipc_callbacks_t ipc_cbs = {
.on_dec_period = decrease_period,