Skip to content

Commit

Permalink
Preparing to support GPIOs on older kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Jun 1, 2024
1 parent 33288a3 commit d351bcc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/gpio.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "gpio.h"

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
const char *paths[] = {"/dev/gpiochip0", "/sys/class/gpio/gpiochip0"};
const char **path = paths;

int fd_gpio = 0;

char gpio_count = 0;
Expand All @@ -12,7 +14,6 @@ void gpio_deinit(void) {
fd_gpio = 0;
}

#ifdef __arm__
int gpio_init(void) {
while (*path++) {
if (access(*path, 0)) continue;
Expand Down Expand Up @@ -81,6 +82,10 @@ int gpio_write(char pin, bool value) {
return EXIT_SUCCESS;
}
#else
void gpio_deinit(void) {

}

int gpio_init(void) {
return EXIT_SUCCESS;
}
Expand All @@ -92,4 +97,4 @@ int gpio_read(char pin, bool *value) {
int gpio_write(char pin, bool value) {
return EXIT_SUCCESS;
}
#endif
#endif
7 changes: 4 additions & 3 deletions src/gpio.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#include <fcntl.h>
#include <linux/version.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

#ifdef __arm__
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
#include <linux/gpio.h>
#include <sys/ioctl.h>
#endif

#define GPIO_ERROR(x, ...) fprintf(stderr, "%s \033[31m%s\033[0m\n", "[gpio] (x)", ##__VA_ARGS__)

void gpio_deinit(void);
int gpio_init(void);
int gpio_read(char pin, bool *value);
int gpio_write(char pin, bool value);
int gpio_write(char pin, bool value);

0 comments on commit d351bcc

Please sign in to comment.