diff --git a/pad_server/Makefile b/pad_server/Makefile index eafad4e..09bc97b 100644 --- a/pad_server/Makefile +++ b/pad_server/Makefile @@ -5,6 +5,8 @@ OUT = pad SRCDIR = $(abspath ./src) SRCS = $(wildcard $(SRCDIR)/*.c) SRCS += $(wildcard ../packets/*.c) +EXCLUDE_SRCS = $(SRCDIR)/gpio_actuator.c +SRCS := $(filter-out $(EXCLUDE_SRCS), $(SRCS)) OBJS = $(patsubst %.c,%.o,$(SRCS)) diff --git a/pad_server/src/gpio_actuator.c b/pad_server/src/gpio_actuator.c new file mode 100644 index 0000000..07d120e --- /dev/null +++ b/pad_server/src/gpio_actuator.c @@ -0,0 +1,56 @@ +// BEWARE, this file is expluded from Linux compilation +// because the of the missing nuttx header file + +#include "gpio_actuator.h" +#include "stdio.h" +#include +#include +#include +#include +#include + +int gpio_actuator_on(actuator_t *act) { + // TODO: change the device to be actuator specific + int fd = open("/dev/gpio12", O_RDWR); + if (fd < 0) { + fprintf(stderr, "Failed to open gpio with err %d\n", errno); + return -1; + } + + int err = ioctl(fd, GPIOC_WRITE, true); + if (err < 0) { + fprintf(stderr, "Failed to communicate via ioctl with err %d\n", errno); + return -1; + } + + if (close(fd) == -1) { + fprintf(stderr, "Failed to close gpio with err %d\n", errno); + return -1; + } + + fprintf(stdout, "Actuator #%d turned on\n", act->id); + return 0; +} + +int gpio_actuator_off(actuator_t *act) { + // TODO: change the device to be actuator specific + int fd = open("/dev/gpio12", O_RDWR); + if (fd < 0) { + fprintf(stderr, "Failed to open gpio with err %d\n", errno); + return -1; + } + + int err = ioctl(fd, GPIOC_WRITE, false); + if (err < 0) { + fprintf(stderr, "Failed to communicate via ioctl with err %d\n", errno); + return -1; + } + + if (close(fd) == -1) { + fprintf(stderr, "Failed to close gpio with err %d\n", errno); + return -1; + } + + fprintf(stdout, "Actuator #%d turned off\n", act->id); + return 0; +} diff --git a/pad_server/src/gpio_actuator.h b/pad_server/src/gpio_actuator.h new file mode 100644 index 0000000..09a22c4 --- /dev/null +++ b/pad_server/src/gpio_actuator.h @@ -0,0 +1,4 @@ +#include "actuator.h" + +int gpio_actuator_on(actuator_t *act); +int gpio_actuator_off(actuator_t *act); diff --git a/pad_server/src/gpio_dummy_actuator.c b/pad_server/src/gpio_dummy_actuator.c new file mode 100644 index 0000000..0898007 --- /dev/null +++ b/pad_server/src/gpio_dummy_actuator.c @@ -0,0 +1,11 @@ +#include "gpio_actuator.h" +#include "stdio.h" + +int gpio_actuator_on(actuator_t *act) { + printf("Dummy actuator #%d turned on\n", act->id); + return 0; +} +int gpio_actuator_off(actuator_t *act) { + printf("Dummy actuator #%d turned off\n", act->id); + return 0; +} diff --git a/pad_server/src/state.c b/pad_server/src/state.c index 0c760c6..56454e1 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -7,24 +7,16 @@ #include #include "actuator.h" +#include "gpio_actuator.h" #include "state.h" -static int dummy_on(actuator_t *act) { - printf("Actuator #%d turned on\n", act->id); - return 0; -} -static int dummy_off(actuator_t *act) { - printf("Actuator #%d turned off\n", act->id); - return 0; -} - /* TODO: docs */ void padstate_init(padstate_t *state) { pthread_rwlock_init(&state->rw_lock, NULL); // TODO: Is this right? Can we assume if the program is running then the pad is armed? state->arm_level = ARMED_PAD; for (unsigned int i = 0; i < NUM_ACTUATORS; i++) { - actuator_init(&state->actuators[i], i, dummy_on, dummy_off, NULL); + actuator_init(&state->actuators[i], i, gpio_actuator_on, gpio_actuator_off, NULL); } }