/***************************************************************
 * Name:      wxArduALL
 * Purpose:   Code for Application Frame
 * Author:    seta43 ()
 * Created:   17/03/2020
 * Copyright: seta43 ()
 * Versión 3	14/03/2020
 * Para usar con awxardu.ino
 * xsetaseta@gmail.com
 *  seta43.duckdns.org
 **************************************************************/

#include "main.h"
#include "define.h"
#include "port.h"

#include "unistd.h"

#define  BOTONES 1

char CLAVE[] = "AWXAR";

#define MAXINPUT 100
int tiempo0=6,tiempo2=2;
unsigned char portok;
bool inicio=1;

int  ContadorI=0;
char Binput[MAXINPUT];
char Binput2[10];

int portB,portC,portD;

char VARI[MAXINPUT];

void leerB(void);
void leerC(void);
void leerD(void);
//_________________________

#if YesWindows
    static 	char *myport[]= {"com1","com2","com3","com4","com5","com6","com7","com8","com9","\\\\.\\COM10","\\\\.\\COM11","\\\\.\\COM12"};
#else
	static char *myport[]= {(char *)"/dev/ttyUSB0", (char *)"/dev/ttyUSB1", (char *)"/dev/ttyACM0", (char *)"/dev/ttyACM1", (char *)"/dev/ttyACM2", (char *)"/dev/ttyS0", (char *)"/dev/ttyS1"};
#endif	


DECLARE_APP(MiAplicacion)
// Give wxWidgets the means to create a MiAplicacion object
IMPLEMENT_APP(MiAplicacion)

BEGIN_EVENT_TABLE(arduwxFrame,wxFrame)
 
END_EVENT_TABLE();



arduwxFrame::arduwxFrame(wxWindow* parent,wxWindowID id)
{
  //(*Initialize(arduwxFrame)
    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(612,508));
	SetBackgroundColour(wxColour(240,236,138));
    A1_1 = new wxButton(this, ID_A1_1, _("ON"), wxPoint(64,296), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A1_1"));
    A5_1 = new wxButton(this, ID_A5_1, _("ON"), wxPoint(64,456), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A5_1"));
    A4_1 = new wxButton(this, ID_A4_1, _("ON"), wxPoint(64,416), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A4_1"));
    A3_1 = new wxButton(this, ID_A3_1, _("ON"), wxPoint(64,376), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A3_1"));
    A2_1 = new wxButton(this, ID_A2_1, _("ON"), wxPoint(64,336), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A2_1"));
    A0_1 = new wxButton(this, ID_A0_1, _("ON"), wxPoint(64,256), wxSize(48,33), 0, wxDefaultValidator, _T("ID_A0_1"));
    D13_1 = new wxButton(this, ID_D13_1, _("ON"), wxPoint(496,16), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D13_1"));
    D13_0 = new wxButton(this, ID_D13_0, _("OFF"), wxPoint(552,16), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D13_0"));
    D2_1 = new wxButton(this, ID_D2_1, _("ON"), wxPoint(496,456), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D2_1"));
    D3_1 = new wxButton(this, ID_D3_1, _("ON"), wxPoint(496,416), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D3_1"));
    D4_1 = new wxButton(this, ID_D4_1, _("ON"), wxPoint(496,376), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D4_1"));
    D5_1 = new wxButton(this, ID_D5_1, _("ON"), wxPoint(496,336), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D5_1"));
    D6_1 = new wxButton(this, ID_D6_1, _("ON"), wxPoint(496,296), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D6_1"));
    D7_1 = new wxButton(this, ID_D7_1, _("ON"), wxPoint(496,256), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D7_1"));
    D8_1 = new wxButton(this, ID_D8_1, _("ON"), wxPoint(496,216), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D8_1"));
    D9_1 = new wxButton(this, ID_D9_1, _("ON"), wxPoint(496,176), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D9_1"));
    D10_1 = new wxButton(this, ID_D10_1, _("ON"), wxPoint(496,136), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D10_1"));
    D11_1 = new wxButton(this, ID_D11_1, _("ON"), wxPoint(496,96), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D11_1"));
    D12_1 = new wxButton(this, ID_D12_1, _("ON"), wxPoint(496,56), wxSize(48,33), 0, wxDefaultValidator, _T("ID_D12_1"));
    D11_0 = new wxButton(this, ID_D11_0, _("OFF"), wxPoint(552,96), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D11_0"));
    D12_0 = new wxButton(this, ID_D12_0, _("OFF"), wxPoint(552,56), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D12_0"));
    A1_0 = new wxButton(this, ID_A1_0, _("OFF"), wxPoint(120,296), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A1_0"));
    A5_0 = new wxButton(this, ID_A5_0, _("OFF"), wxPoint(120,456), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A5_0"));
    A4_0 = new wxButton(this, ID_A4_0, _("OFF"), wxPoint(120,416), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A4_0"));
    A3_0 = new wxButton(this, ID_A3_0, _("OFF"), wxPoint(120,376), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A3_0"));
    A2_0 = new wxButton(this, ID_A2_0, _("OFF"), wxPoint(120,336), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A2_0"));
    A0_0 = new wxButton(this, ID_A0_0, _("OFF"), wxPoint(120,256), wxSize(45,33), 0, wxDefaultValidator, _T("ID_A0_0"));
    D2_0 = new wxButton(this, ID_D2_0, _("OFF"), wxPoint(552,456), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D2_0"));
    D6_0 = new wxButton(this, ID_D6_0, _("OFF"), wxPoint(552,296), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D6_0"));
    D3_0 = new wxButton(this, ID_D3_0, _("OFF"), wxPoint(552,416), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D3_0"));
    D4_0 = new wxButton(this, ID_D4_0, _("OFF"), wxPoint(552,376), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D4_0"));
    D5_0 = new wxButton(this, ID_D5_0, _("OFF"), wxPoint(552,336), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D5_0"));
    D7_0 = new wxButton(this, ID_D7_0, _("OFF"), wxPoint(552,256), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D7_0"));
    D8_0 = new wxButton(this, ID_D8_0, _("OFF"), wxPoint(552,216), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D8_0"));
    D9_0 = new wxButton(this, ID_D9_0, _("OFF"), wxPoint(552,176), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D9_0"));
    D10_0 = new wxButton(this, ID_D10_0, _("OFF"), wxPoint(552,136), wxSize(45,33), 0, wxDefaultValidator, _T("ID_D10_0"));
    
    led12 = new wxCheckBox(this, ID_CHECKBOX2, _("D12"), wxPoint(432,64), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX2")); 
    led3 = new wxCheckBox(this, ID_CHECKBOX11, _("D3"), wxPoint(432,424), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX11")); 
    led4 = new wxCheckBox(this, ID_CHECKBOX10, _("D4"), wxPoint(432,384), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX10"));
    led5 = new wxCheckBox(this, ID_CHECKBOX9, _("D5"), wxPoint(432,344), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX9"));
    led6 = new wxCheckBox(this, ID_CHECKBOX8, _("D6"), wxPoint(432,304), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX8"));
    led7 = new wxCheckBox(this, ID_CHECKBOX7, _("D7"), wxPoint(432,264), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX7"));
    led8 = new wxCheckBox(this, ID_CHECKBOX6, _("D8"), wxPoint(432,224), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX6"));
    led9 = new wxCheckBox(this, ID_CHECKBOX5, _("D9"), wxPoint(432,184), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX5"));
    led10 = new wxCheckBox(this, ID_CHECKBOX4, _("D10"), wxPoint(432,144), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX4"));
    led11 = new wxCheckBox(this, ID_CHECKBOX3, _("D11"), wxPoint(432,104), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX3"));
    ledA1 = new wxCheckBox(this, ID_CHECKBOX14, _("A1"), wxPoint(8,304), wxSize(41,24), 0, wxDefaultValidator, _T("ID_CHECKBOX14"));
    ledA0 = new wxCheckBox(this, ID_CHECKBOX15, _("A0"), wxPoint(8,264), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX15"));
    ledA5 = new wxCheckBox(this, ID_CHECKBOX18, _("A5"), wxPoint(8,464), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX18"));
    ledA4 = new wxCheckBox(this, ID_CHECKBOX17, _("A4"), wxPoint(8,424), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX17"));
    ledA3 = new wxCheckBox(this, ID_CHECKBOX16, _("A3"), wxPoint(8,384), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX16"));
    ledA2 = new wxCheckBox(this, ID_CHECKBOX13, _("A2"), wxPoint(8,344), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX13"));
    led13 = new wxCheckBox(this, ID_CHECKBOX12, _("D13"), wxPoint(432,24), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX12"));
    led2 = new wxCheckBox(this, ID_CHECKBOX1, _("D2"), wxPoint(432,464), wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
    
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Arduino - wx"), wxPoint(224,16), wxSize(160,24), 0, _T("ID_STATICTEXT1"));
    wxFont StaticText1Font(15,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("17 Salidas - OUT"), wxPoint(24,80), wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    StaticText2->SetForegroundColour(wxColour(34,45,221));
    wxFont StaticText2Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    StaticText2->SetFont(StaticText2Font);
    miPuerto = new wxTextCtrl(this, ID_TEXTCTRL1, _("No device"), wxPoint(10,120), wxSize(150,16), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    wxFont miPuertoFont(9,wxFONTFAMILY_TELETYPE,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,_T("Courier 10 Pitch"),wxFONTENCODING_DEFAULT);
    miPuerto->SetFont(miPuertoFont);
    
    Bexit = new wxButton(this, ID_BEXIT, _("EXIT"), wxPoint(10,160), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BEXIT"));
    Bexit->SetForegroundColour(wxColour(113,18,18));
    StaticBitmap1 = new wxStaticBitmap(this, ID_STATICBITMAP1, wxBitmap(wxImage(_T("a1.png")).Rescale(wxSize(248,325).GetWidth(),wxSize(248,325).GetHeight())), wxPoint(176,120), wxSize(248,325), wxSIMPLE_BORDER, _T("ID_STATICBITMAP1"));
    StaticText3 = new wxStaticText(this, ID_STATICTEXT3, _("seta43.duckdns.org"), wxPoint(24,16), wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    StaticText3->SetForegroundColour(wxColour(116,23,25));
    
    Timer1.SetOwner(this, ID_TIMER1);
    Timer1.Start(10);
    
    Connect(ID_TIMER1,wxEVT_TIMER,(wxObjectEventFunction)&arduwxFrame::OnTimer1Trigger);
    
	Connect(ID_BEXIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnBexitClick);
    Connect(ID_D13_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD13_1Click);
    Connect(ID_D13_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD13_0Click);
  
	Connect(ID_A1_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA1_1Click);
    Connect(ID_A5_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA5_1Click);
    Connect(ID_A4_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA4_1Click);
    Connect(ID_A3_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA3_1Click);
    Connect(ID_A2_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA2_1Click);
    Connect(ID_A0_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA0_1Click);

    Connect(ID_D2_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD2_1Click);
    Connect(ID_D3_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD3_1Click);
    Connect(ID_D4_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD4_1Click);
    Connect(ID_D5_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD5_1Click);
    Connect(ID_D6_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD6_1Click);
    Connect(ID_D7_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD7_1Click);
    Connect(ID_D8_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD8_1Click);
    Connect(ID_D9_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD9_1Click);
    Connect(ID_D10_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD10_1Click);
    Connect(ID_D11_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD11_1Click);
    Connect(ID_D12_1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD12_1Click);
    Connect(ID_D11_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD11_0Click);
    Connect(ID_D12_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD12_0Click);
    Connect(ID_A1_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA1_0Click);
    Connect(ID_A5_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA5_0Click);
    Connect(ID_A4_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA4_0Click);
    Connect(ID_A3_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA3_0Click);
    Connect(ID_A2_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA2_0Click);
    Connect(ID_A0_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnA0_0Click);

    Connect(ID_D2_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD2_0Click);
    Connect(ID_D6_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD6_0Click);
    Connect(ID_D3_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD3_0Click);
    Connect(ID_D4_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD4_0Click);
    Connect(ID_D5_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD5_0Click);
    Connect(ID_D7_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD7_0Click);
    Connect(ID_D8_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD8_0Click);
    Connect(ID_D9_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD9_0Click);
    Connect(ID_D10_0,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&arduwxFrame::OnD10_0Click);


	openLed();

#if YesWindows 
  #if DEBUG
	AllocConsole();
	freopen("CON", "w", stdout); // redirects stdout
	freopen("CON", "w", stderr); // redirects stderr
  #endif
#endif

}

void arduwxFrame::OnTimer1Trigger(wxTimerEvent& event)
{
 int nbytes;
  	
 
	tiempo0--; tiempo2--;
	if(tiempo0<0)
		{
		 tiempo0=300;
#if DEBUG 		 
		 printf("3Segundo\n");
#endif	

				 leerB();
			 	 leerC();
			  	 leerD();
		}

//__________________________________________________
// cada 0,5 segundos
    if(portok==1 && tiempo2<0)
    {
        tiempo2=10;
#if YesWindows
						while(( nbytes = ReadSerie(hSerial, Binput2, 1)>0 ))
#else
						while(( nbytes = read(fdrs232, Binput2, 1)>0 ))
#endif
							{
								Binput[ContadorI]=Binput2[0];
								if(ContadorI<MAXINPUT-2)   ContadorI++;
//								printf("Contador=%d %d  CHAR=%c\n",ContadorI,Binput2[0],Binput2[0]);
								if (Binput2[0] == '\n')
								   {
									if(ContadorI<2)ContadorI=2;
									Binput[ContadorI-1]=0;
									ContadorI=0;
									sprintf(VARI,"%s",Binput);			
									analizar();									
								   }
							}
	}
}


void arduwxFrame::analizar(void)
{
 int z;
    //printf("%s\n",VARI);
    for(z=0; z<99; z++) if(VARI[z]==',')  VARI[z]=0;
    if(0==strncmp(VARI,"RPORTB",6))
		{
			z=atoi(VARI+7);
			if(z & 32) led13->SetValue(1); else led13->SetValue(0);
			if(z & 16) led12->SetValue(1); else led12->SetValue(0);
			if(z & 8) led11->SetValue(1); else led11->SetValue(0);
			if(z & 4) led10->SetValue(1); else led10->SetValue(0);
			if(z & 2) led9->SetValue(1); else led9->SetValue(0);
			if(z & 1) led8->SetValue(1); else led8->SetValue(0);
			goto salirAna;
		}
	if(0==strncmp(VARI,"RPORTC",6))
		{
			z=atoi(VARI+7);
			if(z & 32) ledA5->SetValue(1); else ledA5->SetValue(0);
			if(z & 16) ledA4->SetValue(1); else ledA4->SetValue(0);
			if(z & 8) ledA3->SetValue(1); else ledA3->SetValue(0);
			if(z & 4) ledA2->SetValue(1); else ledA2->SetValue(0);
			if(z & 2) ledA1->SetValue(1); else ledA1->SetValue(0);
			if(z & 1) ledA0->SetValue(1); else ledA0->SetValue(0);
			goto salirAna;
		}
	if(0==strncmp(VARI,"RPORTD",6))
		{
			z=atoi(VARI+7);
			if(z & 128) led7->SetValue(1); else led7->SetValue(0);
			if(z & 64) led6->SetValue(1); else led6->SetValue(0);
			if(z & 32) led5->SetValue(1); else led5->SetValue(0);
			if(z & 16) led4->SetValue(1); else led4->SetValue(0);
			if(z & 8) led3->SetValue(1); else led3->SetValue(0);
			if(z & 4) led2->SetValue(1); else led2->SetValue(0);
			goto salirAna;
		}
 salirAna:
 ;		
}




arduwxFrame::~arduwxFrame()
{
    //(*Destroy(arduwxFrame)    //*)
}



void arduwxFrame::OnBexitClick(wxCommandEvent& event)
{
    Close();
}

void leerB(void){ PrintSerial((char *)"IPORTB\n");}
void leerC(void){ PrintSerial((char *)"IPORTC\n");}
void leerD(void){ PrintSerial((char *)"IPORTD\n");}

void arduwxFrame::OnA0_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 14,1\n");	 leerC();
}

void arduwxFrame::OnA0_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 14,0\n");	 leerC();
}

void arduwxFrame::OnA1_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 15,1\n");	 leerC();
}

void arduwxFrame::OnA1_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 15,0\n");	 leerC();
}

void arduwxFrame::OnA2_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 16,1\n");	 leerC();
}

void arduwxFrame::OnA2_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 16,0\n");	 leerC();
}

void arduwxFrame::OnA3_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 17,1\n");	 leerC();
}

void arduwxFrame::OnA3_0Click(wxCommandEvent& event)
{
 PrintSerial((char *)"SET 17,0\n");	 leerC();
}

void arduwxFrame::OnA4_1Click(wxCommandEvent& event)
{
 PrintSerial((char *)"SET 18,1\n");	 leerC();
}

void arduwxFrame::OnA4_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 18,0\n");	 leerC();
}

void arduwxFrame::OnA5_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 19,1\n");	 leerC();
}

void arduwxFrame::OnA5_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 19,0\n");	 leerC();
}



void arduwxFrame::OnD13_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 13,1\n"); leerB();
}

void arduwxFrame::OnD13_0Click(wxCommandEvent& event)
{		
	PrintSerial((char *)"SET 13,0\n"); leerB();
}


void arduwxFrame::OnD12_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 12,1\n"); leerB();
}

void arduwxFrame::OnD12_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 12,0\n"); leerB();
}

void arduwxFrame::OnD11_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 11,1\n"); leerB();
}

void arduwxFrame::OnD11_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 11,0\n"); leerB();
}

void arduwxFrame::OnD10_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 10,1\n"); leerB();
}

void arduwxFrame::OnD10_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 10,0\n"); leerB();
}

void arduwxFrame::OnD9_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 9,1\n"); leerB();
}

void arduwxFrame::OnD9_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 9,0\n"); leerB();	
}

void arduwxFrame::OnD8_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 8,1\n"); leerB();
}

void arduwxFrame::OnD8_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 8,0\n"); leerB();
}

void arduwxFrame::OnD7_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 7,1\n"); leerD();
}

void arduwxFrame::OnD7_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 7,0\n"); leerD();
}

void arduwxFrame::OnD6_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 6,1\n"); leerD();
}

void arduwxFrame::OnD6_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 6,0\n"); leerD();
}

void arduwxFrame::OnD5_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 5,1\n"); leerD();
}

void arduwxFrame::OnD5_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 5,0\n"); leerD();
}

void arduwxFrame::OnD4_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 4,1\n"); leerD();
}

void arduwxFrame::OnD4_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 4,0\n"); leerD();
}

void arduwxFrame::OnD3_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 3,1\n"); leerD();
}

void arduwxFrame::OnD3_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 3,0\n"); leerD();
}

void arduwxFrame::OnD2_1Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 2,1\n"); leerD();
}

void arduwxFrame::OnD2_0Click(wxCommandEvent& event)
{
	PrintSerial((char *)"SET 2,0\n"); leerD();
}


//__________________________________
int arduwxFrame::SearchPort2(void)
{
//	#define MAXINPUT 100
	int z,zz,nbytes;
	int  ContadorI=0;
	char Binput[MAXINPUT];
	char Binput2[10];
	
 #if YesWindows
		for (z=0;z<11;z++)
 #else
			for (z=0;z<7;z++)
 #endif
			{
				MODEMDEVICE=myport[z];
				printf("Test Port %s\n",MODEMDEVICE);			
				if(0==IniRs232())
				   {
					printf("OK %s\n",MODEMDEVICE);	
					DelayMs(700);
					ContadorI=0;
					for(zz=0;zz<20;zz++)
					   {
						DelayMs(100);
#if YesWindows
						while(( nbytes = ReadSerie(hSerial, Binput2, 1)>0 ))
#else
						while(( nbytes = read(fdrs232, Binput2, 1)>0 ))
#endif
							{
								Binput[ContadorI]=Binput2[0];
								if(ContadorI<MAXINPUT-2)   ContadorI++;
								if (Binput2[0] == '\n')
								   {
									if(ContadorI<2)ContadorI=2;
									Binput[ContadorI-1]=0;
									ContadorI=0;
									if(!strncmp(Binput,CLAVE,5))
									   {
										printf("%s found at %s\n",CLAVE,MODEMDEVICE);
										CloseRs232();
										return z ;
									   }									
								    }
							}
						}

					}
			 }
	printf("%s no found \n",CLAVE);
	return -1;
}



int arduwxFrame::openLed(void)
{
  int z;
  wxString msg;
		if(inicio==1)
		{
			inicio=0;
			z=SearchPort2();
		if(z==-1)
			 {
				 portok=0;
				 printf("No %s\n",CLAVE);
				 msg.Printf(wxT("No encontrado OSCI"));
				 wxMessageBox(msg, wxT("ERROR"), wxOK | wxICON_INFORMATION, this);
				 msg.Printf(wxT("No port"));
				 miPuerto->SetValue(msg);
				 return -1;
			 }
			else
		     {

			MODEMDEVICE=myport[z];
			#if YesWindows
				if(hSerial)CloseRs232();
			#else
				if(fdrs232)CloseRs232();
			#endif
									
			#if DEBUG     
				printf("Init\n");
			#endif
			#if YesWindows
				if(hSerial)CloseRs232();
			#else 
				if(fdrs232)CloseRs232();    
			#endif
			
			if(0==IniRs232())
						{
							msg= wxString::FromUTF8(MODEMDEVICE);
							miPuerto->SetValue(msg+_(" "));      
							printf("SI %s\n",MODEMDEVICE);									    
							portok=1;
						}																		
			 }		 
		}							
 return 0;
}


