/****************************************************************************/
/* Copyright 2016 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/

#include <xc.h>
#include <string.h>
#include <stdio.h>
#include "device.h"
#include "uart1.h"
#include "timer.h"


extern unsigned short AlarmCnt;//Global incremented in AlarmISR

//---------------------------------------------------------------------
void ClearRxFifo (void)
{
    int i=0;
    
    for (i=0;i<RxFifoSz;i++)RxFifo[i]=0x00;
    RxPutPt=RxGetPt=&RxFifo[0];   //RxFifo is Empty when PutPt=GetPt
    return;
}
//---------------------------------------------------------------------
void vPutCharU ( unsigned short cChar, int chan )
{
	if (chan ==1)
    {
        while (U1STAbits.UTXBF); // Waits when the output buffer is full
        U1TXREG = cChar;         // Puts the character to the buffer
    }
    
    if (chan ==2)
     {
        while (U2STAbits.UTXBF); // Waits when the output buffer is full
        U2TXREG = cChar;         // Puts the character to the buffer
    }       
}

/*---------------------------------------------------------------------
  Function name: vPutStrU
  Description: Sends string to UART
  Input parameters: String to send, UART to send to
  Output parameters: -
-----------------------------------------------------------------------*/
void vPutStrU ( unsigned char* pcStr, int chan )
{
    unsigned short i=0;
	while( *pcStr != 0 )
	{
        i=0;
		vPutCharU(*pcStr++,chan);
        //while(i<0x1000){i++;};
	}
}
void vPutStrMaxon ( unsigned short pcStr[], int size, int chan )
{
    int i;
    unsigned short hi, lo;

    //pcStr[0] is the DLE STX sync pair - always sent raw, never stuffed
    vPutCharU( (pcStr[0] & 0xFF00)>>8, chan );
    vPutCharU(  pcStr[0] & 0x00FF,     chan );

    for(i=1; i<size+1; i++)
    {
        hi = (pcStr[i] & 0xFF00)>>8;
        lo =  pcStr[i] & 0x00FF;

        vPutCharU(hi,chan);
        if(hi == 0x90) vPutCharU(hi,chan);   //double any DLE in the body

        vPutCharU(lo,chan);
        if(lo == 0x90) vPutCharU(lo,chan);
    }
}
/*---------------------------------------------------------------------
  Function name: cGetCharU
  Description: Receives character from UART1 (non-blocking)
  Input parameters: UART to read from
  Output parameters: Received character
-----------------------------------------------------------------------*/
unsigned char cGetCharU( int chan )
{
    if(chan==1)
    {
        if ( U1STAbits.URXDA )    
        {return U1RXREG;}
        else
        {return(0);
        }
    }
    if(chan==2)
    {
        if ( U2STAbits.URXDA )    
        {return U2RXREG;}
        else
        {return(0);
        }
    }
    return(0);
}

int RxStr2(int maxchar, char *buf,int ms_to1,int ms_bet) //read RxD1 to LF
 {
  //Read up to maxchars or until LF is read from Rx port into 
  //buf or until ms_bet millisecs elapse
  //between characters. ms_to1 = timeout to reception of 1st char. 
  //Returns number of chars read
  //A NULL is appended but not included in the character count so adjust
  //maxchar accordingly to avoid buffer overrun.
  
  char *ptr;

  AlarmCnt = 0; //reset alarm
    
  while( AlarmCnt<ms_to1  && !U1STAbits.URXDA){}; //wait for first char
  
  if(!U1STAbits.URXDA)return(0); //bail if didnt get anything
  
  for (ptr=buf;ptr<buf+maxchar;ptr++)
   {
      AlarmCnt=0; //reset
      while ( !U1STAbits.URXDA) //Wait for a char
      {
        if ( AlarmCnt >=ms_bet )  //count times the 1 ms alarm has gone off
         {*ptr='\0';//append a NULL but don't count it
           return (ptr-buf) ; //quit if a timeout
         }
       }
     
    if ( (*ptr=(char)cGetCharU(1)) == 0x0d )  //store the waiting character
      {//read an LF
       ptr++;//count the LF 
       break; //quit when LF is read
      }
   }
  *ptr='\0';//append a NULL but don't count it
  return (ptr-buf);
 } // end RxStr2
//*******************************************************************************
int RxStr(int maxchar,unsigned char *buf,int ms_to1,int ms_bet) //read RxD2 to LF or ;
 {
  //Read up to maxchars, until LF, or ; is read from Rx port into 
  //buf or until ms_bet millisecs elapse
  //between characters. ms_to1 = timeout to reception of 1st char. 
  //Returns number of chars read
  //A NULL is appended but not included in the character count so adjust
  //maxchar accordingly to avoid buffer overrun.
  
  unsigned char *ptr;
  unsigned short Acnt = 0;
  unsigned short rxbitflag = 0;

  AlarmCnt = 0; //reset alarm  
  /*printf("AlarmCnt = %d,U1STAbits.OERR=%d, U1STAbits.URXDA = %d\r\n",AlarmCnt,U1STAbits.OERR,U1STAbits.URXDA);
  while( AlarmCnt<ms_to1  && !U1STAbits.URXDA){}; //wait for first char
  Acnt = AlarmCnt;
  rxbitflag = U1STAbits.URXDA;
  printf("AlarmCnt = %d, U1STAbits.URXDA = %d\r\n",Acnt,U1STAbits.URXDA);
  if(!U1STAbits.URXDA)return(0); //bail if didnt get anything
  */
  for (ptr=buf;ptr<buf+maxchar;ptr++)
   {
      AlarmCnt=0; //reset
      while ( !U1STAbits.URXDA) //Wait for a char
      {
        if ( AlarmCnt >=ms_bet )  //count times the 1 ms alarm has gone off
         {*ptr='\0';//append a NULL but don't count it
           return (ptr-buf) ; //quit if a timeout
         }
       }
    *ptr=(char)cGetCharU(1);  //store the waiting character
    //LATAbits.LATA0 ^= 1;
    
    //if (  (*ptr == 0x0a) || (*ptr == 0x3b) ) 
    //  {//read an LF or semicolon
    //   ptr++;//count the LF 
    //   break; //quit when LF is read
    //  }
   }
  *ptr='\0';//append a NULL but don't count it
  //printf("Waited %d Alarms with bit stat = %d\n",Acnt,rxbitflag);
  
  return (ptr-buf);
 } // end RxStr
//******************************************************************************
int UI_parse (int ms)
{
    int i=0;
    //int j=0;
    //int lf=-1;
    int found=0;
    char tmp[8];
    AlarmCnt = 0; //reset alarm
    
    while( !found && (AlarmCnt < ms) ) //wait for ;
    {
        if (U2STAbits.URXDA) //char available
        {
         tmp[i] = cGetCharU(2); //Read one character form UART1
         printf("%c",tmp[i]);
         if (tmp[i]==0x0d){found=i;}
         i++;
        }
    }
    
    if(found)
    {
       sscanf(tmp,"%d",&i);
    }
    
    
return(i); 
}
//******************************************************************************
