//*****************************************************************************
// File Name    : gpstest.c
// 
// Title                : example usage of gps processing library functions
// Revision             : 1.0
// Notes                : 
// Target MCU   : Atmel AVR series
// Editor Tabs  : 4
// 
// Revision History:
// When                 Who                     Description of change
// -----------  -----------     -----------------------
// 10-Sep-2002  pstang          Created the program
//*****************************************************************************

//----- Include Files ---------------------------------------------------------
#include <avr/io.h>             // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h>      // include interrupt support
//#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "global.h"             // include our global settings
#include "uart2.h"              // include dual-uart function library
#include "rprintf.h"    // include printf function library
#include "timer.h"              // include timer function library (timing, PWM, etc)
#include "gps.h"                // include gps data support
//#include "tsip.h"             // include TSIP gps packet handling
#include "nmea.h"               // include NMEA gps packet handling
#include "vt100.h"              // include VT100 terminal commands
//#include "utm.h"              // Lat Lon to UTM conversion

#include <util/delay.h>

// LCD Library
#include "lcd_hd44780.h"

static int lcd_putc_stream(char c, FILE *unused)
{
        return lcd_putc(c);
}

// Define Output Stream to LCD
static FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putc_stream, NULL, _FDEV_SETUP_WRITE);

// uartRxOverflow is a global variable defined in uart.c/uart2.c
// we define it here as <extern> here so that we can use its value
// in code contained in this file
extern unsigned short uartRxOverflow[2];

void gpsTsipTest(void);
void gpsNmeaTest(void);


//----- Begin Code ------------------------------------------------------------
int main(void)
{
        sbi(DDRC, 0);           // sets PC0 to be an output
        cbi(PORTC, 0);          // sets PC0 to output a HIGH
        _delay_ms(100);
        sbi(PORTC, 0);          // sets PC0 to output a LOW
        _delay_ms(100);
        cbi(PORTC, 0);          // sets PC0 to output a HIGH
        _delay_ms(100);
        sbi(PORTC, 0);          // sets PC0 to output a LOW
        _delay_ms(100);
        // initialize our libraries
        // initialize the UART (serial port)
//      uartInit();
        uart1Init();

        sbi(DDRC, 1);           // sets PC0 to be an output
        cbi(PORTC, 1);          // sets PC0 to output a HIGH
        _delay_ms(100);
        sbi(PORTC, 1);          // sets PC0 to output a LOW
        _delay_ms(100);
        cbi(PORTC, 1);          // sets PC0 to output a HIGH
        _delay_ms(100);
        sbi(PORTC, 1);          // sets PC0 to output a LOW
        _delay_ms(100);

        // set the baud rate of UART 0 for our debug/reporting output
//      uartSetBaudRate(0,9600);
        // set uart0SendByte as the output for all rprintf statements
//      rprintfInit(uart0SendByte);

        // initialize the timer system
        timerInit();
        // initialize vt100 library
//      vt100Init();
        
        // print a little intro message so we know things are working
//      vt100ClearScreen();
//      rprintf("\r\nWelcome to GPS Test!\r\n");
//      timerPause(1000);

        lcd_init();                                     // Init LCD (interface and display module)
        rprintfInit(lcd_putc);
        rprintfProgStrM("Ahoj...xxxxx"); rprintfCRLF();
        _delay_ms(500);
        lcd_clear();

        // run example gps processing loop
        // (pick the one appropriate for your GPS packet format)
        gpsNmeaTest();
        
        return 0;
}


void gpsNmeaTest(void)
{
        // set the baud rate of UART 1 for NMEA
        uartSetBaudRate(1,4800);

        // clear screen
//      vt100ClearScreen();
        // initialize gps library
        gpsInit();
        // initialize gps packet decoder 
        nmeaInit();

        DDRD = 0b10100000;      // sets PD7 O, PD6 I, PD5 O
        cbi(PORTD, 7);          // sets PD7 to output a LOW
        sbi(PORTD, 5);          // sets PD5 to output a HIGH

        // begin gps packet processing loop
        while(1)
        {
                // process received gps packets until receive buffer is exhausted
                while( nmeaProcess(uartGetRxBuffer(1))== NMEA_NODATA);
                if((inb(PIND) & 0b01000000)==0)
                        gpsInfoPrintLCD();
                else
                        gpsInfoPrintLCD2();

/*
                sbi(DDRC, 0);           // sets PC0 to be an output
                cbi(PORTC, 0);          // sets PC0 to output a LOW
                _delay_ms(5);
                sbi(PORTC, 0);          // sets PC0 to output a HIGH
*/
        }
}