/* 
* classCanApplication.cpp
*
* Created: 07/02/2020 14:36:41
* Author: Ledo
*/


#include "classCanApplication.h"

// default constructor
classCanApplication::classCanApplication()
{
} //classCanApplication

// default destructor
classCanApplication::~classCanApplication()
{
} //~classCanApplication


void classCanApplication::Can_initialize(void)
{
	uint32_t ul_sysclk, cnt = 0;
	uint8_t uc_key;

	/* Enable CAN0 clock. */
	PMC->PMC_PCER1 |= (1 << (ID_CAN0 - 32));

	ul_sysclk = 96000000; //CPU Freq MHz
	myCan.can_init(CAN0, ul_sysclk, CAN_BPS_500K);

	/* Disable all CAN0 interrupts. */
	myCan.can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
	
	/* Init CAN0 Mailbox 0 to Transmit Mailbox. To all nodes */
	myCan.reset_mailbox_conf(&tx_mailbox);
	tx_mailbox.ul_mb_idx = CAN_COMM_TXMB_ID;
	tx_mailbox.uc_obj_type = CAN_MB_TX_MODE;
	tx_mailbox.uc_tx_prio = CAN_TX_PRIO;
	tx_mailbox.uc_id_ver = 0;
	tx_mailbox.ul_id_msk = 0;
	myCan.can_mailbox_init(CAN0, &tx_mailbox);
	
	/* Init CAN0 Mailbox 1 to Reception Mailbox. CAN_IO_Board */
	myCan.reset_mailbox_conf(&io_rx_mailbox);
	io_rx_mailbox.ul_mb_idx = CAN_COMM_RXMB_IO_ID;
	io_rx_mailbox.uc_obj_type = CAN_MB_RX_MODE;
	io_rx_mailbox.uc_id_ver = 0;
	io_rx_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
	io_rx_mailbox.ul_id = CAN_MID_MIDvA(CAN_IO_ID);
	io_rx_mailbox.uc_length = 8;
	myCan.can_mailbox_init(CAN0, &io_rx_mailbox);

	/* Init CAN0 Mailbox 3 to Reception Mailbox. CAN_WAGO */
	myCan.reset_mailbox_conf(&wago_rx_mailbox);
	wago_rx_mailbox.ul_mb_idx = CAN_COMM_RXMB_WAGO_ID;
	wago_rx_mailbox.uc_obj_type = CAN_MB_RX_MODE;
	wago_rx_mailbox.uc_id_ver = 0;
	wago_rx_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
	wago_rx_mailbox.ul_id = CAN_MID_MIDvA(CAN_WAGO_TXPDO1_COB_ID);
	wago_rx_mailbox.uc_length = 8;
	myCan.can_mailbox_init(CAN0, &wago_rx_mailbox);
	

	/* Init CAN0 Mailbox 5 to Reception Mailbox. Agitador*/
	myCan.reset_mailbox_conf(&motor_rx_mailbox);
	motor_rx_mailbox.ul_mb_idx = CAN_COMM_RXMB_MOTOR_ID;
	motor_rx_mailbox.uc_obj_type = CAN_MB_RX_MODE;
	motor_rx_mailbox.uc_id_ver = 0;
	motor_rx_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
	motor_rx_mailbox.ul_id = CAN_MID_MIDvA(CAN_MOTOR_ID);
	myCan.can_mailbox_init(CAN0, &motor_rx_mailbox);
	
	
	
	/* Enable CAN0 mailbox interrupts. */
	myCan.can_enable_interrupt(CAN0, 1 << CAN_COMM_RXMB_MOTOR_ID);
	myCan.can_enable_interrupt(CAN0, 1 << CAN_COMM_RXMB_IO_ID);
	myCan.can_enable_interrupt(CAN0, 1 << CAN_COMM_RXMB_WAGO_ID);

	/* Configure and enable interrupt of CAN0. */
	NVIC_EnableIRQ(CAN0_IRQn);
	
	//All nodes Start (to Operational State)
	tx_mailbox.ul_id = 0;
	tx_mailbox.ul_datal =0x01;
	tx_mailbox.uc_length = 2;
	myCan.can_mailbox_write(CAN0, &tx_mailbox);
	/* Send out the information in the mailbox. */
	myCan.can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
	
}

void classCanApplication::decode_can_msg_motor(void)
{
	ul2byte.data32 = motor_rx_mailbox.ul_datal;
	MotorRead.Enable = ul2byte.bytes[0];
	MotorRead.Brake = ul2byte.bytes[1];
	MotorRead.Dir = ul2byte.bytes[2];
	MotorRead.Speed = ul2byte.bytes[3];
	ul2byte.data32 = motor_rx_mailbox.ul_datah;
	MotorRead.Power = ul2byte.bytes[0];
	MotorRead.T = ul2byte.bytes[1] | (ul2byte.bytes[2] << 8);
	MotorRead.Hall = ul2byte.bytes[3];
	
}

void classCanApplication::decode_can_msg_WAGO(void)
{
	switch((uint8_t)WagoCmd)
	{
		case InputsReadPDO:
		ul2byte.data32 = wago_rx_mailbox.ul_datal;
		Wago_inputs.I0 = (ul2byte.bytes[0] & 1) == 1 ? true:false;
		Wago_inputs.I1 = (ul2byte.bytes[0] & 2) == 2 ? true:false;
		Wago_inputs.I2 = (ul2byte.bytes[0] & 4) == 4 ? true:false;
		Wago_inputs.I3 = (ul2byte.bytes[0] & 8) == 8 ? true:false;
		Wago_inputs.I4 = (ul2byte.bytes[0] & 16) == 16 ? true:false;
		Wago_inputs.I5 = (ul2byte.bytes[0] & 32) == 32 ? true:false;
		Wago_inputs.I6 = (ul2byte.bytes[0] & 64) == 64 ? true:false;
		Wago_inputs.I7 = (ul2byte.bytes[0] & 128) == 128 ? true:false;
		Wago_inputs.I8 = (ul2byte.bytes[1] & 1 == 1) ? true:false;
		Wago_inputs.I9 = (ul2byte.bytes[1] & 2) == 2 ? true:false;
		Wago_inputs.I10 = (ul2byte.bytes[1] & 4) == 4 ? true:false;
		Wago_inputs.I11 = (ul2byte.bytes[1] & 8) == 8 ? true:false;
		Wago_inputs.inputs0 = ul2byte.bytes[0];
		Wago_inputs.inputs1 = ul2byte.bytes[1];
		break;
		case InputsReadSDO:
		break;
		default:
		break;

	}
}


void classCanApplication::decode_can_msg_IO(void)
{
	uint32_t ultemp;
	ultemp = io_rx_mailbox.ul_datal;
	ul2byte.data32 = ultemp ;

	//Digital Inputs State decodes
	InputsState.bInput0 = ultemp & INPUT0;
	InputsState.bInput1 = ultemp & INPUT1;
	InputsState.bInput2 = ultemp & INPUT2;
	InputsState.bInput3 = ultemp & INPUT3;
	InputsState.bInput4 = ultemp & INPUT4;
	InputsState.bInput5 = ultemp & INPUT5;
	InputsState.bInput6 = ultemp & INPUT6;
	InputsState.bInput7 = ultemp & INPUT7;
	InputsState.bInput8 = ultemp & INPUT8;
	InputsState.bInput9 = ultemp & INPUT9;
	InputsState.bInput10 = ultemp & INPUT10;
	InputsState.bInput11 = ultemp & INPUT11;
	InputsState.bInput12 = ultemp & INPUT12;
	InputsState.bInput13 = ultemp & INPUT13;
	InputsState.bInput14 = ultemp & INPUT14;
	InputsState.bInput15 = ultemp & INPUT15;
	
	InputsState.PuertoA = ul2byte.bytes[0];
	InputsState.PuertoB = ul2byte.bytes[1];

	//Digital Outputs State decodes
	ultemp = io_rx_mailbox.ul_datah;
	ul2byte.data32 = ultemp;
	OutsStateRead.bOutput0 = ultemp & OUT0;
	OutsStateRead.bOutput1 = ultemp & OUT1;
	OutsStateRead.bOutput2 = ultemp & OUT2;
	OutsStateRead.bOutput3 = ultemp & OUT3;
	OutsStateRead.bOutput4 = ultemp & OUT4;
	OutsStateRead.bOutput5 = ultemp & OUT5;
	OutsStateRead.bOutput6 = ultemp & OUT6;
	OutsStateRead.bOutput7 = ultemp & OUT7;
	OutsStateRead.bOutput8 = ultemp & OUT8;
	OutsStateRead.bOutput9 = ultemp & OUT9;
	OutsStateRead.bOutput10 = ultemp & OUT10;
	OutsStateRead.bOutput11 = ultemp & OUT11;
	OutsStateRead.bOutput12 = ultemp & OUT12;
	OutsStateRead.bOutput13 = ultemp & OUT13;
	OutsStateRead.bOutput14 = ultemp & OUT14;
	OutsStateRead.bOutput15 = ultemp & OUT15;
	OutsStateRead.bOutput16 = ultemp & OUT16;
	OutsStateRead.PuertoC = ul2byte.bytes[0];
	OutsStateRead.PuertoD = ul2byte.bytes[1];
	OutsStateRead.PuertoR = ul2byte.bytes[2];
}

/*****************************************************
* CAN BUS Loop.
* As default, CPU reads CAN Nodes vars
*****************************************************/
void classCanApplication::CAN_Loop(void)
{
	switch(CAN_ring_counter)
	{
		case 0:
		send_cmd2_motor(CAN_Motor_Cmd,Motor_cmdData);
		CAN_Motor_Cmd = Read_vars;//default
		break;
		case 1:
		send_cmd2_IO(CAN_IO_Cmd,Outputs_mask);
		CAN_IO_Cmd = Read_IO_vars;//default
		break;
		case 2:
		send_cmd2_Wago(WagoCmd,wago_sIndex,wago_data);
		WagoCmd = InputsReadPDO;//default;
		break;
		default:
		break;
	}
	CAN_ring_counter++;
	if(CAN_ring_counter>=CAN_NODES)
	CAN_ring_counter = 0;
	
	if(wagoScreen)
	  wagoPage_Update();
	if(ioScreen)
	  ioPage_Update();
	if(motorScreen)
	  motorPage_Update();
	  
	  
}

void classCanApplication::send_cmd2_motor(CAN_MOTOR_CMD cmd,uint8_t data)
{

	tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_MOTOR_ID);
	tx_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN;
	tx_mailbox.ul_datal = (uint32_t) ((data<<8) | cmd);
	tx_mailbox.ul_datah = 0x00u;
	myCan.can_mailbox_write(CAN0, &tx_mailbox);
	/* Send out the information in the mailbox. */
	myCan.can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
}



void classCanApplication::send_cmd2_IO(CAN_IO_CMD cmd, uint32_t mask)
{
	tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_IO_ID);
	tx_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN;
	tx_mailbox.ul_datal = ((mask << 8) | cmd);
	tx_mailbox.ul_datah = 0x00u;
	myCan.can_mailbox_write(CAN0, &tx_mailbox);
	/* Send out the information in the mailbox. */
	myCan.can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
}

void classCanApplication::send_cmd2_Wago(WAGO_CMD cmd,uint8_t sIndex, uint8_t data)
{

	switch((uint8_t)cmd)
	{
		case NodeStart:
		tx_mailbox.ul_id = 0;
		tx_mailbox.ul_datal =0x01;
		tx_mailbox.uc_length = 2;
		break;
		case NodeStop:
		tx_mailbox.ul_datal =0x02;
		tx_mailbox.uc_length = 2;
		break;
		case NodeReset:
		tx_mailbox.ul_datal =0x81;
		tx_mailbox.uc_length = 2;
		break;
		case InputsReadSDO:
		tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_WAGO_RXSDO_COB_ID);
		tx_mailbox.uc_length = 4;
		tx_mailbox.ul_datal = 0x200040 | (wago_sIndex << 24);
		tx_mailbox.ul_datah = 0x80ffffff;
		break;
		case InputsReadPDO:
		tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_WAGO_TXPDO1_COB_ID);
		tx_mailbox.uc_length = 2;
		tx_mailbox.ul_datal = 0x01;
		break;
		case OutputSetSDO:
		tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_WAGO_RXSDO_COB_ID);
		tx_mailbox.uc_length = 5;
		tx_mailbox.ul_datal = 0x21002f | (wago_sIndex << 24);
		tx_mailbox.ul_datah = data;
		break;
		case OutputSetPDO:
		tx_mailbox.ul_id = CAN_MID_MIDvA(CAN_WAGO_RXPDO1_COB_ID);
		tx_mailbox.uc_length = 1;
		tx_mailbox.ul_datal = data;
		break;
		
		default:
		break;
	}
	myCan.can_mailbox_write(CAN0, &tx_mailbox);
	/* Send out the information in the mailbox. */
	myCan.can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
}

void classCanApplication::wagoPage_load(void)
{
	int x;
	Gui.drawTestPage((uint8_t*)("Wago 750-337 IO Example"));
    Gui.setFont(font48);
    x = strlen("Digital Inputs State");
    x = (800-x * Gui.cfont.x_size) / 2;
	Gui.print("Digital Inputs State",x,50,0,VGA_BLUE,VGA_WHITE);
	
    x = strlen("Digital Outputs Control");
    x = (800-x * Gui.cfont.x_size) / 2;
    Gui.print("Digital Outputs Control",x,240,0,VGA_BLUE,VGA_WHITE);
	ioScreen = false;
	motorScreen = false;
	wagoScreen = true;
	
}

void classCanApplication::ioPage_load(void)
{
	int x;
	Gui.drawTestPage((uint8_t*)("Ledoelectronics CAN IO Example"));
	Gui.setFont(font48);
	x = strlen("Digital Inputs State");
	x = (800-x * Gui.cfont.x_size) / 2;
	Gui.print("Digital Inputs State",x,50,0,VGA_BLUE,VGA_WHITE);
	
	x = strlen("Digital Outputs Control");
	x = (800-x * Gui.cfont.x_size) / 2;
	Gui.print("Digital Outputs Control",x,240,0,VGA_BLUE,VGA_WHITE);
	motorScreen = false;
	wagoScreen = false;
	ioScreen = true;
	
}

void classCanApplication::motorPage_load(void)
{
	Display.Tft_Clear(VGA_GREEN);
	Gui.drawRectangle(1,1,799,479,VGA_WHITE);
	Gui.drawRectangle(2,2,798,478,VGA_WHITE);
	Gui.setFont(font48);
	//Title Label
	Gui.print("CAN Motor Board Test",150,20,0,VGA_BLUE,VGA_GREEN);
	//Run, Stop and Reset Buttons
	drawButton("Run",40,100,200,100,VGA_WHITE,VGA_BLACK,VGA_WHITE);
	drawButton("Stop",300,100,200,100,VGA_WHITE,VGA_BLACK,VGA_WHITE);
	drawButton("RW",560,100,200,100,VGA_WHITE,VGA_BLACK,VGA_WHITE);
	//Speed input box
	Gui.drawFillRectangle(40,250,200,350,VGA_WHITE);
	Gui.drawRectangle(40,250,200,350,VGA_BLACK);
	Gui.setFont(font48);
	Gui.printNumI(MotorRead.Speed,80,280,3,'0',VGA_BLUE,VGA_WHITE);
	Gui.print("%",225,280,0,VGA_BLUE,VGA_GREEN);
	//Speed Button
	drawButton("Speed",300,250,200,100,VGA_WHITE,VGA_BLACK,VGA_WHITE);
	
	wagoScreen = false;
	ioScreen = false;
	motorScreen = true;

}

void classCanApplication::drawButton(char *string, int16_t x, int16_t y,int width,
int heigh,uint16_t forecolor,
uint16_t backcolor,uint16_t bcolor)
{
	Gui.drawFillRectangle(x,y,x+width,y+heigh,backcolor);
	Gui.drawRectangle(x,y,x+width,y+heigh,bcolor);
	Gui.drawRectangle(x+1,y+1,x+width-1,y+heigh-1,bcolor);
	
	switch(strlen(string))
	{
		case 1:
		Gui.print(string, x + width / 3 + 20, y + heigh / 3, 0, forecolor, backcolor);
		break;
		case 2:
		case 3:
		Gui.print(string, x + width / 3, y + heigh / 3, 0, forecolor, backcolor);
		break;
		case 4:
		case 5:
		Gui.print(string, x + width / 4 - 10, y + heigh / 3, 0, forecolor, backcolor);
		break;
		default:
		break;
		
	}

}



void classCanApplication::drawBox(int num,int x, int y, int width,int height,bool state)
{
	Gui.setFont(font16x16);
	if(state)
	{
	   Gui.drawFillRectangle(x,y,x+width,y+height,VGA_GREEN);
	   Gui.printNumI(num,x+5,y+20,2,'0',VGA_WHITE,VGA_GREEN);
	}
	  else
	  {
	     Gui.drawFillRectangle(x,y,x+width,y+height,VGA_GRAY);
		 Gui.printNumI(num,x+5,y+20,2,'0',VGA_WHITE,VGA_GRAY);
	  }
	Gui.drawRectangle(x,y,x+width,y+height,VGA_BLACK);
}

void classCanApplication::wagoPage_Update(void)
{
	//Wago Inputs
	int xx = 15;
	drawBox(0,xx,120,48,50,Wago_inputs.I0);
	xx +=65;
	drawBox(1,xx,120,48,50,Wago_inputs.I1);
	xx +=65;
	drawBox(2,xx,120,48,50,Wago_inputs.I2);
	xx +=65;
	drawBox(3,xx,120,48,50,Wago_inputs.I3);
	xx +=65;
	drawBox(4,xx,120,48,50,Wago_inputs.I4);
	xx +=65;
	drawBox(5,xx,120,48,50,Wago_inputs.I5);
	xx +=65;
	drawBox(6,xx,120,48,50,Wago_inputs.I6);
	xx +=65;
	drawBox(7,xx,120,48,50,Wago_inputs.I7);
	xx +=65;
	drawBox(8,xx,120,48,50,Wago_inputs.I8);
	xx +=65;
	drawBox(9,xx,120,48,50,Wago_inputs.I9);
	xx +=65;
	drawBox(10,xx,120,48,50,Wago_inputs.I10);
	xx +=65;
	drawBox(11,xx,120,48,50,Wago_inputs.I11);
	//Wago Outputs
	xx=15;
	drawBox(00,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q0 = !Wago_outputs.Q0;
		if(Wago_outputs.Q0)
		 Wago_outputs.outputs |= 1;
		  else
		   Wago_outputs.outputs &= ~1;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
  
	}
	xx += 100;
	drawBox(01,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q1 = !Wago_outputs.Q1;
		if(Wago_outputs.Q1)
		Wago_outputs.outputs |= 2;
		else
		Wago_outputs.outputs &= ~2;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(02,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q2 = !Wago_outputs.Q2;
		if(Wago_outputs.Q2)
		Wago_outputs.outputs |= 4;
		else
		Wago_outputs.outputs &= ~4;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(03,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q3 = !Wago_outputs.Q3;
		if(Wago_outputs.Q3)
		Wago_outputs.outputs |= 8;
		else
		Wago_outputs.outputs &= ~8;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(04,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q4 = !Wago_outputs.Q4;
		if(Wago_outputs.Q4)
		Wago_outputs.outputs |= 16;
		else
		Wago_outputs.outputs &= ~16;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(05,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q5 = !Wago_outputs.Q5;
		if(Wago_outputs.Q5)
		Wago_outputs.outputs |= 32;
		else
		Wago_outputs.outputs &= ~32;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(06,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q6 = !Wago_outputs.Q6;
		if(Wago_outputs.Q6)
		Wago_outputs.outputs |= 64;
		else
		Wago_outputs.outputs &= ~64;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	xx += 100;
	drawBox(07,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Wago_outputs.Q7 = !Wago_outputs.Q7;
		if(Wago_outputs.Q7)
		Wago_outputs.outputs |= 128;
		else
		Wago_outputs.outputs &= ~128;
		send_cmd2_Wago(OutputSetPDO,wago_sIndex,Wago_outputs.outputs);
	}
	
}

bool classCanApplication::outputBoxPressed(int x, int y)
{
	int32_t X,Y;
	if(TOUCH_PRESSED())
	{
		X = Touch.TCGetX();
		Y = Touch.TCGetY();
		if((X>x) && (X<(x+80)))
		if((Y>y) && (Y<(y+80)))
		 return true;
	}
	
	return false;
}

void classCanApplication::ioPage_Update(void)
{
	//CAN IO Board Inputs
	int xx = 15;
	drawBox(0,xx,120,48,50,InputsState.bInput0);
	xx +=65;
	drawBox(1,xx,120,48,50,InputsState.bInput1);
	xx +=65;
	drawBox(2,xx,120,48,50,InputsState.bInput2);
	xx +=65;
	drawBox(3,xx,120,48,50,InputsState.bInput3);
	xx +=65;
	drawBox(4,xx,120,48,50,InputsState.bInput4);
	xx +=65;
	drawBox(5,xx,120,48,50,InputsState.bInput5);
	xx +=65;
	drawBox(6,xx,120,48,50,InputsState.bInput6);
	xx +=65;
	drawBox(7,xx,120,48,50,InputsState.bInput7);
	xx +=65;
	drawBox(8,xx,120,48,50,InputsState.bInput8);
	xx +=65;
	drawBox(9,xx,120,48,50,InputsState.bInput9);
	xx +=65;
	drawBox(10,xx,120,48,50,InputsState.bInput10);
	xx +=65;
	drawBox(11,xx,120,48,50,InputsState.bInput11);
	//CAN IO Outputs
	xx=15;
	drawBox(00,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 1;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(01,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 2;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(02,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 4;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(03,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 8;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(04,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 16;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(05,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 32;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(06,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 64;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	xx += 100;
	drawBox(07,xx,300,80,80,true);
	if(outputBoxPressed(xx,300))
	{
		Outputs_mask ^= 128;
		send_cmd2_IO( Outs_update_all,Outputs_mask);
	}
	
}

void classCanApplication::motorPage_Update(void)
{
	uint8_t tecla;
	
	if(Touch.TCIsPenOn())
	{
		if(speedInput() & !motor_speed_set)
		{
			motor_speed_set = true;
			Keys.drawKeys();
		}
				
		if(runKey())
		 send_cmd2_motor(Motor_On,Motor_cmdData);
				
		if(stopKey())
		 send_cmd2_motor(Motor_Off,Motor_cmdData);
		if(rwKey())
		 send_cmd2_motor(Dir_left,Motor_cmdData);
		if(speedKey())
		{
		   if((Motor_cmdData > 0) && (Motor_cmdData  <= 100))
		    send_cmd2_motor(Rpm_set,Motor_cmdData);
		}
	}
			
			
	if(motor_speed_set && Touch.TCIsPenOn())
	{
		tecla = Keys.presetKey();
		if(tecla < 10)
		{
			Keys.keyValue = Keys.keyValue * 10UL + tecla;
			Gui.printNumI(Keys.keyValue,600,50,3,'0',VGA_BLACK,VGA_WHITE);
			return;
		}
	    if(tecla == 10)
		{
			Keys.keyValue = 0;
			Gui.printNumI(Keys.keyValue,600,50,3,'0',VGA_BLACK,VGA_WHITE);
			return;
		}

		if(tecla == 12)
		{
			motor_speed_set = false;
			motorPage_load();
		}
		
		if(tecla == 13)
		{
			motor_speed_set = false;
			Motor_cmdData = Keys.keyValue;
			MotorRead.Speed = Motor_cmdData;
			motorPage_load();
		}
				
	}
	
}

bool classCanApplication::runKey(void)
{
	if(Touch.TCGetX()>40 && Touch.TCGetX()<240)
	if(Touch.TCGetY()>100 && Touch.TCGetY()<200)
	return true;
	return false;
}

bool classCanApplication::stopKey(void)
{
	if(Touch.TCGetX()>300 && Touch.TCGetX()<500)
	if(Touch.TCGetY()>100 && Touch.TCGetY()<200)
	return true;
	return false;
}

bool classCanApplication::rwKey(void)
{
	if(Touch.TCGetX()>560 && Touch.TCGetX()<780)
	if(Touch.TCGetY()>100 && Touch.TCGetY()<200)
	return true;
	return false;
}

bool classCanApplication::speedInput(void)
{
	if(Touch.TCGetX()>40 && Touch.TCGetX()<220)
	if(Touch.TCGetY()>250 && Touch.TCGetY()<350)
	return true;
	return false;
}

bool classCanApplication::speedKey(void)
{
	if(Touch.TCGetX()>300 && Touch.TCGetX()<500)
	if(Touch.TCGetY()>250 && Touch.TCGetY()<350)
	return true;
	return false;
}








