/* Program to demonstrate storing pictures in a variable-length array */

#include <MyroC.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* Note, some MyroC platforms incorrectly fail to open a new picture window by
   the same name (crashing the program). The workaround is to use a
   non-blocking--but slightly longer than intended--rDisplayPicture command,
   while making it blocking by including a call to sleep for the intended
   display duration. The compile-time choice is indicated below. */

/* Support platform hack for displaying frames correctly */
#ifndef NON_BLOCKING_DISPLAY
#define NON_BLOCKING_DISPLAY 0
#endif

const double FRAME_DISPLAY_SECONDS = 1.0; /* Duration to display a frame */

int
main (void)
{
  printf ("Starting program:  ");
  rConnect ("/dev/rfcomm0");
  rSetForwardness ("fluke-forward");

  printf ("program asking Scribbler 2 to take and display pictures\n");

  /* determine how many pictures */
  int numTokens, numPics, i;
  printf ("enter number of pictures to be taken:  ");
  numTokens = scanf("%d", &numPics);

  if (numTokens!=1 || numPics<0)
  {
    printf("a positive number is required\n");
    return 1; /* exit, indicating failure */
  }
    
  Picture pics[numPics]; /* Declare a C99 VLA */
  
  printf ("taking %d pictures, while turning left\n", numPics);
  for (i = 0; i < numPics; i++)
  {
    pics[i] = rTakePicture();
    rTurnLeft (0.3, 0.3);
  }

  sleep (1);
  printf ("showing pictures in original order\n");
  for (i = 0; i < numPics; i++)
  {
    if (NON_BLOCKING_DISPLAY) {
      rDisplayPicture (&pics[i], -FRAME_DISPLAY_SECONDS-2, "movieFrame");
      sleep (FRAME_DISPLAY_SECONDS);
    } else
      rDisplayPicture (&pics[i], FRAME_DISPLAY_SECONDS, "movieFrame");
  }

  sleep (1);
  printf ("showing pictures in reverse order\n");
  for (i = numPics-1; i >= 0; i--)
  {
    if (NON_BLOCKING_DISPLAY) {
      rDisplayPicture (&pics[i], -FRAME_DISPLAY_SECONDS-2, "movieFrame");
      sleep (FRAME_DISPLAY_SECONDS);
    } else
      rDisplayPicture (&pics[i], FRAME_DISPLAY_SECONDS, "movieFrame");
  }

  printf ("Closing connection to robot....");
  rDisconnect();
  printf (" finished.\n\n");

  return 0;
} // main
 
