/***************************************************************
 * Name:      wxosciv5ALL
 * Purpose:   Code for Application Frame
 * Author:    seta43 ()
 * Created:   2018-05-20
 * Copyright: seta43 ()
 * Versión 3	14/03/2020
 * Para usar con osciSDLv5.ino
 * xsetaseta@gmail.com
 *  seta43.duckdns.org
 **************************************************************/

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


//#define  BOTONES 1

char CLAVE[] = "OSCI5";

//_________________________

#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	


//Velocidad de muestreo
// windows lento
#if YesWindows 
    #define XTIEMPO3	20
#else
	#define XTIEMPO3	20
#endif

#define MAXENTRADA 50
#define MAXINPUT 1030

char buffer[MAXENTRADA];

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

int tiempo0=6,tiempo2=2,tiempo1=2, tiempo3=2;
int  DatosPan[MAXINPUT];
int	 IndexDatos;
int  xtiempo2=20;

char VARI[100];


int DIVISION=16;
int MUESTRAS;
int MUESTREO;

int MODO=0;
int MODOLINE=0;

int DESPLAZAMIENTO;
int ZOOM=1;

int V33,VMAXR;

bool V1v1;

unsigned char portok;

bool inicio=1;

        #define ID_RADIOBUTTON1	1000
        #define ID_RADIOBUTTON2	1001
        #define ID_RADIOBUTTON3  1002
        #define ID_RADIOBUTTON4  1003
        #define ID_STATICTEXT1  1004
        #define ID_TVOLTIOS  1005
        #define ID_STATICTEXT2  1006
        #define ID_TDIVISOR  1007
        #define ID_STATICTEXT3  1008
        #define ID_EXIT  1009
        #define ID_DEVICE  1010
        #define ID_CONSOLA  1011
        #define ID_MODODIBUJO  1012
        #define ID_STIMER  1013
        #define ID_SDIVISOR  1014
        #define ID_SZOOM  1015
        #define ID_SDESPLAZAMIENTO  1016
        #define ID_STATICTEXT4  1017
        #define ID_ACERCA  1018
		#define ID_BUP_T	1019
		#define ID_DOWN_T	1020
		#define ID_STATUSBAR1  1021
		#define ID_TIMER1  1022
		#define ID_VREF 1022

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

BEGIN_EVENT_TABLE(wxosciv2Frame,wxFrame)
 
END_EVENT_TABLE();



wxosciv2Frame::wxosciv2Frame(wxWindow* parent,wxWindowID id)
{

   //(*Initialize(wxosciv2Frame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
#if YesWindows
    SetClientSize(wxSize(810,500));
    SetMinSize(wxSize(810,500));
    SetMaxSize(wxSize(810,500));
#else
    SetClientSize(wxSize(810,490));
    SetMinSize(wxSize(810,490));
    SetMaxSize(wxSize(810,490));
#endif	
    SetBackgroundColour(wxColour(217,222,66));
    {
    	wxIcon FrameIcon;
    	FrameIcon.CopyFromBitmap(wxBitmap(wxImage(_T("wxosciv2.png"))));
    	SetIcon(FrameIcon);
    }
    RadioButton1 = new wxRadioButton(this, ID_RADIOBUTTON1, _("Auto"), wxPoint(16,320), wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON1"));
    RadioButton2 = new wxRadioButton(this, ID_RADIOBUTTON2, _("Tri. UP"), wxPoint(16,352), wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON2"));
    RadioButton3 = new wxRadioButton(this, ID_RADIOBUTTON3, _("Tri. DOWN"), wxPoint(16,384), wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON3"));
    RadioButton4 = new wxRadioButton(this, ID_RADIOBUTTON4, _("Voltimetro"), wxPoint(16,416), wxDefaultSize, 0, wxDefaultValidator, _T("ID_RADIOBUTTON4"));
    RadioButton4->SetValue(true);
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("A0"), wxPoint(128,414), wxSize(33,14), 0, _T("ID_STATICTEXT1"));
    wxFont StaticText1Font(13,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    Tvoltios = new wxTextCtrl(this, ID_TVOLTIOS, _("Volt"), wxPoint(160,408), wxSize(96,30), wxTE_READONLY, wxDefaultValidator, _T("ID_TVOLTIOS"));
    wxFont TvoltiosFont(16,wxFONTFAMILY_TELETYPE,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Courier 10 Pitch"),wxFONTENCODING_DEFAULT);
    Tvoltios->SetFont(TvoltiosFont);
    StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("TIMER"), wxPoint(168,364), wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    Tdivisor = new wxTextCtrl(this, ID_TDIVISOR, _("divisor"), wxPoint(304,416), wxSize(56,22), wxTE_READONLY, wxDefaultValidator, _T("ID_TDIVISOR"));
    wxFont TdivisorFont(9,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    Tdivisor->SetFont(TdivisorFont);
    StaticText3 = new wxStaticText(this, ID_STATICTEXT3, _("OSCIwx v3"), wxPoint(120,304), wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    StaticText3->SetForegroundColour(wxColour(104,10,30));
    wxFont StaticText3Font(18,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    StaticText3->SetFont(StaticText3Font);
    Bexit = new wxButton(this, ID_EXIT, _("Exit"), wxPoint(672,400), wxDefaultSize, 0, wxDefaultValidator, _T("ID_EXIT"));
    //Bdevice = new wxComboBox(this, ID_DEVICE, wxEmptyString, wxPoint(600,300), wxSize(152,30), 0, 0, 0, wxDefaultValidator, _T("ID_DEVICE"));
    //Bdevice->SetSelection( Bdevice->Append(_("Select Device")) );
    
    Svref = new wxChoice(this, ID_VREF, wxPoint(420,376), wxSize(80,30), 0, 0, 0, wxDefaultValidator, _T("ID_VREF"));
    Svref->SetSelection( Svref->Append(_("VCC")) );
    Svref->Append(_("1V1"));
    
    
    Bconsola = new wxTextCtrl(this, ID_CONSOLA, _("---"), wxPoint(580,336), wxSize(172,22), wxTE_READONLY, wxDefaultValidator, _T("ID_CONSOLA"));
    wxFont BconsolaFont(7,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    Bconsola->SetFont(BconsolaFont);
    BModoDibujo = new wxToggleButton(this, ID_MODODIBUJO, _("LINE"), wxPoint(408,344), wxSize(64,24), 0, wxDefaultValidator, _T("ID_MODODIBUJO"));
    BModoDibujo->SetValue(true);
    Stimer = new wxSpinCtrl(this, ID_STIMER, _T("20"), wxPoint(216,360), wxSize(40,30), 0, 7, 30, 20, _T("ID_STIMER"));
    Stimer->SetValue(_T("20"));
    Sdivisor = new wxChoice(this, ID_SDIVISOR, wxPoint(304,376), wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_SDIVISOR"));
    Sdivisor->Append(_("8.880 m/s"));
    Sdivisor->Append(_("17.900 m/s"));
    Sdivisor->Append(_("35.500 m/s"));
    Sdivisor->SetSelection( Sdivisor->Append(_("66.000 m/s")) );
    Sdivisor->Append(_("124.000 m/s"));
    Sdivisor->Append(_("212.000 m/s"));
    Szoom = new wxChoice(this, ID_SZOOM, wxPoint(304,344), wxSize(96,32), 0, 0, 0, wxDefaultValidator, _T("ID_SZOOM"));
    Szoom->SetSelection( Szoom->Append(_("ZOOMx1")) );
    Szoom->Append(_("ZOOMx2"));
    Szoom->Append(_("ZOOMx3"));
    Szoom->Append(_("ZOOMx4"));
    Szoom->Append(_("ZOOMx5"));
    Szoom->Append(_("ZOOMx6"));
    Szoom->Append(_("ZOOMx7"));
    Szoom->Append(_("ZOOMx8"));
    Szoom->Append(_("ZOOMx9"));
    Szoom->Append(_("ZOOMx10"));
    Sdesplazamiento = new wxScrollBar(this, ID_SDESPLAZAMIENTO, wxPoint(0,272), wxSize(800,16), wxSB_HORIZONTAL, wxDefaultValidator, _T("ID_SDESPLAZAMIENTO"));
    Sdesplazamiento->SetScrollbar(0, 1, 800, 1);
    StaticText4 = new wxStaticText(this, ID_STATICTEXT4, _("F=(muestras/s)/(9.8666*divisiones)"), wxPoint(288,312), wxDefaultSize, 0, _T("ID_STATICTEXT4"));
    wxFont StaticText4Font(10,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Sans"),wxFONTENCODING_DEFAULT);
    StaticText4->SetFont(StaticText4Font);
    Bacerca = new wxButton(this, ID_ACERCA, _("Acerca"), wxPoint(560,400), wxDefaultSize, 0, wxDefaultValidator, _T("ID_ACERCA"));
    BUP_T = new wxButton(this, ID_BUP_T, _("*"), wxPoint(112,352), wxSize(24,24), 0, wxDefaultValidator, _T("ID_BUP_T"));
    BDOWN_T = new wxButton(this, ID_DOWN_T, _("*"), wxPoint(112,384), wxSize(24,24), 0, wxDefaultValidator, _T("ID_DOWN_T"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, ID_EXIT, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, ID_ACERCA, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
   
    Timer1.SetOwner(this, ID_TIMER1);
    Timer1.Start(10);
       
    Connect(ID_EXIT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxosciv2Frame::OnQuit);
	Connect(ID_EXIT,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnQuit);
    Connect(ID_ACERCA,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnAbout);
	Connect(ID_ACERCA,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxosciv2Frame::OnAbout);
	
	Connect(ID_RADIOBUTTON1,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnRadioButton1);
    Connect(ID_RADIOBUTTON2,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnRadioButton1);
    Connect(ID_RADIOBUTTON3,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnRadioButton1);
    Connect(ID_RADIOBUTTON4,wxEVT_COMMAND_RADIOBUTTON_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnRadioButton1);
	
	Connect(ID_MODODIBUJO,wxEVT_COMMAND_TOGGLEBUTTON_CLICKED,(wxObjectEventFunction)&wxosciv2Frame::OnModoDibujo);
	
	Connect(ID_STIMER,wxEVT_COMMAND_SPINCTRL_UPDATED,(wxObjectEventFunction)&wxosciv2Frame::OnStimerChange);
    Connect(ID_SDIVISOR,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnSdivisorSelect);
    Connect(ID_SZOOM,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnSzoomSelect);
    
    Connect(ID_VREF,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnVrefSelect);
	
	Connect(ID_SDESPLAZAMIENTO,wxEVT_SCROLL_CHANGED,(wxObjectEventFunction)&wxosciv2Frame::OnSdesplazamientoScrollChanged);
	
	Connect(ID_TIMER1,wxEVT_TIMER,(wxObjectEventFunction)&wxosciv2Frame::OnTimer1Trigger);
	Connect(wxEVT_PAINT,(wxObjectEventFunction)&wxosciv2Frame::OnPaint);
	
	Connect(ID_DEVICE,wxEVT_COMMAND_COMBOBOX_SELECTED,(wxObjectEventFunction)&wxosciv2Frame::OnBdeviceSelected);
	
	Connect(ID_BUP_T,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxosciv2Frame::OnBUP_TClick);
    Connect(ID_DOWN_T,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxosciv2Frame::OnBDOWN_TClick);
      
	m_bitmap = new wxBitmap(800, 270, 32); 
	dcBuffer=new wxMemoryDC(*m_bitmap);  
	 	
	
	ModifiG();
	openOsci();

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

}



void wxosciv2Frame::OnPaint(wxPaintEvent& event)
{
#if BOTONES
	printf("Boton PAINT \n");
#endif
		
		if(!dcPantalla)
			{
				dcPantalla=new wxClientDC(this);
				dcBuffer->Clear();
			}	

		Repintado();

#if YesWindows
	wxPaintDC dc(this);
	dc.Blit(0,0,800,270, dcBuffer, 0, 0, wxCOPY, true);	
#else
	dcPantalla->Blit(0,0,800,270, dcBuffer, 0, 0, wxCOPY, true);	
#endif	

}

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

void wxosciv2Frame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void wxosciv2Frame::OnAbout(wxCommandEvent& event)
{
  wxString msg;
		msg.Printf(wxT("wxOsciloscopioV5\nxsetaseta@gmail.com\n%s"),    wxVERSION_STRING);
		wxMessageBox(msg, wxT("SETA43"), wxOK | wxICON_INFORMATION, this);
}

void wxosciv2Frame::OnRadioButton1(wxCommandEvent& event)
{
	if(RadioButton4->GetValue())		{		MODO=0; 		}
	if(RadioButton1->GetValue())		{		MODO=1; 		}
	
	if(RadioButton2->GetValue())		
		{
		 MODO=2;
		 if(MODO==1)wxMilliSleep(300);
		 MODO=2; 
		 FinContador=0;
		 ContadorI=0;
		 PrintSerial((char *)"GETUP=\n"); 
		}
	if(RadioButton3->GetValue())
		{
		 MODO=3;
		 if(MODO==1)wxMilliSleep(300);
		 MODO=3; 
		 FinContador=0;
		 ContadorI=0;
		 PrintSerial((char *)"GETDOWN=\n"); 
		}

#if BOTONES
	printf("Evento de Radio MODO=%d\n",MODO);
#endif	
}

void wxosciv2Frame::OnModoDibujo(wxCommandEvent& event)
{
	if(BModoDibujo->GetValue())
        {
          MODOLINE=0;
          BModoDibujo->SetLabel(_("LINE"));         
        }
        else
        {

          MODOLINE=1;
          BModoDibujo->SetLabel(_("POINT"));
        }
#if BOTONES        
       printf("Evento de MODOLINEA=%d\n",MODOLINE);
#endif       
}

void wxosciv2Frame::OnBdeviceSelected(wxCommandEvent& event)
{

}

void wxosciv2Frame::OnStimerChange(wxSpinEvent& event)
{
		xtiempo2=Stimer->GetValue();
		ModifiG();
#if BOTONES
	printf("Evento de TimerChange=%d\n",xtiempo2);
#endif	
}

void wxosciv2Frame::OnSdivisorSelect(wxCommandEvent& event)
{
	int z;
 wxString msg;
 
		z=Sdivisor->GetSelection();

	switch(z) 
	{
    case 0:
		sprintf(VARI,"DIV128");   
    break;
	case 1:
		sprintf(VARI,"DIV64");   
    break;
    case 2:
		sprintf(VARI,"DIV32");   
    break;
    case 3:
		sprintf(VARI,"DIV16");   
    break;
    case 4:
		sprintf(VARI,"DIV8");   
    break;
    case 5:
		sprintf(VARI,"DIV4");   
    break;
	}
	
	msg= wxString::FromUTF8(VARI);
	Tdivisor->SetValue(msg); 										
	PrintSerial(VARI);	PrintSerial((char *)"\n");		
	
#if BOTONES
	printf("Evento de Divisor=%d %s\n",z,VARI);
#endif	
}

void wxosciv2Frame::OnSzoomSelect(wxCommandEvent& event)
{
	int z;
		z=Szoom->GetSelection();
		ZOOM=z+1;
#if BOTONES
	printf("Evento de ZoomSelect=%d\n",z);
#endif	
}


void wxosciv2Frame::OnVrefSelect(wxCommandEvent& event)
{
	int z;
		z=Svref->GetSelection();
	switch(z) 
		{
		case 0:
			sprintf(VARI,"VREF0");
			V1v1=0;   
		break;
		case 1:
			sprintf(VARI,"VREF1");
			V1v1=1;   
		break;
	}
		
	PrintSerial(VARI);	PrintSerial((char *)"\n");	
		
#if BOTONES
	printf("Evento de ZoomSelect=%d\n",z);
#endif	
}


void wxosciv2Frame::OnSdesplazamientoScrollChanged(wxScrollEvent& event)
{
	int z;
	 z=Sdesplazamiento->GetThumbPosition();
	 DESPLAZAMIENTO=z;
#if BOTONES
	printf("Evento de Desplazamiento=%d\n",z);
#endif	
}

void wxosciv2Frame::OnBUP_TClick(wxCommandEvent& event)
{
		RadioButton2->SetValue(1);
		 MODO=2;
		 if(MODO==1)wxMilliSleep(300);
		 MODO=2; 
		 FinContador=0;
		 ContadorI=0;
		 PrintSerial((char *)"GETUP=\n"); 
#if BOTONES
	printf("Evento de GETUP_B\n");
#endif		 	
}

void wxosciv2Frame::OnBDOWN_TClick(wxCommandEvent& event)
{
		 RadioButton3->SetValue(1);
		 MODO=3;
		 if(MODO==1)wxMilliSleep(300);
		 MODO=3; 
		 FinContador=0;
		 ContadorI=0;
		 PrintSerial((char *)"GETDOWN=\n"); 
#if BOTONES
	printf("Evento de TGETDOWN_T\n");
#endif		 	
}

void wxosciv2Frame::OnTimer1Trigger(wxTimerEvent& event)
{
 int nbytes;
 unsigned int z,zz;
  
	tiempo1--;	tiempo2--;	tiempo3--;
	
#if DEBUG  
	tiempo0--;
	if(tiempo0<0)
		{
		 tiempo0=100;
		 printf("1Segundo\n");
		}
#endif	

// ____________________________________
    tiempo2--;
    if(portok==1 && tiempo2<0)
    {
        tiempo2=5;
#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-1)	
					{
						ContadorI++;
						//printf(" <<%d %d %c\n",ContadorI,Binput2[0],Binput2[0]);
						//sprintf(VARI,"Conta=%d",ContadorI);	printConsole(VARI);
						if(ContadorI==820)
							{
							 for(z=10,zz=0;z<810;z++,zz++) DatosPan[zz]=Binput[z];																		
							 Representar(0);
							 if(!strncmp((char *)Binput+810,"FIN",3))
							 {
								 tiempo3=XTIEMPO3;							 
							 }
							}
					}
					else
					{ 	FinContador=1;}									
			}
		}	
// cada __tiempo1
	if(portok==1 && MODO==0 && tiempo1<0)
		{	
			RepresentarVol((unsigned char)Binput[15]);
			tiempo1=xtiempo2;			
			FinContador=0;
			ContadorI=0;
			PrintSerial((char *)"VOL=\n");
		}
// cada __tiempo3 
		if(portok==1 && MODO==1 && tiempo3<0)
		{
			tiempo3=60; //120;			
			FinContador=0;
			ContadorI=0;
			PrintSerial((char *)"GETUP=\n"); 
		}		
}

void wxosciv2Frame::ModifiG()
{
 	
#if BOTONES
	printf("ZOOM=%d    DESPLAZAMIENTO=%d    Timer=%d\n",ZOOM,DESPLAZAMIENTO,xtiempo2);
#endif	
}	

void wxosciv2Frame::Repintado(void)
{	
 int z;
			dcBuffer->SetBrush(*wxWHITE_BRUSH); 
			dcBuffer->SetPen( wxPen( wxColor(0,0,0), 0) ); 
			dcBuffer->DrawRectangle( 0, 0, 800, 270 );

			dcBuffer->SetPen(wxPen(wxColor(0,255,0), 1, wxSOLID));
			for(z=30;z<800;z+=10)	 dcBuffer->DrawLine(z,4,z,260);
			
			
			dcBuffer->SetPen(wxPen(wxColor(0,0,0), 1, wxSOLID));
			for(z=30;z<800;z+=100)	 dcBuffer->DrawLine(z,4,z,260);
			for(z=4;z<261;z+=51) 	 dcBuffer->DrawLine(31,z,800,z);
			
			wxFont  LabelFont = wxFont( 8, wxSWISS, wxFONTSTYLE_NORMAL, wxNORMAL, false, _("Courier") );
			dcBuffer->SetFont( LabelFont );
			if(V1v1==0) 
			{			
			dcBuffer->DrawText(wxT("5"), 5, 0); 
			dcBuffer->DrawText(wxT("2.5"), 5, 128);
			} 
			else
			{
			 dcBuffer->DrawText(wxT("1V1"), 5, 0); 
			 dcBuffer->DrawText(wxT("0.55"), 5, 128);	
			}
			dcBuffer->DrawText(wxT("0"), 5, 246);	
	 						
}	


//________________________________________
void wxosciv2Frame::Representar(int z)
{
  //printf("Representar\n");
  int x,xx;
  long z1;
  xx=0;
  
	for(x=0;x<800 ;x++)
		{
		 z1=(1683*(unsigned char)DatosPan[x])/(V33*10);		
		 DatosPan[x]=z1;
		}	
	Repintado();
	dcBuffer->SetPen(wxPen(wxColor(255,0,0), 1, wxSOLID));

	if(MODOLINE==1)
	{
		 if(ZOOM==1)
			{for(x=DESPLAZAMIENTO;x<800 && xx<800;x++,xx+=ZOOM) dcBuffer->DrawPoint(xx+30,260-DatosPan[x]); 	}
		  else
		    {for(x=DESPLAZAMIENTO;x<798 && xx<800+ZOOM;x++,xx+=ZOOM) dcBuffer->DrawLine(xx+30,260-DatosPan[x],xx+30+ZOOM,260-DatosPan[x]);}		  
	}
	else
	{
		 for(x=DESPLAZAMIENTO;x<798 && xx<800+ZOOM;x++,xx+=ZOOM) dcBuffer->DrawLine(xx+30,260-DatosPan[x],xx+30+ZOOM,260-DatosPan[x+1]);
	}
	dcPantalla->Blit(0,0,800,270, dcBuffer, 0, 0, wxCOPY, true);
}

void wxosciv2Frame::RepresentarVol(int z)
{
 long z1;
 
	z1=(z*3300)/V33;
	if(V1v1==1) { z1*=10; z1/=45;}	
#if DEBUG 	
	//printf("Voltios=%li mV\n",z1);
	//printf("%d   %d",V33,z);	
#endif	
	sprintf(VARI,"%limV",z1);
	wxString msg(VARI, wxConvUTF8);
    Tvoltios->SetValue(msg); 

	z1=(1683*z)/(V33*10);
	z=z1;

    Repintado();
    dcBuffer->SetPen(wxPen(wxColor(0,0,255), 1, wxSOLID));	
	dcBuffer->DrawLine(0,259-z,800,259-z);
	dcPantalla->Blit(0,0,800,270, dcBuffer, 0, 0, wxCOPY, true);	
}


int wxosciv2Frame::openOsci(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"));
				 Bconsola->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
				MiBAUDIOS=CBR_115200;
				if(hSerial)CloseRs232();
			#else 
				MiBAUDIOS=B115200;
				if(fdrs232)CloseRs232();    
			#endif
			
			if(0==IniRs232())
						{
							msg= wxString::FromUTF8(MODEMDEVICE);
							Bconsola->SetValue(msg+_(" :115200,8,N,1"));      
							printf("SI %s\n",MODEMDEVICE);		    
							portok=1;
							V33=test3v3();
							PrintSerial((char *) "DIV16");	PrintSerial((char *)"\n");
						}																		
			 }		 
		}							
 return 0;
}

//______________________test 3.3V	
int wxosciv2Frame::test3v3(void)
{
 unsigned int zz;
 int nbytes;
 
 ContadorI=0;	
 printf("Test 3V3");
 
// Vaciar Buffer 
#if YesWindows
	DelayMs(2000);
	PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);	
#else
	DelayMs(2000);		
	tcflush(fdrs232,TCIOFLUSH); 
#endif			
//______________________________________________		
 PrintSerial((char *)"TEST=\n");

	for(zz=0;zz<15;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];
			  //printf(" %d %d\n",ContadorI,Binput2[0]);
			  if(ContadorI<MAXINPUT-1)	
					{
					 ContadorI++;
					 if(ContadorI>29) goto salir;						
					}													
			}
		}
salir:
	zz=(unsigned char)Binput[10]+(unsigned char)Binput[11]+(unsigned char)Binput[12]+(unsigned char)Binput[13];
	zz=zz>>2;
	printf(" Test 3v3=%d\n",zz);
	//printf(" ---%s\n",Binput);
	return(zz);
}



//__________________________________
int wxosciv2Frame::SearchPort2(void)
{
//	#define MAXINPUT 100
	int z,zz,nbytes;
	int  ContadorI=0;
	char Binput[MAXINPUT];
	char Binput2[10];
	
	 wxString msg;

 #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())
				   {
					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))
									   {
										//tcflush(fdrs232,TCIOFLUSH);
										printf("%s found at %s\n",CLAVE,MODEMDEVICE);
										CloseRs232();
										return z ;
									   }
								    }
							}
						}

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




