1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-19 20:33:21 +00:00

fix to close #34, gpio value file is now opened as read/write instead of read only

This commit is contained in:
Robert Wolterman
2016-12-31 21:49:52 +00:00
parent 7cce06b472
commit 9a2e81f093

View File

@ -215,7 +215,9 @@ int open_value_file(int gpio)
// create file descriptor of value file
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio); BUF2SMALL(filename);
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
// Changed this to open Read/Write to prevent a ton of file open/closes from happening when using
// the GPIO for SOFTPWM
if ((fd = open(filename, O_RDWR | O_NONBLOCK)) < 0) {
char err[256];
snprintf(err, sizeof(err), "open_value_file: could not open '%s' (%s)", filename, strerror(errno));
add_error_msg(err);
@ -376,17 +378,23 @@ int gpio_get_direction(int gpio, unsigned int *value)
int gpio_set_value(int gpio, unsigned int value)
{
int fd, e_no;
// This now uses the value file descriptor that is set in the other struct
// in an effort to minimize opening/closing this
int fd = fd_lookup(gpio);
int e_no;
char filename[MAX_FILENAME];
char vstr[16];
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio); BUF2SMALL(filename);
if ((fd = open(filename, O_WRONLY)) < 0) {
char err[256];
snprintf(err, sizeof(err), "gpio_set_value: could not open '%s' (%s)", filename, strerror(errno));
add_error_msg(err);
return -1;
if (!fd)
{
if ((fd = open_value_file(gpio)) == -1) {
char err[256];
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
add_error_msg(err);
return -1;
}
}
if (value) {
@ -396,7 +404,6 @@ int gpio_set_value(int gpio, unsigned int value)
}
ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno;
close(fd);
if (s != strlen(vstr)) {
char err[256];