/**************************************************************************
Name            - mode_priority 
Parameters      - None
Responsibility  - Determine if any switch inputs require a "mode" change.
                  Updates the "mode" variable is necessary.
                
Notes           -
                
Returns         - Nothing
Author          - Bill (N9CX)
**************************************************************************/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/eeprom.h>


#include "switches.h"
#include "main.h"
#include "sw_timers.h"
#include "uart_getchar.h"

void mode_priority(void){
        switch( mode ) {
            case MANUAL:
                if(switches.calibrate) {
                    // calibrate has priority over manual mode
                    az_rotor.cal_submode = INITIAL;
                    el_rotor.cal_submode = INITIAL;
                    mode = CALIBRATE;
                    request_lcd_refresh = TRUE;
                    az_rotor.pwr_ctl(&az_rotor, PWR_OFF);
                    el_rotor.pwr_ctl(&el_rotor, PWR_OFF);
                }
                else if(switches.auto_track) {
                    // switch to auto mode
                    mode = AUTO;
                    az_rotor.pwr_ctl(&az_rotor, PWR_OFF);
                    el_rotor.pwr_ctl(&el_rotor, PWR_OFF);
                    az_rotor.first_pass = TRUE;
                    el_rotor.first_pass = TRUE;
                    request_lcd_refresh = TRUE;
                    new_auto_bearing = FALSE;
                }
                break;
            case AUTO:
                if(switches.man_az_cw   ||
                   switches.man_az_ccw  ||
                   switches.man_el_up   ||
                   switches.man_el_down     
                                        ) {
                    // Terminate auto tracking & switch to the manual mode
                    az_rotor.limit = FALSE;
                    el_rotor.limit = FALSE;
                    az_rotor.pwr_ctl(&az_rotor, PWR_OFF);
                    el_rotor.pwr_ctl(&el_rotor, PWR_OFF);
                    az_rotor.first_pass = TRUE;
                    el_rotor.first_pass = TRUE;
                    request_lcd_refresh = TRUE;

                    // must be a race condition 
                    // next line should not be needed!
	            switches.auto_track = 0;

                    mode = MANUAL;
                }
                break;
            case CALIBRATE:
                if(switches.man_az_cw   ||
                   switches.man_az_ccw  ||
                   switches.man_el_up   ||
                   switches.man_el_down     
                                        ) {
                    /***********************
                    Terminate the calibrate feature early
                    ***********************/
                    az_rotor.limit = FALSE;
                    el_rotor.limit = FALSE;
                    az_rotor.pwr_ctl(&az_rotor, PWR_OFF);
                    el_rotor.pwr_ctl(&el_rotor, PWR_OFF);
                    mode = MANUAL;
                    request_lcd_refresh = TRUE;
                }
                break;
            case IDLE:
            case INITIALIZE:
            case DETECT:
                break;
	    default:
		break;
		mode = MANUAL;
                request_lcd_refresh = TRUE;
        }
}
