Fork me on GitHub

GPIO

This library provides General Purpose Input/Output (GPIO) hardware access interface.

C API Reference mgos_gpio.h

#include "mgos_gpio.h"

See on GitHub: mgos_gpio.h, mgos_gpio.c


enum mgos_gpio_mode { MGOS_GPIO_MODE_INPUT = 0, MGOS_GPIO_MODE_OUTPUT = 1 };

enum mgos_gpio_mode { MGOS_GPIO_MODE_INPUT = 0, MGOS_GPIO_MODE_OUTPUT = 1 };

enum mgos_gpio_mode { MGOS_GPIO_MODE_INPUT = 0, MGOS_GPIO_MODE_OUTPUT = 1 };

enum mgos_gpio_pull_type {
  MGOS_GPIO_PULL_NONE = 0,
  MGOS_GPIO_PULL_UP = 1,
  MGOS_GPIO_PULL_DOWN = 2
};

enum mgos_gpio_int_mode {
  MGOS_GPIO_INT_NONE = 0,
  MGOS_GPIO_INT_EDGE_POS = 1,
  MGOS_GPIO_INT_EDGE_NEG = 2,
  MGOS_GPIO_INT_EDGE_ANY = 3,
  MGOS_GPIO_INT_LEVEL_HI = 4,
  MGOS_GPIO_INT_LEVEL_LO = 5
};

typedef void (*mgos_gpio_int_handler_f)(int pin, void *arg);

bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode);

Set mode - input or output

bool mgos_gpio_set_pull(int pin, enum mgos_gpio_pull_type pull);

Set pull-up or pull-down type.

bool mgos_gpio_read(int pin);

Read pin input level.

void mgos_gpio_write(int pin, bool level);

Set pin's output level.

bool mgos_gpio_toggle(int pin);

Flip output pin value. Returns value that was written.

bool mgos_gpio_set_int_handler(int pin, enum mgos_gpio_int_mode mode,
                               mgos_gpio_int_handler_f cb, void *arg);

Install a GPIO interrupt handler.

Note that this will not enable the interrupt, this must be done explicitly with mgos_gpio_enable_int().

bool mgos_gpio_enable_int(int pin);

Enable interrupt on the specified pin.

bool mgos_gpio_disable_int(int pin);

Disables interrupt (without removing the handler).

void mgos_gpio_remove_int_handler(int pin, mgos_gpio_int_handler_f *old_cb,
                                  void **old_arg);

Removes a previosuly set interrupt handler.

If cb and arg are not NULL, they will contain previous handler and arg.

bool mgos_gpio_set_button_handler(int pin, enum mgos_gpio_pull_type pull_type,
                                  enum mgos_gpio_int_mode int_mode,
                                  int debounce_ms, mgos_gpio_int_handler_f cb,
                                  void *arg);

Handle a button on the specified pin.

Configures the pin for input with specified pull-up and performs debouncing: upon first triggering user's callback is invoked immediately but further interrupts are inhibited for the following debounce_ms millseconds.

Typically 50 ms of debouncing time is sufficient. int_mode is one of the MGOS_GPIO_INTEDGE* values and will specify whether the handler triggers when button is pressed, released or both. Which is which depends on how the button is wired: if the normal state is pull-up (typical), then MGOS_GPIO_INT_EDGE_NEG is press and _POS is release.

Calling with cb = NULL will remove a previously installed handler.

Note: implicitly enables the interrupt.

enum mgos_init_result mgos_gpio_init();

JAVASCRIPT API Reference api_gpio.js

load("api_gpio.js");


GPIO.set_mode(pin, mode)

Set GPIO pin mode. mode can be either GPIO.MODE_INPUT or GPIO.MODE_OUTPUT.

GPIO.set_pull(pin, type)

Set GPIO pin pull type. type can be either GPIO.PULL_NONE, GPIO.PULL_UP, or GPIO.PULL_DOWN.

GPIO.toggle(pin)

Toggle the level of certain GPIO pin. Return value: 0 or 1, indicating the resulting pin level.

GPIO.write(pin, level)

Set GPIO pin level to either 0 or 1. Return value: none.

GPIO.read(pin)

Read GPIO pin level. Return value: 0 or 1.

GPIO.enable_int(pin)

Enable interrupts on GPIO pin. This function must be called AFTER the interrupt handler is installed. Return value: 1 in case of success, 0 otherwise.

GPIO.disable_int(pin)

Disable interrupts on GPIO pin. Return value: 1 in case of success, 0 otherwise.

GPIO.set_int_handler(pin, mode, handler)

Install GPIO interrupt handler. mode could be one of: GPIO.INT_NONE, GPIO.INT_EDGE_POS, GPIO.INT_EDGE_NEG, GPIO.INT_EDGE_ANY, GPIO.INT_LEVEL_HI, GPIO.INT_LEVEL_LO. Return value: 1 in case of success, 0 otherwise. Example:

GPIO.set_mode(pin, GPIO.MODE_INPUT);
GPIO.set_int_handler(pin, GPIO.INT_EDGE_NEG, function(pin) {
   print('Pin', pin, 'got interrupt');
}, null);
GPIO.enable_int(pin);

GPIO.set_button_handler(pin, pull, intmode, period, handler)

Install GPIO button handler. pull is pull type, intmode is interrupt mode, period is debounce interval in milliseconds, handler is a function that receives pin number. Return value: 1 in case of success, 0 otherwise. Example:

GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
  print('Button press, pin: ', x);
}, null);