//Osciloscopio
//seta43
//v3 -1/08/2017
//v4 - 14/03/2020
//
// xsetaseta@gmail.com
//-------------------

char clave[] = "OSCI5";

//#define _DEBUG 
#define MAXENTRADA 50


  byte datos[800];
  int analogValue;
 
  char buffer[MAXENTRADA];
  int contador=0;
  char stringComplete = false; 

int comando1,comando2,valorComando1,valorComando2,nucomandos;

int test33v(void)
{
  int z;
  //Analog Input A1
  ADMUX=(1<<ADLAR)|(0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(1<<MUX0);
  
  z=analogReadFast();
  
  //Analog Input A0
  ADMUX=(1<<ADLAR)|(0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);
  return(z);
}

 void setup() 
 {
   int zx;
  Serial.begin(115200); 
 for (zx = 0; zx < 3; zx++) { Serial.println(clave); delay(200); }

  
  bitWrite(ADCSRA,ADPS2,1);
  bitWrite(ADCSRA,ADPS1,0);
  bitWrite(ADCSRA,ADPS0,0);
  
  //Analog Input A0
  ADMUX=(1<<ADLAR)|(0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);
  
 }
 
 void loop() 
 {
 
 while (Serial.available())
    {
      char inChar = (char)Serial.read(); 
      buffer[contador]=inChar;  
      if (inChar == '\n')
          {
            stringComplete = true;
            buffer[contador]=0;
          }       
      if(contador<MAXENTRADA)contador++;
    }
  if (stringComplete) 
    {     
      Orden(buffer);
      contador=0;
      stringComplete = false;   
    }

 }
 
  

void Orden(char *bufferS)
{
  int i=0;
      for (i=0;i<MAXENTRADA;++i)  buffer[i]=bufferS[i];
      MinToMay();
     // Serial.println(buffer);     
      trazar();
      runFuniones();
}  

int Frecu(void)
{
  unsigned int x,x1;
        for (x=0;x<800 && analogReadFast()<128;x++);
        for (x=0;x<800 && analogReadFast()>128;x++);
        for (x1=0;x1<800 && analogReadFast()<128;x1++);
        for (x=0;x<800 && analogReadFast()>128;x++);
  return(x1+x);
}

int runFuniones(void)
{
  unsigned int z,x,x1;
  byte h,l;
  if(strstr(buffer,"OSCI="))
      {
        Serial.println("OK");
        goto salir0;
      }
  if(strstr(buffer,"VOL="))
      {        
        Serial.println("VOL     ");
        for (x=0;x<10;x++)
             {
               analogValue=analogReadFast();
               Serial.write(analogValue);
             } 
        Serial.println("FIN     ");        
        goto salir0;
      }  
  if(strstr(buffer,"GETUP="))
      {         
        for (x=0;x<800 && analogReadFast()>=128;x++);
        for (x=0;x<800 && analogReadFast()<=128;x++);      
        for (x=0;x<800;x++) datos[x]=analogReadFast();
        //Serial.println("DAT     ");
        //z=Frecu();
        //h=z>>8;
        //l=z & 0xFF; 
        Serial.write('D'); Serial.write('A'); Serial.write('T'); Serial.write(' ');
        Serial.write(' '); Serial.write(' '); Serial.write(h); Serial.write(l);
        Serial.write(13); Serial.write(10);
        for (x=0;x<800;x++)
             {
               analogValue=datos[x];
               Serial.write(analogValue);
             } 
        Serial.println("FIN     ");
        goto salir0;
      } 
   if(strstr(buffer,"GETDOWN="))
      {         
        for (x=0;x<800 && analogReadFast()<=128;x++);
        for (x=0;x<800 && analogReadFast()>=128;x++);          
        for (x=0;x<800;x++) datos[x]=analogReadFast(); 
        //Serial.println("DAT     ");
        //z=Frecu();
        //h=z>>8;
        //l=z & 0xFF;
        Serial.write('D'); Serial.write('A'); Serial.write('T'); Serial.write(' ');
        Serial.write(' '); Serial.write(' '); Serial.write(h); Serial.write(l);
        Serial.write(13); Serial.write(10);
        for (x=0;x<800;x++)
             {
               analogValue=datos[x];
               Serial.write(analogValue);
             } 
        Serial.println("FIN     ");   
        goto salir0;
      }            
  
   if(strstr(buffer,"VREF0"))
      {
        ADMUX=(1<<ADLAR)|(0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);
        //Serial.println("OK-VREF0  vcc   ");
        goto salir0;
      }
  if(strstr(buffer,"VREF1"))
      {
        //Serial.println("OK-VREF1 1V1    ");
        ADMUX=(1<<ADLAR)|(1<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0);
        goto salir0;
      }

  
  if(strstr(buffer,"DIV2"))
      {
        bitWrite(ADCSRA,ADPS2,0);
        bitWrite(ADCSRA,ADPS1,0);
        bitWrite(ADCSRA,ADPS0,1);
        goto salir0;
      }
  if(strstr(buffer,"DIV4"))
      {
        bitWrite(ADCSRA,ADPS2,0);
        bitWrite(ADCSRA,ADPS1,1);
        bitWrite(ADCSRA,ADPS0,0);
        goto salir0;
      }
  if(strstr(buffer,"DIV8"))
      {
        bitWrite(ADCSRA,ADPS2,0);
        bitWrite(ADCSRA,ADPS1,1);
        bitWrite(ADCSRA,ADPS0,1);
        goto salir0;
      }
  if(strstr(buffer,"DIV16"))
      {
        bitWrite(ADCSRA,ADPS2,1);
        bitWrite(ADCSRA,ADPS1,0);
        bitWrite(ADCSRA,ADPS0,0);
        goto salir0;
      }
  if(strstr(buffer,"DIV32"))
      {
        bitWrite(ADCSRA,ADPS2,1);
        bitWrite(ADCSRA,ADPS1,0);
        bitWrite(ADCSRA,ADPS0,1);
        goto salir0;
      }
  if(strstr(buffer,"DIV64"))
      {
        bitWrite(ADCSRA,ADPS2,1);
        bitWrite(ADCSRA,ADPS1,1);
        bitWrite(ADCSRA,ADPS0,0);
        goto salir0;
      }
  if(strstr(buffer,"DIV128"))
      {
        bitWrite(ADCSRA,ADPS2,1);
        bitWrite(ADCSRA,ADPS1,1);
        bitWrite(ADCSRA,ADPS0,1);
        goto salir0;
      }
  if(strstr(buffer,"TEST="))
      {
        Serial.println("TEST    ");
        for (x=0;x<10;x++)
             {
               analogValue=test33v();
               Serial.write(analogValue);
             } 
        Serial.println("FIN     ");    
        goto salir0;
      }
  
 salir0:
 ;
}
  
void MinToMay(void)
{
  int i=0;
  int desp='a'-'A';

  for (i=0;buffer[i]!='\0';++i)
  {
    if(buffer[i]>='a'&&buffer[i]<='z') buffer[i]=buffer[i]-desp;
  }
}  

int trazar(void)
{
  int z;


  comando1=0;      
  comando2=0;
  nucomandos=0;
  #ifdef _DEBUG   
     Serial.print("comandos>");Serial.println(buffer);
  #endif 
  
      for(z=0;z<MAXENTRADA;z++)
        {
          if(buffer[z]=='\n' || buffer[z]==0) { break;}
          if(buffer[z]==32 || buffer[z]==',' ) 
            {
              if(comando1==0)
                {buffer[z]=0; comando1=z+1;nucomandos++;}
                else
                {buffer[z]=0; comando2=z+1;nucomandos++;}
            }
        }
    valorComando1=atoi(buffer+comando1);
    valorComando2=atoi(buffer+comando2);
#ifdef _DEBUG   
  Serial.print("Numerocomandos>");Serial.println(nucomandos);
  Serial.print("comando0>");Serial.println(buffer);
  Serial.print("comando1>");Serial.println(buffer+comando1);
  Serial.print("comando2>");Serial.println(buffer+comando2);
  Serial.print("Valorcomando1>");Serial.println(valorComando1);
  Serial.print("Valorcomando2>");Serial.println(valorComando2);
#endif  
  return(nucomandos);
}
  
  
 //Read ADC
int analogReadFast()
{
 ADCSRA|=(1<<ADSC);
 // ADSC is cleared when the conversion finishes
 while (bit_is_set(ADCSRA, ADSC));
        return ADCH;
}
