classdef tempcontroller < handle
    %TEMPCONTROLLER Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        ui
        interface
        tempdata
        tempdatapoint
        
        tempplot
    end
    
    methods
        function obj = tempcontroller(int)
           obj.interface = int;
           obj.ui = [];
           obj.tempdata = zeros(5000, 2);
           obj.tempdatapoint = 1;
        end
        
        function show(obj)
            if isempty(obj.ui)
                obj.ui = tempui();
                set(obj.ui, 'UserData', obj);
            else
                set(obj.ui, 'Visible', 'on');
            end
        end
        
        % Just add two data points at a time, one for set temp, one for
        % current temp.
        function adddata(obj, data)
            if obj.tempdatapoint < size(obj.tempdata, 1)
                obj.tempdata(obj.tempdatapoint, :) = data;
                obj.tempdatapoint = obj.tempdatapoint + 1;
            else
                obj.tempdata = vertcat(obj.tempdata, zeros(5000, 2));
                obj.tempdata(obj.tempdatapoint, :) = data;
                obj.tempdatapoint = obj.tempdatapoint + 1;
            end
            if ~isempty(obj.ui) && isempty(obj.tempplot)
                handles = guidata(obj.ui);
                obj.tempplot = plot(handles.axes1, obj.tempdata(1:(obj.tempdatapoint - 1), :));
            elseif ~isempty(obj.ui)
                set(obj.tempplot(1), 'XData', 1:(obj.tempdatapoint - 1));
                set(obj.tempplot(1), 'YData', obj.tempdata(1:(obj.tempdatapoint - 1), 1));
                set(obj.tempplot(2), 'XData', 1:(obj.tempdatapoint - 1));
                set(obj.tempplot(2), 'YData', obj.tempdata(1:(obj.tempdatapoint - 1), 2));
            end
        end
        
        % Just add two data points at a time, one for set temp, one for
        % current temp.
        function adddatablock(obj, data)
            if obj.tempdatapoint + size(data, 1) < size(obj.tempdata, 1)
                obj.tempdata(obj.tempdatapoint:obj.tempdatapoint + size(data, 2) - 1, :) = rot90(data);
                obj.tempdatapoint = obj.tempdatapoint + size(data, 2);
            else
                obj.tempdata = vertcat(obj.tempdata, zeros(size(data, 2)+500, 2));
                obj.tempdata(obj.tempdatapoint:obj.tempdatapoint + size(data, 2) - 1, :) = rot90(data);
                obj.tempdatapoint = obj.tempdatapoint + size(data, 2);
            end
            if ~isempty(obj.ui) && isempty(obj.tempplot)
                handles = guidata(obj.ui);
                obj.tempplot = plot(handles.axes1, obj.tempdata(1:(obj.tempdatapoint - 1), :));
            elseif ~isempty(obj.ui)
                set(obj.tempplot, 'XData', 1:(obj.tempdatapoint -1));
                set(obj.tempplot, 'YData', obj.tempdata(1:(obj.tempdatapoint - 1)));
            end
        end
        
        function clear(obj)
            obj.tempdatapoint = 1;
        end
        
        function delete(obj)
            delete(obj.ui);
        end
        
        function receive(~, ~, ~)
        end
        
        function settemperature(obj, temp)
            if temp >= 15 && temp <= 40
                cd = commanddata();
                cd.Temperature = temp;
                obj.interface.sendcommand(command(cd, commandtype.setpoint, obj));
                obj.interface.sendmessage(['Temperature set to ' num2str(temp) ' degrees C']);
            elseif temp < 15
                obj.interface.sendmessage('Temperature set too low.');
            elseif temp > 40
                obj.interface.sendmessage('Temperature set too high.');
            end
        end
    end
end

