// USBDlg.cpp : implementation file
//

#include "stdafx.h"
#include "USB.h"
#include "USBDlg.h"
//####################### AZICUETANO ##########################
#include "usb2550.h"
//####################### AZICUETANO ##########################

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUSBDlg dialog

CUSBDlg::CUSBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUSBDlg::IDD, pParent)
	, Sumando1(0)
	, Sumando2(0)
	, Resultado(0)
	, valor_recibido_pic(0)
	, Teclado(0)
	, Caracter(0)
	, Letra(0)
{
	//{{AFX_DATA_INIT(CUSBDlg)
		// NOTE: the ClassWizard will add member initialization here

//####################### AZICUETANO ##########################

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

	strcpy(vid_pid,"vid_04d8&pid_0011");		///// VID y PID Aqui!!!!!!!!!! 
	strcpy(in_pipe,"\\MCHP_EP1");
	strcpy(out_pipe,"\\MCHP_EP1");
         
    myInPipe = MPUSBOpen(0,vid_pid,in_pipe,MP_READ,0);	
	myOutPipe = MPUSBOpen(0,vid_pid,out_pipe,MP_WRITE,0);

    if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
		MessageBox("USB no puede ser abierto!!!",NULL,MB_OK);

//####################### AZICUETANO ##########################

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUSBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUSBDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_EDIT1, Sumando1);
	DDX_Text(pDX, IDC_EDIT2, Sumando2);
	DDX_Text(pDX, IDC_EDIT3, Resultado);
	DDX_Text(pDX, IDC_EDIT4, valor_recibido_pic);
	DDX_Text(pDX, IDC_EDIT6, Teclado);
	DDX_Text(pDX, IDC_EDIT5, Caracter);
}

BEGIN_MESSAGE_MAP(CUSBDlg, CDialog)
	//{{AFX_MSG_MAP(CUSBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnLedverde)
	ON_BN_CLICKED(IDC_BUTTON2, OnLedrojo)
	
	//}}AFX_MSG_MAP
	ON_EN_CHANGE(IDC_EDIT1, &CUSBDlg::OnEnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, &CUSBDlg::OnEnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT3, &CUSBDlg::OnEnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT5, &CUSBDlg::OnEnChangeEdit5)
	ON_BN_CLICKED(IDC_BUTTON3, &CUSBDlg::OnBnClickedButton3)
	ON_EN_CHANGE(IDC_EDIT4, &CUSBDlg::OnEnChangeEdit4)
	ON_BN_CLICKED(IDC_BUTTON4, &CUSBDlg::OnBnClickedButton4)
	ON_EN_UPDATE(IDC_EDIT4, &CUSBDlg::OnEnUpdateEdit4)
	//ON_BN_CLICKED(IDC_BUTTON6, &CUSBDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CUSBDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON8, &CUSBDlg::OnBnClickedButton6)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUSBDlg message handlers

BOOL CUSBDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUSBDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CUSBDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUSBDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUSBDlg::OnLedverde() 
{
	// TODO: Add your control notification handler code here

	DWORD SentLength;
	BYTE send_buf[2];
	send_buf[0] = 0x02;             // Para los leds
    send_buf[1] = 0x01;				// Encendemos Verde
	MPUSBWrite(myOutPipe,send_buf,2,&SentLength,1000);	
	
}

void CUSBDlg::OnLedrojo() 
{
	// TODO: Add your control notification handler code here

	DWORD SentLength;
	BYTE send_buf[2];
	send_buf[0] = 0x02;             // Para los leds
    send_buf[1] = 0x02;				// Encendemos Rojo
	MPUSBWrite(myOutPipe,send_buf,2,&SentLength,1000);

}

void CUSBDlg::OnEnChangeEdit1()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void CUSBDlg::OnEnChangeEdit2()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void CUSBDlg::OnEnChangeEdit3()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}
void CUSBDlg::OnEnChangeEdit5()//obtiene del edit4 la cadena y mostrar en el lcd
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}


void CUSBDlg::OnBnClickedButton7()// recibe del teclado y muestra
{
 //CString cadena4;
 //GetDlgItemText(IDC_EDIT5,cadena4);
	DWORD SentLength,RecvLength;
	char send_buff[2];
	CString cadena1;

    GetDlgItemText(IDC_EDIT5,cadena1);
//	DWORD SentLength,RecvLength;
//	BYTE rec_buff[1];
 //   MPUSBRead(myInPipe,rec_buff,1,&RecvLength,1000);
//	Teclado=BYTE(rec_buff[0]);//recibir del teclado y mostrar
//	Letra=_ttoi(cadena1);	

	send_buff[0] = 0x01;
	send_buff[1] = BYTE(Letra);
    MPUSBWrite(myOutPipe,send_buff,2,&SentLength,1000);

//	UpdateData(FALSE);	

}

void CUSBDlg::OnBnClickedButton6()//caja para mostrar al lcd,20 caracteres 
{
	DWORD SentLength,RecvLength;
	BYTE send_buff[2];
	//BYTE rec_buff[1];	
	CString cadena1;
	//CString cadena2;
	//CString cadena3;
	GetDlgItemText(IDC_EDIT5,cadena1);	// Metemos en 'cadena1' el valor que escribamos en casilla IDC_EDIT1.
	//GetDlgItemText(IDC_EDIT2,cadena2);	// Metemos en 'cadena2' el valor que escribamos en casilla IDC_EDIT2.

	Caracter=_ttoi(cadena1);			// Pasamos los valores a INT.
	//Sumando2=_ttoi(cadena2);			// Pasamos los valores a INT.

	send_buff[0] = 0x01;
	send_buff[1] = BYTE(Caracter);
//	send_buff[2] = BYTE(Sumando2);

	MPUSBWrite(myOutPipe,send_buff,2,&SentLength,1000); // enviamos al pic las 2 variables
 

 //Resultado=BYTE(rec_buff[0]);//recibir del teclado y mostrar

	UpdateData(FALSE);	

}

void CUSBDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	
	DWORD SentLength,RecvLength;
	BYTE send_buff[3];
	BYTE rec_buff[1];
    
	
	CString cadena1;
	CString cadena2;
	CString cadena3;
    

	int caca=0;

	GetDlgItemText(IDC_EDIT1,cadena1);	// Metemos en 'cadena1' el valor que escribamos en casilla IDC_EDIT1.
	GetDlgItemText(IDC_EDIT2,cadena2);	// Metemos en 'cadena2' el valor que escribamos en casilla IDC_EDIT2.

	Sumando1=_ttoi(cadena1);			// Pasamos los valores a INT.
	Sumando2=_ttoi(cadena2);			// Pasamos los valores a INT.

	send_buff[0] = 0x00;
	send_buff[1] = BYTE(Sumando1);
	send_buff[2] = BYTE(Sumando2);

	MPUSBWrite(myOutPipe,send_buff,3,&SentLength,1000); // enviamos al pic las 2 variables

	MPUSBRead(myInPipe,rec_buff,1,&RecvLength,1000); //recibimos del pic el resultado

	cadena3.Format("%d",BYTE(caca));	// Pasamos la variable auxiliar a una variable del tipo CString.

	Resultado=BYTE(rec_buff[0]);

	UpdateData(FALSE);					// AZICUETANO - Se puede pasar o bien FALSE para dar valor a los controles a partir de las variables miembro, o bien TRUE para transferir a las variables miembro los valores actuales de los controles.



//	Resultado=(int)rec_buf;

//	UpdateData(FALSE);					// AZICUETANO - Se puede pasar o bien FALSE para dar valor a los controles a partir de las variables miembro, o bien TRUE para transferir a las variables miembro los valores actuales de los controles.



//	Sumando1=_ttoi(cadena1);			// Pasamos los valores a INT.
//	Sumando2=_ttoi(cadena2);			// Pasamos los valores a INT.
	
//	caca=Sumando1+Sumando2;				// Efectuamos la suma en una variable auxiliar.

//	cadena3.Format("%d",BYTE(caca));	// Pasamos la variable auxiliar a una variable del tipo CString.

//	Resultado=BYTE(caca);

//	UpdateData(FALSE);					// AZICUETANO - Se puede pasar o bien FALSE para dar valor a los controles a partir de las variables miembro, o bien TRUE para transferir a las variables miembro los valores actuales de los controles.

//	MessageBox(cadena3);




	///////////////////////////////////////////////////////
	///////////////////////////////////////////////////////
//	DWORD SentLength,RecvLength;
//	BYTE send_buf[3];
//	char rec_buf[1];
//	send_buf[0] = 0x00;             // Para Sumar
//	send_buf[1] = (BYTE) m_Sum1;			// Envia sumando 1 ?????????
//	send_buf[2] = (BYTE) m_Sum2;			// Envia sumando 2 ?????????
//	MPUSBWrite(myOutPipe,send_buf,3,&SentLength,1000);
//	MPUSBRead(myInPipe,rec_buf,1,&RecvLength,1000);
//	m_Res=(int)rec_buf;						// ??????????
	///////////////////////////////////////////////////////
	///////////////////////////////////////////////////////

//	Resultado=Sumando1+Sumando2;
//	MessageBox("USB no puede ser abierto!!!",NULL,MB_OK);

//	MessageBox("Resultado=%d",NULL,MB_OK);
}

void CUSBDlg::OnEnChangeEdit4()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
//		MessageBox("morales feo");

		
}

void CUSBDlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here

	DWORD SentLength,RecvLength;
	BYTE rec_buff[1];
	int n=0;
	
	for(n=0;n<50;n++)
	{
//		Sleep(500);

		MPUSBRead(myInPipe,rec_buff,1,&RecvLength,1000);

		valor_recibido_pic=BYTE(rec_buff[0]);

		UpdateData(FALSE);					// AZICUETANO - Se puede pasar o bien FALSE para dar valor a los controles a partir de las variables miembro, o bien TRUE para transferir a las variables miembro los valores actuales de los controles.
		//morLES//
		//BYTE(rec_buff[0])=valor_recibido_pic;
		//CUSBDlg::OnInitDialog();
		//CUSBDlg::Repaint();
		//CDialog Morales=new CDialog();
		
//		Morales.OnPaint();

		//MessageBox("morales feo");

//		CDialog::DoDataExchange(pDX);

//		CDialog::OnPaint();

		Sleep(50);

		//OnEnChangeEdit4();
	}
}

void CUSBDlg::OnEnUpdateEdit4()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO:  Add your control notification handler code here

//	MessageBox("morales feo");
}
//}