/**
 * \file
 *
 * \brief Sleep Walking Hands-on
 * after the application initialization function, the user will use
 * these buttons to change the state of the sequential state machine
 * implemented in the code as follow:
 * After having set the Clock
 * •   CS0 used to wake up the SAM4L device from the Sleepwalking™ mode
 * •   PB0 used to go back in Sleepwalking™.
 * The 7segments LCD display is used to display the state if the state
 *  machine is in Sleepwalking or in Active State
 * At power up, the application jumps directly in Sleepwalking state
 * with the Peripheral Event System enabled, allowing the
 * interconnection between peripherals. 2 different configurations
 * (the last one is optional) are implemented in the code:
 * •  1 generator and 1 user:
 * •	AST as generator
 * •	CATB as user
 * •  1 generator and 2 users (optional)
 * •	AST as generator
 * •	CATB as user
 * •	ADCIFE as user
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * Bare minimum empty user application template
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# Minimal main function that starts with a call to board_init()
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
/***********************************************************************/
/*                          Include                                    */
/***********************************************************************/
#include <asf.h>
#include "string.h"

/* From module: QTouch Library for SAM4L */
#include "touch_api_sam4l.h"

/***********************************************************************/
/*                          Define                                     */
/***********************************************************************/
/* Main sequence State definitions */
#define INIT_SLEEPWALKING               0
#define SLEEPWALKING                    1
#define INIT_ACTIVE_MODE                2
#define ACTIVE_MODE                     3

#define NORMAL       0 /* Default state */
#define SET_HOUR     1 /* Hour value set state */
#define SET_MINUTE   2 /* Minute value set state */
#define LOW_POWER    3 /* Low power state */

#define LCD_CONTRAST_LEVEL 5

extern volatile char seq_state;

/* Initial values of the CALV register used for the AST calendar Mode */
extern struct ast_calendar calendar;

/**
 * Initialize the LCDA for the example.
 */
extern void init_lcda(void);

/**
 * Function used to switch the main clock from PLL0 using OSC0
 * to RCFAST oscillator
 */
extern void switch_to_slower_clock(void);

/**
 * Function used to switch the main clock from  RCFAST oscillator 
 * to PLL0 using OSC0
 */
extern void switch_to_full_speed_clock(void);

/**
 * Set the clock time.
 */
extern void set_clock_time(void);

/**
 * Initialize the External Interrupt Controller for the example.
 */
extern void init_eic(void);

/**
 * Initialize the Peripheral Event Controller for the CATB.
 */
extern void CATB_PEVC_init(void);


/**
 * CATB intouch callback.
 */
extern void CATB_callback_sleepwalking(void);

/**
 * Initialize the PDCA transfer for the example.
 */
extern void init_pdca(void);

/**
 * Initialize the Peripheral Event Controller for PDCA.
 */
extern void PDCA_PEVC_init(void);

/**
 * Initialize the Asynchronous Timer (AST) in calendar mode using 
 * the 32Khz oscillator as clock source.
 */
extern void init_ast(void);

