Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #define GPIO_DIR_IN 1
- #define GPIO_DIR_OUT 0
- int gpio_export(unsigned gpio)
- {
- int fd, len;
- char buf[11];
- fd = open("/sys/class/gpio/export", O_WRONLY);
- if (fd < 0) {
- perror("gpio/export");
- return fd;
- }
- len = snprintf(buf, sizeof(buf), "%d", gpio);
- write(fd, buf, len);
- close(fd);
- return 0;
- }
- int gpio_unexport(unsigned gpio)
- {
- int fd, len;
- char buf[11];
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- if (fd < 0) {
- perror("gpio/export");
- return fd;
- }
- len = snprintf(buf, sizeof(buf), "%d", gpio);
- write(fd, buf, len);
- close(fd);
- return 0;
- }
- int gpio_dir(unsigned gpio, unsigned dir)
- {
- int fd, len;
- char buf[60];
- len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
- fd = open(buf, O_WRONLY);
- if (fd < 0) {
- perror("gpio/direction");
- return fd;
- }
- if (dir == GPIO_DIR_OUT)
- write(fd, "out", 4);
- else
- write(fd, "in", 3);
- close(fd);
- return 0;
- }
- int gpio_dir_out(unsigned gpio)
- {
- return gpio_dir(gpio, GPIO_DIR_OUT);
- }
- int gpio_dir_in(unsigned gpio)
- {
- return gpio_dir(gpio, GPIO_DIR_IN);
- }
- int set_gpio_value(unsigned gpio, unsigned value)
- {
- int fd, len;
- char buf[60];
- len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
- fd = open(buf, O_WRONLY);
- if (fd < 0) {
- perror("gpio/value");
- return fd;
- }
- if (value)
- write(fd, "1", 2);
- else
- write(fd, "0", 2);
- close(fd);
- return 0;
- }
- int get_gpio_value(unsigned gpio)
- {
- FILE *fp;
- int c, d, len;
- char buf[60];
- len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
- //fp = fopen("/sys/class/gpio/gpio54/value", "r");
- fp = fopen(buf, "r");
- while((c = fgetc(fp)) != EOF){
- if (c == '0'){
- d = 0;
- }
- if (c == '1'){
- d = 1;
- }
- }
- fclose(fp);
- return d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement