FAR void *trv_nxlistener(FAR void *arg)
{
  int ret;

  /* Process events forever */

  for (;;)
    {
      /* Handle the next event.  If we were configured blocking, then
       * we will stay right here until the next event is received.  Since
       * we have dedicated a while thread to servicing events, it would
       * be most natural to also select CONFIG_NX_BLOCKING -- if not, the
       * following would be a tight infinite loop (unless we added addition
       * logic with nx_eventnotify and sigwait to pace it).
       */

      ret = nx_eventhandler(g_hnx);
      if (ret < 0)
        {
          /* An error occurred... assume that we have lost connection with
           * the server.
           */

          trv_abort("Lost server connection: %d\n", errno);
        }

      /* If we received a message, we must be connected */

      if (!g_trv_nxrconnected)
        {
          g_trv_nxrconnected = true;
          sem_post(&g_trv_nxevent);
          trv_debug("Connected to server\n");
        }
    }
}
Exemple #2
0
static int trv_joystick_calibrate(void)
{
  struct ajoy_sample_s sample;
  int ret;

  printf("Calibrating the joystick...\n");

  /* Get the center position */

  printf("Center the joystick and press any button\n");
  ret = trv_joystick_sample(&sample);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: trv_joystick_sample() failed: %d\n", errno);
      return ret;
    }

  g_trv_joystick.centerx = sample.as_x;
  g_trv_joystick.centery = sample.as_y;

  /* Get the left/right calibration data */

  printf("Move the joystick to the far RIGHT and press any button\n");
  ret = trv_joystick_sample(&sample);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: trv_joystick_sample() failed: %d\n", errno);
      return ret;
    }

  g_trv_joystick.lplus      = (sample.as_x > g_trv_joystick.centerx);
  g_trv_joystick.leftslope  = trv_joystick_slope(sample.as_x - g_trv_joystick.centerx, MAX_RATE);
  g_trv_joystick.lturnslope = trv_joystick_slope(sample.as_x - g_trv_joystick.centerx, MAX_TURN);

  printf("Move the joystick to the far LEFT and press any button\n");
  ret = trv_joystick_sample(&sample);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: trv_joystick_sample() failed: %d\n", errno);
      return ret;
    }

  g_trv_joystick.rightslope = trv_joystick_slope(sample.as_x - g_trv_joystick.centerx, -MAX_RATE);
  g_trv_joystick.rturnslope = trv_joystick_slope(sample.as_x - g_trv_joystick.centerx, -MAX_TURN);

  /* Get the forward/backward calibration data */

  printf("Move the joystick to the far FORWARD and press any button\n");
  ret = trv_joystick_sample(&sample);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: trv_joystick_sample() failed: %d\n", errno);
      return ret;
    }

  g_trv_joystick.fplus      = (sample.as_y > g_trv_joystick.centery);
  g_trv_joystick.fwdslope   = trv_joystick_slope(sample.as_y - g_trv_joystick.centery, MAX_RATE);
  g_trv_joystick.uturnslope = trv_joystick_slope(sample.as_y - g_trv_joystick.centery, MAX_TURN);

  printf("Move the joystick to the far BACKWARD and press any button\n");
  ret = trv_joystick_sample(&sample);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: trv_joystick_sample() failed: %d\n", errno);
      return ret;
    }

  g_trv_joystick.backslope  = trv_joystick_slope(sample.as_y - g_trv_joystick.centery, -MAX_RATE);
  g_trv_joystick.dturnslope = trv_joystick_slope(sample.as_y - g_trv_joystick.centery, -MAX_TURN);

  printf("Calibrated:\n");
  trv_debug("  X: center=%d, R-slope=%08lx L-slope=%08lx\n",
            g_trv_joystick.centerx, g_trv_joystick.rightslope,
            g_trv_joystick.leftslope);
  trv_debug("  Y: center=%d, F-slope=%08lx B-slope=%08lx\n",
            g_trv_joystick.centery, g_trv_joystick.fwdslope,
            g_trv_joystick.backslope);
  return OK;
}