/************************************************************************
Function:       novacomm1_parser / modifiew (1-17-2014)for Andrew/Mac Doppler use
Paramerers:     None
Responsibility: Parse the array pointed to by cptr to locate the satellite
                name, satellite azmith, & satellite elevation.
                If found it translates the ASCII azmith and elevation strings
                to integers and loads p_rotor->auto_bearing & el_auto_bearing.


Returns:        TRUE (non-zero) if a successful parse
                Zero if parse fails
Global Usage:
                Writes -  sat_az[], sat_el[] arrays
                         p_rotor->auto_bearing, el_auto_bearing
Author:         Bill (N9CX)
************************************************************************/

#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/eeprom.h>

#include "uart_getchar.h"
#include "switches.h"
#include "rotor.h"
#include "main.h"
#include "auto_track.h"

char Mac_Doppler_parser(char *cptr, int sz){
    char *ptr_nm = cptr;
    char *ptr_az = cptr;
    char *ptr_el;
    char *ptr_tmp;

    int found_az = FALSE;
    int found_el = FALSE;
    int found_name= FALSE;
    char result=FALSE;
    int tmp_az_bearing, tmp_el_bearing;

          while(ptr_az < (cptr+sz) ){
              /* string looks like this: "W090 030"
                 Where 090 is the bearing & 030 is the azimuth
              */
              if(*ptr_az == 'W' ){
                  /*
                  Found the "W" azmith indicator

                  The string does not include a sat name so use - "SAT"
                  */
                  
                  strcpy(sat_name, "SAT");
                  found_name = TRUE;

                  /*
                  The first ASCII digit of the azimuth is 1 place forward
                  in the array.
                  */
                  ++ptr_az;
                  break;
              }
              else ++ptr_az;  
          }
          if(found_name){
              /*
               ptr_az is pointing at the first digit of 
               the azimuth number.
               Find the end of the digits now
              */
              found_az = TRUE;
              ptr_tmp = ptr_az+3;
          }
          if(found_az){
            /*******************************************************
            Terminate the azmith substrign at the first non numeric
            ASCII character (expected to be an ASCII "space".
            ********************************************************/
            *ptr_tmp = 0;
            strcpy(sat_az, ptr_az);

             ptr_el = ++ptr_tmp;
             //ptr_el now points to the first ASCII digit of the elevation

             found_el = TRUE;
             //Assume the end of the elevation number (ASCII)is 3 places right

             ptr_tmp = ptr_el +3;
             /*
             Making the assumption that these are really digits here.
             If this works we can add code to verify - just to be sure
             So terminate the substring
             */
             *ptr_tmp = 0;
              strcpy(sat_el, ptr_el);
          }
          
          if(found_az && found_el){
                tmp_az_bearing = atoi(sat_az);
                tmp_el_bearing = atoi(sat_el);
                if(tmp_az_bearing >= 0 && tmp_az_bearing <= 360){
                    if(tmp_el_bearing >= 0 && tmp_el_bearing <= 360){
                        az_rotor.auto_bearing = tmp_az_bearing;
                        el_rotor.auto_bearing = tmp_el_bearing;
                        result = TRUE;
                    }
                }
          }
          return result;
}

