/****************************************************************************/
/**
* \file dcf77.h
*
* \brief Contains the routine for the time data extraction from a DCF77
*        module.
*
* \author   Michael Skutek
* \date	    11.09.2011
* \version  0.4
*
* Platform:    AVR ATmega (tested on ATmega168)
*
* Include Files:
*       - avr/io.h:             port accessing (part of avr-gcc)
*       - dcf77.h:              all internal definitions
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
****************************************************************************/



#ifndef DCF77_H_INCLUDED
#define DCF77_H_INCLUDED


/******************************************
	\internal
	Internal value definitions
*******************************************/

#define DCF77_UNINIT_VALUE                     0xff          //!< uninitialized value (Do not change!)

#define DCF77_SYNC_START_MIN                    197          //!< minimum time for start sync cycle [ms * 10]
#define DCF77_SYNC_START_MAX                    203          //!< maximum time for start sync cycle [ms * 10]

#define DCF77_SYNC_MIN                           97          //!< minimum time for bit cycle [ms * 10]
#define DCF77_SYNC_MAX                          103          //!< maximum time for bit cycle [ms * 10]

#define DCF77_HIGH_MIN                           17          //!< minimum time for symbol 1 [ms * 10]
#define DCF77_HIGH_MAX                           23          //!< minimum time for symbol 1 [ms * 10]

#define DCF77_LOW_MIN                             7          //!< minimum time for symbol 0 [ms * 10]
#define DCF77_LOW_MAX                            13          //!< minimum time for symbol 0 [ms * 10]

//DCF77 signal "active low" or "active high" definition
#define DCF77_ACTIVE_LOW                          0          //!< signal is "active low"
#define DCF77_ACTIVE_HIGH                         1          //!< signal is "active high"


/******************************************
	\internal
	Command definitions
*******************************************/

#define DCF77_RESET_TIME                          0          //!< reset command (start new time synchronization)
#define DCF77_GET_TIME                            1          //!< normal operation (get time)


/******************************************
	\internal
	Weekday definitions
*******************************************/

#define DCF77_MONDAY                              1          //!< value for monday
#define DCF77_TUESDAY                             2          //!< value for tuesday
#define DCF77_WEDNESDAY                           3          //!< value for wednesday
#define DCF77_THURSDAY                            4          //!< value for thursday
#define DCF77_FRIDAY                              5          //!< value for friday
#define DCF77_SATURDAY                            6          //!< value for saturday
#define DCF77_SUNDAY                              7          //!< value for sunday


/******************************************
	\internal
	Internal state definitions (return values
  of dcf77_sync())
*******************************************/

#define DCF77_STARTSYNC_SEARCH01                  0          //!< first part of start sync search (first rising edge)
#define DCF77_STARTSYNC_SEARCH02                  1          //!< second part of start sync search (second rising edge)
#define DCF77_STARTSYNC_COMPLETED                 2          //!< start sync completed (start of new minute)
#define DCF77_TIMESYNC_SEARCH01                   3          //!< first part of time sync search (rising edge)
#define DCF77_TIMESYNC_SEARCH02                   4          //!< second part of time sync search (falling edge)
#define DCF77_MINUTE_COMPLETED                    5          //!< time synchronization completed for minute and real time structure updated
#define DCF77_HOUR_COMPLETED                      6          //!< time synchronization completed for hour and real time structure updated
#define DCF77_DAY_COMPLETED                       7          //!< time synchronization completed for day and real time structure updated
#define DCF77_MONTH_COMPLETED                     8          //!< time synchronization completed for month and real time structure updated
#define DCF77_YEAR_COMPLETED                      9          //!< time synchronization completed for year and real time structure updated
#define DCF77_WEEKDAY_COMPLETED                  10          //!< time synchronization completed for weekday and real time structure updated
#define DCF77_TIMESYNC_COMPLETED                 11          //!< time synchronization completed and real time structure updated



/******************************************
	\internal
	Structure definitions
*******************************************/

typedef struct real_time_struc {
  uint8_t current_hour;                                      //!< contains the value for the current hour [0-23]
  uint8_t current_minute;                                    //!< contains the value for the current minute [0-59]
  uint8_t current_second;                                    //!< contains the value for the current second [0-59]
  uint8_t prev_second;                                       //!< contains the previous second value [0-59]

  uint8_t current_year;                                      //!< contains the value for the current year [2000-2099]
  uint8_t current_month;                                     //!< contains the value for the current month [0-12]
  uint8_t current_day;                                       //!< contains the value for the current day [0-31]
  uint8_t current_weekday;                                   //!< contains the value for the current weekday name [1-7]
} real_time_data;


/******************************************
	\internal
	Function prototypes
*******************************************/

uint8_t dcf77_sync(uint8_t, uint8_t, real_time_data *, uint8_t, uint8_t);


#endif // DCF77_H_INCLUDED
