Exemplo n.º 1
0
static void gpio_edge_detection_cb(void* data) {
  int fd;
  iotjs_gpio_t* gpio = (iotjs_gpio_t*)data;
  struct pollfd pollfd;

  if ((fd = gpio_get_value_fd(gpio)) < 0) {
    DLOG("GPIO Error: cannot start edge detection");
    return;
  }

  memset(&pollfd, 0, sizeof(pollfd));
  pollfd.fd = fd;
  pollfd.events = POLLPRI | POLLERR;

  if (!gpio_clear_dummy_value(fd))
    return;

  while (true) {
    if ((fd = gpio_get_value_fd(gpio)) < 0)
      return;

    if (gpio_edge_poll(&pollfd) > 0) {
      gpio_emit_change_event(gpio);
    } else {
      DLOG("GPIO Error on poll: %s", strerror(errno));
    }
  }
}
Exemplo n.º 2
0
/****************************************************************
 * gpio_get_value
 ****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
    int fd, len;
    char buf[MAX_BUF];

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);

    fd = open(buf, O_RDONLY);
    if (fd < 0) {
        perror("gpio/get-value");
        return fd;
    }

    gpio_get_value_fd(fd, value);

    close(fd);
    return 0;
}