/*---------------------------------------------------------------------------------

    based on

	Simple console print demo  (libnds 1.3.1)
	by dovoto

---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>

// include libxm7 header file
#include "libxm7.h"

// include the XM module (thanks Lasse!)
#include <Miner_bin.h>

// "reserve" FIFO Channel "FIFO_USER_07"
#define FIFO_XM7	(FIFO_USER_07)

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------

	consoleDemoInit();  //setup the sub screen for printing

	iprintf("\n\n  Welcome ;) to libXM7 example\n");
	iprintf("\n Press A to start and B to stop\n\n");
	
	u16 res, playing=0, count=0;
	
    // allocate memory for the module	
	XM7_ModuleManager_Type* Module = malloc (sizeof(XM7_ModuleManager_Type));

    // turn on master sound
	fifoSendValue32(FIFO_SOUND, SOUND_MASTER_ENABLE);

	while(1) {
	 
	    // read keys
	    scanKeys();
		
		if ((keysDown() & KEY_A)) {
		 
			if(playing)
			{
				fifoSendValue32(FIFO_XM7, 0);
				XM7_UnloadXM(Module);
			}
			 
			// load the XM 'attached' file
			res = XM7_LoadXM(Module, (XM7_XMModuleHeader_Type*)Miner_bin);
			
			// ensure data gets written to main RAM (good practice!)
			DC_FlushAll();
			
			if (res!=0) {
				iprintf("\n Loading went bad for error 0x%04x\n",res);
				return (1); 
			}
			
			// sending pointer to the libxm7 engine on ARM7
			fifoSendValue32(FIFO_XM7, (u32)Module);
			iprintf(" Playing... (%d)\n", count++);
			playing=1;
		}
		  
		  
		if ((keysDown() & KEY_B) && (playing!=0)) {
		  
		  // sending a ZERO to the libxm7 engine on ARM7
		  fifoSendValue32(FIFO_XM7, 0);
		  iprintf("STOP!\n\n");
		  playing=0;
		}
     
		swiWaitForVBlank();
	}
	
	// unload the module (in this example we never reach this point...)
	XM7_UnloadXM (Module);
	
	// turn off master sound
	fifoSendValue32(FIFO_SOUND, SOUND_MASTER_DISABLE);

	return 0;
}
