#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"
using namespace std;

/**************************************************************
Remember to download and run the message.rxe program on the NXT
***************************************************************/

//set up the NXT
Connection *connection = new Bluetooth();
Brick *nxt = new Brick(connection);
int main()
{
  unsigned int i=1;
  string temp;
  string text("Hello world ");
  string input;
  char number[100];

  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(40);
    cout << "Connected" << endl;
    cout << "Read and write a message to the mailbox system on the NXT - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
      itoa(i,number,10);
      temp = number;
      input = text + temp;
      cout <<"Input to NXT   : " << input << endl;
      nxt->write_msg(input, 0, false); //"Hello world 'i'" is placed in inbox 0 on the NXT
      Sleep(100); //wait for nxt
      cout <<"Answer from nxt: " << nxt->read_msg(1,true) << endl; //Retrive the string in inbox 1 (the NXT will place the same string )
      i++;
    }
    connection->disconnect();
  }
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    cout << e.who() << endl;
    connection->disconnect();
  }
  return 0;
}