From 07fd35b117eef063625a7506b43adb55d5e221c7 Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Tue, 26 Nov 2024 09:45:30 -0500 Subject: [PATCH 1/4] Added gpio drivers --- pad_server/Makefile | 2 ++ pad_server/src/gpio_actuator.c | 51 ++++++++++++++++++++++++++++ pad_server/src/gpio_actuator.h | 4 +++ pad_server/src/gpio_dummy_actuator.c | 11 ++++++ pad_server/src/state.c | 12 ++----- 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 pad_server/src/gpio_actuator.c create mode 100644 pad_server/src/gpio_actuator.h create mode 100644 pad_server/src/gpio_dummy_actuator.c 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..8c93431 --- /dev/null +++ b/pad_server/src/gpio_actuator.c @@ -0,0 +1,51 @@ +#include "gpio_actuator.h" +#include "include/nuttx/ioexpander/gpio.h" +#include "stdio.h" +#include +#include +#include +#include + +int gpio_actuator_on(actuator_t *act) { + 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; + } + + printf("Actuator #%d turned on\n", act->id); + return 0; +} + +int gpio_actuator_off(actuator_t *act) { + 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; + } + + printf("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 55343e2..4247e78 100644 --- a/pad_server/src/state.c +++ b/pad_server/src/state.c @@ -6,24 +6,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); } } From 0ab585be2660398e83a57061752fde05b751373d Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:13:21 -0500 Subject: [PATCH 2/4] Fixed nuttx import --- pad_server/src/gpio_actuator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pad_server/src/gpio_actuator.c b/pad_server/src/gpio_actuator.c index 8c93431..df2e84d 100644 --- a/pad_server/src/gpio_actuator.c +++ b/pad_server/src/gpio_actuator.c @@ -1,8 +1,8 @@ #include "gpio_actuator.h" -#include "include/nuttx/ioexpander/gpio.h" #include "stdio.h" #include #include +#include #include #include From a412cc1311cd1ff34dca78138fc373a7add7d382 Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:20:54 -0500 Subject: [PATCH 3/4] Changed from printf to fprintf for consistency --- pad_server/src/gpio_actuator.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pad_server/src/gpio_actuator.c b/pad_server/src/gpio_actuator.c index df2e84d..8a82400 100644 --- a/pad_server/src/gpio_actuator.c +++ b/pad_server/src/gpio_actuator.c @@ -7,6 +7,7 @@ #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); @@ -24,11 +25,12 @@ int gpio_actuator_on(actuator_t *act) { return -1; } - printf("Actuator #%d turned on\n", act->id); + 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); @@ -46,6 +48,6 @@ int gpio_actuator_off(actuator_t *act) { return -1; } - printf("Actuator #%d turned off\n", act->id); + fprintf(stdout, "Actuator #%d turned off\n", act->id); return 0; } From 4f5d0f137b1b8df56aadbc1a8061fb4c2538e2b2 Mon Sep 17 00:00:00 2001 From: bskdany <54674556+bskdany@users.noreply.github.com> Date: Sat, 30 Nov 2024 14:40:51 -0500 Subject: [PATCH 4/4] Added compilation warning --- pad_server/src/gpio_actuator.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pad_server/src/gpio_actuator.c b/pad_server/src/gpio_actuator.c index 8a82400..07d120e 100644 --- a/pad_server/src/gpio_actuator.c +++ b/pad_server/src/gpio_actuator.c @@ -1,3 +1,6 @@ +// BEWARE, this file is expluded from Linux compilation +// because the of the missing nuttx header file + #include "gpio_actuator.h" #include "stdio.h" #include