diff options
author | 2016-11-10 09:16:35 +0200 | |
---|---|---|
committer | 2016-11-10 09:16:35 +0200 | |
commit | d8b27510426e95470f9579395f50f353bd105d83 (patch) | |
tree | 39d888fe9a25acd96e917486f7db05ded495466c | |
parent | 5abe95c474262dfc0d43cd8fdd5248c9b48a8ecf (diff) | |
download | librin-d8b27510426e95470f9579395f50f353bd105d83.tar.gz librin-d8b27510426e95470f9579395f50f353bd105d83.tar.bz2 librin-d8b27510426e95470f9579395f50f353bd105d83.zip |
gpio: implement some more features.
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | include/rin/gpio.h | 16 | ||||
-rw-r--r-- | src/gpio/gpio.c | 50 |
3 files changed, 68 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac index 588049b..34345d5 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -dnl Copyright (C) 2015 Gediminas Jakutis +dnl Copyright (C) 2015-2016 Gediminas Jakutis dnl dnl This configure.ac is free software; you can redistribute it and/or dnl modify it under the terms of the GNU Lesser General Public @@ -31,7 +31,7 @@ AM_INIT_AUTOMAKE LT_INIT # Checks for header files. -AC_CHECK_HEADERS([fcntl.h inttypes.h stdio.h stdint.h stdlib.h string.h sys/types.h sys/stat.h time,h unistd.h]) +AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h stdio.h stdint.h stdlib.h string.h sys/types.h sys/stat.h time,h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_INT32_T diff --git a/include/rin/gpio.h b/include/rin/gpio.h index 83ca934..1ca2169 100644 --- a/include/rin/gpio.h +++ b/include/rin/gpio.h @@ -21,16 +21,32 @@ #ifndef LIBRIN_GPIO_INCLUDED #define LIBRIN_GPIO_INCLUDED +#include <limits.h> +#include <time.h> + enum rin_gpiotype { rin_gpio_in, rin_gpio_out, rin_gpio_closed }; +struct rin_gpio_dataset { + unsigned int signalpin; + unsigned int clockpin; + struct timespec *pulseduration; + struct timespec *delay; + void *data; + size_t len; +}; + unsigned int rin_gpio_open(const unsigned int num, const enum rin_gpiotype type); unsigned int rin_gpio_close(const unsigned int num); unsigned int rin_gpio_setval(const unsigned int num, const unsigned int val); unsigned int rin_gpio_readval(const unsigned int num, unsigned int * const val); +unsigned int rin_gpio_writesequence(const struct rin_gpio_dataset * const dataset); +unsigned int rin_gpio_on(const unsigned int num); +unsigned int rin_gpio_off(const unsigned int num); +unsigned int rin_gpio_pulse(const unsigned int num, const struct timespec * const pulselen); unsigned int rin_gpio_pin_exists(const unsigned int pin); unsigned int rin_gpio_get_pincount(void); diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index 45639a3..4c48919 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -118,6 +118,56 @@ unsigned int rin_gpio_readval(const unsigned int num, unsigned int * const val) return 0; } + +unsigned int rin_gpio_writesequence(const struct rin_gpio_dataset * const dataset) +{ + unsigned char *data; + size_t i; + + if (!dataset || !dataset->data) { + return 1; + } + data = dataset->data; + for (i = 0; i < dataset->len; ++i) { + unsigned char a; + unsigned char bit; + + a = data[i / CHAR_BIT]; + bit = !(!(a & (1 << (i % CHAR_BIT)))); + rin_gpio_setval(dataset->signalpin, bit); + rin_gpio_pulse(dataset->clockpin, dataset->pulseduration); + if (dataset->delay) { + nanosleep(dataset->delay, NULL); + } + } + return 0; +} + +unsigned int rin_gpio_on(const unsigned int num) +{ + return rin_gpio_setval(1, num); +} + +unsigned int rin_gpio_off(const unsigned int num) +{ + return rin_gpio_setval(0, num); +} + +unsigned int rin_gpio_pulse(const unsigned int num, const struct timespec * const pulselen) +{ + unsigned int ret; + + ret = rin_gpio_on(num); + if (ret) { + return ret; + } + if (pulselen) { + nanosleep(pulselen, NULL); + } + ret = rin_gpio_off(num); + return ret; +} + /* * right now is rpi V1 specific, should be rewritten when/if we support * other platforms. |