create library
This commit is contained in:
@@ -9,11 +9,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define IN 0
|
||||
#define OUT 1
|
||||
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
#include <gpio_sys.h>
|
||||
|
||||
#define PIN 24
|
||||
#define POUT 4
|
||||
@@ -22,6 +18,97 @@
|
||||
#define DIRECTION_MAX 35
|
||||
#define VALUE_MAX 30
|
||||
|
||||
static int gpio_export(int pin);
|
||||
static int gpio_unexport(int pin);
|
||||
static int gpio_direction(int pin, int dir);
|
||||
|
||||
int gpio_open(const struct gpio_sys *gpio)
|
||||
{
|
||||
int res;
|
||||
|
||||
assert(NULL != gpio);
|
||||
|
||||
syslog(LOG_DEBUG, "Start opening gpio pin %d.\n", gpio->pin);
|
||||
res = gpio_export(gpio->pin);
|
||||
if(res < 0) {
|
||||
syslog(LOG_ERR, "Unable to export pin %d.\n", gpio->pin);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = gpio_direction(gpio->pin, gpio->direction);
|
||||
if(res < 0) {
|
||||
syslog(LOG_ERR, "Unable to set direction for pin %d.\n", gpio->pin);
|
||||
return res;
|
||||
}
|
||||
|
||||
syslog(LOG_DEBUG, "Gpio pin %d successfully opened.\n", gpio->pin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_close(const struct gpio_sys *gpio)
|
||||
{
|
||||
int res;
|
||||
|
||||
assert(NULL != gpio);
|
||||
|
||||
syslog(LOG_DEBUG, "Start closing gpio pin %d\n.", gpio->pin);
|
||||
res = gpio_unexport(gpio->pin);
|
||||
if(res < 0) {
|
||||
syslog(LOG_ERR, "Unable to unexport pin %d.\n", gpio->pin);
|
||||
return res;
|
||||
}
|
||||
|
||||
syslog(LOG_DEBUG, "Gpio pin %d successfully closed.\n", gpio->pin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_read(const struct gpio_sys *gpio)
|
||||
{
|
||||
char path[VALUE_MAX];
|
||||
char value_str[BUF_SIZE];
|
||||
int fd;
|
||||
|
||||
assert(NULL != gpio);
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", gpio->pin);
|
||||
fd = open(path, O_RDONLY);
|
||||
if(-1 == fd) {
|
||||
syslog(LOG_ERR, "Failed to open pin %d value for reading.\n", gpio->pin);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(-1 == read(fd, value_str, BUF_SIZE)) {
|
||||
syslog(LOG_ERR, "Failed to read value for pin %d.\n", gpio->pin);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return(atoi(value_str));
|
||||
}
|
||||
|
||||
int gpio_write(const struct gpio_sys *gpio, int value)
|
||||
{
|
||||
static const char s_values_str[] = "01";
|
||||
char path[VALUE_MAX];
|
||||
int fd;
|
||||
|
||||
assert(NULL != gpio);
|
||||
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", gpio->pin);
|
||||
fd = open(path, O_WRONLY);
|
||||
if(-1 == fd) {
|
||||
syslog(LOG_ERR, "Failed to open pin %d value for writing.\n", gpio->pin);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
|
||||
syslog(LOG_ERR, "Failed to write value for pin %d.\n", gpio->pin);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int gpio_export(int pin)
|
||||
{
|
||||
char buffer[BUF_SIZE];
|
||||
@@ -30,7 +117,7 @@ static int gpio_export(int pin)
|
||||
|
||||
fd = open("/sys/class/gpio/export", O_WRONLY);
|
||||
if(-1 == fd) {
|
||||
fprintf(stderr, "Failed to open export for writing!\n");
|
||||
syslog(LOG_ERR, "Failed to open export for writing.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -49,7 +136,7 @@ static int gpio_unexport(int pin)
|
||||
|
||||
fd = open("/sys/class/gpio/unexport", O_WRONLY);
|
||||
if(-1 == fd) {
|
||||
fprintf(stderr, "Failed to open unexport for writing!\n");
|
||||
syslog(LOG_ERR, "Failed to open unexport for writing.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -69,88 +156,15 @@ static int gpio_direction(int pin, int dir)
|
||||
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
|
||||
fd = open(path, O_WRONLY);
|
||||
if(-1 == fd) {
|
||||
fprintf(stderr, "Failed to open gpio direction for writing!\n");
|
||||
syslog(LOG_ERR, "Failed to open direction for writing.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(-1 == write(fd, &directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
|
||||
fprintf(stderr, "Failed to set direction!\n");
|
||||
syslog(LOG_ERR, "Failed to set direction.\n");
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int gpio_read(int pin)
|
||||
{
|
||||
char path[VALUE_MAX];
|
||||
char value_str[BUF_SIZE];
|
||||
int fd;
|
||||
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
|
||||
fd = open(path, O_RDONLY);
|
||||
if(-1 == fd) {
|
||||
fprintf(stderr, "Failed to open gpio value for reading!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(-1 == read(fd, value_str, BUF_SIZE)) {
|
||||
fprintf(stderr, "Failed to read value!\n");
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return(atoi(value_str));
|
||||
}
|
||||
|
||||
static int gpio_write(int pin, int value)
|
||||
{
|
||||
static const char s_values_str[] = "01";
|
||||
char path[VALUE_MAX];
|
||||
int fd;
|
||||
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
|
||||
fd = open(path, O_WRONLY);
|
||||
if(-1 == fd) {
|
||||
fprintf(stderr, "Failed to open gpio value for writing!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
|
||||
fprintf(stderr, "Failed to write value!\n");
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int repeat = 10;
|
||||
|
||||
if(-1 == gpio_export(POUT) || -1 == gpio_export(PIN)) {
|
||||
return(1);
|
||||
}
|
||||
|
||||
if(-1 == gpio_direction(POUT, OUT) || -1 == gpio_direction(PIN, IN)) {
|
||||
return(2);
|
||||
}
|
||||
|
||||
do {
|
||||
if(-1 == gpio_write(POUT, repeat % 2)) {
|
||||
return(3);
|
||||
}
|
||||
|
||||
printf("I'm reading %d in GPIO %d\n", gpio_read(PIN), PIN);
|
||||
usleep(500 * 1000);
|
||||
} while(repeat--);
|
||||
|
||||
if(-1 == gpio_unexport(POUT) || -1 == gpio_unexport(PIN)) {
|
||||
return(4);
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user