/****************************************************************************/
/* Copyright 2004 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.                                                     */
/****************************************************************************/
#if defined(__PCM__)
#include <16f873.h>
#fuses XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=3580000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use fast_io(C)
#endif

#include <stdlib.h>
#include <string.h>
#include "freq_gen.h"

/********************************** GLOBS ***********************************/

long rtccTicks = 0;
int freqDuration = 0;
int freqEnabled = FALSE;
#byte portB = 0x06
#byte portC = 0x07

/********************************** GLOBS ***********************************/

#INT_RTCC
rtccIsr()
{
    if ( !freqEnabled )
    {
        rtccTicks = 0;
        /* stay in process commands */
        freqDuration = 0;
        return;
    }

    rtccTicks++;

    if ( FREQ_DURATION == rtccTicks )
    {
        rtccTicks = 0;
        ++freqDuration;
    }
}

void enable()
{
    freqEnabled = TRUE;
    printf("freq gen enabled\r\n");
    /*portB = PORTB_LED_ON;*/
	return;
}

void disable()
{
    freqEnabled = FALSE;
    printf("freq gen disabled\r\n");
    /*portB = PORTB_LED_OFF;*/
    return;
}

void processCmds()
{
    int b, c;

    b = portB & 0x01;

    if (!b && !freqEnabled)
    {
		enable();
    }

    else if (b && freqEnabled)
    {
		disable();
    }

    else if ( kbhit() )
    {
        c = getc();

        switch ( toupper(c))
        {
            case '1':   enable();
                        break;
            case '2':   disable();
                        break;
            case '?':   printf("1 - enable freq gen\r\n");
                        printf("2 - disable freq gen\r\n");
                        printf("? - help menu\r\n");
                        break;
        }
    }

    return;
}

main()
{
    /* setup port b bit 0 as input, 1 as output */
    set_tris_b(0x01);

    /* setup port c bits 0-6 as outputs, 7 as input */
    set_tris_c(0x80);

    /* init freq pins */
    portC = PORTC_CLEAR_ALL;

    /* setup the system clock */
    setup_counters(RTCC_INTERNAL, RTCC_DIV_64);

    /* make sure interrupts are enabled */
    enable_interrupts(INT_RTCC);
    enable_interrupts(GLOBAL);

    /* welcome message */
    printf("Freq Gen version %d.%d\r\n",
            FREQ_GEN_VERSION, FREQ_GEN_SUB_VERSION);
    /*
    while(freqDuration <=2)
	{
		portB = PORTB_LED_ON;		
		delay_ms(100);
		portB = PORTB_LED_OFF;
	}*/

    /* main loop */
    for(;;)
    {
        /* wait for things to settle down */
        delay_ms(50);

        /* process any queued cmds */
        while ( freqDuration <= 5 )
        processCmds();

		/* Magnetic source output */

        /* 1000 Hz */
        while ( freqDuration == 6 )
        {
            /* wave up */
            portC = PORTC_1000HZ_FREQ_UP;
            delay_us(500);
            /* wave down */
            portC = PORTC_1000HZ_FREQ_DN;
            delay_us(490);
        }

        /* 316 Hz */
        while ( freqDuration == 7 )
        {
            /* wave up */
            portC = PORTC_316HZ_FREQ_UP;
            delay_us(1499);
            /* wave down */
            portC = PORTC_316HZ_FREQ_DN;
            delay_us(1491); /* (8 us delay) */
        }

        /* 100 Hz */
        while ( freqDuration == 8 )
        {
            /* wave up */
            portC = PORTC_100HZ_FREQ_UP;
            delay_ms(5);
            /* wave down */
            portC = PORTC_100HZ_FREQ_DN;
            delay_ms(5);
        }


        /* clear all the pins */
        portC = PORTC_CLEAR_ALL;

        /* wait for things to settle down */
        delay_ms(50);

        /* process any queued cmds */
        while ( freqDuration <= 14 )
        processCmds();

		/* Current source output */

        /* 1000 Hz */
        while ( freqDuration == 15 )
        {
            /* wave up */
            portC = PORTC_CURRENT_UP;
            delay_us(500);
            /* wave down */
            portC = PORTC_CURRENT_DN;
            delay_us(490);
        }

        /* 316 Hz */
        while ( freqDuration == 16 )
        {
            /* wave up */
            portC = PORTC_CURRENT_UP;
            delay_us(1499);
            /* wave down */
            portC = PORTC_CURRENT_DN;
            delay_us(1491); /* (8 us delay) */
        }

        /* 100 Hz */
        while ( freqDuration == 17 )
        {
            /* wave up */
            portC = PORTC_CURRENT_UP;
            delay_ms(5);
            /* wave down */
            portC = PORTC_CURRENT_DN;
            delay_ms(5);
        }

        /* clear all the pins */
        portC = PORTC_CLEAR_ALL;

        /* do it again ! */
        freqDuration = 0;
    }
}
