/* sensorsTestSuite.c
 * Test suite to test sensor functions.
 * Author: Dilan Ustek
 * Oct. 23 2013
 * Rewritten Nov. 20 2013
 
 Testing for:

 rGetLightsAll
 rGetLightTxt
 rGetObstacleAll
 rGetObstacleTxt
 rGetLine
 rGetBrightnessAll
 rGetBrightness
 rGetIRAll
 rGetIRTxt
 rGetInfo
 rGetStall

 *
 * This program and all MyroC software is licensed under the Creative Commons
 * Attribution-Noncommercial-Share Alike 3.0 United States License.
 * Details may be found at http://creativecommons.org/licenses/by-nc-sa/3.0/us/
 */

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

int main(){

  eSpeakConnect();
  rConnect("/dev/rfcomm0"); 
  
  eSpeakTalk("Testing light sensors. Test gives the number in thousands.");
  int vals[3];
  rGetLightsAll(vals, 3);
  
  if (vals[0] != 0 && vals[1] != 0 && vals[2] != 0){
    eSpeakTalk("The values received from light sensors were all non-zero. Sample size  = 10.");
  }
  else
    eSpeakTalk("The values received from the light sensors were %d, %d, %d", vals[0] /1000, vals[1]/1000, vals[2]/1000);
 
  rGetLightsAll(vals, 5);
  
  if (vals[0] != 0 && vals[1] != 0 && vals[2] != 0){
    eSpeakTalk("The values received from light sensors were all non-zero. Sample size  = 10.");
  }
  else
    eSpeakTalk("The values received from the light sensors were %d, %d, %d", vals[0]/ 1000, vals[1]/1000, vals[2]/1000);

  double leftLight = rGetLightTxt("left", 3);
  if (leftLight > 0)
    eSpeakTalk("The value of the left sensor is %f. Sample size  = 3.", leftLight/1000);
  else 
    eSpeakTalk("The value of the left sensor is zero. Sample size  = 3.");

   leftLight = rGetLightTxt("left", 10);
  if (leftLight > 0)
    eSpeakTalk("The value of the left sensor is %f. Sample size  = 10.", leftLight/1000);
  else 
    eSpeakTalk("The value of the left sensor is zero. Sample size  = 10.");

  
  double rightLight = rGetLightTxt("right", 3);
  if (rightLight > 0)
    eSpeakTalk("The value of the right sensor is %f. Sample size  = 3.", rightLight/1000);
  else 
    eSpeakTalk("The value of the right sensor is zero. Sample size  = 3.");


   rightLight = rGetLightTxt("right", 10);
  if (rightLight > 0)
    eSpeakTalk("The value of the right sensor is %f. Sample size  = 10. ", rightLight/1000);
  else
    eSpeakTalk("The value of the right sensor is zero. Sample size  = 10.");

  double middleLight = rGetLightTxt("middle", 3);
  if (leftLight > 0)
    eSpeakTalk("The value of the middle sensor is %f. Sample size  = 3.", middleLight/1000);
  else 
    eSpeakTalk("The value of the middle sensor is zero. Sample size  = 3.");

   middleLight = rGetLightTxt("middle", 10);
  if (leftLight > 0)
    eSpeakTalk("The value of the middle sensor is %f. Sample size  = 10.", middleLight/1000);
  else 
    eSpeakTalk("The value of the middle sensor is zero. Sample size  = 10.");



  eSpeakTalk("Testing obstacle sensors. Test gives the number in thousands.");
  eSpeakTalk("Please place an object in front of the right sensor of the robot. Sample size is 3.");
  int obstacleVals[3];
  rGetObstacleAll(obstacleVals, 3);
  
  if (obstacleVals[2] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its right. Value is %d. Sample size  = 3.", obstacleVals[2]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its right.  Sample size  = 3.");

  eSpeakTalk("Please place an object in front of the right sensor of the robot. Sample size is 10.");

  rGetObstacleAll(obstacleVals, 10);
  
  if (obstacleVals[2] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its right. Value is %d. Sample size  = 10.", obstacleVals[2]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its right.  Sample size  = 10.");



  eSpeakTalk("Please place an object in front of the left sensor of the robot.");
  rGetObstacleAll(obstacleVals, 3);
  
  if (obstacleVals[0] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its left.  Value is %d. Sample size is 3.", obstacleVals[0]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its left. Sample size is 3.");

  eSpeakTalk("Please place an object in front of the left sensor of the robot.");
  rGetObstacleAll(obstacleVals, 10);
  
  if (obstacleVals[0] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its left.  Value is %d. Sample size is 10.", obstacleVals[0]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its left. Sample size is 10.");

  eSpeakTalk("Please place an object in front of the robot.");
  rGetObstacleAll(obstacleVals, 3);
  
  if (obstacleVals[1] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object in front of it.   Value is %d. Sample size is 3.", obstacleVals[1]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object in front of it.  Sample size is 3.");

  eSpeakTalk("Please place an object in front of the robot.");
  rGetObstacleAll(obstacleVals, 10);
  
  if (obstacleVals[1] > 0){
    eSpeakTalk("Robot has successfully detected that there is an object in front of it.   Value is %d. Sample size is 10.", obstacleVals[1]/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object in front of it.  Sample size is 10.");


  eSpeakTalk("Testing obstacle sensors text.");
  eSpeakTalk("Again, please place an object in front of the right sensor of the robot. ");
  
  int obstacleVal = rGetObstacleTxt("right", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its right. Value is %d. Sample size is 3.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its right. Sample size is 3.");

   obstacleVal = rGetObstacleTxt("right", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its right.  Value is %d. Sample size is 10.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its right. Sample size is 10.");


  eSpeakTalk("Please place an object in front of the left sensor of the robot.");
  
  obstacleVal = rGetObstacleTxt("left", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its left. Value is %d.   Sample size is 3.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its left. Sample size is 3.");

  obstacleVal = rGetObstacleTxt("left", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object on its left. Value is %d.   Sample size is 10.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object on its left.  Sample size is 10.");


  eSpeakTalk("Please place an object in front of the robot.");
  
 obstacleVal = rGetObstacleTxt("middle", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object in front of it.  Sample size is 3.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object in front of it.  Sample size is 3.");
 
  obstacleVal = rGetObstacleTxt("middle", 3);
  if (obstacleVal > 0){
    eSpeakTalk("Robot has successfully detected that there is an object in front of it.  Sample size is 10.", obstacleVal/1000);
  }
  else
    eSpeakTalk("Robot has failed to detect the object in front of it.  Sample size is 10.");



  eSpeakTalk("Testing get Line. Please place robot on top of a black line that is on a white surface.");
  int lineVals[2];
   
  rGetLine(lineVals, 3);

  if(lineVals[0] == 0)
    eSpeakTalk("Left sensor returned 0 which means the line is seen.   Sample size is 3.");
  else eSpeakTalk("Left sensor returned 1 which means the line is not seen.   Sample size is 3.");

  if(lineVals[1] == 0)
    eSpeakTalk("Right sensor returned 0 which means the line is seen.   Sample size is 3. ");
  else eSpeakTalk("Right sensor returned 1 which means the line is not seen.   Sample size is 3.");  
   
   
  rGetLine(lineVals, 10);

  if(lineVals[0] == 0)
    eSpeakTalk("Left sensor returned 0 which means the line is seen.   Sample size is 10.");
  else eSpeakTalk("Left sensor returned 1 which means the line is not seen.   Sample size is 10.");

  if(lineVals[1] == 0)
    eSpeakTalk("Right sensor returned 0 which means the line is seen.   Sample size is 10.");
  else eSpeakTalk("Right sensor returned 1 which means the line is not seen.   Sample size is 10.");  


  eSpeakTalk("Testing brightness sensors. Test returns results 0 through 25.");
  int brightVals[3];
  rGetBrightAll(brightVals, 3);
   
  if (brightVals[0] != 0 && brightVals[1] != 0 && brightVals[2] != 0){
    eSpeakTalk("The values in received from brightness sensors were all non-zero. Values were %d, %d, %d.  Sample size is 3.", brightVals[0]/10, brightVals[1]/10, brightVals[2]/10);
  }

  int brightnessVal = rGetBrightTxt("left", 3)/10;
  if (brightnessVal != 0)
    eSpeakTalk("left brightness sensor returned non zero. Value is %d.  Sample size is 3.", brightnessVal);
  else
    eSpeakTalk("left brightness sensor returned zero.   Sample size is 3.");
 
   brightnessVal = rGetBrightTxt("right", 3)/10;
  if (brightnessVal != 0)
    eSpeakTalk("right brightness sensor returned non zero.  Value is %d.   Sample size is 3.", brightnessVal);
  else
    eSpeakTalk("right brightness sensor returned zero.   Sample size is 3.");
   
   brightnessVal = rGetBrightTxt("middle", 3)/10;
  if (brightnessVal != 0)
    eSpeakTalk("middle brightness sensor returned non zero.  Value is %d.   Sample size is 3.", brightnessVal);
  else
    eSpeakTalk("middle brightness sensor returned zero.   Sample size is 3.");


  rGetBrightAll(brightVals, 10);
   
  if (brightVals[0] != 0 && brightVals[1] != 0 && brightVals[2] != 0){
    eSpeakTalk("The values in received from brightness sensors were all non-zero.   Values were %d, %d, %d.  Sample size is 10.", brightVals[0]/10, brightVals[1]/10, brightVals[2]/10);
  }

 brightnessVal = rGetBrightTxt("left", 10)/10;
 if (brightnessVal != 0)
    eSpeakTalk("left brightness sensor returned non zero. Value is %d.    Sample size is 10.", brightnessVal);
  else
    eSpeakTalk("left brightness sensor returned zero.   Sample size is 10.");

 brightnessVal = rGetBrightTxt("right", 10)/10;
  if (brightnessVal != 0)
    eSpeakTalk("right brightness sensor returned non zero.  Value is %d.   Sample size is 10.", brightnessVal);
  else
    eSpeakTalk("right brightness sensor returned zero.   Sample size is 10.");
  
 brightnessVal = rGetBrightTxt("middle", 10)/10;
  if (brightnessVal != 0)
    eSpeakTalk("middle brightness sensor returned non zero.  Value is %d.   Sample size is 10.", brightnessVal);
  else
    eSpeakTalk("middle brightness sensor returned zero.   Sample size is 10.");



  eSpeakTalk("Testing get IR. Please place an object in front of the robot. Sample size is 3.");
  int irVals[2];
   
  rGetIRAll(irVals, 3);

  if(irVals[0] == 0)
    eSpeakTalk("Left sensor returned 0 which means the object is seen. Sample size is 3.");
  else eSpeakTalk("Left sensor returned 1 which means the object is not seen. Sample size is 3.");

  if(irVals[1] == 0)
    eSpeakTalk("Right sensor returned 0 which means the object is seen. Sample size is 3.");
  else eSpeakTalk("Right sensor returned 1 which means the object is not seen. Sample size is 3.");  

  if (rGetIRTxt("left", 3) == 0)
    eSpeakTalk("Left sensor returned 0 whıch means the object is seen. Sample size is 3.");
  else eSpeakTalk("Left sensor returned 1 which means the object is not seen. Sample size is 3.");

  if(rGetIRTxt("right", 3) == 0)
    eSpeakTalk("Right sensor returned 0 which means the object is seen. Sample size is 3.");
  else eSpeakTalk("Right sensor returned 1 which means the object is not seen. Sample size is 3."); 



  eSpeakTalk("Testing get IR. Please place an object in front of the robot. Sample size is 10");   
  rGetIRAll(irVals, 10);

  if(irVals[0] == 0)
    eSpeakTalk("Left sensor returned 0 which means the object is seen. Sample size is 10.");
  else eSpeakTalk("Left sensor returned 1 which means the object is not seen. Sample size is 10.");

  if(irVals[1] == 0)
    eSpeakTalk("Right sensor returned 0 which means the object is seen. Sample size is 10.");
  else eSpeakTalk("Right sensor returned 1 which means the object is not seen. Sample size is 10.");  

  if (rGetIRTxt("left", 3) == 0)
    eSpeakTalk("Left sensor returned 0 which means the object is seen. Sample size is 10.");
  else eSpeakTalk("Left sensor returned 1 which means the object is not seen. Sample size is 10.");

  if(rGetIRTxt("right", 3) == 0)
    eSpeakTalk("Right sensor returned 0 which means the object is seen. Sample size is 10.");
  else eSpeakTalk("Right sensor returned 1 which means the object is not seen. Sample size is 10."); 


  
  eSpeakTalk("Testing getStall.");
  eSpeakTalk("Please put something in front of the robot.");
  if (rGetStall(3) == 1)
    eSpeakTalk("Robot has successfully detected that it is stalled.");
  else if (rGetStall(3) == 0)
    eSpeakTalk("Robot has failed to detect that it is stalled.");
  else 
    eSpeakTalk("get stall function should return 0 or 1. I got a different number,");


  eSpeakTalk("Please make sure there is nothing in front of the robot.");
  if (rGetStall(3) == 1)
    eSpeakTalk("Robot has failed to detect that it is not stalled.");
  else if (rGetStall(3) == 0)
    eSpeakTalk("Robot has successfully detected that it is not stalled.");
  else 
    eSpeakTalk("get stall function should return 0 or 1. I got a different number,");
}
