%USERS - RUN THIS SCRIPT FIRST
%This script opens the GUI control window for the perimeter


%Initialize and connect to serial port object
s = serial('COM6');
fopen(s);
s.terminator = 'z';
s.baudrate = 115200;


%Initialize GUI
fHand = figure('Units','pixels','Position',[520,290,700,511]);
set(fHand,'Color',[.8 1 .8])

%Define a title tag
title_str = uicontrol('style','text','position',[0 473 217 38],'string',...
    'Perimeter Control Pannel','FontSize',14,'BackgroundColor',[.8 1 .8]); 

%Define a quit button, which closes the window and serial connection when pressed
quitbutton=uicontrol('style','pushbutton',...
    'string','Quit', ...
    'fontsize',12, ...
    'position',[550,450,70,20], ...
    'callback', 'quitfunction(s);');


%Define a set of radio buttons to specify side
chooseSide = uibuttongroup('Units','Pixels','Position',[33 286 120 100],...
    'title','Choose Side');
R_button = uicontrol('Style', 'Radio', 'String', 'Right',...
    'Position',[13, 51, 87, 23],'parent',chooseSide);
L_button = uicontrol('Style', 'Radio', 'String', 'Left',...
    'Position',[13, 28, 87, 23],'parent',chooseSide);
set(chooseSide,'SelectedObject',[]);  % No default selection
set(chooseSide,'SelectionChangeFcn','side = selectSide(chooseSide,R_button,L_button);');
side = 'Z';

%Define a set of radio buttons to specify axis
chooseAxis = uibuttongroup('Units','Pixels','Position',[30 90 182 151],'title','Choose Axis');
xPos_button = uicontrol('Style', 'Radio', 'String', 'Positive X axis',...
    'Position',[13, 101, 138, 23],'parent',chooseAxis);
xNeg_button = uicontrol('Style', 'Radio', 'String', 'Negative X axis',...
    'Position',[13, 78, 138, 23],'parent',chooseAxis);
yPos_button = uicontrol('Style', 'Radio', 'String', 'Positive Y axis',...
    'Position',[13, 51, 138, 23],'parent',chooseAxis);
yNeg_button = uicontrol('Style', 'Radio', 'String', 'Negative Y axis',...
    'Position',[13, 28, 138, 23],'parent',chooseAxis);
set(chooseAxis,'SelectedObject',[]);  % No default selection
set(chooseAxis,'SelectionChangeFcn','[axis direction] = selectAxis(chooseAxis,xPos_button,xNeg_button,yPos_button,yNeg_button);');
axis = 'Z';
direction = 'Z';

%Define a Laser On button for each laser, which toggles the state of the laser when pressed
Rlaser_button = uicontrol('Style','pushbutton','position',[155,349,89,22],'BackgroundColor',[0 1 0],...
    'ForegroundColor',[0 0 0],'String','R Laser On','Callback','lasertoggle(s,Rlaser_button,''R'')');
Llaser_button = uicontrol('Style','pushbutton','position',[155,311,89,22],'BackgroundColor',[0 1 0],...
    'ForegroundColor',[0 0 0],'String','L Laser On','Callback','lasertoggle(s,Llaser_button,''L'')');

%Define a Start Scan button
scan_button = uicontrol('Style','pushbutton','position',[85,400,69,22],'BackgroundColor',[0 .498 0],...
    'ForegroundColor',[1 1 1],'String','Start Scan','Callback','[angleData blindSpotData] = startScan(mode,side,axis,s,direction,angleData,table1,blindSpotData,table2,statusBox);');
angle = 0;
start=0;
finish=0;

%Define commands to set laser blink rate
blink_label = uicontrol('Style','text','string','Set Laser Blink Rate (msec)','units','pixels','position',[247 425 153 18]);
blink_box = uicontrol('Style', 'edit','string','0','Units','pixels','position',[260 400 90 20]);
blink_button = uicontrol('Style','pushbutton','String','Set Blink Rate','units','pixels','position',[360 400 70 22],'callback','bRate = blinkRate(s,blink_box,side);');
bRate = 0;

%Create a table to store returned angles for perimeter mode
angleData = zeros(4,2);
table1 = uitable('units','pixels','Position',[250 150 323 101],'ColumnName',{'Left Eye','Right Eye'},...
   'RowName',{'Positive X Axis','Negative X Axis','Positive Y Axis','Negative Y Axis'},'data',angleData);
table1_label = uicontrol('style','text','string','Perimiter Mode','units','pixels','position',[255,245,90,20]);

%Create a table to store returned angles is blind spot mode
blindSpotData = zeros(4,2);
table2 = uitable('units','pixels','position',[250 10 375 101],'ColumnName',{'Left Eye','Right Eye'},...
    'RowName',{'X Axis Inside Edge','X Axis Outside Edge','Y Axis Inside Edge','Y Axis Outside Edge'},'data',blindSpotData);
table2_label = uicontrol('style','text','string','Blindspot Mode','units','pixels','position',[255,105,90,20]);

%Create a set of radio buttons to select mode
chooseMode = uibuttongroup('Units','Pixels','Position',[260 285 175 100],'title','Choose Mode');
pMode_button = uicontrol('Style', 'Radio', 'String', 'Perimeter Mode',...
    'Position',[13, 51, 150, 23],'parent',chooseMode);
bMode_button = uicontrol('Style', 'Radio', 'String', 'Blindspot Mode',...
    'Position',[13, 28, 150, 23],'parent',chooseMode);
set(chooseMode,'SelectedObject',[]);  % No default selection
set(chooseMode,'SelectionChangeFcn','mode = selectMode(chooseMode,pMode_button,bMode_button);');
mode = 'Z';

%Define a plot button
plotButton = uicontrol('style','pushbutton','string','Plot Visual Field','units','pixels','position',[450 400 150 30],...
    'BackgroundColor',[.749 0 .749],'ForegroundColor',[1 1 1],'Callback','dispWindow; [RP RB LP LB] = createPlot(angleData,blindSpotData); blinkRates = [blinkRates bRate]; h4legend = [h4legend RP]; legend(h4legend,num2str(blinkRates),''location'',''east'');');

%Define a button to save data
saveButton = uicontrol('style','pushbutton','string','Save Data','units','pixels',...
    'position', [450 300 150 30],'Callback','saveMyData(bRate,angleData,blindSpotData,save_box);');
save_box = uicontrol('style','edit','string','PerimiterData.txt','units','pixels','position',[450 270 150 30]);

%Define a button to load data
loadButton = uicontrol('style','pushbutton','string','Load Previous Data','units','pixels',...
    'position', [450 350 150 30],'Callback','[bRate angleData blindSpotData] = loadMyData(table1,table2,blink_box);');

%Define motor status box, which displays the current position of the servo during scans
statusBox = uicontrol('style','text','units','pixels','Position',[50 40 30 20]);


%Wait for Arduino to send ready character
fgetl(s);
fgetl(s);








%Close and delete serial port object
%fclose(s);
%delete(s);