/*    mija 2008 
      demo for LCD NOKIA5110 and MCP9800 and GPS modul

          CPU ATMEGA644P
      fcpu = 1MHz

          !! define PIN,PORT,DDR for IOpin !!
*/

#include <stdint.h>

#define MAX_RX_BUF                              150
#define MAX_NMEA_LOAD                   100

// PIN TL1
#define TL1             PB4             // input 
 #define TL1_DDR        DDRB
 #define TL1_PORT       PORTB
 #define TL1_PIN        PINB

// PIN TL2
#define TL2             PB2             // input //int2
 #define TL2_DDR        DDRB
 #define TL2_PORT       PORTB
 #define TL2_PIN        PINB

// PIN TL3
#define TL3             PB3             // input  
 #define TL3_DDR        DDRB
 #define TL3_PORT       PORTB
 #define TL3_PIN        PINB

// PIN GPS
#define GPS             PD4             
 #define GPS_DDR        DDRD
 #define GPS_PORT       PORTD

// UART SW
#define USB                     PD5             // input 
 #define USB_DDR        DDRD
 #define USB_PORT       PORTD
 #define USB_PIN        PIND

// i2C SW
#define SDA                     PC1
 #define SDA_PORT       PORTC
 #define SDA_DDR        DDRC
 #define SDA_PIN        PINC

#define SCL                     PC0
 #define SCL_PORT       PORTC
 #define SCL_DDR        DDRC

// LED
#define LED             PD7
 #define LED_PORT       PORTD
 #define LED_DDR        DDRD

// Vref
#define REF                     PA0
 #define REF_PORT       PORTA
 #define REF_DDR        DDRA

// interni
#define TL1_INIT        TL1_DDR &= ~(_BV(TL1))
#define TL1_INPUT       (TL1_PIN & _BV(TL1))
#define TL1_PULLUP      TL1_PORT |= _BV(TL1)

#define TL2_INIT        TL2_DDR &= ~(_BV(TL2))
#define TL2_INPUT       (TL2_PIN & _BV(TL2))
#define TL2_PULLUP      TL2_PORT |= _BV(TL2)

#define TL3_INIT        TL3_DDR &= ~(_BV(TL3))
#define TL3_INPUT       (TL3_PIN & _BV(TL3))
#define TL3_PULLUP      TL3_PORT |= _BV(TL3)

#define GPS_OFF         GPS_PORT |= _BV(GPS)
#define GPS_ON          GPS_PORT &= (~(_BV(GPS)))
#define GPS_INIT        GPS_DDR |= _BV(GPS)
#define GPS_INPUT   (!(GPS_PORT & _BV(GPS)))

#define USB_INIT        USB_DDR &= ~(_BV(USB))
#define USB_INPUT       (USB_PIN & _BV(USB))
#define USB_PULLUP      USB_PORT |= _BV(USB)

#define SCL_INIT        SCL_DDR |= _BV(SCL)
#define SCL_L           SCL_PORT &= ~(_BV(SCL))
#define SCL_H           SCL_PORT |= _BV(SCL)

#define SDA_OUT         SDA_DDR |= _BV(SDA)
#define SDA_L           SDA_PORT &= ~(_BV(SDA))
#define SDA_H           SDA_PORT |= _BV(SDA)
#define SDA_IN          SDA_DDR &= ~(_BV(SDA))
#define SDA_INPUT       (SDA_PIN & _BV(SDA))
#define SDA_FLOAT       SDA_IN;SDA_L

#define LED_ON          LED_PORT |= _BV(LED)
#define LED_OFF         LED_PORT &= (~(_BV(LED)))
#define LED_INIT        LED_DDR |= _BV(LED)
#define LED_INPUT   (LED_PORT & _BV(LED))

#define REF_ON          REF_PORT |= _BV(REF)
#define REF_OFF         REF_PORT &= (~(_BV(REF)))
#define REF_INIT        REF_DDR |= _BV(REF)

#define ADC_ON          ADCSRA |= _BV(ADEN)
#define ADC_OFF         ADCSRA &= ~(_BV(ADEN))

#define USART_PC_ON     UCSR1B = _BV(RXCIE1)  | _BV(RXEN1) | _BV(TXEN1)
#define USART_PC_OFF            UCSR1B = 0

#define KEY1            0
#define KEY2            1
#define KEY3            2

#define KEY1_LONG       1
#define KEY2_LONG       2
#define KEY3_LONG       3
#define KEY1_SHORT      4
#define KEY2_SHORT      5
#define KEY3_SHORT      6

#define ID_OFF          100
#define ID_START        101
#define ID_SETUP        102

#ifndef TYPEDEF_OK
        //typedef struct{uint8_t a; uint8_t b}GPS_SEND;
        typedef struct
        {
                uint8_t sec;
                uint8_t min;
                uint8_t hour;
        }TIME_T;

        typedef struct
        {
                uint8_t day;
                uint8_t mon;
                uint8_t year;
        }DATE_T;

typedef struct
{
        double lat;
        double lon;
        double alt;
        double speed;
        double course;
        int16_t temperature;
        TIME_T time;
        DATE_T date;
}POINT_T;

typedef struct
{
        uint8_t id;
        uint8_t elevation;
        uint16_t azimut;
        uint8_t SNR;
}SATELITE_DETAIL;

typedef struct
{
        //GGA
        uint8_t fix_position;
        uint8_t satelites_used;
        double altitude;
        double geoid;
        uint16_t age_diff_corr;
        uint16_t diff_id;
        //GSA
        char mode1;
        char mode2;
        uint8_t satelite_id[12];
        double PDOP;
        double HDOP;
        double VDOP;
        //GSV
        uint8_t gsv_num_msg;
        uint8_t gsv_msg; 
        uint8_t gsv_satelites_view;
        SATELITE_DETAIL satelit_detail[12];
        //RMC
        uint8_t second;
        uint8_t minute;
        uint8_t hour;
        uint8_t day;
        uint8_t month;
        uint8_t year;
        double latitude;
        double longitude;
        char ns_indicator;
        char we_indicator;
        char status;
        //VTG
        double course;
        double speed;
}DATA_GPS;

#endif

#define TYPEDEF_OK