function varargout = w16(varargin)
% W16 MATLAB code for w16.fig
%      W16, by itself, creates a new W16 or raises the existing
%      singleton*.
%
%      H = W16 returns the handle to a new W16 or the handle to
%      the existing singleton*.
%
%      W16('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in W16.M with the given input arguments.
%
%      W16('Property','Value',...) creates a new W16 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before w16_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to w16_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help w16

% Last Modified by GUIDE v2.5 29-May-2017 07:33:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @w16_OpeningFcn, ...
                   'gui_OutputFcn',  @w16_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before w16 is made visible.
function w16_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to w16 (see VARARGIN)

% Choose default command line output for w16
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes w16 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = w16_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slTemp_Callback(hObject, eventdata, handles)
% hObject    handle to slTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slTemp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slTemp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in cmdSense.
function cmdSense_Callback(hObject, eventdata, handles)
% hObject    handle to cmdSense (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ser

set(handles.cmdSense,'Enable','off')
set(handles.cmdStop,'Enable','on')

w = instrfind;
if isempty(w) == 0
    fclose(w);
end

ser = serial('COM4');
st = get(handles.cmdStop,'Enable');

measure(st,handles)


% --- Executes on button press in cmdStop.
function cmdStop_Callback(hObject, eventdata, handles)
% hObject    handle to cmdStop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.cmdSense,'Enable','on')
set(handles.cmdStop,'Enable','off')

w = instrfind;
fclose(w);

st = get(handles.cmdStop,'Enable');

measure(st,handles)


function measure(st,handles)

global ser

while(strcmp(st,'on') == 1)
    
    fopen(ser);

    inf = fread(ser,6);

    pos = find(inf == 4);

    cant = size(pos,1);

    p = 1;
    i = 0;

    if cant ~= 1
        while(p <= cant)
            if pos(p) < 4
                pr = 3 - pos(p);
                pt = 6 - pr;
            else 
                pt = pos(p)-3;
            end

            if inf(pt) == 1
                pos = pos(p);
            end
            p = p + 1;
        end
    end  
    
    if pos == 6
        pos = 0;
    end   
    pos = pos+1;

    l = inf(pos); 
    if pos == 6
        pos = 0;
    end    
    pos = pos+1;

    h = inf(pos);
    
    val = h*256 + l;
    
    V = 2.5-val*5/(20*512);
    R = 10000/(5/V-1);
    B = 3750;
    R25 = 10000;
    
    TC = 1/(log(R/R25)/B+(1/(25+273.15)))-273.15;
    TF = TC*9/5+32;
    
    set(handles.txtC,'String',num2str(TC))
    set(handles.txtF,'String',num2str(TF))
    set(handles.slTemp,'Value',TC)
    
    if TC >= 27
        set(handles.color,'BackgroundColor',[1 0 0])
        set(handles.txtStat,'String','Hot')
    elseif TC <=24
        set(handles.color,'BackgroundColor',[0 0 1])
        set(handles.txtStat,'String','Cold')
    else
        set(handles.color,'BackgroundColor',[0 1 0])
        set(handles.txtStat,'String','Normal')
    end
    
    pause(0.01);
    fclose(ser);
end


