Microchip® Advanced Software Framework

xmega/applications/xmega_a1_xplained_demo/main.c File Reference

XMEGA-A1 Xplained demo application.

Copyright (c) 2010-2018 Microchip Technology Inc. and its subsidiaries.

#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 <util/delay.h>

Macros

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

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

Variables

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

#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().

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.

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().

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().

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().

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().

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().

void save_gamedata ( void  )
static
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().

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().

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 occurred.

Note
Modifies global variable save_timeout.

References save_timeout, and tc_write_clock_source().

Referenced by init_save_timer().

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().

uint8_t sequence_buffer_length

Length of currently recorded button sequence in buffer.

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