function Dopp
% Miroslav Kasal 2015
% Settings
prompt = {'AzEl.dat Directory:','ComPort:','BaudRate:','Loop [s]:',...
         'Freq [Hz]:','multipl:','T/R [off/-on/-JT]:'};
dlg_title = 'Settings';
defaultans = {'c:\Dop\azel.dat','COM6','9600','0.1','58000000','2','-on'};
answer = inputdlg(prompt,dlg_title,[1 50; 1 11; 1 11; 1 11; 1 11; 1 11; 1 15],defaultans);
azel = answer{1};
ComPort = answer{2};
Baudrate = str2double(answer{3});
t = str2double(answer{4});
NoFq = str2double(answer{5});
k = str2double(answer{6});
TRsw = answer{7};
%----------------------------------------------------
% GUI    
%----------------------------------------------------
h=figure;
set(h,'Name','WSJT-X EME DopplerShift Compensation by OK2AQ & G3WDG',...
    'NumberTitle','off','Menubar','None','position',[360 278 560 180])

h20=uicontrol;
set(h20,'Style','Edit')
set(h20,'Units','normalized')
set(h20,'Position',[0.025 0.1 0.1 0.2])
h201=uicontrol;
set(h201,'Style','Text','String','Period [s]','FontSize',10)
set(h201,'Units','normalized')
set(h201,'Position',[0.025 0.3 0.1 0.1])
h30=uicontrol;
set(h30,'Style','Edit','FontSize',14)
set(h30,'Units','normalized')
set(h30,'Position',[0.875 0.1 0.1 0.2])
h1=uicontrol;
set(h1,'Style','Edit')
set(h1,'Units','normalized')
set(h1,'Position',[0.025 0.6 0.2 0.2])
h11=uicontrol;
set(h11,'Style','Text','String','Frequency','FontSize',10)
set(h11,'Units','normalized')
set(h11,'Position',[0.025 0.8 0.2 0.1])
h2=uicontrol;
set(h2,'Style','Edit')
set(h2,'Units','normalized')
set(h2,'Position',[0.275 0.6 0.2 0.2])
h21=uicontrol;
set(h21,'Style','Text','String','DxDopp','FontSize',10)
set(h21,'Units','normalized')
set(h21,'Position',[0.275 0.8 0.2 0.1])
h3=uicontrol;
set(h3,'Style','Edit')
set(h3,'Units','normalized')
set(h3,'Position',[0.525 0.6 0.2 0.2])
h31=uicontrol;
set(h31,'Style','Text','String','SelfDopp','FontSize',10)
set(h31,'Units','normalized')
set(h31,'Position',[0.525 0.8 0.2 0.1])
h4=uicontrol;
set(h4,'Style','Edit')
set(h4,'Units','normalized')
set(h4,'Position',[0.775 0.6 0.2 0.2])
h41=uicontrol;
set(h41,'Style','Text','String','DxEcho','FontSize',10)
set(h41,'Units','normalized')
set(h41,'Position',[0.775 0.8 0.2 0.1])
u=uibuttongroup;
h5=uicontrol;
set(h5,'Style','Radio','String','None','FontSize',10)
set(h5,'Units','normalized','parent',u)
set(h5,'Position',[0.025 0.4 0.2 0.2])
h6=uicontrol;
set(h6,'Style','Radio','String','Full Doppler','FontSize',10)
set(h6,'Units','normalized','parent',u)
set(h6,'Position',[0.275 0.4 0.2 0.2])
h7=uicontrol;
set(h7,'Style','Radio','String','ConstFreqMoon','FontSize',10)
set(h7,'Units','normalized','parent',u)
set(h7,'Position',[0.525 0.4 0.2 0.2])
h8=uicontrol;
set(h8,'Style','Radio','String','On DxEcho','FontSize',10)
set(h8,'Units','normalized','parent',u)
set(h8,'Position',[0.775 0.4 0.2 0.2])
h9=uicontrol;
set(h9,'Style','Edit')
set(h9,'Units','normalized')
set(h9,'Position',[0.275 0.1 0.2 0.2])
h10=uicontrol;
set(h10,'Style','Edit')
set(h10,'Units','normalized')
set(h10,'Position',[0.525 0.1 0.2 0.2])
h40=uicontrol;
set(h40,'Style','Radio','String','RxFreq','FontSize',10)
set(h40,'Units','normalized','Value',1)
set(h40,'Position',[0.275 0.3 0.2 0.1])
set(h40,'Value',1)
h50=uicontrol;
set(h50,'Style','Radio','String','TxFreq','FontSize',10)
set(h50,'Units','normalized','Value',1)
set(h50,'Position',[0.525 0.3 0.2 0.1])
h60=uicontrol;
set(h60,'Style','Toggle','String','Reset','FontSize',10)
set(h60,'Units','normalized','Value',0)
set(h60,'Position',[0.15 0.09 0.1 0.1])
h65=uicontrol;
set(h65,'Style','Toggle','String','RxFull','FontSize',10)
set(h65,'Units','normalized','Value',0)
set(h65,'Position',[0.15 0.21 0.1 0.1])
h70=uicontrol;
set(h70,'Style','Edit','FontSize',8)
set(h70,'Units','normalized')
set(h70,'Position',[0.75 0.21 0.1 0.1])
h75=uicontrol;
set(h75,'Style','Toggle','String','Stop','FontSize',10)
set(h75,'Units','normalized','Value',0)
set(h75,'Position',[0.75 0.09 0.1 0.1])
h80=uicontrol;
set(h80,'Style','Radio','String','2x','FontSize',10)
set(h80,'Units','normalized','Value',0)
set(h80,'Position',[0.4 0.3 0.1 0.1])
h90=uicontrol;
set(h90,'Style','Radio','String','2x','FontSize',10)
set(h90,'Units','normalized','Value',0)
set(h90,'Position',[0.65 0.3 0.1 0.1])

set([h1 h2 h3 h4 h9 h10],'FontSize',14,'ForegroundColor','blue',...
    'BackgroundColor','white')
set(h20,'FontSize',14,'ForegroundColor','black')
g=0;
e=0;
n=0;

%Serial Port
%--------------------------------------
s = serial(ComPort);
s.Baudrate=Baudrate;
s.StopBits=1;
s.Terminator='LF';
s.Parity='none';
s.FlowControl='none';
s.ReadAsyncMode='continuous';
s.DataTerminalReady='off';
s.Timeout=0.2;
fopen(s)

% Loop
while 1
    % Script for data reading from the file azel.dat
    % ----------------------------------------------
    fid=fopen(azel);
    A=textscan(fid,'%s','delimiter',',');
    fclose(fid);
    T=(A{1}{1});
    F=str2double(A{1}{13});
    DxDopp=round(str2double(A{1}{14}));
    SelfDopp=round(str2double(A{1}{16}));
    TR=(A{1}{19});
    DxEcho=round(2*(DxDopp-SelfDopp/2));
       
    if  get(h5,'Value')==1
        RxFreq=0;
        TxFreq=0;
    end
    if  get(h6,'Value')==1
        RxFreq=DxDopp;
        TxFreq=-DxDopp;
    end   
    if  get(h7,'Value')==1
        RxFreq=round(SelfDopp/2);
        TxFreq=round(-SelfDopp/2);
    end
    if  get(h8,'Value')==1&&get(h65,'Value')==0
        RxFreq=DxDopp-g;
        TxFreq=-SelfDopp;
    end
    if  get(h8,'Value')==1&&get(h65,'Value')==1
        RxFreq=DxDopp;
        TxFreq=DxEcho-DxDopp;
    end
    
set(h20,'String',num2str(t))
set(h1,'String',num2str(F))
set(h2,'String',num2str(DxDopp))
set(h3,'String',num2str(SelfDopp))
set(h4,'String',num2str(DxEcho))
set(h70,'String',T)
    
    if get(h40,'Value')==1&&get(h80,'Value')==0
       RxDDS=NoFq+RxFreq/k;
       set(h9,'String',num2str(RxFreq))
    end
    if get(h40,'Value')==1&&get(h80,'Value')==1
       RxDDS=NoFq+RxFreq;   
       set(h9,'String',num2str(k*RxFreq))
    end
    if get(h50,'Value')==1&&get(h90,'Value')==0
       TxDDS=NoFq+TxFreq/k;
       set(h10,'String',num2str(TxFreq))
    end
    if get(h50,'Value')==1&&get(h90,'Value')==1
       TxDDS=NoFq+TxFreq;
       set(h10,'String',num2str(k*TxFreq))
    end
    
    % Script for diferential Dopp compansation
    % ----------------------------------------
    if  get(h8,'Value')==1&&e==0
        g=DxDopp;
        e=1;
    end
    
    %Tx_Rx Switch according to DSR change
    %------------------------------------
    if TRsw=='off'
       s.PinStatus;
       c=struct2cell(s.PinStatus)';
       d=strcmp(c(3),'off');
        if d==1
           set(h30,'String','Rx','BackgroundColor','Green')
           fprintf(s,['f ',int2str(RxDDS),'\n'])
        end
        if d==0
           set(h30,'String','Tx','BackgroundColor','Red')
           fprintf(s,['f ',int2str(TxDDS),'\n'])
        end
    end
    if TRsw=='-on'
       s.PinStatus;
       c=struct2cell(s.PinStatus)';
       d=strcmp(c(3),'on');
        if d==1
           set(h30,'String','Rx','BackgroundColor','Green')
           fprintf(s,['f ',int2str(RxDDS),'\n'])
        end
        if d==0
           set(h30,'String','Tx','BackgroundColor','Red')
           fprintf(s,['f ',int2str(TxDDS),'\n'])
        end
    end
    if TRsw=='-JT'
        if TR=='R'
           set(h30,'String','Rx','BackgroundColor','Green')
           fprintf(s,['f ',int2str(RxDDS),'\n'])
        end
        if TR=='T'
           set(h30,'String','Tx','BackgroundColor','Red')
           fprintf(s,['f ',int2str(TxDDS),'\n'])
        end
    end
    
    if get(h60,'Value')==1
       set(h9,'Value',1)
       g=0;
       e=0;
       set(h60,'Value',0)
    end
       pause(t)
    if get(h75,'Value')==1
       fprintf(s,['f ',int2str(NoFq),'\n']) 
       fclose(s)
       return
    end
end




