/**************************************************************************
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 "rotor.h"
#include "main.h"
#include "sw_timers.h"
#include "uart_getchar.h"

void initialize_auto_mode(void);
void initialize_manual_mode(void);

void mode_priority(void){
        switch( mode ) {
            case MANUAL:
                if(switches.calibrate) {
                    // calibrate has priority over manual mode
                    mode = CALIBRATE;
                    initialize_calibrate_mode();
                    request_lcd_refresh = TRUE;
                }
                else if(switches.auto_track) {
                    // switch to auto mode
                    mode = AUTO;
                    // Initialization for AUTO mode
                    initialize_auto_mode();
                }
                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
                   // Just to make sure it is off
                   switches.auto_track = 0;
                   mode = MANUAL;
                   initialize_manual_mode();
                }
                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
                    ***********************/
                    mode = MANUAL;
                   initialize_manual_mode();
                   request_lcd_refresh = TRUE;
                }
                break;
            case IDLE:
            case INITIALIZE:
            case DETECT:
                break;
	    default:
		break;
		mode = MANUAL;
                request_lcd_refresh = TRUE;
        }
}
