/* program using multiple robots to
      take and display a series of pictures from each robot
*/

#include "MyroC.h"
#include "eSpeakPackage.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>    // for sleep

int main (int argc, char * argv [])
{

  printf ("argc is %d\n", argc);
  /* variable declarations */
  ROBOT_CONNECTION_TYPE robot[argc];  // robot IDs of robot connections 
  const char * r_name [argc];  // names of the robots
  int i;

  rSetBluetoothEcho ('n');
  eSpeakConnect ();
  eSpeakTalk ("Program displaying pictures from multiple robots");
  /* connect to the robots */
  for (i = 0; i < argc-1; i++)
    {
      robot [i] = rConnect (argv[i+1]);
      r_name [i] = rGetName ();
      printf ("trying to connect to %s\n", r_name[i]);
      char greeting [40];
      strcpy (greeting, "connected to ");
      strcat (greeting, r_name[i]);
      eSpeakTalk (greeting);
    }

  /* take and display series of pictures from the robots */
  int j;
  for (j = 0; j < 2; j++)
    {
      for (i = 0; i < argc-1; i++)
	{
	  rSetConnection (robot[i]);
	  Picture pic = rTakePicture ();
	  char commentary [40];
	  strcpy (commentary, "new picture from ");
	  strcat (commentary, r_name[i]);
	  //eSpeakTalk (commentary);
	  printf ("commentary:  %s\n", commentary);
	  rDisplayPicture (&pic, 0.0, r_name [i]);
          if (i % 2)
	    rTurnRight (0.4, 0.4);
	  else
	    rTurnLeft (0.4, 0.4);
	  sleep (2);
	}
    }

  eSpeakTalk ("disconnecting from eSpeak and robots");

  for (i = 0; i < argc-1; i++)
    {
      printf ("disconnect loop, i = %d\n", i);
      rSetConnection (robot[i]);
      rDisconnect ();
      printf ("disconnected from %s\n", r_name[i]);
    }

  printf ("closing image window[s]\n");
  eSpeakTalk ("closing each image window\n");
  rWaitTimedImageDisplay ();

  printf ("closing eSpeak\n");
  eSpeakTalk ("demonstration concluded");

  eSpeakDisconnect ();
  printf ("demonstration concluded\n");

  return 0;

}
