Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example gpio drivers #46

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pad_server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
56 changes: 56 additions & 0 deletions pad_server/src/gpio_actuator.c
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
#include <fcntl.h>
#include <nuttx/ioexpander/gpio.h>
#include <sys/ioctl.h>
#include <unistd.h>

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;
}
4 changes: 4 additions & 0 deletions pad_server/src/gpio_actuator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "actuator.h"

int gpio_actuator_on(actuator_t *act);
int gpio_actuator_off(actuator_t *act);
11 changes: 11 additions & 0 deletions pad_server/src/gpio_dummy_actuator.c
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 2 additions & 10 deletions pad_server/src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@
#include <stdio.h>

#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);
}
}

Expand Down