Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

MGOS_GPIO_INT_EDGE_ANY doesn't work

Hello, I'm trying to write a handler for button click that must respond to both a POSITIVE and NEGATIVE button state change. I use esp8266 platform.
Here is a code example:

#include "mgos.h"
#include "mgos_mqtt.h"

#define BUTTON_PIN 12

static void led_timer_cb(void *arg) {
//  bool val = mgos_gpio_toggle(mgos_sys_config_get_pins_led());
  LOG(LL_INFO, ("pin state: %d", mgos_gpio_read(BUTTON_PIN)));
  (void) arg;
}

static void net_cb(int ev, void *evd, void *arg) {
  switch (ev) {
    case MGOS_NET_EV_DISCONNECTED:
      LOG(LL_INFO, ("%s", "Net disconnected"));
      break;
    case MGOS_NET_EV_CONNECTING:
      LOG(LL_INFO, ("%s", "Net connecting..."));
      break;
    case MGOS_NET_EV_CONNECTED:
      LOG(LL_INFO, ("%s", "Net connected"));
      break;
    case MGOS_NET_EV_IP_ACQUIRED:
      LOG(LL_INFO, ("%s", "Net got IP address"));
      break;
  }

  (void) evd;
  (void) arg;
}

static void button_cb(int pin, void *arg) {
  char topic[100], message[100];
  struct json_out out = JSON_OUT_BUF(message, sizeof(message));
  snprintf(topic, sizeof(topic), "/devices/%s/events",
           mgos_sys_config_get_device_id());
  json_printf(&out, "{total_ram: %lu, free_ram: %lu}",
              (unsigned long) mgos_get_heap_size(),
              (unsigned long) mgos_get_free_heap_size());
  bool res = mgos_mqtt_pub(topic, message, strlen(message), 1, false);
  LOG(LL_INFO, ("Pin: %d, published: %s", pin, res ? "yes" : "no"));
  (void) arg;
}

enum mgos_app_init_result mgos_app_init(void) {
  /* Blink built-in LED every second */
    mgos_gpio_set_mode(BUTTON_PIN, MGOS_GPIO_MODE_INPUT);
//  mgos_gpio_set_mode(mgos_sys_config_get_pins_led(), MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(1000, MGOS_TIMER_REPEAT, led_timer_cb, NULL);

  /* Publish to MQTT on button press */
    mgos_gpio_set_button_handler(BUTTON_PIN,
                               MGOS_GPIO_PULL_NONE, MGOS_GPIO_INT_EDGE_ANY, 50,
                               button_cb, NULL);

  /* Network connectivity events */
  mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

It's almost a demo-c example. I pulled down the listened button via 10KOM resistor manually, that's why I set MGOS_GPIO_PULL_NONE (actually it doesn't have matter. I tried different variants).
But when I press the button happens nothing. In a console, I can see that the button's state changes, but handler doesn't output its log.

When I use an MGOS_GPIO_INT_EDGE_POS or an MGOS_GPIO_INT_EDGE_NEG mode the handler works well. So the problem is in the MGOS_GPIO_INT_EDGE_ANY mode.

PS near a half year ago this code worked well. Something has happend since that time.

Thanks a lot for a help!

Comments

Sign In or Register to comment.