function varargout = VChat(varargin)
% VCHAT M-file for VChat.fig
%      VCHAT, by itself, creates a new VCHAT or raises the existing
%      singleton*.
%
%      H = VCHAT returns the handle to a new VCHAT or the handle to
%      the existing singleton*.
%
%      VCHAT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in VCHAT.M with the given input arguments.
%
%      VCHAT('Property','Value',...) creates a new VCHAT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before VChat_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to VChat_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 VChat

% Last Modified by GUIDE v2.5 28-Nov-2011 00:58:06

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @VChat_OpeningFcn, ...
                   'gui_OutputFcn',  @VChat_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 VChat is made visible.
function VChat_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 VChat (see VARARGIN)

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

handles.index = 0;
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = VChat_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;



function ChatWindow_Callback(hObject, eventdata, handles)
% hObject    handle to ChatWindow (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,'String') returns contents of ChatWindow as text
%        str2double(get(hObject,'String')) returns contents of ChatWindow as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in ChatWin.
function ChatWin_Callback(hObject, eventdata, handles)
% hObject    handle to ChatWin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns ChatWin contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ChatWin


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

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ChatBox_Callback(hObject, eventdata, handles)
% hObject    handle to ChatBox (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,'String') returns contents of ChatBox as text
%        str2double(get(hObject,'String')) returns contents of ChatBox as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


 


% --- Executes on key press with focus on pbsend and none of its controls.
function pbsend_KeyPressFcn(hObject, eventdata, handles)

handles.index = handles.index + 1;

str = get(handles.ChatBox,'String');
set(handles.ChatBox,'String','');
% to set matrix dimension for unequal length strings
str(1,50) = 'p';
handles.chatstr(handles.index,:) = ['Prashant:' str];
set(handles.ChatWin,'String',handles.chatstr)
set(handles.ChatWin,'Value',handles.index)
 %string from microcontroller
 
 % simulation for typing
 pause(1)
set(handles.Istyping,'String','t45 is Typing..')
pause(0.5)
set(handles.Istyping,'String','t45 is Typing...')
pause(0.5)
set(handles.Istyping,'String','t45 is Typing....')
pause(0.5)
set(handles.Istyping,'String','t45 is Typing.......')

 str1 = query(handles.obj1, str);
 str1(1,52) = 'p';
handles.index = handles.index + 1;
handles.chatstr(handles.index,:) = ['ATtiny:' str1];
set(handles.ChatWin,'String',handles.chatstr)
set(handles.ChatWin,'Value',handles.index)
 



 


 

% Update handles structure
guidata(hObject, handles);


% hObject    handle to pbsend (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



function ChatWinTxt_Callback(hObject, eventdata, handles)
% hObject    handle to ChatWinTxt (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,'String') returns contents of ChatWinTxt as text
%        str2double(get(hObject,'String')) returns contents of ChatWinTxt as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pbconnect.
function pbconnect_Callback(hObject, eventdata, handles)


status = get(handles.pbconnect,'String')

if strcmp(status,'Connect')

    set(handles.pbconnect,'String','Connecting')
    % Find a serial port object.
    handles.obj1 = instrfind('Type', 'serial', 'Port', 'COM9', 'Tag', '');

    % Create the serial port object if it does not exist
    % otherwise use the object that was found.
    if isempty(handles.obj1)
        handles.obj1 = serial('COM9');
    else
        fclose(handles.obj1);
        handles.obj1 = handles.obj1(1)
    end

    % Connect to instrument object, handles.obj1.
    fopen(handles.obj1);

    % Configure instrument object, handles.obj1.
    set(handles.obj1, 'BaudRate', 112500);
    set(handles.obj1, 'Terminator', {0,0});

    % Communicating with instrument object, handles.obj1.
    data1 = query(handles.obj1, 'm')

    if (data1 == 'm')
        set(handles.pbconnect,'String','Disconnect')
    else
        set(handles.pbconnect,'String','Error')
    end
    
else


% Disconnect from instrument object, handles.obj1.
fclose(handles.obj1);
set(handles.pbconnect,'String','Connect')

end

% hObject    handle to pbconnect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject, handles);