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

//set up the NXT
Connection *connection = new Bluetooth();
Sensor_port port = IN_1;
Sensor *sensor1;
int main()
{
  unsigned int choice;

  cout << "Please select the sensor type on sensor port 1" << endl;
  cout << "Press 1 for sonar sensor" << endl;
  cout << "Press 2 for touch sensor" << endl;
  cout << "Press 3 for tilt sensor" << endl;
  cout << "Press 4 for light sensor" << endl;
  cin >> choice;//get input from keyboard
  switch ( choice ){
    case 1 :
      sensor1 = new Sonar(port, connection);
    break;
    case 2 :
      sensor1 = new Touch(port, connection);
    break;
    case 3 :
      sensor1 = new Tilt(port, connection);
    break;
    case 4 :
      sensor1 = new Light(port, connection);
    break;
    default :
      sensor1 = new Sensor(port, connection, NO_SENSOR , PERIOD_MODE );
  }

  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(40);
    cout << "Connected" << endl;
    cout << "Print sensor value - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
      cout << sensor1->print() << endl;
    }
    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;
}