/**************************************************************************
Function Name   - az_rotor_bearing 
Parameters      - None
Responsibility  - Calculate the rotor azmith 
                    az_rotor.bearing
                
Notes           - 
Returns         - Nothing
Author          - Bill (N9CX)
**************************************************************************/
#include <inttypes.h>
#include <avr/eeprom.h>

#include "rotor.h"
#include "main.h"

void az_rotor_bearing(void){

    if(az_rotor.cw) az_rotor.bearing =  (az_rotor.pulse_count *
                                        az_rotor.deg_per_pulse) +
                                        az_rotor.deg_update;
    else if(az_rotor.pulse_count){
        az_rotor.bearing =  (az_rotor.pulse_count *
                            az_rotor.deg_per_pulse) -
                            az_rotor.deg_update;
    }
    /*******************************************************************
    The rotor last moved CCW and the pulse count is 0.
    Since the bearing is decreasing and deg_update is the correction 
    factor that is normally subtracted from the pulse_count the fromula 
    is now  deg_per_pulse -  deg_update
    *******************************************************************/
    else az_rotor.bearing = az_rotor.deg_per_pulse - az_rotor.deg_update;

    /******************************************************************
    defensive code - just in case we slipped a tic or two.
                     A tic is one timer counter interrupt.
    *******************************************************************/
    if(az_rotor.bearing < 0) {
        az_rotor.bearing = 0;
        az_rotor.deg_update = 0;
    }
    if(az_rotor.bearing > 360){
        az_rotor.bearing = 360;
        az_rotor.deg_update = 0;
    }
}
