//#include "inside_globals.h"
#include <stdio.h>

int result = 0;
int i = 0;
int value;

//UTILITY FUNCTIONS
//Fills the results array with manchester bits, all of value (fillValue)
void fillResultsAlternating(int start, int fillValue)
{
  int first, second;
  if(fillValue == 0)
  {
    first = 1200;
    second = 2200;
  }
  else 
  {
    first = 2200;
    second = 1200;
  }
  for(i = 0; i <(2*NUM_RESULTS-start); i++)
  {
  
    if(i%4 == 0)
    {
      value = first;
    }
    else if(i%2 == 0)
    {
      value = second;
    }
  
    results[start+i] = value;
  }
}

//Fills the results array with manchester bits (all 1s)
void fillResultsOnes(int start)
{
  for(i = 0; i <(2*NUM_RESULTS-start); i++)
  {
  
    if(i%4 == 0)
    {
      value = 2200;
    }
    else if(i%2 == 0)
    {
      value = 1200;
    }
  
    results[start+i] = value;
  }
}
//Puts the Manchester sequence in the results array starting at the start index
void fillResultsSequence(int start)
{
  int bit = 0;          //Value of the bit in the manchester Sequence
  int offset = 0;       //Offset from start in the results array to fill
  long long shifted;    //Shifted Manchester sequence
  for(i=0; i <44; i++)
  {
    offset = 43 - i;
    shifted = (manchSequence >> i);
    bit = (manchSequence >> i)&(0x01);
    
    if(bit == 1)
    {
      results[(start+offset)%1760] = 2200;
    }
    else
    {
      results[(start+offset)%1760] = 1200;
    }
  }
}

void clearResults()
{
    int i;
  for(i=0;i<2*NUM_RESULTS;i++)
  {
    results[i] = 0;
  }
}

//TESTS//
//       - Normal Case
int normalThresholdTest()
 {
  fillResultsAlternating(0,0);
  result = 0;
  printf("Computing Threshold with normal case... \t");

  threshold = computeThreshold(0,44);
  if(threshold == 1700)
  {
    printf("OK\n\r");
  }
  else
  {
    result =1;
    printf("FAILED\n\r");
  }
  return result;
 }
 
  //	- Extreme High (One extremely high value)
 int highThresholdTest()
 {
   fillResultsAlternating(0,0);
   result = 0;

  results[20] = 10200;
  printf("Computing Threshold with one high value %d... \t",results[20]);
  threshold = computeThreshold(0,44);
  if(threshold == 5700)
  {
    printf("OK\n\r");
  }
  else
  {
    result=1;
    printf("FAILED\n\r");
  }  
  return result;
 }

  //    - Extreme Low (One value at 0)
 int lowThresholdTest()
 {
   fillResultsAlternating(0,0);
   result = 0;
  results[20] = 0;
  printf("Computing Threshold with one low value %d... \t",results[20]);
  threshold = computeThreshold(0,44);
  if(threshold == 1100)
  {
    printf("OK\n\r");
  }
  else
  {
    result=1;
    printf("FAILED\n\r");
  }  
  return result;
 }

//- Extreme High and Low
int extremesThresholdTest()
 {
   fillResultsAlternating(0,0);
   result = 0;
  results[20] = 0;
  results[21] = 10000;
  printf("Computing Threshold with one low value %d and one high value %d... \t",results[20],results[21]);
  threshold = computeThreshold(0,44);
  if(threshold == 5000)
  {
    printf("OK\n\r");
  }
  else
  {
    result=1;
    printf("FAILED\n\r");
  }  
  return result;
 }

//      - All 0s
int zerosThresholdTest()
{
  int i;
  for(i=0;i<44;i++)
  {
    results[i] = 0;
  }
  
  printf("Computing Threshold with all zeros... \t");
  threshold = computeThreshold(0,44);
  if(threshold == 0)
  {
    printf("OK\n\r");
  }
  else
  {
    result=1;
    printf("FAILED\n\r");
  }  
  return result;
}

	
   
//- Threshold computation with values past the end of the current buffer

//int findSequence(int start, int end);		//Searches for sync sequence in results[] between (start) and (end)

//- Normal distribution (use real data as a sample)
//	- Sequence from 0 to 44. 

int normalFind(int start)
{
    int found;
    int bufferStart;
    
    clearResults();
    
    if(start<880)
    {
      bufferStart = 0;
    }
    else
    {
      bufferStart = NUM_RESULTS;
    }
    printf("Finding Sequence Starting at %d... \t",start);
    fillResultsSequence(start);
    found = findSequence(bufferStart,bufferStart+NUM_RESULTS);
    if(found==1 && originalStart== start)
    {
      result = 0;
      printf("OK\n\r");
    }
    else
    {
      result = 1;
      printf("FAILED\n\r");
    }
    
    int paws= 1;
    return result;
  
  
}

//	- Sequence from 100 to 144
//	- Sequence from 836 to 880
//		- Sweep beginning up to 880 (these will require two calls and a second half buffer fill)
//	-Sequence from 1000-1044
//	-Sequence from 1715 - 1759
//		- Sweep beginning up to 1759 (again, requires two buffer writes)
int normalFindWithOverflow(int start)
{
    int found;
    int bufferStart;
    
    clearResults();
	fillResultsSequence(start);
	printf("Finding Sequence Starting at %d... \t",start);
	
	//Check to see on what side of the buffer the DMA_ISR would operate
    if(start<880)
    {
      bufferStart = 0;
    }
    else
    {
      bufferStart = NUM_RESULTS;
    }
    
	found = findSequence(bufferStart,bufferStart+NUM_RESULTS);
	
	bufferStart = (bufferStart+NUM_RESULTS)%(2*NUM_RESULTS);
	//found = findSequence(bufferStart,bufferStart+NUM_RESULTS);
    if(found==1 && originalStart== start)
    {
      result = 0;
      printf("OK\n\r");
    }
	else
    {
      result = 1;
      printf("FAILED\n\r");
    }
    
	
	
  return result;
}

//int getData(int bufStart, int bufEnd);		//Translates data bits from samples in results[] between (bufStart) and (bufEnd)
//	- Test given all 0s
int getDataTest(int start, int fillValue)
{
	int gotData = 0;
	dataStart = 0;
        
	printf("Getting 80 bytes of data (all %d s)...\t",fillValue);
        
        
	fillResultsAlternating(0,fillValue);
	
	gotData = getData(0,NUM_RESULTS);
	if(gotData)
	{
		printf("FAILED\n\r");
		return 1;
	}
	gotData = getData(NUM_RESULTS,2*NUM_RESULTS);
	if(gotData)
	{
		printf("FAILED\n\r");
		return 1;
	}
	gotData = getData(0,NUM_RESULTS);
	if(gotData == 22)
	{
          //Verify results is filled with the fillValue sent to this function
          int i;
          if(fillValue == 0)
          {
            for(i=0;i<80;i++)
            {
              if(data[i] != 0)
              {
                printf("FAILED\n\r");
		return 1;
              }
            }
          }
          else if(fillValue == 1)
          {
            for(i=0;i<80;i++)
            {
              if(data[i] != 0xFF)
              {
                printf("FAILED\n\r");
		return 1;
              }
            }
          }
		printf("OK\n\r");
		return 0;
	}
	else
	{
		return 1;
	}
	
}
/*
	-Generate random 80 byte payload and check that it is correct

int getPackNum(void);                           //Get the number of the packet received
	- Set pack num from 0 to 99 and test it


int popcount(long long x);
	- Test all bit strings with 1s in every combination of places in the 44 bit sequence

long long transSansThresh(int start, int length);
	- Test all 1s translation*/
//	- Test all 0s translation

int testTransSansThresh()
{
  long long value;
  
  printf("Testing a single bit translation...\t");
  results[0] = 1200;
  results[1] = 1200;
  results[2] = 2200;
  results[3] = 2200;
  value = transSansThresh(0,4);
  if(value!=3)
  {
    result+=1;
    printf("FAILED \n\r");
  }
  else
  {
    printf("OK \n\r");
  }
  return result;
}
   
            
/*	- Test alternating 1s and zeros translation
          
	- Test the other alternating 1s and 0s translation
	- Test single sample error at all 44 positions
- Test consecutive bit errors at all 44 positions
- Test Sequence shifted by one sample
- Test sequence shifted by two samples 
- Test Sequence shifted by three samples
- Test Sequence shifted by a whole bit (4 samples)

Testing DMA_ISRB
Set DMA_Interrupt flag manually
	- Results all zeros
	- Results all zeros except for sample just under the threshold 
	- Results all zero except sample just over threshold
	- Sequence with single bit error
	- Correct sequence
	- Sequence starts 4 samples before threshold hit
		-Sweep to four samples after threshold hit
	- Sequence runs over middle buffer divide
	- Sequence runs over end buffer
  */
