/*from page 385 in book*/

/*allows user to receive serial data*/
void disable_pcode_serial(){
  poke(0x3c, 1);
}
/*allows interaction with ic on host computer*/
void enable_pcode_serial(){
poke(0x3c, 0);
}

void serial_putchar(int c){
  while(!(peek(0x102e) & 0x80));

  /*send char*/
  poke(0x102f, c);
}

int serial_getchar(int c){

  while(!(peek(0x102e) & 0x20));

  /*send char*/
  return peek(0x102f);
}
/* Sends one of global arrays determined by which
 * distance_from_light if 0
 * angle_from_light if 1
 */
void send_array(int size, int which){
  int i, j, c;

  /*sending size via while loop*/
  c = size;
  while(1){
      if(c==0){
        serial_putchar(c);
        break;
      }else{
        i = c%10;
        printf("i= %d\n", i);
        serial_putchar(i);
        c = c/10;
      }
  }
  /*clearing line*/
  c = 10;
  serial_putchar(c);
  /*sending array int by int*/
  for(j=0;j<size;j++){
    if(which==0){
      c=distance_from_light[j];
    }else{
      c=angle_from_light[j];
    }
    if(c<0){
      serial_putchar('-');
      c= c*-1;
    }
    while(1){
      if(c==0){
        serial_putchar(c);
        break;
      }else{
        i = c%10;
        printf("i= %d\n", i);
        serial_putchar(i);
        c = c/10;

      }
    }
    c = 10;
    serial_putchar(c);
  }
}

/* This function takes and int size and calls functions that
 * send both of the global arrays of that size.
*/
int send_data(int size){
  int c;
  while(1){
  printf("Hit start button to send data.\n");
  while(!start_button());
  disable_pcode_serial();

  printf("Transmitting...\n");
  c=10;
  serial_putchar(c);
  serial_putchar('a');
  /*sending arrays, distance, then degree*/
  send_array(size, 0);
  send_array(size, 1);

  /*send exit when done*/
  serial_putchar('x');

  printf("done\n");
  enable_pcode_serial();
  }
}




