/* Program uses a struct containing an array of doubles and an int to move
 *   in a square.
 */

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

#define NUM_ACTIONS 8

/* declare struct that can be used in procedures and main */
typedef struct {
  double speed;
  double time;
} movement_t;


/* set speed and action for a move by the Scribbler 2 robot */
/* must pass address of pointer to get values out of procedure */
void
initialize(movement_t* move, int index)
{
  (*move).speed = 1 - (.1 * index);
  (*move).time = (index / 2.0) + 0.5;
}

/* print values in movement struct */
void printMove(movement_t move)
{
  printf ("robot action:  time = %lf,   speed = %lf\n", move.time, move.speed);
}

/* move robot */
void moveRobot (movement_t move [], int size)
{
  int i;
  for (i = 0; i < size; i++)
    {
      rForward(move[i].speed, move[i].time);
      /* Command to make a (roughly) 90 degree turn to the left */
      rTurnLeft(1, 0.8);
    }
}

int main()
{
  rConnect("/dev/rfcomm0");
  rSetForwardnessTxt("fluke-forward");

  int a; /* Loop variable */
  
  /* Declares an array of NUM_ACTIONS movement structs */
  movement_t actions[NUM_ACTIONS];

  /* Loop to initialize the values in the structs */
  for (a = 0; a < NUM_ACTIONS; a++)
    {
      initialize(&actions[a], a);
    }

  /* Loop to print the Scribbler times and speed(s). */
  for (a = 0; a < NUM_ACTIONS; a++)
    {
      printMove(actions[a]);
    }

  moveRobot (actions, NUM_ACTIONS);

  rBeep (1, 600);

  rDisconnect();
  return 0;
} // main
