refactor(lab03): split in dedicated files
Co-authored-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -1,47 +1,16 @@
|
|||||||
|
#include "button.h"
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
|
||||||
|
|
||||||
#define GPIO_EXPORT "/sys/class/gpio/export"
|
#define GPIO_EXPORT "/sys/class/gpio/export"
|
||||||
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
||||||
|
|
||||||
static int open_led(const char* gpio_path, const char* pin) {
|
int btn_open(const char* gpio_path, const char* pin) {
|
||||||
|
|
||||||
// unexport pin out of sysfs (reinitialization)
|
|
||||||
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
|
||||||
write(f, pin, strlen(pin));
|
|
||||||
close(f);
|
|
||||||
|
|
||||||
// export pin to sysfs
|
|
||||||
f = open(GPIO_EXPORT, O_WRONLY);
|
|
||||||
write(f, pin, strlen(pin));
|
|
||||||
close(f);
|
|
||||||
|
|
||||||
// config pin
|
|
||||||
char direction_path[100];
|
|
||||||
strcpy(direction_path, gpio_path);
|
|
||||||
strcat(direction_path, "/direction");
|
|
||||||
|
|
||||||
f = open(direction_path, O_WRONLY);
|
|
||||||
write(f, "out", 3);
|
|
||||||
close(f);
|
|
||||||
|
|
||||||
// open gpio value attribute
|
|
||||||
char value_path[100];
|
|
||||||
strcpy(value_path, gpio_path);
|
|
||||||
strcat(value_path, "/value");
|
|
||||||
|
|
||||||
f = open(value_path, O_RDWR);
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int open_btn(const char* gpio_path, const char* pin) {
|
|
||||||
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
||||||
write(f, pin, strlen(pin));
|
write(f, pin, strlen(pin));
|
||||||
close(f);
|
close(f);
|
||||||
10
src/03-led-controller/button.h
Normal file
10
src/03-led-controller/button.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef BUTTON_H
|
||||||
|
#define BUTTON_H
|
||||||
|
|
||||||
|
#define GPIO_EXPORT "/sys/class/gpio/export"
|
||||||
|
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
||||||
|
|
||||||
|
int btn_open(const char* gpio_path, const char* pin);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
48
src/03-led-controller/led.c
Normal file
48
src/03-led-controller/led.c
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include "led.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
int led_open(const char* gpio_path, const char* pin) {
|
||||||
|
|
||||||
|
// unexport pin out of sysfs (reinitialization)
|
||||||
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
||||||
|
write(f, pin, strlen(pin));
|
||||||
|
close(f);
|
||||||
|
|
||||||
|
// export pin to sysfs
|
||||||
|
f = open(GPIO_EXPORT, O_WRONLY);
|
||||||
|
write(f, pin, strlen(pin));
|
||||||
|
close(f);
|
||||||
|
|
||||||
|
// config pin
|
||||||
|
char direction_path[100];
|
||||||
|
strcpy(direction_path, gpio_path);
|
||||||
|
strcat(direction_path, "/direction");
|
||||||
|
|
||||||
|
f = open(direction_path, O_WRONLY);
|
||||||
|
write(f, "out", 3);
|
||||||
|
close(f);
|
||||||
|
|
||||||
|
// open gpio value attribute
|
||||||
|
char value_path[100];
|
||||||
|
strcpy(value_path, gpio_path);
|
||||||
|
strcat(value_path, "/value");
|
||||||
|
|
||||||
|
f = open(value_path, O_RDWR);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void led_on(int led) {
|
||||||
|
pwrite(led, "1", sizeof("1"), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void led_off(int led) {
|
||||||
|
pwrite(led, "0", sizeof("0"), 0);
|
||||||
|
}
|
||||||
14
src/03-led-controller/led.h
Normal file
14
src/03-led-controller/led.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef CSEL_WORKSPACE_LED_H
|
||||||
|
#define CSEL_WORKSPACE_LED_H
|
||||||
|
|
||||||
|
#define GPIO_EXPORT "/sys/class/gpio/export"
|
||||||
|
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
||||||
|
|
||||||
|
#define GPIO_LED "/sys/class/gpio/gpio10"
|
||||||
|
#define LED "10"
|
||||||
|
|
||||||
|
int led_open(const char* gpio_path, const char* pin);
|
||||||
|
void led_on(int led);
|
||||||
|
void led_off(int led);
|
||||||
|
|
||||||
|
#endif //CSEL_WORKSPACE_LED_H
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "setup.c"
|
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
#include "led.h"
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* status led - gpioa.10 --> gpio10
|
* status led - gpioa.10 --> gpio10
|
||||||
@@ -47,19 +45,11 @@ const char* GPIO_BTN[NBR_BTN] = {GPIO_BTN1, GPIO_BTN2, GPIO_BTN3};
|
|||||||
const char* BTN[NBR_BTN] = {BTN1, BTN2, BTN3};
|
const char* BTN[NBR_BTN] = {BTN1, BTN2, BTN3};
|
||||||
|
|
||||||
|
|
||||||
void led_on(int led) {
|
|
||||||
pwrite(led, "1", sizeof("1"), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void led_off(int led) {
|
|
||||||
pwrite(led, "0", sizeof("0"), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void* btn_thread(void* arg) {
|
void* btn_thread(void* arg) {
|
||||||
// Open all button with the right flags
|
// Open all button with the right flags
|
||||||
int btn[NBR_BTN] = {0};
|
int btn[NBR_BTN] = {0};
|
||||||
for(int i=0; i<NBR_BTN; i++) {
|
for(int i=0; i<NBR_BTN; i++) {
|
||||||
btn[i] = open_btn(GPIO_BTN[i], BTN[i]);
|
btn[i] = btn_open(GPIO_BTN[i], BTN[i]);
|
||||||
if (btn[i] < 0) {
|
if (btn[i] < 0) {
|
||||||
perror("Failed to open button");
|
perror("Failed to open button");
|
||||||
}
|
}
|
||||||
@@ -135,7 +125,7 @@ void* btn_thread(void* arg) {
|
|||||||
static void* timer_thread(void* arg) {
|
static void* timer_thread(void* arg) {
|
||||||
ThreadData* data = (ThreadData*)arg;
|
ThreadData* data = (ThreadData*)arg;
|
||||||
|
|
||||||
int led = open_led(GPIO_LED, LED);
|
int led = led_open(GPIO_LED, LED);
|
||||||
led_off(led);
|
led_off(led);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user