chore(MP/daemon): tidy up
This commit is contained in:
@@ -136,6 +136,7 @@ void epoll_init(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void* epoll_thread(void* arg) {
|
static void* epoll_thread(void* arg) {
|
||||||
|
(void) arg;
|
||||||
while (1) {
|
while (1) {
|
||||||
struct epoll_event events[MAX_BTN];
|
struct epoll_event events[MAX_BTN];
|
||||||
int n = epoll_wait(epoll_fd, events, MAX_BTN, -1);
|
int n = epoll_wait(epoll_fd, events, MAX_BTN, -1);
|
||||||
@@ -162,11 +163,12 @@ static void* epoll_thread(void* arg) {
|
|||||||
}
|
}
|
||||||
pread(btn->fd, buf, sizeof(buf), 0);
|
pread(btn->fd, buf, sizeof(buf), 0);
|
||||||
if (buf[0] == '1') {
|
if (buf[0] == '1') {
|
||||||
printf("Button %s pressed\n", btn->pin);
|
// printf("Button %s pressed\n", btn->pin);
|
||||||
if (btn->callback != NULL) {
|
if (btn->callback != NULL) {
|
||||||
btn->callback();
|
btn->callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,28 +9,18 @@
|
|||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
#include "timer/timer.h"
|
#include "timer/timer.h"
|
||||||
#include "gpio/led.h"
|
#include "gpio/led.h"
|
||||||
#include "gpio/button.h"
|
#include "gpio/button.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define GPIO_BTN1 "/sys/class/gpio/gpio0"
|
|
||||||
#define BTN1 "0"
|
|
||||||
#define GPIO_BTN2 "/sys/class/gpio/gpio2"
|
|
||||||
#define BTN2 "2"
|
|
||||||
#define GPIO_BTN3 "/sys/class/gpio/gpio3"
|
|
||||||
#define BTN3 "3"
|
|
||||||
|
|
||||||
#define NBR_BTN 3
|
|
||||||
|
|
||||||
#define DEFAULT_TIME_MS 1000
|
#define DEFAULT_TIME_MS 1000
|
||||||
#define DUTY_CYCLE_PERCENT 2
|
#define DUTY_CYCLE_PERCENT 2
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
long flash_period_ms;
|
atomic_int flash_period_ms;
|
||||||
int timer_fd;
|
int timer_fd;
|
||||||
int epoll_fd;
|
int epoll_fd;
|
||||||
} ThreadData;
|
} ThreadData;
|
||||||
@@ -38,18 +28,18 @@ typedef struct {
|
|||||||
ThreadData data;
|
ThreadData data;
|
||||||
|
|
||||||
void period_inc() {
|
void period_inc() {
|
||||||
data.flash_period_ms += 100;
|
int period = atomic_fetch_add(&data.flash_period_ms, 100);
|
||||||
printf("period_inc: flash_period_ms=%ld\n", data.flash_period_ms);
|
printf("period_inc: flash_period_ms=%d\n", period + 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void period_dec() {
|
void period_dec() {
|
||||||
data.flash_period_ms -= 100;
|
int period = atomic_fetch_sub(&data.flash_period_ms, 100);
|
||||||
printf("period_dec: flash_period_ms=%ld\n", data.flash_period_ms);
|
printf("period_dec: flash_period_ms=%d\n", period - 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void period_reset() {
|
void period_reset() {
|
||||||
data.flash_period_ms = DEFAULT_TIME_MS;
|
atomic_store(&data.flash_period_ms, DEFAULT_TIME_MS);
|
||||||
printf("period_reset: flash_period_ms=%ld\n", data.flash_period_ms);
|
printf("period_reset: flash_period_ms=%d\n", DEFAULT_TIME_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* timer_thread(void* arg) {
|
static void* timer_thread(void* arg) {
|
||||||
@@ -58,8 +48,6 @@ static void* timer_thread(void* arg) {
|
|||||||
led_t* led = led_init(LED_POWER);
|
led_t* led = led_init(LED_POWER);
|
||||||
led_off(led);
|
led_off(led);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct epoll_event ev;
|
struct epoll_event ev;
|
||||||
|
|
||||||
int isLedOn = 0;
|
int isLedOn = 0;
|
||||||
@@ -77,19 +65,19 @@ static void* timer_thread(void* arg) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
long time_on_ms = data->flash_period_ms / 100 * DUTY_CYCLE_PERCENT; // 2% duty
|
int period = atomic_load(&data->flash_period_ms);
|
||||||
long time_off_ms = data->flash_period_ms - time_on_ms; // rest of the period
|
|
||||||
|
long time_on_ms = period / 100 * DUTY_CYCLE_PERCENT; // 2% duty
|
||||||
|
long time_off_ms = period - time_on_ms; // rest of the period
|
||||||
|
|
||||||
int delay = 0;
|
int delay = 0;
|
||||||
if (isLedOn == 0) {
|
if (isLedOn == 0) {
|
||||||
delay = time_on_ms; // 2% duty
|
delay = time_on_ms; // 2% duty
|
||||||
// led_on(led);
|
led_on(led);
|
||||||
led_toggle(led);
|
|
||||||
isLedOn = 1;
|
isLedOn = 1;
|
||||||
} else {
|
} else {
|
||||||
delay = time_off_ms; // rest of the period
|
delay = time_off_ms; // rest of the period
|
||||||
// led_off(led);
|
led_off(led);
|
||||||
led_toggle(led);
|
|
||||||
isLedOn = 0;
|
isLedOn = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,22 +89,19 @@ static void* timer_thread(void* arg) {
|
|||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
openlog("CSEL Logs", LOG_PID, LOG_USER);
|
|
||||||
syslog(LOG_INFO, "Start logging silly led-controller");
|
|
||||||
|
|
||||||
data.flash_period_ms = DEFAULT_TIME_MS;
|
atomic_store(&data.flash_period_ms, DEFAULT_TIME_MS);
|
||||||
|
|
||||||
// Create timerfd
|
// Create timerfd
|
||||||
data.timer_fd = timer_create_empty();
|
data.timer_fd = timer_create_empty();
|
||||||
timer_set_time(&data.timer_fd, data.flash_period_ms);
|
timer_set_time(&data.timer_fd, data.flash_period_ms);
|
||||||
|
|
||||||
// Create epoll instance
|
// Create epoll instance for the timer
|
||||||
data.epoll_fd = epoll_create1(0);
|
data.epoll_fd = epoll_create1(0);
|
||||||
if (data.epoll_fd == -1) {
|
if (data.epoll_fd == -1) {
|
||||||
perror("ERROR while create epoll");
|
perror("ERROR while create epoll");
|
||||||
exit(20);
|
exit(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
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_inc = btn_init(BTN_INCREASE);
|
||||||
|
|||||||
Reference in New Issue
Block a user