/********************************************************************
 *  Shinobi Library Sample
 *  Copyright (c) 1998-2000 SEGA
 *
 *  Library : Peripheral
 *  Module  : Vibration
 *  File    : vib.c
 *  Date    : 2000-01-13
 *  Version : 2.00
 ********************************************************************/

#include <shinobi.h>
#include <ninja.h>
#include "vib.h"

/*-------------------------------------------------------------------------*/
/* マクロ定義 */

/*-------------------------------------------------------------------------*/
/* グローバル変数宣言 */

/*-------------------------------------------------------------------------*/
/* スタティック変数宣言 */
Sint32 vib_port;
Sint32 cursor_pos;
static const PDS_PERIPHERAL* per;
static PDS_VIBPARAM vprm;

static Sint32 continuous;
static Sint32 wave_type;
static Sint32 power;
static Sint32 frequency;
static Sint32 incline;

static const char* str_cont[] = {
	"NO", "YES",
};

static const char* str_wtype[] = {
	"NORMAL",
	"EXHALATION",
	"CONVERGENCE",
};


/*-------------------------------------------------------------------------*/
/* スタティック関数プロトタイプ */
static Sint32 search_vib(void);
static void vib_menu(Sint32 x, Sint32 y, Uint32 port);


/*-------------------------------------------------------------------------*/
/* 初期化 */
void vib_init(void)
{
	vib_port = -1;

	memset(&vprm, 0, sizeof(vprm));

	continuous = 0;
	wave_type = 0;
	power = 7;
	frequency = 15;
	incline = 1;
}


/*-------------------------------------------------------------------------*/
/* 終了 */
void vib_exit(void)
{
	/* Shinobi2では sbExitSystem()で自動的に振動停止する */
#if 0
	if (vib_port >= 0) {
		pdVibMxStop(vib_port);
	}
#endif
}

/*-------------------------------------------------------------------------*/
/* メイン */
void vib_main(void)
{
	per = pdGetPeripheral(PDD_PORT_A0);

	if (per->info->type == 0) {
		return;
	}

	vib_port = search_vib();
	if (vib_port < 0) {
		njPrintC(NJM_LOCATION(4, 10), "PLEASE CONNECT VIBRATION DEVICE");
	} else {
		njPrintC(NJM_LOCATION(4, 6), "SET PARAMETERS AND PRESS A BUTTON");
		njPrintC(NJM_LOCATION(4, 7), "    (PRESS B BUTTON TO STOP)");
		vib_menu(6, 10, vib_port);
	}
}

/*-------------------------------------------------------------------------*/
/* 振動デバイスを探す */
static Sint32 search_vib(void)
{
	static const Uint32 port[] = {
		PDD_PORT_A1, PDD_PORT_A2,
		PDD_PORT_B1, PDD_PORT_B2,
		PDD_PORT_C1, PDD_PORT_C2,
		PDD_PORT_D1, PDD_PORT_D2,
	};
	Sint32 i;

	for (i = 0; i < 8; i++) {
		if (pdVibMxIsReady(port[i])) {
			return port[i];
		}
	}

	return -1;
}

/*-------------------------------------------------------------------------*/
/* メニュー部分 */
static void vib_menu(Sint32 x, Sint32 y, Uint32 port)
{

	njPrint(NJM_LOCATION(x + 2, y + 0), "CONTINUOUS: %s", str_cont[continuous]);
	njPrint(NJM_LOCATION(x + 2, y + 2), "WAVE TYPE : %s", str_wtype[wave_type]);
	njPrint(NJM_LOCATION(x + 2, y + 4), "POWER     : %2d", power);
	njPrint(NJM_LOCATION(x + 2, y + 6), "FREQUENCY : %4.1fHz",
													(frequency + 1) / 2.f);
	njPrint(NJM_LOCATION(x + 2, y + 8), "INCLINE   : %d", incline);

	njPrint(NJM_LOCATION(x, y + cursor_pos * 2), ">");

	if (per->press & PDD_DGT_KU) {
		if (cursor_pos > 0) cursor_pos--;
	}
	if (per->press & PDD_DGT_KD) {
		if (cursor_pos < 4) cursor_pos++;
	}

	if (per->press & PDD_DGT_KL) {
		switch (cursor_pos) {
			case 0:
				if (continuous > 0) continuous--;
				break;
			case 1:
				if (wave_type > 0) wave_type--;
				break;
			case 2:
				if (power > -7 ) power--;
				break;
			case 3:
				if (frequency > 7) frequency--;
				break;
			case 4:
				if (incline > 0) incline--;
				break;
		}
	}

	if (per->press & PDD_DGT_KR) {
		switch (cursor_pos) {
			case 0:
				if (continuous < 1) continuous++;
				break;
			case 1:
				if (wave_type < 2) wave_type++;
				break;
			case 2:
				if (power < 7 ) power++;
				break;
			case 3:
				if (frequency < 59) frequency++;
				break;
			case 4:
				if (incline < 255) incline++;
				break;
		}
	}

	if (per->press & (PDD_DGT_ST | PDD_DGT_TA | PDD_DGT_TC)) {
		vprm.unit = 1;
		vprm.flag = 0;
		vprm.freq = frequency;
		vprm.power = power;
		vprm.inc = incline;

		if (continuous == 1) {
			vprm.flag = PDD_VIB_FLAG_CONTINUOUS;
			if (wave_type == 1) {
				vprm.flag |= PDD_VIB_FLAG_EXHALATION;
			} else if (wave_type == 2) {
				vprm.flag |= PDD_VIB_FLAG_CONVERGENCE;
			}
		}

		pdVibMxStart(port, &vprm);
	}

	if (per->press & PDD_DGT_TB) {
		pdVibMxStop(port);
	}
}

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

