Skip to content

Commit

Permalink
Naïve version of pin export #4
Browse files Browse the repository at this point in the history
  • Loading branch information
zouppen committed Mar 26, 2015
1 parent b5b9b6f commit b6962df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <unistd.h>
#include "gpio.h"

#define GPIO_PATH "/sys/class/gpio/"

static int gpio_open_one(char *fmt, gint value, int flags);

static int gpio_open_one(char *fmt, gint value, int flags)
Expand All @@ -46,8 +44,14 @@ static int gpio_open_one(char *fmt, gint value, int flags)
err(2,"Unable to open GPIO %s", pathname);
}

void gpio_open(struct gpio *gpio, gint value)
void gpio_open(FILE* export, struct gpio *gpio, gint value)
{
// Try to export it first. Export creates the GPIO files
int exported = fprintf(export, "%d\n", value);
int flushed = fflush(export);
printf("Export of %d %s\n", value, exported < 0 || flushed != 0 ? "failed" : "ok");

// Open value, edge and direction for given GPIO pin
gpio->value = gpio_open_one(GPIO_PATH "gpio%d/value", value, O_RDWR);
gpio->edge = gpio_open_one(GPIO_PATH "gpio%d/edge", value, O_WRONLY);
gpio->direction = gpio_open_one(GPIO_PATH "gpio%d/direction", value, O_WRONLY);
Expand Down
4 changes: 3 additions & 1 deletion src/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <glib.h>
#include <stdbool.h>

#define GPIO_PATH "/sys/class/gpio/"

struct gpio {
int value;
int edge;
Expand All @@ -33,7 +35,7 @@ struct gpio {
/**
* Open given GPIO pin and populate gpio structure. Dies if pin is not
* accessible. */
void gpio_open(struct gpio *gpio, gint value);
void gpio_open(FILE* export, struct gpio *gpio, gint value);

/**
* Read value in given GPIO pin.
Expand Down
9 changes: 6 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,23 @@ int main(int argc, char *argv[])

// Open gpio pins

// FIXME export GPIO first!
FILE *export = fopen(GPIO_PATH "export", "w");
if (export == NULL) err(2,"Unable to open %s", GPIO_PATH "export");

dev->row = g_new(struct gpio, dev->rows);
for (gsize i=0; i<dev->rows; i++) {
gpio_open(dev->row+i, gpio_row_ix[i]);
gpio_open(export, dev->row+i, gpio_row_ix[i]);
}
g_free(gpio_row_ix);

dev->col = g_new(struct gpio, dev->cols);
for (gsize i=0; i<dev->cols; i++) {
gpio_open(dev->col+i, gpio_col_ix[i]);
gpio_open(export, dev->col+i, gpio_col_ix[i]);
}
g_free(gpio_col_ix);

fclose(export);

keypad_setup(dev);
keypad_loop(dev);

Expand Down

0 comments on commit b6962df

Please sign in to comment.