diff --git a/utility/SPIDEV/gpio.cpp b/utility/SPIDEV/gpio.cpp index b6bac03a..3c27330b 100644 --- a/utility/SPIDEV/gpio.cpp +++ b/utility/SPIDEV/gpio.cpp @@ -35,19 +35,21 @@ GPIO::~GPIO() int fd; void GPIO::Gopen(int port, int DDR) -{ - fd = open(dev_name, O_RDONLY); - if (fd >= 0){ - close(fd); - }else{ - dev_name = "/dev/gpiochip0"; +{ fd = open(dev_name, O_RDONLY); - if (fd >= 0){ - close(fd); - }else{ - throw GPIOException("can't open /dev/gpiochip"); + if (fd >= 0) { + close(fd); + } + else { + dev_name = "/dev/gpiochip0"; + fd = open(dev_name, O_RDONLY); + if (fd >= 0) { + close(fd); + } + else { + throw GPIOException("can't open /dev/gpiochip"); + } } - } } void GPIO::Gclose(int port) @@ -56,59 +58,57 @@ void GPIO::Gclose(int port) int GPIO::Gread(int port) { - + struct gpiohandle_request rq; struct gpiohandle_data data; int fd, ret; fd = open(dev_name, O_RDONLY); - if (fd >= 0){ - rq.lineoffsets[0] = port; - rq.flags = GPIOHANDLE_REQUEST_INPUT; - rq.lines = 1; - ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq); - if(ret == -1){ - throw GPIOException("Can't get line handle from IOCTL"); + if (fd >= 0) { + rq.lineoffsets[0] = port; + rq.flags = GPIOHANDLE_REQUEST_INPUT; + rq.lines = 1; + ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq); + if (ret == -1) { + throw GPIOException("Can't get line handle from IOCTL"); + return ret; + } + close(fd); + ret = ioctl(rq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); + if (ret == -1) { + throw GPIOException("Can't get line value from IOCTL"); + return ret; + } + close(rq.fd); return ret; - } - close(fd); - ret = ioctl(rq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); - if(ret == -1){ - throw GPIOException("Can't get line value from IOCTL"); - return ret; - } - close(rq.fd); - return ret; } return -1; - } void GPIO::Gwrite(int port, int value) { - + struct gpiohandle_request rq; struct gpiohandle_data data; int fd, ret; fd = open(dev_name, O_RDONLY); - if(fd < 0){ - throw GPIOException("Can't open dev"); - return; + if (fd < 0) { + throw GPIOException("Can't open dev"); + return; } rq.lineoffsets[0] = port; rq.flags = GPIOHANDLE_REQUEST_OUTPUT; rq.lines = 1; ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq); - if(ret == -1){ - throw GPIOException("Can't get line handle from IOCTL"); - return; + if (ret == -1) { + throw GPIOException("Can't get line handle from IOCTL"); + return; } close(fd); data.values[0] = value; ret = ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); - if(ret == -1){ - throw GPIOException("Can't set line value from IOCTL"); - return; + if (ret == -1) { + throw GPIOException("Can't set line value from IOCTL"); + return; } close(rq.fd); - } diff --git a/utility/SPIDEV/gpio.h b/utility/SPIDEV/gpio.h index 6b7f5809..71b0f429 100644 --- a/utility/SPIDEV/gpio.h +++ b/utility/SPIDEV/gpio.h @@ -17,7 +17,6 @@ #include #include - /** Specific excpetion for SPI errors */ class GPIOException : public std::runtime_error { @@ -51,9 +50,8 @@ class GPIO static void Gwrite(int port, int value); virtual ~GPIO(); - -private: +private: }; #endif // RF24_UTILITY_SPIDEV_GPIO_H_