Defines | Enumerations | Functions | Variables

main.c File Reference

XMEGA-A1 Xplained demo application. More...

#include <board.h>
#include <compiler.h>
#include <gpio.h>
#include <nvm.h>
#include <sysclk.h>
#include <tc.h>
#include <conf_example.h>
#include "sound.h"
#include "snd_samples.h"
#include <util/delay.h>
Include dependency graph for xmega/applications/xmega_a1_xplained_demo/main.c:

Defines

#define F_CPU   sysclk_get_cpu_hz()
#define SEQUENCE_BUFFER_MAX_LENGTH   255
 Maximum length of buffer for button press sequence.
#define SEQUENCE_HEADER_LENGTH   2
 Length of header for saving button presses to EEPROM.

Enumerations

enum  game_mode {
  GAME_RECORD_MODE,
  GAME_PLAY_MODE
}
 

Enumeration of game modes.

More...
enum  sound_mode {
  SOUND_SAMPLES,
  SOUND_SINEWAVE
}
 

Enumeration of sound playback modes.

More...

Functions

int main (void)
 Main function.
void timeout_callback (void)
 Timer/counter overflow interrupt callback function.
Internal application functions
static void start_debounce_timer (void)
 Set up and start timer for debouncing of the push buttons.
static void init_save_timer (void)
 Set up timer for measuring inactivity before saving.
static void start_save_timer (void)
 Start timer for measuring inactivity before saving.
static void play_game (uint8_t buttons, enum sound_mode soundmode, enum game_mode *gamemode)
 Compares button presses with stored game data.
static void record_buttons (uint8_t buttons, enum sound_mode soundmode)
 Record button presses and play sound.
static void save_gamedata (void)
 Save current game data to EEPROM.
static void load_gamedata (void)
 Load game data from EEPROM.

Variables

static volatile uint8_t save_timeout
 Counter for timeout-period before saving button sequence.
uint8_t sequence_buffer [SEQUENCE_BUFFER_MAX_LENGTH]
 Buffer to contain the button press sequence.
uint8_t sequence_buffer_length
 Length of currently recorded button sequence in buffer.

Detailed Description

XMEGA-A1 Xplained demo application.

Copyright (C) 2010-2011 Atmel Corporation. All rights reserved.


Define Documentation

#define F_CPU   sysclk_get_cpu_hz()
#define SEQUENCE_BUFFER_MAX_LENGTH   255

Maximum length of buffer for button press sequence.

Referenced by load_gamedata(), main(), and save_gamedata().

#define SEQUENCE_HEADER_LENGTH   2

Length of header for saving button presses to EEPROM.

Referenced by main(), play_game(), and record_buttons().


Enumeration Type Documentation

enum game_mode

Enumeration of game modes.

Enumerator:
GAME_RECORD_MODE 

Record button presses for the game.

GAME_PLAY_MODE 

Play game with recorded button sequence.

enum sound_mode

Enumeration of sound playback modes.

Enumerator:
SOUND_SAMPLES 

One-shot playback of samples.

SOUND_SINEWAVE 

Looped playback of sinewaves.


Function Documentation

void init_save_timer ( void  ) [static]

Set up timer for measuring inactivity before saving.

The timer/counter is set up in normal, up-counting mode with a period of approximately 1.6 seconds.

References PMIC_LVL_LOW, sysclk_get_per_hz(), tc_enable(), tc_set_overflow_interrupt_callback(), tc_set_overflow_interrupt_level(), tc_write_period(), and timeout_callback().

Referenced by main().

Here is the call graph for this function:

void load_gamedata ( void  ) [static]

Load game data from EEPROM.

References nvm_eeprom_read_byte(), sequence_buffer, and SEQUENCE_BUFFER_MAX_LENGTH.

Referenced by main().

Here is the call graph for this function:

int main ( void  )

Main function.

Initializes the board, checks the jumper configuration and sets up the game accordingly before the main loop is entered. The main loop is where the state of the board buttons is read out and passed on to the game functions record_buttons or play_game, depending on the state of the game.

References board_init(), cpu_irq_enable, GAME_PLAY_MODE, GAME_RECORD_MODE, gpio_configure_pin, gpio_pin_is_low, init_save_timer(), IOPORT_DIR_INPUT, IOPORT_DIR_OUTPUT, IOPORT_INIT_LOW, IOPORT_PULL_UP, load_gamedata(), play_game(), pmic_enable_level(), PMIC_LVL_LOW, record_buttons(), save_gamedata(), save_timeout, sequence_buffer, sequence_buffer_length, SEQUENCE_BUFFER_MAX_LENGTH, SEQUENCE_HEADER_LENGTH, sound_init(), SOUND_SAMPLES, SOUND_SINEWAVE, start_debounce_timer(), start_save_timer(), and sysclk_init.

Here is the call graph for this function:

void play_game ( uint8_t  buttons,
enum sound_mode  soundmode,
enum game_mode gamemode 
) [static]

Compares button presses with stored game data.

Uses a timer to debounce buttons. The method used requires the function to be called often, but not necessarily regularly.

Parameters:
buttonsMask with pressed buttons.
soundmodeIndicates whether to play a sound sample or sinewave.
gamemodeGame mode pointer -- the mode is changed if the game is won.

References GAME_RECORD_MODE, save_timeout, sequence_buffer, sequence_buffer_length, SEQUENCE_HEADER_LENGTH, sound_is_playing(), sound_play_note(), sound_play_sample(), SOUND_SAMPLES, SOUND_SINEWAVE, sound_stop_timer(), start_save_timer(), tc_clear_overflow(), and tc_is_overflow().

Referenced by main().

Here is the call graph for this function:

void record_buttons ( uint8_t  buttons,
enum sound_mode  soundmode 
) [static]

Record button presses and play sound.

Note:
This modifies the global variables sequence_buffer and sequence_buffer_length.
Parameters:
buttonsMask with pressed buttons.
soundmodeIndicates whether to play a sound sample or sinewave.

References sequence_buffer, sequence_buffer_length, SEQUENCE_HEADER_LENGTH, sound_play_note(), sound_play_sample(), SOUND_SAMPLES, SOUND_SINEWAVE, tc_clear_overflow(), and tc_is_overflow().

Referenced by main().

Here is the call graph for this function:

void save_gamedata ( void  ) [static]

Save current game data to EEPROM.

References nvm_eeprom_atomic_write_page(), nvm_eeprom_flush_buffer(), nvm_eeprom_load_page_to_buffer(), sequence_buffer, sequence_buffer_length, and SEQUENCE_BUFFER_MAX_LENGTH.

Referenced by main().

Here is the call graph for this function:

void start_debounce_timer ( void  ) [static]

Set up and start timer for debouncing of the push buttons.

The timer/counter is set up in normal, up-counting mode with a period of approximately 10 ms.

References tc_enable(), tc_get_resolution(), tc_set_resolution(), tc_set_wgm(), TC_WG_NORMAL, and tc_write_period().

Referenced by main().

Here is the call graph for this function:

void start_save_timer ( void  ) [static]

Start timer for measuring inactivity before saving.

The timer/counter should be initialized with init_save_timer before this function is called.

References tc_write_clock_source().

Referenced by main(), and play_game().

Here is the call graph for this function:

void timeout_callback ( void  )

Timer/counter overflow interrupt callback function.

This updates the variable for the save timeout and stops its timer/counter once timeout has occured.

Note:
Modifies global variable save_timeout.

References save_timeout, and tc_write_clock_source().

Referenced by init_save_timer().

Here is the call graph for this function:


Variable Documentation

volatile uint8_t save_timeout [static]

Counter for timeout-period before saving button sequence.

Referenced by main(), play_game(), and timeout_callback().

uint8_t sequence_buffer[SEQUENCE_BUFFER_MAX_LENGTH]

Buffer to contain the button press sequence.

Referenced by load_gamedata(), main(), play_game(), record_buttons(), and save_gamedata().

Length of currently recorded button sequence in buffer.

Referenced by main(), play_game(), record_buttons(), and save_gamedata().

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines