//**********************************************
//         Stepper Driver Routine
// version 2.0
// Call with signed int -2 through 2 to drive motor L/R fast/slow, or stop
// Returns 16bit signed angular position in motor steps
// which can be arbitrarily large.
//  
//
//**********************************************

//libraries
#include 
//#include "ad_lib.h"
//#include "kbhit.h"
#include "shiftreg.h"
#include "PLS.h"

//defines
#define DEBUG 0  //to get debugging output, set to 1 and uncomment appropriate lines

//Global Variables


signed int AngularPosition;


signed int RotateTurret (signed int rotation, unsigned int pulses) 
{

    unsigned int newPeriod;  //ranges from 10 to 640.  the # is .1 miliseconds.
    unsigned char FastPeriod = 35;  //set very slow right now for testing
	unsigned char SlowPeriod = 100;  //not really used yet
	unsigned char initialPORTGvalue;  //reads port G before messing with it to not klobber what's there
    		   //note: PLS_CHAN1 outputs to port A, pin 4. PLS_CHAN2 outputs to port A, pin 3.
    //unsigned char theKey;
    
    PLS_Init();	  
    //AD_Init();
    
    //while(1) {
    initialPORTGvalue = ReadPORTG();
    
    //if (kbhit() ) { rotation = getchar(); }	  //ONLY USE TO OPERATE INDEPENDENT OF MAIN
    
    	 //if (DEBUG) printf("pulses= %u", pulses);
    
    if (rotation == 2) //fast
        {   WritePORTG(0xFD & initialPORTGvalue);  //set port G, pin 1 low
         newPeriod = FastPeriod;   //pulses = 100;  
         PLS_PulseStart(newPeriod,pulses,PLS_CHAN2);
            AngularPosition = AngularPosition + pulses;
        }
    else if (rotation == 1) //slow
        {   WritePORTG(0xFD & initialPORTGvalue);  //set port G, pin 1 low
         newPeriod = SlowPeriod;   //pulses = 100;  
         PLS_PulseStart(newPeriod,pulses,PLS_CHAN2);
            AngularPosition = AngularPosition + pulses;
        }
    else if (rotation == 0) 
        {   WritePORTG(0xFD & initialPORTGvalue);  //set port G, pin 1 low
         PLS_End(); 
        }
    else if (rotation == -1) //slow
        {   WritePORTG(0x02 | initialPORTGvalue);  //set port G, pin 1 high
         newPeriod = SlowPeriod;   //pulses = 100;  
         PLS_PulseStart(newPeriod,pulses,PLS_CHAN2);
            AngularPosition = AngularPosition - pulses;
        }
    else if (rotation == -2) //fast
        {   WritePORTG(0x02 | initialPORTGvalue);  //set port G, pin 1 high
         newPeriod = FastPeriod;   //pulses = 100;  
         PLS_PulseStart(newPeriod,pulses,PLS_CHAN2);
            AngularPosition = AngularPosition - pulses;
        }
    else printf("%d: BAD rotation value", rotation);
    
    //    if (DEBUG) printf("   %d\n", AngularPosition);
    //} // bracket for while loop
    
return AngularPosition;

//end
}