/****************************************************************************/
/* Copyright 2016 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.  
 * Change log
 * 18MAY BDJ 
 *  -Adding rotational maintenance function
 *  -switch baud to 19200 so straight through programming comms
 *  
 * 18MAY16 BDJ Last touched                                                 */
/****************************************************************************/

//#include <p24fxxxx.h>
//#include <p24FV16KM202.h>
#include <stdio.h>
#include <string.h>
#include <xc.h>
#include "uart1.h"
#include "timer.h"
#include "eeprom.h"
#include "elmo.h"

#define VER "1.2"

/* general definitions */
#include "typedefs.h" //True/False, UINT16, etc...


// Configuration Bits to make the part run from Internal FRCDIV
// Oscillator.



// PIC24FV16KM202 Configuration Bit Settings

// 'C' source line config statements

// FBS
#pragma config BWRP = OFF               // Boot Segment Write Protect (Disabled)
#pragma config BSS = OFF                // Boot segment Protect (No boot program flash segment)

// FGS
#pragma config GWRP = OFF               // General Segment Write Protect (General segment may be written)
#pragma config GSS0 = OFF                // General Segment Code Protect (No Protection)

// FOSCSEL
#pragma config FNOSC = FRC              // Oscillator Select (Fast RC Oscillator (FRC))
#pragma config SOSCSRC = ANA            // SOSC Source Type (Analog Mode for use with crystal)
#pragma config LPRCSEL = LP             // LPRC Oscillator Power and Accuracy (Low Power, Low Accuracy Mode)
#pragma config IESO = ON                // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))

// FOSC
#pragma config POSCMOD = NONE           // Primary Oscillator Configuration bits (Primary oscillator disabled)
#pragma config OSCIOFNC = 1          // CLKO Enable Configuration bit (CLKO output signal enabled)
#pragma config POSCFREQ = HS            // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8MHz)
#pragma config SOSCSEL = SOSCHP         // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Both Clock Switching and Fail-safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPS = PS32768          // Watchdog Timer Postscale Select bits (1:32768)
#pragma config FWPSA = PR128            // WDT Prescaler bit (WDT prescaler ratio of 1:128)
#pragma config FWDTEN = OFF              // Watchdog Timer Enable bits (WDT enabled in hardware)
#pragma config WINDIS = OFF             // Windowed Watchdog Timer Disable bit (Standard WDT selected(windowed WDT disabled))

// FPOR
#pragma config BOREN = BOR3             // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware, SBOREN bit disabled)
//#pragma config RETCFG = 1             //  (Retention regulator is not available)
#pragma config PWRTEN = ON              // Power-up Timer Enable bit (PWRT enabled)
#pragma config I2C1SEL = PRI            // Alternate I2C1 Pin Mapping bit (Use Default SCL1/SDA1 Pins For I2C1)
//#pragma config BORV = V18               // Brown-out Reset Voltage bits (Brown-out Reset set to lowest voltage (1.8V))
#pragma config MCLRE = ON               // MCLR Pin Enable bit (RA5 input pin disabled, MCLR pin enabled)

// FICD
#pragma config ICS = PGx3               // ICD Pin Placement Select bits (EMUC/EMUD share PGC3/PGD3)

//globals
unsigned long AlarmCnt;//Global incremented in AlarmISR






int main(void)
{
    //unsigned int i;
    CHAR ch;
    int position=-1;
    
    
    // Set up output pin for LED
    TRISAbits.TRISA0 = 0;
    
    vInitTimer1();    // Initialization of Timer1
    
    // init uarts
    vInitU1(); //main comms to vehicle
    vInitU2(); //comms to elmo
    
    //get the last valid position
    position = eeRead(ePOS);
    
    //make sure its set in the elmo
    elmo_UI_set(position);
    
    //LED ON means good power up
    LATAbits.LATA0 = 1; //leave LED on
    
 //               while(1)
 //           {        position++;
 //                    eeWrite(position);
 //                   printf("EEPROM=%d\n",eeRead());
 //                   DelaySecs (60);
 //           }
    
    printf("%s\n",VER);
    while(1)
    {   
      printf("S>");     //prompt 
      while (!U1STAbits.URXDA); //wait for char
      ch = cGetCharU(1); //Read one character form UART1
      printf("%c\n",ch);  //echo
        
      switch(ch) //====================================================
       { // and depending upon the ch, do the appropriate action =======
         case '?' :  break; // causes directory prompt to be displayed
         case ' ' :  break; // wait for another character
         case  -1 :  break; // wait for another character
     
         case 'P' :  // read position
                    position = eeRead(ePOS);
                    printf("EEPROM=%d, Elmo UI[1]=%d\n",position,elmo_UI_get());
	                break;
         case 'C' :  // clear position
                    position = 0;
                    eeWrite(position,ePOS);
                    printf("EEPROM=%d\n",eeRead(ePOS));
                    elmo_UI_set(position);
	                break;
         case '+' :  //increase position
                    position++;
                     eeWrite(position,ePOS);
                    printf("EEPROM=%d\n",eeRead(ePOS));
                    elmo_UI_set(position);
                    break;
        case '-' :  //decrease position
                    position--;
                     eeWrite(position,ePOS);
                    printf("EEPROM=%d\n",eeRead(ePOS)); 
                    elmo_UI_set(position);
	                break; 
        case 'S' : //sample!!!   
                    elmo_sample();
                    break;
        case 'E' : //talk directly to the elmo (power cycle to return)  
                    elmo_passthru();
                    break;
        case 'R' : //rotate carousel 1x           
                    elmo_rotational_maint();
                    break;         
        case '1' : //change pump time  
                    elmo_change_pump_time();
                    printf("New pump time (s) =%d\n",eeRead(ePUMP));
                    break;         
        case 'T' : //test code chunks
                    printf("EEPROM=%d\n",eeRead(ePUMP));
                    elmo_change_pump_time();
                    printf("EEPROM=%d\n",eeRead(ePUMP));
                    break;          
        default  :  printf(" input unknown \n");

       } //==============================================================
        
    }
}




