//----------------------------------------------
// DECLARATION of the Program VARIABLES Base
//----------------------------------------------
#define VNA_CMD_NONE          0
#define VNA_CMD_CONNECT       1
#define VNA_CMD_DISCONNECT    2
#define VNA_CMD_CAL_OPEN      3
#define VNA_CMD_CAL_SHORT     4
#define VNA_CMD_CAL_LOAD      5
#define VNA_CMD_CAL_ISOL      6
#define VNA_CMD_CAL_THRU      7
#define VNA_CMD_CAL_DONE      8
#define VNA_CMD_CAL_SAVE      9
#define VNA_CMD_CAL_RECALL   10
#define VNA_CMD_SCAN         11
#define VNA_CMD_ABORT        12
#define VNA_CMD_TERMINATE    13

#define VNA_STATE_IDLE        0
#define VNA_STATE_CONNECTED   1
#define VNA_STATE_BUSY        2
#define VNA_STATE_ERROR       3

int have_open=0;
int have_short=0;
int have_load=0;
int have_isol=0;
int have_thru=0;

float f_start=1;
float f_stop=800;
int contin=0;

#define VNA_MAX_POINTS 2001

U32   vna_frequency[VNA_MAX_POINTS];
float vna_s11_raw_re[VNA_MAX_POINTS];
float vna_s11_raw_im[VNA_MAX_POINTS];
float vna_s21_raw_re[VNA_MAX_POINTS];
float vna_s21_raw_im[VNA_MAX_POINTS];

float vna_s11_re[VNA_MAX_POINTS];
float vna_s11_im[VNA_MAX_POINTS];
float vna_s11_mag[VNA_MAX_POINTS];
float vna_s11_db[VNA_MAX_POINTS];
float vna_s11_fase[VNA_MAX_POINTS];
float vna_s11_ros[VNA_MAX_POINTS];
float vna_s11_r[VNA_MAX_POINTS];
float vna_s11_x[VNA_MAX_POINTS];

float vna_s21_re[VNA_MAX_POINTS];
float vna_s21_im[VNA_MAX_POINTS];
float vna_s21_fase[VNA_MAX_POINTS];
float vna_s21_mag[VNA_MAX_POINTS];
float vna_s21_db[VNA_MAX_POINTS];

float vna_z_re[VNA_MAX_POINTS];
float vna_z_im[VNA_MAX_POINTS];

int px[VNA_MAX_POINTS];
int py[VNA_MAX_POINTS];

volatile int vna_state=VNA_STATE_IDLE;
volatile int vna_abort=0;
volatile int vna_acquired=0;
volatile int vna_data_valid=0;
volatile int vna_cal_valid=0;
char         vna_status[128]="Not connected";

short int vna_port_index=0;
char      vna_port_name[64]="";
U32       vna_f_start=1000000UL;
U32       vna_f_stop=1000000000UL;
U16       vna_points=401;

void vna_clear_calibration(void);

int dove=0,quale=0,lungo=800,max=800;
float ffreq;
char rlc=0;
char slow=0;
float rlc_val=0;
char rlc_ok=0;
float freq_best;
float rlc_valx=0;

COM *vna_com=COM1;

volatile int vna_command=VNA_CMD_NONE;
volatile int progress=0;
char vna_cal_file[128]="vna.cal";
//char FNAME[80];
//char TMPNAME[80];

int hh;
#include "autoplot_y2.h"

void remblank(char *s)
{
    int i;
    for (i=0;i<strlen(s);i++)
    if (s[i]==32) {s[i]=0;return;}
}

void draw_s11_smith(int xc,int yc,int radius)
{
    int i;
    int x1,y1,x2,y2;
    float re,im;
    
    if(!vna_data_valid)
    return;
    
    re=vna_s11_re[0];
    im=vna_s11_im[0];
    
    x1=xc+(int)((float)radius*re);
    y1=yc-(int)((float)radius*im);
    for(i=1;i<vna_acquired;i++)
    {
        px[i-1]=x1;
        py[i-1]=y1;
        re=vna_s11_re[i];
        im=vna_s11_im[i];
        
        x2=xc+(int)((float)radius*re);
        y2=yc-(int)((float)radius*im);
        
        v_linec_antialias(x1,y1,x2-x1,y2-y1,YELLOW,2);
        if (i==1) v_rectfillc(x1-2,y1-2,5,5,YELLOW);
        x1=x2;
        y1=y2;
    }
    i=quale;
    re=vna_s11_re[i];
    im=vna_s11_im[i];
    x1=xc+(int)((float)radius*re);
    y1=yc-(int)((float)radius*im);
    v_linec_antialias(x1-7,y1,15,0,RED,3);
    v_linec_antialias(x1,y1-7,0,15,RED,3);
    
}
#include "vna_litevna.h"
#include "rlc.h"

/*
 Variabili MB1, MW1, MD1, MF1...:
 sono globali predefinite da PROTEUS e possono essere
 usate direttamente dagli oggetti della pagina.
*/

//----------------------------------------------

