gpio: add mraa_gpio_read_dir
Add support for reading the direction of a GPIO and expose it through mraa_gpio_read_dir. Signed-off-by: Constantin Musca <constantin.musca@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
084883c210
commit
cc4fe26c09
@@ -154,6 +154,15 @@ mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode);
|
||||
*/
|
||||
mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir);
|
||||
|
||||
/**
|
||||
* Read Gpio direction
|
||||
*
|
||||
* @param dev The Gpio context
|
||||
* @param dir The address where to store the Gpio direction
|
||||
* @return Result of operation
|
||||
*/
|
||||
mraa_result_t mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir);
|
||||
|
||||
/**
|
||||
* Close the Gpio context
|
||||
* - Will free the memory for the context and unexport the Gpio
|
||||
|
||||
@@ -654,6 +654,38 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
||||
{
|
||||
char value[5];
|
||||
char filepath[MAX_SIZE];
|
||||
int fd, rc;
|
||||
mraa_result_t result = MRAA_SUCCESS;
|
||||
|
||||
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
|
||||
fd = open(filepath, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
|
||||
memset(value, '\0', sizeof(value));
|
||||
rc = read(fd, value, sizeof(value));
|
||||
close(fd);
|
||||
if (rc <= 0) {
|
||||
return MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
|
||||
if (strcmp(value, "out\n") == 0) {
|
||||
*dir = MRAA_GPIO_OUT;
|
||||
} else if (strcmp(value, "in\n") == 0) {
|
||||
*dir = MRAA_GPIO_IN;
|
||||
} else {
|
||||
result = MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
mraa_gpio_read(mraa_gpio_context dev)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user