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

View File

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

View File

@@ -16,19 +16,19 @@
#define GPIO_BTN_BASE "/sys/class/gpio/gpio" #define GPIO_BTN_BASE "/sys/class/gpio/gpio"
#define MAX_BTN 10 #define MAX_BTN 10
btn_t* btn_list[MAX_BTN]; BTN* btn_list[MAX_BTN];
int epoll_fd; int epoll_fd;
struct epoll_event ev[MAX_BTN]; struct epoll_event ev[MAX_BTN];
atomic_int btn_tail = 0; atomic_int btn_tail = 0;
pthread_t epoll_thread_id; 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(); void epoll_init();
static void* epoll_thread(void* arg); static void* epoll_thread(void* arg);
btn_t* btn_init(btn_type_t type) { BTN* BTN_init(BTN_type type) {
btn_t* btn = malloc(sizeof(btn_t)); BTN* btn = malloc(sizeof(BTN));
if (btn == NULL) return NULL; if (btn == NULL) return NULL;
char gpio_path[32] = GPIO_BTN_BASE; char gpio_path[32] = GPIO_BTN_BASE;
@@ -97,17 +97,17 @@ btn_t* btn_init(btn_type_t type) {
if (tail == 0) { if (tail == 0) {
epoll_init(); epoll_init();
} }
btn_add_epoll_event(btn, tail); BTN_add_epoll_event(btn, tail);
return btn; 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; btn->callback = callback;
} }
// TODO add mutex to protect this function // 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) // 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
@@ -148,7 +148,7 @@ static void* epoll_thread(void* arg) {
char buf[2]; char buf[2];
int fd = events[i].data.fd; int fd = events[i].data.fd;
int tail = -1; int tail = -1;
btn_t* btn = NULL; BTN* btn = NULL;
for (int j = 0; j < MAX_BTN; j++) { for (int j = 0; j < MAX_BTN; j++) {
if (btn_list[j] == NULL) continue; if (btn_list[j] == NULL) continue;
if (btn_list[j]->fd == fd) { if (btn_list[j]->fd == fd) {

View File

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

View File

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

View File

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

View File

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