
/************************************************************************
NOTE - Depreciated file - Historical reference only!
Returns:    nonzero if a successful parse
            zero if parse fails
************************************************************************/

#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.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 parse_rx_buffer(void){
    int found_az = FALSE;
    int found_el = FALSE;
    int found_name= FALSE;
    int i, j, az_index;
    char result=FALSE;
    int tmp_az_bearing, tmp_el_bearing;

          for(i=0; i< RX_BUFFER_SZ; ++i){
              if(rx_buffer[i] == 'A' && rx_buffer[i+1] == 'Z'){
                  /*
                  found first digit of the azmith
                  but go back amd load the satellite name array first
                  */
                  az_index = i+2;
                  for(j=0; j<i-1;++j){
                      sat_name[j] = rx_buffer[j];
                      if(j == (sizeof(sat_name)-1)) break;
                  }
                  // Terminate the name string
                  sat_name[j]=0;
                  found_name = TRUE;
              }
          }
          if(found_name){
                  /***** Found the satellite name ok **********/

                  /*
                    az_index should be pointing at the first digit of 
                    the azimuth number.
                   */
                  for(j=0; isdigit(rx_buffer[az_index+j]); ++j){
                        sat_az[j] = rx_buffer[az_index+j];
                  }
                  // Terminate the string
                  sat_az[j] = 0;
                  found_az = TRUE;
          }
          /*
           finished witht he azmith number array
           now look for the "EL" letters. Start from where we left off
           */
          for(i=az_index +j; i< RX_BUFFER_SZ; ++i){
                if(rx_buffer[i] == 'E' && rx_buffer[i+1] == 'L'){
                      // found first digit of the elevation
                      for(j=0,i=i+2; isdigit(rx_buffer[i]); ++i){
                        sat_el[j++] = rx_buffer[i];
                      }
                      sat_el[j] = 0;
                }
                found_el = TRUE;
          }
          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;
}

