Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

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

% Last Modified by GUIDE v2.5 25-Nov-2009 12:59:23

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @fibreobjgui_OpeningFcn, ...
                   'gui_OutputFcn',  @fibreobjgui_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 fibreobjgui is made visible.
function fibreobjgui_OpeningFcn(hObject, eventdata, handles, mainGuiHandle, serialObject)
% 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 fibreobjgui (see VARARGIN)

% save the main GUI handle to handles
handles.mainGuiHandle = mainGuiHandle;

% saves the serial object to handles
handles.serialObject = serialObject;

% turns the collect data flag off
setappdata(mainGuiHandle, 'collectFibreDataFlag', false)

% checks if its broadcasting or not
broadcast = fibreobjstatus(serialObject);

% defines the objects to be changed when broadcasting or not
handles.objectsToChange = [ handles.ledOffButton;...
                            handles.ledOnButton;...
                            handles.averagesMenu;...
                            handles.timeMenu;...
                            handles.channel1;...
                            handles.channel2;...
                            handles.internalDiode;...
                            handles.noise;...
                            handles.temperature;...
                            handles.reference;...
                            handles.read;...
                            handles.write;...
                            handles.address;...
                            handles.bytes;...
                            handles.value;
                            handles.command];

% enables/disables things
set(handles.objectsToChange, 'Enable', onoff(~broadcast))
    
% sets the value of the broadcasting button
set(handles.broadcastButton, 'Value', broadcast)

% gets the status if broadcasting or not
if broadcast
    % get it out of the serial object
    ledMode = serialObject.UserData.ledMode;
    activeChannels = serialObject.UserData.activeChannels;
    averages = serialObject.UserData.averages;
    delayTime = serialObject.UserData.delayTime;
    
else
    % get it the proper way
    [ledMode, activeChannels] = fibreobjreadconfig(serialObject);
    
    % and the rest
    averages = fibreobjreadaverages(serialObject);
    delayTime = fibreobjreaddelaytime(serialObject);
end

% ticks/unticks the channel options
set(handles.ledOnButton, 'Value', ledMode)
set(handles.ledOffButton, 'Value', ~ledMode)

% defines the channel handles
handles.channels = [handles.channel1;...
                    handles.channel2;...
                    handles.internalDiode;...
                    handles.noise;...
                    handles.temperature;...
                    handles.reference];

% for each channel
for m = 1:numel(handles.channels)
    % sets it
    set(handles.channels(m), 'Value', activeChannels(m))
end

% and the averages and delay time
set(handles.averagesMenu, 'String', num2str(averages))
set(handles.timeMenu, 'String', num2str(delayTime))

% Update handles structure
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function fibreobjgui_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)


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

% unsets the other one
set(hObject, 'Value', 1)
set(handles.ledOffButton, 'Value', 0)

% runs the generic configuration callback
writeConfigCallback(handles)


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

% unsets the other one
set(hObject, 'Value', 1)
set(handles.ledOnButton, 'Value', 0)

% runs the generic configuration callback
writeConfigCallback(handles)


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

% Hint: get(hObject,'Value') returns toggle state of broadcastButton

% starts/stops broadcast
if get(hObject, 'Value')
    % its pressed, so we want to start the broadcasting
    try
        % disables all the rest of the buttons
        set(handles.objectsToChange, 'Enable', 'off')
        
        % empties the response box
        set(handles.response, 'String', '')
        
        % stops it
        fibreobjstart(handles.serialObject)
        
    catch
        % errors if it didn't work
        errortooltip(hObject, 'Could not start data broadcasting.')
        
        % resets the button
        set(hObject, 'Value', 0)
        
        % re-enables everything again
        set(handles.objectsToChange, 'Enable', 'on')
    end
    
else
    % start broadcasting
    try
        % sends it
        fibreobjstop(handles.serialObject)
        
        % enables all the buttons etc.
        set(handles.objectsToChange, 'Enable', 'on')
        
    catch
        % errors
        errortooltip(hObject, 'Could not stop data broadcasting.')
        
        % resets the button
        set(hObject, 'Value', 1)
    end
end


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

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

% gets the string
allValues = get(hObject, 'String');

% gets the value
value = get(hObject, 'Value');

% tries to send that command
try
    % sends it
    fibreobjwriteaverages(handles.serialObject, allValues{value})
    
catch
    % displays an error tooltip if it didn't work
    errortooltip(hObject, 'Did not correctly set the new average number.')
end


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

% Hint: popupmenu 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 timeMenu.
function timeMenu_Callback(hObject, eventdata, handles)
% hObject    handle to timeMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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

% sends the command to change the transmission frequency (time)

% gets the string
allValues = get(hObject, 'String');

% gets the value
value = get(hObject, 'Value');

% tries to send that command
try
    % sends it
    fibreobjwritedelaytime(handles.serialObject, allValues{value})
    
catch
    % displays an error tooltip if it didn't work
    errortooltip(hObject, 'Did not correctly set the new time.')
end


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

% Hint: popupmenu 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 protocolsButton.
function protocolsButton_Callback(hObject, eventdata, handles)
% hObject    handle to protocolsButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% defines the next file
fileName = 'Communications Protocols for the FOR PIC Controller.docx';

% opens the text file with notes in it
if ispc
    % opens the text file in notepad
    winopen(fileName)
    
else
    % gets the full path
    pathName = mfilename('fullpath');
    
    % brings up a dialog
    msgbox(['The file is ', fileparts(pathName), filesep, fileName])
end


% --- Executes when user attempts to close fibreobjgui.
function fibreobjgui_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to fibreobjgui (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% turn the fibre collect data flag back on
setappdata(handles.mainGuiHandle, 'collectFibreDataFlag', true)

% Hint: delete(hObject) closes the figure
delete(hObject);


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

% Hint: get(hObject,'Value') returns toggle state of channel1

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% Hint: get(hObject,'Value') returns toggle state of channel2

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


% generic callback for changing the configuration
function writeConfigCallback(hObject, eventdata, handles)

% sends the command to change all of the configuration
fibreobjwriteconfig(handles.serialObject, logical(get(handles.ledOnButton, 'Value')), logical(cell2mat(get(handles.channels, 'Value')')))

% if any of the non-standard channels are selected, simply a message
if any(hObject == handles.channels(3:end)) && get(hObject, 'Value')
    % bring up a message box
    msgbox('The data for this channel is not stored in the main GUI, but can be accessed using the intensity = fibreobjreadintensity(serialObject) command if necessary.')
end


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

% Hint: get(hObject,'Value') returns toggle state of internalDiode

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% Hint: get(hObject,'Value') returns toggle state of noise

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% Hint: get(hObject,'Value') returns toggle state of Temperature

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% Hint: get(hObject,'Value') returns toggle state of temperature

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% Hint: get(hObject,'Value') returns toggle state of reference

% runs the generic callback
writeConfigCallback(hObject, eventdata, handles)


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

% checks the address
newAddress = str2double(get(hObject, 'String'));

% check it
if newAddress ~= uint8(newAddress)
    % complain
    warntooltip(hObject, 'Address must be an integer from 0 to 254.')
    
    % set it to 0
    set(hObject, 'String', 0)    
end


% --- Executes during object creation, after setting all properties.
function address_CreateFcn(hObject, eventdata, handles)
% hObject    handle to address (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



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

% checks the address
bytes = str2double(get(hObject, 'String'));

% check it
if isnan(bytes) || bytes ~= round(bytes) || bytes < 1 || isinf(bytes)
    % complain
    warntooltip(hObject, 'The number of bytes must be a positive integer.')
    
    % set it to 1
    set(hObject, 'String', '1')
end


% --- Executes during object creation, after setting all properties.
function bytes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bytes (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



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

% checks the value
newValue = str2double(get(hObject, 'String'));

% gets the bytes too
newBytes = str2double(get(hObject, 'String'));

% if the bytes isn't specified yet, go no further
if isnan(newBytes)
    % stop there
    warntooltip(hObject, 'Bytes not specified yet.')
    
elseif newBytes == 3 || bytes > 4
    % also stop there
    warntooltip(hObject, 'Number format is not defined for byte numbers other than 1, 2, or 4.')

else
    % format the maximum number depending on the number of bytes
    switch newBytes
        case 1
            % int8
            maxNumber = intmax('uint8');
            
        case 2
            % int16
            maxNumber = intmax('uint16');
            
        case 4
            % int32
            maxNumber = intmax('uint32');
            
    end
    
    % check the new value
    if newValue ~= round(newValue) || newValue < 0 || newValue > maxNumber
        % complain
        warntooltip(hObject, sprintf('Value must be an integer from 0 to %d.', maxNumber))
    end
end


% --- Executes during object creation, after setting all properties.
function value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to value (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 command.
function command_Callback(hObject, eventdata, handles)
% hObject    handle to command (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% gets the write status
if get(handles.write, 'Value')
    % set the type to write
    type = 'write';
    
    % gets the value too
    value = str2double(get(handles.value, 'String'));
    
else
    % reading instead
    type = 'read';
end

% gets the rest
address = str2double(get(handles.address, 'String'));
bytes = str2double(get(handles.bytes, 'String'));

% sends the command
try
    % depends...
    if strcmp(type, 'write')
        % sends it
        fibreobjcomm(type, handles.serialObject, address, bytes, value)

    else
        % reads it out
        set(handles.response, 'String', num2str(fibreobjcomm(type, handles.serialObject, address, bytes)))
    end

catch
    % brings an an error
    errortooltip(hObject, 'Error: could not send command.')
end


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


% --- Executes during object creation, after setting all properties.
function response_CreateFcn(hObject, eventdata, handles)
% hObject    handle to response (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

Contact us