saul/gpio: add support for inverted mode gpios

This commit is contained in:
Gaëtan Harter 2017-09-08 13:34:49 +02:00
parent be3029d890
commit c75470e602
2 changed files with 18 additions and 5 deletions

View File

@ -32,13 +32,20 @@ extern "C" {
#endif #endif
#ifdef MODULE_SAUL_GPIO #ifdef MODULE_SAUL_GPIO
typedef enum {
SAUL_GPIO_INVERTED = (1 << 0), /**< pin is used as inverted */
SAUL_GPIO_INIT_CLEAR = 1 << 1, /**< set pin inactive after initialization */
SAUL_GPIO_INIT_SET = 1 << 2, /**< set pin active after initialization */
} saul_gpio_flags_t;
/** /**
* @brief Direct mapped GPIO configuration values * @brief Direct mapped GPIO configuration values
*/ */
typedef struct { typedef struct {
const char *name; /**< name of the device connected to this pin */ const char *name; /**< name of the device connected to this pin */
gpio_t pin; /**< GPIO pin to initialize and expose */ gpio_t pin; /**< GPIO pin to initialize and expose */
gpio_mode_t mode; /**< pin mode to use */ gpio_mode_t mode; /**< pin mode to use */
saul_gpio_flags_t flags; /**< Configuration flags */
} saul_gpio_params_t; } saul_gpio_params_t;
#endif /* MODULE_SAUL_GPIO */ #endif /* MODULE_SAUL_GPIO */

View File

@ -29,7 +29,10 @@
static int read(const void *dev, phydat_t *res) static int read(const void *dev, phydat_t *res)
{ {
const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev; const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev;
res->val[0] = (gpio_read(p->pin)) ? 1: 0; int inverted = (p->flags & SAUL_GPIO_INVERTED);
res->val[0] = (gpio_read(p->pin)) ? !inverted : inverted;
memset(&(res->val[1]), 0, 2 * sizeof(int16_t)); memset(&(res->val[1]), 0, 2 * sizeof(int16_t));
res->unit = UNIT_BOOL; res->unit = UNIT_BOOL;
res->scale = 0; res->scale = 0;
@ -39,7 +42,10 @@ static int read(const void *dev, phydat_t *res)
static int write(const void *dev, phydat_t *state) static int write(const void *dev, phydat_t *state)
{ {
const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev; const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev;
gpio_write(p->pin, state->val[0]); int inverted = (p->flags & SAUL_GPIO_INVERTED);
int value = (state->val[0] ? !inverted : inverted);
gpio_write(p->pin, value);
return 1; return 1;
} }