Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C gpio libary using sysfs by Victor Ocampo <theshoutingparrot@protonmail.com>
- #include "gpio_sysfs.h"
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- #include <unistd.h>
- static uint8_t num_of_digits(int n) {
- uint8_t i;
- int x;
- x = 10;
- i = 0;
- n = abs(n);
- while(i < 10) {
- if(n < x) return(i+1);
- x *= 10;
- i++;
- }
- return(0);
- }
- static uint8_t first_digit(unsigned int n) {
- while(n >= 10)
- n /= 10;
- return(n);
- }
- static char *int_to_string(unsigned int n) {
- uint8_t i, nofd, ldig;
- unsigned int x;
- char *str;
- str = (char *)malloc(sizeof(char) * 12);
- if(str == NULL) {
- fputs("Malloc failed!", stderr);
- exit(1);
- }
- nofd = num_of_digits(n);
- x = 1;
- for(i = 0; i<nofd-1; i++)
- x *= 10;
- for(i=0; i<nofd; i++) {
- ldig = first_digit(n);
- n -= ldig * x;
- x /= 10;
- *(str+i) = ldig + '0';
- }
- *(str+i) = '\0';
- return(str);
- }
- bool is_gpio_exported(uint8_t gpio) { //Checks if the gpio in question is exported
- DIR *dir;
- struct dirent *ent;
- char *gpio_file;
- gpio_file = (char *)malloc(sizeof(char) * 7);
- strcpy(gpio_file, "gpio");
- strcat(gpio_file, int_to_string(gpio));
- if( (dir = opendir(GPIODIR)) != NULL) {
- while( (ent = readdir(dir)) != NULL)
- if(!strcmp(ent->d_name, gpio_file))
- return(true);
- closedir(dir);
- }
- else {
- perror("");
- return(false);
- }
- return(false);
- }
- uint8_t gpio_export(uint8_t gpio) { // Exports gpio pin, does it by writing to 'export' file in directory '/sys/class/gpio/export'
- FILE *export;
- if(is_gpio_exported(gpio)) {
- fprintf(stderr, "gpio %d: already exported", gpio);
- return(GPIO_ALREADY_EXPORTED_ERROR);
- }
- export = fopen(EXPORTFILE, "w");
- if(!export) {
- fputs("Error opening export file: '"EXPORTFILE"'", stderr);
- return(FILE_ERROR);
- }
- fprintf(export, "%d", gpio);
- fclose(export);
- return(0);
- }
- uint8_t gpio_unexport(uint8_t gpio) { // Unexports gpio pin, does it by writing to 'unexport' file
- FILE *unexport;
- if(!is_gpio_exported(gpio))
- return(GPIO_NOT_EXPORTED_ERROR);
- unexport = fopen(UNEXPORTFILE, "w");
- if(!unexport) {
- fputs("Error opening file: '"UNEXPORTFILE"'", stderr);
- return(FILE_ERROR);
- }
- fprintf(unexport, "%d", gpio);
- fclose(unexport);
- return(0);
- }
- uint8_t gpio_change_direction(uint8_t gpio, char *direction) {
- FILE *fdirection;
- char *direction_filename;
- direction_filename = (char *)malloc(sizeof(char) * (strlen(GPIODIR) + GPIO_FILENAME_CHAR_SIZE + strlen("/direction")));
- strcpy(direction_filename, GPIODIR);
- strcat(direction_filename, "gpio");
- strcat(direction_filename, int_to_string(gpio));
- strcat(direction_filename, "/direction");
- fdirection = fopen(direction_filename, "w");
- puts("Hello");
- printf("fdirection: %p %d %s\n", fdirection, fdirection, direction_filename);
- if(fdirection == NULL) {
- puts("Helerror timelo!");
- return(FILE_ERROR);
- }
- fputs(direction, fdirection);
- fclose(fdirection);
- return(0);
- }
- uint8_t gpio_read_direction(uint8_t gpio, char *d) {
- FILE *direction;
- char *direction_filename;
- if(!is_gpio_exported(gpio)) {
- fprintf(stderr, "gpio %d not exported\n", gpio);
- return(GPIO_NOT_EXPORTED_ERROR);
- }
- direction_filename = (char *)malloc(sizeof(char) * (strlen(GPIODIR) + GPIO_FILENAME_CHAR_SIZE + strlen("direction")));
- strcpy(direction_filename, GPIODIR);
- strcat(direction_filename, "gpio");
- strcat(direction_filename, int_to_string(gpio));
- strcat(direction_filename, "/direction");
- direction = fopen(direction_filename, "r");
- if(!direction) {
- fprintf(stderr, "Error opening file: '%s'\n", direction_filename);
- return(FILE_ERROR);
- }
- fscanf(direction, "%s", d);
- fclose(direction);
- return(0);
- }
- uint8_t gpio_write(uint8_t gpio, bool v) {
- FILE *value;
- char *value_filename, *direction;
- if(!is_gpio_exported(gpio)) {
- fprintf(stderr, "gpio %d: not exported\n", gpio);
- return(GPIO_NOT_EXPORTED_ERROR);
- }
- direction = (char *)malloc(sizeof(char) * 3);
- gpio_read_direction(gpio, direction);
- printf("direction: %s\n", direction);
- if(strcmp(direction, GPIO_OUT)) {
- fprintf(stderr, "gpio %d: wrong direction for write function\n", gpio);
- return(GPIO_WRONG_DIRECTION_ERROR);
- }
- value_filename = (char *)malloc(sizeof(char) * (strlen(GPIODIR) + GPIO_FILENAME_CHAR_SIZE + strlen("value")));
- strcpy(value_filename, GPIODIR);
- strcat(value_filename, "gpio");
- strcat(value_filename, int_to_string(gpio));
- strcat(value_filename, "/value");
- value = fopen(value_filename, "w");
- if(!value) {
- fprintf(stderr, "Error opening file: '%s'\n", value_filename);
- return(FILE_ERROR);
- }
- fprintf(value, "%d", v);
- fclose(value);
- return(0);
- }
- uint8_t gpio_read(uint8_t gpio, uint8_t *v) {
- FILE *value;
- char *value_filename, *direction;
- if(!is_gpio_exported(gpio)) {
- fprintf(stderr, "gpio %d: not exported\n", gpio);
- return(GPIO_NOT_EXPORTED_ERROR);
- }
- direction = (char *)malloc(sizeof(char) * 3);
- gpio_read_direction(gpio, direction);
- if(strcmp(direction, GPIO_IN)) {
- fprintf(stderr, "gpio %d: wrong direction for read function\n", gpio);
- return(GPIO_WRONG_DIRECTION_ERROR);
- }
- value_filename = (char *)malloc(sizeof(char) * (strlen(GPIODIR) + GPIO_FILENAME_CHAR_SIZE + strlen("value")));
- strcpy(value_filename, GPIODIR);
- strcat(value_filename, "gpio");
- strcat(value_filename, int_to_string(gpio));
- strcat(value_filename, "/value");
- value = fopen(value_filename, "r");
- if(!value) {
- fprintf(stderr, "Error opening file: '%s'\n", value_filename);
- return(FILE_ERROR);
- }
- fscanf(value, "%d", (int *)v);
- fclose(value);
- return(0);
- }
- int main(int argv, char *argc[]) {
- int i;
- char *d;
- d = (char *)malloc(sizeof(char) * 3);
- gpio_export(24);
- gpio_read_direction(24, d);
- printf("direction: %s\n", d);
- gpio_change_direction(24, GPIO_OUT);
- gpio_read_direction(24, d);
- printf("direction: %s\n", d);
- gpio_unexport(24); //Works if this is removed and then the program is ran again
- return(0);
- }
Add Comment
Please, Sign In to add comment