#include "page1.h"

// -------------------------------+ 
//    form  page1
// -------------------------------+ 
void form_page1(void)
{
  object(TPage,(void **)&page1,page1_Body,0,0,&Null,None,1,0,"PAGE");
}

// -------------------------------
// methods:
// -------------------------------

void scan_coms(void)
{
    
    int n,ret;
    if (Co1)
    {
        sprintf(Co1->items[-1],"SCAN");
        Co1->numitems=0;
        for (n=1;n<16;n++)
        {
            progress=n;
            sprintf(vna_port_name,"COM%d",(int)n);
            assign_com(vna_com,vna_com);
            ret=com_open(vna_port_name,300);
            wait(10);
            com_close(vna_com);
            if(ret<0) continue;
            sprintf(Co1->items[Co1->numitems++],"COM%d",n);
        }
        co1=-1;
    }
}

void Event(connect_onmousedown)()
{
    vna_post_command(VNA_CMD_CONNECT);
}

void Event(disconnect_onmousedown)()
{
    vna_post_command(VNA_CMD_DISCONNECT);
}

void Event(cal_open_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_OPEN);
}

void Event(cal_short_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_SHORT);
}

void Event(cal_load_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_LOAD);
}

void Event(cal_isol_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_ISOL);
}

void Event(cal_thru_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_THRU);
}

void Event(cal_done_onmousedown)()
{
    vna_post_command(VNA_CMD_CAL_DONE);
}

void Event(cal_save_onmousedown)()
{
    v_tik(3);
    FILE *fp;
    if(vna_state!=VNA_STATE_CONNECTED)return;
    strcpy(Dialog1->FileType,"*.cal");
    strcpy(Dialog1->FileName,"");
    strcpy(Dialog1->Title,"   Save file");
    Dialog1->Scala=0.6;
    Dialog1->FullScreen=8;       // 0 dimensioni dichiarate 1 pieno schermo  2 open dialog windows 13-09-2011
    if (Dialog1->Execute())
    {
        remblank(Dialog1->FileName);
        sprintf(vna_cal_file,"%s",Dialog1->FileName);
        if (!strstr(vna_cal_file,".cal")) sprintf(vna_cal_file,"%s.cal",Dialog1->FileName);
        vna_post_command(VNA_CMD_CAL_DONE);
        wait(500);
        vna_post_command(VNA_CMD_CAL_SAVE);
    }
}

void Event(cal_recall_onmousedown)()
{
    v_tik(3);
    FILE *fp;
    if(vna_state!=VNA_STATE_CONNECTED)return;
    strcpy(Dialog1->FileType,"*.cal");
    strcpy(Dialog1->FileName,"");
    strcpy(Dialog1->Title,"   Load file");
    Dialog1->Scala=0.6;
    Dialog1->FullScreen=8;       // 0 dimensioni dichiarate 1 pieno schermo  2 open dialog windows 13-09-2011
    if (Dialog1->Execute())
    {
        remblank(Dialog1->FileName);
        sprintf(vna_cal_file,"%s",Dialog1->FileName);
        vna_post_command(VNA_CMD_CAL_RECALL);
    }
    wait(500);
    vna_post_command(VNA_CMD_CAL_DONE);
}

void Event(scan_onmousedown)()
{
    static float rlc_valold=0;
    rlc_ok=do_rlc();               //buono per capacità e resistenze
    if (rlc_ok)
    if (rlc_val)                   //2 letture consecutive quasi uguali
    {
       float ratio=rlc_valold/rlc_val;
       if (ratio > 1.2) rlc_ok=0;
       if (ratio < 0.8) rlc_ok=0;
    }
    rlc_valold = rlc_val;
    vna_data_valid=0;              //nuova scansione
    vna_post_command(VNA_CMD_SCAN);
}

void Event(abort_onmousedown)()
{
    vna_abort=1;
    contin=0;
}

// ------------------------------- 
// methods: 
// ------------------------------- 
void Event(onentry)()
{
    vna_f_start=f_start*1000000;
    vna_f_stop=f_stop*1000000;
}
// ------------------------------- 
volatile int nn=0;
volatile int okok=0;
void scan_com(void)
{
    if (Co1)
    {
        progress=nn;
        sprintf(vna_port_name,"COM%d",(int)nn);
        assign_com(vna_com,vna_port_name);
        okok=com_open(vna_com,300);
        com_close(vna_com);
    }
    
}

void Event(onfirst)()
{
//  exec_task_hidden(scan_coms,0,0);
    int n,ret;
    if (Co1)
    {
        Co1->numitems=0;
        sprintf(Co1->items[-1],"SCAN");
        for (n=1;n<16;n++)
        {
            nn=n;
            okok=0;
            int ta=exec_task_hidden(scan_com,0,0);
            wait(10);
            remove_task_hidden(ta);
            if(okok!=0) continue;
            sprintf(Co1->items[Co1->numitems++],"COM%d",n);
        }
        co1=-1;
    }
}
// ------------------------------- 
void Event(button1_onmousedown)()
{
    exit(0);
}
// -------------------------------
void Event(panel2_onentry)()
{
    return;
}
// ------------------------------- 
void Event(bitmap1_onentry)()
{
    static int quale_old=0;
    MD1=x_x;
    MD2=y_y;
    MD3=me->width;
    MD4=me->height;
    MD5++;
    static unsigned int store=0;    //vettore in ram di salvataggio pixel quadrante
    if (MD5==5)
    {
        store=(int)calloc((MD3+16)*(MD4+16)*4,1);     //creo un vettore in ram di dimensioni sufficienti
        v_copy(MD1,MD2,store,0,MD3,MD4);  //vi salvo i pixel del quadrante
    }
    if(MD5>8)
    if(vna_data_valid)
    {
        v_copy(store,0,MD1,MD2,MD3,MD4);
        draw_s11_smith(MD10,MD11,MD12);
        if (contin)
        {
            Event(scan_onmousedown());
            return;
        }
    }
    if(!contin)
    if(quale!=quale_old)
    {
        vna_data_valid=1;
        quale_old=quale;
        v_copy(store,0,MD1,MD2,MD3,MD4);
        draw_s11_smith(MD10,MD11,MD12);
    }
}
// ------------------------------- 
// ------------------------------- 
void Event(panel7_onentry)()
{
    MD10=x_x;
    MD11=y_y;
    MD12=me->width;
}
// -------------------------------
void Event(estart_onclick)()
{
    vna_clear_calibration();
}
// ------------------------------- 
void Event(estop_onclick)()
{
    vna_clear_calibration();
}
// ------------------------------- 
void Event(epoints_onclick)()
{
    vna_clear_calibration();
}
// ------------------------------- 
void Event(panel8_onentry)()
{
    if (MB22==4) me->height=399; else me->height=188;
    if(1)
    {
        for (int i=0;i<vna_points;i++)
        {
            float re,im,mag,s11_db;;
            float gamma;
            float ros;
            float fase;
            float r,x,den;
            re = vna_s11_re[i];
            im = vna_s11_im[i];
            gamma = sqrtf(re*re + im*im);
            fase = atan2f(im,re) * 180.0f / 3.141592654f;
            if(gamma < 1.0f)
            ros = (1.0f + gamma) / (1.0f - gamma);
            else
            ros = 9999.0f;   /* oppure infinito */
            if (ros > 1000) ros=1000;
            mag = sqrtf(re*re + im*im);
            if(mag > 0.0f) s11_db = 20.0f * log10f(mag);
            else  s11_db = -120.0f;
            vna_s11_fase[i]=fase;
            vna_s11_ros[i]=ros;
            vna_s11_mag[i]=mag;
            vna_s11_db[i]=s11_db;
/* impedenza complessa Z = R + jX */
            
            den = (1.0f-re)*(1.0f-re) + im*im;
            
            if(den > 1.0e-12f)
            {
                r = 50.0f * (1.0f-re*re-im*im) / den;
                
                x = 50.0f * (2.0f*im) / den;
            }
            else
            {
/* impedenza praticamente infinita */
                r = 999999.0f;
                x = 999999.0f;
            }
            vna_s11_r[i]=r;
            vna_s11_x[i]=x;
            
            {
                float gr = vna_s11_re[i];
                float gi = vna_s11_im[i];
                float den;
                den = (1.0f-gr)*(1.0f-gr) + gi*gi;
                vna_z_re[i] =
                50.0f * (1.0f-gr*gr-gi*gi) / den;
                vna_z_im[i] =
                50.0f * (2.0f*gi) / den;
            }
        }
    }
    if(MB21==0)       MD667=AutoPlot_Y2(vna_s11_mag,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB21==1)  MD667=AutoPlot_Y2(vna_s11_fase,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB21==2)  MD667=AutoPlot_Y2(vna_s11_db,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB21==3)  MD667=AutoPlot_Y2(vna_s11_ros,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
}
// ------------------------------- 
void Event(panel9_onentry)()
{
    if(MB22==4)       me2->visible=0; else me2->visible=1;
    if(MB22==0)       MD668=AutoPlot_Y2(vna_s11_mag,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB22==1)  MD668=AutoPlot_Y2(vna_s11_fase,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB22==2)  MD668=AutoPlot_Y2(vna_s11_db,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
    else if(MB22==3)  MD668=AutoPlot_Y2(vna_s11_ros,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
}
// ------------------------------- 
void Event(label2_onentry)()
{
    if(MB21==0)      me->caption="S11_MAG";
    else if(MB21==1) me->caption="S11_PHASE";
    else if(MB21==2) me->caption="S11_DB";
    else             me->caption="ROS";
}
// ------------------------------- 
void Event(panel11_onmousepress)()
{
    dove=mouse_x-x_x+20;
    lungo=me->width-8;
    if(dove<0) dove=0;
    if (dove > (lungo-4)) dove=lungo-4;
    max=me->width-12;
    quale=(dove*(vna_points-1))/max;
}
// ------------------------------- 
void Event(panel12_onentry)()
{
    static int fase=0;
    fase++;
    if (fase > 30)
    {
        fase=31;
        me->top=2;
    }
    me->left=dove;
}
// ------------------------------- 
void Event(panel13_onmousedown)()
{
    MB21 = (MB21 +1)% 4;
}
// -------------------------------
void Event(label5_onentry)()
{
    if(MB21==0)      me->caption="MAG ";
    else if(MB21==1) me->caption="PHASE";
    else if(MB21==2) me->caption="DB  ";
    else             me->caption="ROS ";
}
// ------------------------------- 
void Event(panel8_onmousepress)()
{
    MD666=MD667;
    quale=MD666;
    dove=(Local->panel11->width-8)*quale/vna_points;
}
// ------------------------------- 
void Event(panel9_onmousepress)()
{
    MD666=MD668;
    quale=MD666;
    dove=(Local->panel11->width-8)*quale/vna_points;
}
// ------------------------------- 
void Event(label3_onentry)()
{
    if(MB22==0)      me->caption="S11_MAG";
    else if(MB22==1) me->caption="S11_PHASE";
    else if(MB22==2) me->caption="S11_DB";
    else             me->caption="ROS";
}
// ------------------------------- 
void Event(panel14_onmousedown)()
{
    MB22 = (MB22 +1)% 5;
}
// ------------------------------- 
void Event(label6_onentry)()
{
    if(MB22==0)      me->caption="MAG ";
    else if(MB22==1) me->caption="PHASE";
    else if(MB22==2) me->caption="DB  ";
    else if(MB22==3) me->caption="ROS ";
    else             me->caption="----";
}
// ------------------------------- 
void Event(panel16_onentry)()
{
    if(1)
    {
//      float vna_s11_fase[VNA_MAX_POINTS];
//      float vna_s11_ros[VNA_MAX_POINTS];
//      float vna_s11_mag[VNA_MAX_POINTS];
//      float vna_s11_db[VNA_MAX_POINTS];
        for(int i=0;i<vna_points;i++)
        {
            float re = vna_s21_re[i];
            float im = vna_s21_im[i];
            vna_s21_mag[i] = sqrtf(re*re + im*im);
            vna_s21_fase[i] = atan2f(im,re) * 57.2957795f;
            if(vna_s21_mag[i] > 1.0e-20f) vna_s21_db[i] = 20.0f * log10f(vna_s21_mag[i]);
            else vna_s21_db[i] = -120.0f;
    }  }
    if(1)       MD667=AutoPlot_Y2(vna_s21_fase,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
}
// ------------------------------- 
void Event(panel17_onentry)()
{
    if(1)       MD668=AutoPlot_Y2(vna_s21_db,vna_points,3,YELLOW,WHITE,GRAY,me->color,"F",0,0,f_start,f_stop);
}
// ------------------------------- 
void Event(panel16_onmousepress)()
{
    MD666=MD667;
    quale=MD666;
    dove=(Local->panel11->width-8)*quale/vna_points;
}
// ------------------------------- 
void Event(panel17_onmousepress)()
{
    MD666=MD668;
    quale=MD666;
    dove=(Local->panel11->width-8)*quale/vna_points;
}
// ------------------------------- 
void Event(edit4_onentry)()
{
    ffreq=(float)vna_frequency[quale]/1000000.;
}
// ------------------------------- 
void Event(edit11_onentry)()
{
    me2->visible=rlc_ok;
    if(rlc_valx < 10) me->decimals=2;
    else if(rlc_valx < 100) me->decimals=1;
    else me->decimals=0;
}
// ------------------------------- 
void Event(bitmap3_onentry)()
{
    me2->visible=rlc_ok;
    if(vna_data_valid) Event(scan_onmousedown());
}
// ------------------------------- 
void Event(label10_onentry)()
{
    me2->visible=rlc_ok;
    if(rlc==0)
    {
        if (rlc_val<1000)
        {
            rlc_valx=rlc_val;
            me->caption="ohm";
        }
        else
        {
            rlc_valx=rlc_val/1000;
            me->caption="Kohm";
        }
    }
    else if(rlc==1)
    {
        if (rlc_val<1000)
        {
            rlc_valx=rlc_val;
            me->caption="nHe";
        }
        else
        {
            rlc_valx=rlc_val/1000;
            me->caption="uHe";
        }
    }
    else if(rlc==2)
    {
        if (rlc_val<1000)
        {
            rlc_valx=rlc_val;
            me->caption="pF";
        }
        else
        {
            rlc_valx=rlc_val/1000;
            me->caption="nF ";
        }
    }
}
// ------------------------------- 
void Event(label15_onentry)()
{
    me2->visible=rlc_ok;
}
// ------------------------------- 
void Event(edit13_onentry)()
{
    me2->visible=rlc_ok;
}
// ------------------------------- 
void Event(bitmap1_onmousepress)()
{
    int pxx=mouse_x+x_x;
    int pyy=mouse_y+y_y;
    quale=0;
    float min=1000000;
    float dist;
    for(int i=0;i<vna_points-1;i++)
    {
//     dist=dista(pxx,pyy,px[i],py[i]);
        dist=(pxx-px[i])*(pxx-px[i]) + (pyy-py[i])*(pyy-py[i]);
        if (dist < min)
        {
            quale=i;
            min=dist;
        }
    }
    dove=(Local->panel11->width-8)*quale/vna_points;
}
// ------------------------------- 
// ------------------------------- 
void Event(panel21_onentry)()
{
    me2->visible=!rlc_ok;
}
// ------------------------------- 
void Event(label2_oncreate)()
{
}