diff --git a/src/03-led-controller/setup.c b/src/03-led-controller/button.c similarity index 52% rename from src/03-led-controller/setup.c rename to src/03-led-controller/button.c index d2194f9..87d329b 100644 --- a/src/03-led-controller/setup.c +++ b/src/03-led-controller/button.c @@ -1,47 +1,16 @@ +#include "button.h" + #include -#include -#include #include -#include #include -#include #include #include + #define GPIO_EXPORT "/sys/class/gpio/export" #define GPIO_UNEXPORT "/sys/class/gpio/unexport" -static int open_led(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 btn_open(const char* gpio_path, const char* pin) { int f = open(GPIO_UNEXPORT, O_WRONLY); write(f, pin, strlen(pin)); close(f); diff --git a/src/03-led-controller/button.h b/src/03-led-controller/button.h new file mode 100644 index 0000000..4804300 --- /dev/null +++ b/src/03-led-controller/button.h @@ -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 diff --git a/src/03-led-controller/led.c b/src/03-led-controller/led.c new file mode 100644 index 0000000..701ee0b --- /dev/null +++ b/src/03-led-controller/led.c @@ -0,0 +1,48 @@ +#include "led.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +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); +} diff --git a/src/03-led-controller/led.h b/src/03-led-controller/led.h new file mode 100644 index 0000000..e299c95 --- /dev/null +++ b/src/03-led-controller/led.h @@ -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 diff --git a/src/03-led-controller/main.c b/src/03-led-controller/main.c index 8052122..65578e2 100644 --- a/src/03-led-controller/main.c +++ b/src/03-led-controller/main.c @@ -1,19 +1,17 @@ -#include #include #include #include -#include #include #include -#include #include #include #include #include -#include "setup.c" #include "timer.h" +#include "led.h" +#include "button.h" /* * 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}; -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) { // Open all button with the right flags int btn[NBR_BTN] = {0}; for(int i=0; i