/*---------------------------------------------------------------------------
  Shinobi Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
---------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	<Ninja.h>
#include	"Nj.h"
#include	"perchk.h"
#include	"dbgcons.h"

/*-------------------------------------------------------------------------*/
#define TEXT_W   36           /* コンソールの横幅 Console width  */
#define TEXT_H    8           /* コンソールの行数 Console height */


/*-------------------------------------------------------------------------*/
void PrintPeripheralInfo(Sint32 x, Sint32 y, Uint32 port);
const char* GetPeriStr(Sint32 id);

/*-------------------------------------------------------------------------*/
Uint32 gFrameCount;
char TextBuf[TEXT_W * TEXT_H];
Uint32 gOldPeri;

/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	gFrameCount = 0;
	njSetBackColor(0x00004080, 0x00004080, 0x004080);
	ConInit(2, 20, TEXT_W, TEXT_H, TextBuf);
	gOldPeri = PER_NONE;

	return USER_CONTINUE;
}

/*---------------------------------------------------------------------------
		終了処理
		exit
---------------------------------------------------------------------------*/
Sint32	UserExit(void)
{
	return USER_CONTINUE;
}

/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	const PDS_PERIPHERAL* per;
	Uint32 controller;

	njPrintC(NJM_LOCATION(2, 2), "PERIPHERAL CHECK");
	njPrintH(NJM_LOCATION(30, 2), gFrameCount, 8);

	PrintPeripheralInfo(4, 8, PDD_PORT_A0);

	controller = CheckController(PDD_PORT_A0);

	njPrintC(NJM_LOCATION(14, 6), GetPeriStr(controller));

	if (gOldPeri != controller) {
		if (controller == PER_NONE) {
			ConPrintf("DISCONNECTED.\n");
		} else {
			ConPrintf("%s CONNECTED.\n", GetPeriStr(controller));
		}
	}

	ConDispCursor();
	ConMain();


	gOldPeri = controller;

	gFrameCount++;

	return USER_CONTINUE;
}

/*---------------------------------------------------------------------------
		ペリフェラル情報の表示
		print peripheral info
---------------------------------------------------------------------------*/
void PrintPeripheralInfo(Sint32 x, Sint32 y, Uint32 port)
{
	const PDS_PERIPHERAL* per;

	per = pdGetPeripheral(port);

	njPrintC(NJM_LOCATION(x, y +  0), "SUPPORT:");
	njPrintC(NJM_LOCATION(x, y +  1), "ON     :");
	njPrintC(NJM_LOCATION(x, y +  2), "OFF    :");
	njPrintC(NJM_LOCATION(x, y +  3), "PRESS  :");
	njPrintC(NJM_LOCATION(x, y +  4), "RELEASE:");
	njPrintC(NJM_LOCATION(x, y +  5), "R      :");
	njPrintC(NJM_LOCATION(x, y +  6), "L      :");
	njPrintC(NJM_LOCATION(x, y +  7), "X1     :");
	njPrintC(NJM_LOCATION(x, y +  8), "Y1     :");
	njPrintC(NJM_LOCATION(x, y +  9), "X2     :");
	njPrintC(NJM_LOCATION(x, y + 10), "Y2     :");

	x += 8;
	njPrintH(NJM_LOCATION(x, y), per->support, 8);
	njPrintH(NJM_LOCATION(x, y + 1), per->on, 8);
	njPrintH(NJM_LOCATION(x, y + 2), per->off, 8);
	njPrintH(NJM_LOCATION(x, y + 3), per->press, 8);
	njPrintH(NJM_LOCATION(x, y + 4), per->release, 8);
	njPrintD(NJM_LOCATION(x + (per->r  >= 0), y + 5), per->r, 5);
	njPrintD(NJM_LOCATION(x + (per->l  >= 0), y + 6), per->l, 5);
	njPrintD(NJM_LOCATION(x + (per->x1 >= 0), y + 7), per->x1, 5);
	njPrintD(NJM_LOCATION(x + (per->y1 >= 0), y + 8), per->y1, 5);
	njPrintD(NJM_LOCATION(x + (per->x2 >= 0), y + 9), per->x2, 5);
	njPrintD(NJM_LOCATION(x + (per->y2 >= 0), y + 10), per->y2, 5);
}

/*-------------------------------------------------------------------------*/
const char* GetPeriStr(Sint32 id)
{
	switch (id) {
		case PER_NONE:
			return "NO CONTROLLER\0";
		default:
		case PER_CONTROLLER:
			return "CONTROLLER\0";
		case PER_STICK:
			return "ARCADE STICK\0";
		case PER_RACING:
			return "RACING CONTROLLER\0";
		case PER_GUN:
			return "GUN CONTROLLER\0";
		case PER_KEYBOARD:
			return "KEYBOARD\0";
		case PER_FISHING:
			return "FISHING CONTROLLER\0";
		case PER_TWINSTICK:
			return "TWIN STICK\0";
		case PER_MARACAS:
			return "MARACAS\0";
		case PER_MOUSE:
			return "MOUSE\0";
	}
}
