c - GPIO sample code for Linux -


static int gpiounexport(int pin) {     char buffer[buffer_max];     ssize_t bytes_written;     int fd;      fd = open("/sys/class/gpio/unexport", o_wronly);     if (-1 == fd) {         fprintf(stderr, "failed open unexport writing!\n");         return(-1);     }      bytes_written = snprintf(buffer, buffer_max, "%d", pin);     write(fd, buffer, bytes_written);     close(fd);     return(0); } 

i have few questions relate above gpio code

1)why use ssize_t? why not use int?

2)is /sys/class/gpio/unexport" system file? if not it?

3)snprintf prints somethings buffer, write function redundant? or write function can differently?

4) instead of using open, can use ioctl function instead?

  1. i have no idea why code sample uses ssize_t; return type of snprintf int using int better (to avoid implicit cast). ssize_t signed , in cases @ least large int there no harm done.

  2. yes, system file on linux. writing number of gpio pin file "unexport" gpio pin generic gpio driver, making driver declare not handle pin more (allowing other gpio drivers use it).

  3. you use dprintf write directly file descriptor fd , not need temporary buffer. dprintf less known other printf variants maybe that's why author of original code decided use intermediate buffer instead , call standard low-level write function.

  4. what sort of ioctl function mean?


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -