#include <stdio.h>
#include <msp430.h> 
#include "inside_globals.h"
#include "unitTests.h"


/*
 * hello.c
 */
void main(void) {
	WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
//#ifdef RUN_TESTS

  //Run unit tests to verify functionality of the the program
  int result = 0;
  result = runUnitTests(1);
  printf("Tests completed with %d errors!\n\r", result);
/*#else

  //Run the actual program
  printf("Running Program!\n\r", result);

  P1OUT = 0x00;
  P1DIR |= 0x20; // Set P1.5 to output direction
  //Both of these pins have been used at differ ent times as the DC_READY pin
  P1DIR &= ~0x40; //Set P1.6 to input direction
  P1DIR &= ~0x80; //Set P1.7 to input direction
  P1DIR |= BIT1 + BIT2 + BIT3; //1.1, 1.2, 1.3 to output directionc
  P2OUT = 0x00;
  P2DIR |= 0x03; //Set P2.0,P2.1 to output direction

  int powered  = (P1IN & 0x80);	//Goes high when the DC_READY line is high

  //Wait for P1.6 to go high indicating power is supplied to the MCU
  while(!powered){
    powered  = (P1IN & 0x40);
  }
  //Start in awake mode and transmit awake sequence to the outside sytem
  awakeMode = 1;

  TBCCTL0 = CCIE;                       // Timer B CCR0 interrupt enabled
  TBCCR0 = timer_B_counter;		// Set Timer B high value (count to this value)
  TBCTL = TBSSEL_2 + MC_1+ID_1;         // Uses SMCLK, Continuous Mode, Input clock divide by 2

  //Clock_Init();				//Set clocks.
  //DMA_Init();				//Set DMA registers

  //TX_ON();
  _BIS_SR(GIE);
  _NOP();
  while(1);

#endif
*/
}
int computeThreshold(int start, int end)
{
  int i;
  long sum = 0;
  int elements = end -start;
  int max = results[start];
  int min = results[start];

  int avg = 0;
  int mid = 0;

  //Find min, max, and sum of values in results[] range (start)->(end).
  for(i=start; i<end; i++)
  {

    sum += results[i];
    if(results[i] < min)
    {
      min = results[i];
    }
    if(results[i] > max)
    {
      max = results[i];
    }

  }

  //We can use the mean(avg) value (with the lowest and highest sample removed)
  //Or the median(mid) value as the threshold.
  mid= (max + min)/2;
  avg = (sum-max-min)/(elements-2);

  return mid;
}
int runUnitTests(int tests)
{
  int i = 0;
  int value = 1200;
  int result =0;

  printf("Running Unit Tests...\n\r");
  /*
  int computeThreshold(int start, int end);	//Computes the average value between the alrgest and smallest values in the reults buffer
  - Fill results buffer from 0 to 44 with values and check to make sure the value returned is what we expect. */
  //result+=normalThresholdTest();        //Threshold at a normal level
  result+=highThresholdTest();          //Threshold with  one extra high value
  /*result+=lowThresholdTest();         //Threshold with one extra low value
  result+=extremesThresholdTest();    //Threshold with one low and one high value
  result+=zerosThresholdTest();         //Threshold test with all zeros
  result+=normalFind(0);                //Find Manch Sequence starting at 0
  result+=normalFind(100);              //Find Manch Sequence starting at 100
  result+=normalFind(836);              //Find Manch Sequence starting at 836
  result+=normalFindWithOverflow(875);  //
  result+=getDataTest(0,0);
  result+=getDataTest(0,1);
  result+=testTransSansThresh();*/
  return result;
}
