/* Standard Stellaris includes */
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"

#include "driverlib/udma.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/rom.h"
#include "drivers/sound.h"

#include "MySound.h"


// The DMA control structure table.
//*****************************************************************************
#ifdef ewarm
#pragma data_alignment=1024
tDMAControlTable sDMAControlTable[64];
#elif defined(ccs)
#pragma DATA_ALIGN(sDMAControlTable, 1024)
tDMAControlTable sDMAControlTable[64];
#else
tDMAControlTable sDMAControlTable[64] __attribute__ ((aligned(1024)));
#endif

#ifdef ENABLE_SOUND
	// The sound effect that is played when a key is pressed. 
	static const unsigned short g_pusKeyClick[] =
	{
		0, G5,
	    25, SILENCE
	};
	
	// The sound effect that is played when the correct code is entered.
	static const unsigned short g_pusAccessGranted[] =
	{
	    0, G5,
	    100, SILENCE
	};
	
	static const unsigned short g_pusAccessDenied[] =
	{
	    0, C5,
	    200, SILENCE
	};
#endif

void InitMySound(void)
{
#ifdef ENABLE_SOUND

    // Configure and enable uDMA
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
    SysCtlDelay(10);
    ROM_uDMAControlBaseSet(&sDMAControlTable[0]);
    ROM_uDMAEnable();

    // Initialize the sound driver.
    SoundInit();	
#endif
}

void SoundPlayKeyClick(void)
{
#ifdef ENABLE_SOUND
	SoundPlay(g_pusKeyClick, sizeof(g_pusKeyClick) / 2);
#endif
}

void SoundPlayFault(void)
{
	SoundPlayPassWrong();
}

void SoundPlayPassOK(void)
{
#ifdef ENABLE_SOUND
	SoundPlay(g_pusAccessGranted, sizeof(g_pusAccessGranted) / 2);
#endif
}

void SoundPlayPassWrong(void)
{
#ifdef ENABLE_SOUND
	SoundPlay(g_pusAccessDenied, sizeof(g_pusAccessDenied) / 2);
#endif
}

