/************************************************/
/*      TASK05			task_m05.c	*/
/*		Man Machine Interface		*/
/*				mirs9503	*/
/************************************************/

#include "hard.h"

void initialize();
void led_set3();
extern lre_r, lre_l;

void inzt05()
{
	sys0(5, 0x1010);
	sys2(5, 10, 0, 0);

	mmi_init();
	sys8(5, 1, 2);			/* pow_chk */
}

void nrmt05(a, b)
int a, b;
{}

void timt05(a, b)
int a, b;
{
	int mode, dip;
	int irs, power;
	int led_data;

	dip = dipsw_data();
	power = pow_chk();
	sys8(5, 1, power);		/* pow_chk */

	if(dip != sys9(0, 3))
	{
		initialize();
	}
	sys8(0, 3, dip);

	led_on(OFF);

	irs = sys9(3, 1);
	if(irs > 0){
		led_on(RED);
	}
	
	if(lre_l < 0){
		led_on(GREEN);
	}

	led_set(3, dip);

	switch(dip)
	{
		case 0x00:
			led_data = sys9(4, 1); 
			break;
		case 0x01:
			led_data = sys9(4, 3);	/* uss_r  no error */
			break;
		case 0x02:
			led_data = sys9(3, 1);	/* irs_data() */
			break;
		case 0x03:
			led_data = sys9(2, 3);	/* ts_data */
			break;
		case 0x04:
			led_data = sys9(4, 1);	/* uss_l */
			break;
		case 0x05:
			led_data = sys9(4, 2);	/* uss_m */
			break;
		case 0x06:
			led_data = sys9(4, 3);	/* uss_r */
			break;
		case 0x07:
			led_data = sys9(2, 1);	/* re_r */	
			break;
		case 0x08:
			led_data = sys9(2, 2);	/* re_l */
			break;
		case 0x09:
			led_data = sys9(2, 1);	/* re_r */
			break;
		case 0x0a:
			led_data = power;	/* pow_chk */
			break;
		case 0x0b:			/* lre_r */
			led_data = 11;
			break;
		case 0x0c:
			led_data = sys9(2, 4);	/* lre_r / 100 turn 360[deg] */
			break;
		case 0x0d:
			led_data = 13;
			break;
		case 0x0e:
			led_data = 14;
			break;
		case 0x0f:
			led_data = 15;
			break;
	}
	if(dip != 0x0b){
		led_set3(led_data);
	}
}


interrupt void intt05()
{}

void initialize()
{
	sys8(0, 1, 0);		/* mode */
	sys8(1, 1, 0);		/* duty_r */
	sys8(1, 2, 0);		/* duty_l */
	sys8(2, 1, 0);		/* re_r	*/
	sys8(2, 2, 0);		/* re_l */
	sys8(2, 3, 0);		/* ts_data */
	sys8(3, 1, 0);		/* irs */
	sys8(4, 1, 0);		/* uss_l */
	sys8(4, 2, 0);		/* uss_m */
	sys8(4, 3, 0);		/* uss_r */
	sys8(4, 4, 0);		/* uss counter */
	lre_r = lre_l = 0;
}

void led_set3(led_data)
int led_data;
{
	int d2, d1, d0;

	if(led_data < 0) led_data *= -1;
	
	led_data %= 1000;
	d2 = led_data / 100;
	led_data %= 100;
	d1 = led_data / 10;
	d0 = led_data % 10;

	led_set(2, d2);
	led_set(1, d1);
	led_set(0, d0);
}