示例#1
0
static void button_handler(int id, int irq)
{
  uint8_t newset = board_buttons();

  lowsyslog("IRQ:%d Button %d:%s SET:%02x:\n",
            irq, id, g_buttoninfo[BUTTON_INDEX(id)].name, newset);
  show_buttons(g_oldset, newset);
  g_oldset = newset;
}
示例#2
0
static void show_buttons(uint8_t oldset, uint8_t newset)
{
  uint8_t chgset = oldset ^ newset;
  int i;

  /* Update the count of button presses shown */

#ifdef CONFIG_NSH_BUILTIN_APPS
  if ((chgset & newset) != 0)
    {
      g_nbuttons++;
    }
#endif

  /* Show each button state change */

  for (i = MIN_BUTTON; i <= MAX_BUTTON; i++)
    {
      uint8_t mask = (1 << i);
      if ((chgset & mask) != 0)
        {
          FAR const char *state;

          /* Get the button state */

          if ((newset & mask) != 0)
            {
              state = "depressed";
            }
          else
            {
              state = "released";
            }

          /* Use lowsyslog() because we make be executing from an
           * interrupt handler.
           */

          lowsyslog(LOG_INFO, "  %s %s\n",
                    g_buttoninfo[BUTTON_INDEX(i)].name, state);
        }
    }
}
示例#3
0
void up_pmbuttons(void)
{
  /* Initialize the button GPIOs */

  board_button_initialize();

#ifdef CONFIG_ARCH_IRQBUTTONS
  int i;
  for (i = CONFIG_PM_IRQBUTTONS_MIN; i <= CONFIG_PM_IRQBUTTONS_MAX; i++)
    {
      xcpt_t oldhandler = board_button_irq(i, g_buttonhandlers[BUTTON_INDEX(i)]);

      if (oldhandler != NULL)
        {
          lowsyslog(LOG_WARNING, "WARNING: oldhandler:%p is not NULL!  "
                    "Button events may be lost or aliased!\n",
                    oldhandler);
        }
    }
#endif
}
示例#4
0
int buttons_main(int argc, char *argv[])
{
  uint8_t newset;
  irqstate_t flags;
  int i;

  /* If this example is configured as an NX add-on, then limit the number of
   * samples that we collect before returning.  Otherwise, we never return
   */

#ifdef CONFIG_NSH_BUILTIN_APPS
  long maxbuttons = 1;
  g_nbuttons      = 0;
  if (argc > 1)
    {
      maxbuttons = strtol(argv[1], NULL, 10);
    }
  lowsyslog("maxbuttons: %d\n", maxbuttons);
#endif

  /* Initialize the button GPIOs */

  board_button_initialize();

  /* Register to recieve button interrupts */

#ifdef CONFIG_ARCH_IRQBUTTONS
  for (i = CONFIG_EXAMPLES_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLES_IRQBUTTONS_MAX; i++)
    {
      xcpt_t oldhandler = board_button_irq(i, g_buttoninfo[BUTTON_INDEX(i)].handler);

      /* Use lowsyslog() for compatibility with interrrupt handler output. */

      lowsyslog("Attached handler at %p to button %d [%s], oldhandler:%p\n",
                g_buttoninfo[BUTTON_INDEX(i)].handler, i,
                g_buttoninfo[BUTTON_INDEX(i)].name, oldhandler);

      /* Some hardware multiplexes different GPIO button sources to the same
       * physical interrupt.  If we register multiple such multiplexed button
       * interrupts, then the second registration will overwrite the first.  In
       * this case, the first button interrupts may be aliased to the second
       * interrupt handler (or worse, could be lost).
       */

      if (oldhandler != NULL)
        {
          lowsyslog("WARNING: oldhandler:%p is not NULL!  "
                    "Button events may be lost or aliased!\n",
                    oldhandler);
        }
    }
#endif

  /* Poll button state */

  g_oldset = board_buttons();
#ifdef CONFIG_NSH_BUILTIN_APPS
  while (g_nbuttons < maxbuttons)
#else
  for (;;)
#endif
    {
      /* Get the set of pressed and release buttons. */

      newset = board_buttons();

      /* Any changes from the last sample? */

      if (newset != g_oldset)
        {
          /* Disable interrupts so that output here will not collide with
           * output from an interrupt handler.
           */

          flags = irqsave();

          /* Use lowsyslog() for compatibility with interrrupt handler
           * output.
           */

          lowsyslog("POLL SET:%02x:\n", newset);
          show_buttons(g_oldset, newset);
          g_oldset = newset;
          irqrestore(flags);
        }

      /* Sleep a little... but not long.  This will determine how fast we
       * poll for button changes.
       */

      usleep(150000); /* 150 Milliseconds */
    }

  /* Un-register button handlers */

#if defined(CONFIG_ARCH_IRQBUTTONS) && defined(CONFIG_NSH_BUILTIN_APPS)
  for (i = CONFIG_EXAMPLES_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLES_IRQBUTTONS_MAX; i++)
    {
      (void)board_button_irq(i, NULL);
    }
#endif

  return 0;
}