core: implement gpiod_simple_set_value()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 16:42:17 +0000 (17:42 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 16:42:17 +0000 (17:42 +0100)
Implement a counterpart for gpiod_simple_get_value().

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
core.c
gpiod.h

diff --git a/core.c b/core.c
index 020b1fd6428a7d890bf162c07019febbea3c6e73..974527d0e92979ce51b7354dc52be40caa99ae4d 100644 (file)
--- 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 b0c53df4d95925faf8801c4860c0734a1f8b80f7..1b4d547cf880f02e561daea340c3f3a6a7f3af5e 100644 (file)
--- 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;
+
 /**
  * @}
  *