
/**************************************************************************
Name            -rotor_pwr
Parameters      - The address of a rotor structure (az_rotor or el_rotor)
                  char specifying the desired action
Responsibility  - Manages the power to a rotor motor
                
Notes           - The function has evolved over time.
                  It uses data in the structure pointed to by the first
                  parameter to know how to manipulate power to the rotor.
                  Encapsulating the details fo power management in the 
                  rotor structure enables it to work for both the azmith
                  and elevation rotors.

                  Although not necessary at this time, the rotor structure
                  contains the address of the function that manages power 
                  for it (this function address). Various data are loaded into
                  the rotor sturctures at startup by the initialize() function.
                  Bit masks for power management is stored in the rotor
                  structure also. It is an attempt to enable a single function
                  (this one) to manage power for both the azmith and
                  elevation rotors.
                
Returns         - void (Nothing)
Author          - Bill (N9CX)
**************************************************************************/

/***********************CHANGE HISTORY*************************************
  1-25-2014 wje Moved out of main.c file
**************************************************************************/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/eeprom.h>

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

void save_az_rotor(void);
void save_el_rotor(void);

void rotor_pwr( struct rotor *p_rotor, char ctl){
    switch(ctl){
      case PWR_CW:
            PORTB &= p_rotor->pwr_cw; // 0 to pin 0 of PORT B
            p_rotor->moving = TRUE;
            p_rotor->cw = TRUE;
            p_rotor->ccw = FALSE;
          break;
      case PWR_CCW:
            PORTB &= p_rotor->pwr_ccw; // 0 to pin 1 of PORT B
            p_rotor->moving = TRUE;
            p_rotor->ccw = TRUE;
            p_rotor->cw = FALSE;
          break;
      case PWR_OFF:
            // 1 to both  pins to make sure both SS relays are off
            PORTB |= p_rotor->pwr_off;
            if(p_rotor == &el_rotor) {
                save_el_rotor();
            }
            if(p_rotor == &az_rotor) {
                save_az_rotor();
            }
            p_rotor->moving = FALSE;
          break;
    }
}


void save_az_rotor(void) {
    eeprom_write_block (&az_rotor, &ee_az_rotor, sizeof(struct rotor));
}

void save_el_rotor(void) {
    eeprom_write_block (&el_rotor, &ee_el_rotor, sizeof(struct rotor));
}
