
/**************************************************************************
Name            - Routines called from interrupt vector table
Parameters      - None
Responsibility  - timer1 interrupt (SIG_OVERFLOW1)

          Scans all switches on the system, debounces them and
          sets variables indicating which have been debounced and
          are active. System switches include:

             SWITCH            VARIABLE SET IF DEBOUNCED AND ACTIVE
                               CLEARED IF DEBOUNCED AND INACTIVE
          MAN_AZ_CW_INPUT        switches.man_az_cw
          MAN_AZ_CCW_INPUT       man_az_ccw
          MAN_EL_UP_INPUT        man_el_up
          MAN_EL_DOWN_INPUT      man_el_down
          MAN_CALIBRATE_INPUT    man_calibrate
          MAN_AUTO_TRACK         man_auto_track
          AZ_PULSE_INPUT         az_pusle
          EL_PULSE_INPUT         el_pusle


Notes           -

Returns         - Nothing
Author          - wje
Last change     - 4-12-2006
 **************************************************************************/

#include <inttypes.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>

#include "main.h"
#include "sw_timers.h"
#define LOCAL
#include "switches.h"

// interrupt enterval * DEBOUNCE equals debounce time
// ie 5 MS * 3 = 15 MS debounce time for all switches.
#define DEBOUNCE 3

#define MAN_AZ_CW_INPUT bit_is_clear(PINA, 0)
#define MAN_AZ_CCW_INPUT bit_is_clear(PINA, 1)
#define MAN_EL_UP_INPUT bit_is_clear(PINA, 2)
#define MAN_EL_DOWN_INPUT bit_is_clear(PINA, 3)
#define CALIBRATE_INPUT bit_is_clear(PINA, 4)
#define AUTO_TRACK bit_is_clear(PINA, 5)
#define AZ_PULSE_INPUT bit_is_clear(PIND, 6)
#define EL_PULSE_INPUT bit_is_clear(PIND, 7)

// 1 bit bit-fields use these values so TRUE must = 1 and FLASE must = 0
#define TRUE 1
#define FALSE 0

// counters to debounce the azimuth switch state
static unsigned char man_az_cw_active_cnt;
static unsigned char man_az_cw_inactive_cnt;
static unsigned char man_az_ccw_active_cnt;
static unsigned char man_az_ccw_inactive_cnt;

// counters to debounce the elevation switch state
static unsigned char man_el_up_active_cnt;
static unsigned char man_el_up_inactive_cnt;
static unsigned char man_el_down_active_cnt;
static unsigned char man_el_down_inactive_cnt;

// counters to debounce the pulser switches
static unsigned char el_pulse_active_cnt;
static unsigned char el_pulse_inactive_cnt;
static unsigned char az_pulse_active_cnt;
static unsigned char az_pulse_inactive_cnt;
static unsigned char calibrate_active_cnt;
static unsigned char calibrate_inactive_cnt;

static unsigned char auto_track_active_cnt;
static unsigned char auto_track_inactive_cnt;

SIGNAL (SIG_OVERFLOW1) {

PORTB |= 0x10;
    /************************************************************
    Timer/counter 1 has overflowed and generated an interrupt
    Reset counter registers for a 5 ms overflow (interrupt)
    ************************************************************/
    TCNT1 = 0xfd8e;

    /**********************************************************
    Manage the sw timers - unsigned integers
    other functions set the # of 5 ms intervals representing a 
    timeout condition. The functions detects timeout when the 
    value of the timer is 0.
    **********************************************************/
    if(sw_timer1) --sw_timer1;
    if(az_rotor.pulse_timer) --az_rotor.pulse_timer;
    if(el_rotor.pulse_timer) --el_rotor.pulse_timer;
    if(sw_timer2) --sw_timer2;
    if(sw_timer3) --sw_timer3;
    if(sw_timer4) --sw_timer4;
    if(sw_timer5) --sw_timer5;
    if(az_rotor.wait) --az_rotor.wait;
    if(el_rotor.wait) --el_rotor.wait;

    /*************************************************************
    Debounce the manual rotor direction switches.
    This routine does not cause rotor rotation to start or stop.
    It just reports the debounced state of the manual switches.
    *************************************************************/
    // Is the manual CW directional switch for the azimuth rotor activated?

    if(MAN_AZ_CW_INPUT){ // this should be a macro perhaps - need to define
        // make the inactive count start over since the sw is active
        man_az_cw_inactive_cnt = 0;
        if(!switches.man_az_cw){
            // switch is active (pressed) but not debounced yet
            if(++man_az_cw_active_cnt >= DEBOUNCE){
                // Azmith clockwise SW is active (pressed) and debounced
                man_az_cw_active_cnt = 0;
                switches.man_az_cw = TRUE;
                // Make sure both are never TRUE concurrently!
                switches.man_az_ccw = FALSE;
            }
        }
    }
    else{
           // no manual cw azimuth switch input - is this state debounced?
           // make the active count start over since it is inactive now
           man_az_cw_active_cnt = 0;
           if(switches.man_az_cw) {
              // current debounced state is ACTIVE so this may be a change
              if(++man_az_cw_inactive_cnt >= DEBOUNCE) {
                  // CW input for the azimuth rotor has just ended
                  switches.man_az_cw = FALSE;
                  man_az_cw_inactive_cnt = 0;
               }
          }
    }
    // Is the CCW manual directional switch for the azimuth rotro activated?
    if(MAN_AZ_CCW_INPUT){
        man_az_ccw_inactive_cnt = 0;
        if(!switches.man_az_ccw){
            // CCW SW is active but not debounced yet
            if(++man_az_ccw_active_cnt >= DEBOUNCE){
                // The CCW sw is debounce and is active
                man_az_ccw_active_cnt = 0;
                switches.man_az_ccw = TRUE;
                // this will keep both from being TRUE concurrently!
                switches.man_az_cw = FALSE; 
            }
        }
    }
    else{ // no manual CCW azimuth input - is this state debounced?
            man_az_ccw_active_cnt = 0; // make active count start over again!
        if(switches.man_az_ccw) {
            // current debounced state is ACTIVE so this may be a change
            if(++man_az_ccw_inactive_cnt >= DEBOUNCE) {
                // CCW switch for rotor is debounced and inactive
                man_az_ccw_inactive_cnt = 0;
                switches.man_az_ccw = FALSE;
            }

        }
    }
//-------MANUAL ELEVATION SWITCHES   UP/DOWN ------------------------
    // Is the manual up directional switch for elevation activated?
    if(MAN_EL_UP_INPUT){ // this should be a macro perhaps - need to define
        // make the inactive count start over since the sw is active
        man_el_up_inactive_cnt = 0;
        if(!switches.man_el_up){
            // sw is active (pressed) but not debounced yet
        if(++man_el_up_active_cnt >= DEBOUNCE){
            // Elevation up SW is active (pressed) and debounced
            man_el_up_active_cnt = 0;
            switches.man_el_up = TRUE;
            // Make sure both are never TRUE concurrently!
            switches.man_el_down = FALSE;
        }
        }
    }
    else{ // no manual up elevation input - is this state debounced?
        // make the active count start over since it is inactive now
            man_el_up_active_cnt = 0;
        if(switches.man_el_up) {
            // current debounced state is ACTIVE so this may be a change
            if(++man_el_up_inactive_cnt >= DEBOUNCE) {
                 // UP input for the elevaion rotor has just ended
                 switches.man_el_up = FALSE;
        }

        }
    }
    // Is the DOWN  manual switch for the elevation rotor activated?
    if(MAN_EL_DOWN_INPUT){
        man_el_down_inactive_cnt = 0;
        if(!switches.man_el_down){
            // DOWN SW is active but not debounced yet
        if(++man_el_down_active_cnt >= DEBOUNCE){
            // The DOWN sw is debounce and is active
            man_el_down_active_cnt = 0;
            switches.man_el_down = TRUE;
            // this will keep both from being TRUE concurrently!
            switches.man_el_up = FALSE; 
        }
        }
    }
    else{ // no manual DOWN elevation input - is this state debounced?
            man_el_down_active_cnt = 0; // make active count start over again!
        if(switches.man_el_down) {
            // sw is debounced active so this may be a change
            if(++man_el_down_inactive_cnt >= DEBOUNCE) {
                 // DOWN switch for rotor is debounced and inactive
             man_el_up_inactive_cnt = 0;
                 switches.man_el_down = FALSE;
        }

        }
    }
//-------PULSER SWITCHES   AZIMUTH/ELEVATION ROTORS------------------

    if(AZ_PULSE_INPUT){ 
        az_pulse_inactive_cnt = 0;
        if(!switches.az_pulse){
            // pulser SW is active but not debounced yet
        if(++az_pulse_active_cnt >= DEBOUNCE){
            // The pulser sw is debounce and is active
            az_pulse_active_cnt = 0;
            switches.az_pulse = TRUE;
            az_rotor.new_pulse = TRUE;
        }
        }
    }
    else{ // no pulser azmith switch input - is this state debounced?
            az_pulse_active_cnt = 0; // make active count start over again!
        if(switches.az_pulse) {
            // pulser sw is debounced active so this may be a change
            if(++az_pulse_inactive_cnt >= DEBOUNCE) {
                 // pulser switch for rotor is debounced and inactive
                az_pulse_inactive_cnt = 0;
                 switches.az_pulse = FALSE;
            }

        }
    }
    // Is the PULSER  switch for the elevation rotor closed?
    if(EL_PULSE_INPUT){
        el_pulse_inactive_cnt = 0;
        if(!switches.el_pulse){
            // pulser SW is active but not debounced yet
            if(++el_pulse_active_cnt >= DEBOUNCE){
                // The pulser sw is debounced and is active
                el_pulse_active_cnt = 0;
                switches.el_pulse = TRUE;
                el_rotor.new_pulse = TRUE;
            }
        }
    }
    else{ // no pulser elevation switch input - is this state debounced?
            el_pulse_active_cnt = 0; // make active count start over again!
        if(switches.el_pulse) {
            // pulser sw is debounced active so this may be a change
            if(++el_pulse_inactive_cnt >= DEBOUNCE) {
                 // pulser switch for rotor is debounced and inactive
                el_pulse_inactive_cnt = 0;
                switches.el_pulse = FALSE;
            }

        }
    }

    // Is the CALIBRATE  switch closed?
    if(CALIBRATE_INPUT){
        calibrate_inactive_cnt = 0;
        if(!switches.calibrate){
            // calibrate SW is active but not debounced yet
            if(++calibrate_active_cnt >= DEBOUNCE){
                // The calibrate sw is debounce and is active
                calibrate_active_cnt = 0;
                switches.calibrate = TRUE;
            }
        }
    }
    else{ // calibrate switch open - is this state debounced?
            calibrate_active_cnt = 0; // make active count start over again!
            if(switches.calibrate) {
                // calibrate sw is debounced closed so this may be a change
                if(++calibrate_inactive_cnt >= DEBOUNCE) {
                    // calibrate switch is debounced and open
                    calibrate_inactive_cnt = 0;
                    switches.calibrate = FALSE;
                }

            }
    }
    // Is the AUTO MODE  switch closed?
    if(AUTO_TRACK){
        auto_track_inactive_cnt = 0;
        if(!switches.auto_track){
            // auto_track SW is active but is it debounced yet?
            if(++auto_track_active_cnt >= DEBOUNCE){
                // The auto_track sw is debounced and is active
                auto_track_active_cnt = 0;
                switches.auto_track = TRUE;
            }
        }
    }
    else{
        // auto track switch open - is this state debounced?
        auto_track_active_cnt = 0; // make active count start over again!
        if(switches.auto_track) {
            // auto_track sw is debounced closed - but reads open now.
           if(++auto_track_inactive_cnt >= DEBOUNCE) {
               // auto_track switch is debounced and open
               switches.auto_track = FALSE;
               auto_track_inactive_cnt = 0;
          }
       }
    }
}
