From: Bartosz Golaszewski Date: Fri, 6 Jan 2017 16:42:17 +0000 (+0100) Subject: core: implement gpiod_simple_set_value() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=76227a3fc3f742f0a742f752e5741e68b414ac98;p=qemu-gpiodev%2Flibgpiod.git core: implement gpiod_simple_set_value() Implement a counterpart for gpiod_simple_get_value(). Signed-off-by: Bartosz Golaszewski --- diff --git a/core.c b/core.c index 020b1fd..974527d 100644 --- a/core.c +++ b/core.c @@ -161,6 +161,52 @@ int gpiod_simple_get_value(const char *device, return value; } +int gpiod_simple_set_value(const char *device, unsigned int offset, int value, + bool active_low, void (*cb)(void *), void *data) +{ + struct gpiod_line_request_config config; + struct gpiod_chip *chip; + struct gpiod_line *line; + int status; + + memset(&config, 0, sizeof(config)); + config.consumer = libgpiod_consumer; + config.direction = GPIOD_DIRECTION_OUTPUT; + config.active_state = active_low ? GPIOD_ACTIVE_STATE_LOW + : GPIOD_ACTIVE_STATE_HIGH; + + chip = gpiod_chip_open_lookup(device); + if (!chip) + return -1; + + line = gpiod_chip_get_line(chip, offset); + if (!line) { + gpiod_chip_close(chip); + return -1; + } + + status = gpiod_line_request(line, &config, 0); + if (status < 0) { + gpiod_chip_close(chip); + return -1; + } + + status = gpiod_line_set_value(line, value); + if (status < 0) { + gpiod_line_release(line); + gpiod_chip_close(chip); + return -1; + } + + if (cb) + cb(data); + + gpiod_line_release(line); + gpiod_chip_close(chip); + + return 0; +} + static void line_set_offset(struct gpiod_line *line, unsigned int offset) { line->info.line_offset = offset; diff --git a/gpiod.h b/gpiod.h index b0c53df..1b4d547 100644 --- a/gpiod.h +++ b/gpiod.h @@ -116,6 +116,23 @@ const char * gpiod_strerror(int errnum) GPIOD_API; int gpiod_simple_get_value(const char *device, unsigned int offset, bool active_low) GPIOD_API; +/** + * @brief Set value of a single GPIO line. + * @param device Name, path or number of the gpiochip. + * @param offset GPIO line offset on the chip. + * @param value New value. + * @param active_low The active state of this line - true if low. + * @param cb Callback function that will be called right after the value is + * set. Users can use this, for example, to pause the execution after + * toggling a GPIO. + * @param data User data that will be passed to the callback function. + * @return 0 or 1 if the operation succeeds. On error this routine returns -1 + * and sets the last error number. + */ +int gpiod_simple_set_value(const char *device, unsigned int offset, + int value, bool active_low, void (*cb)(void *), + void *data) GPIOD_API; + /** * @} *