%tempinput.m

%main MATLAB program that is called to start the commuication of the ATMEL

%this program opens the serial port and tells the ATMEL to start transmitting

%the temperature data bitstream

%program also calls UpdateTempGraph, UpdateHTML and get_threshold when needed

 

%opens serial port, com2 to communicate with the ATMEL

HANDLE = cportopen('com2');

 

%sets all of the necessary parameters to communicate with the ATMEL

stat = cportconfig(HANDLE,'BaudRate','9600','fDtrControl','OFF','ReadTotalTimeoutMultiplier','1');

 

%clears the serial port line

cportreset(HANDLE);

 

%TempData hold the values of the temperature

%Initialize the temperature data values to null

TempData = [];

 

%TempDate holds the time of when the temperature was taken

%Initialize the temperature date values to null

TempDate = [];

 

%TempDateS holds the time of when the temperature was taken

% in a string format

TempDateS = [];

 

%clears err last error message

lasterr('');

 

%zeros random loop varibles

printgraph = 0;

resetgraph = 0;

stop = 0;

not6char = 0;           

gottaG = 0;

newthresh = '000';

donewithgui = 0;

 

%tell ATMEL to GO!

temp=cportwrite(HANDLE,'g','');

 

%get the data and run until there is an error or user hits control-C

while(~stop)

   try

      while (1)

         %get the current temp, fan and button status

         %the current Data being read in from the ATMEL

         newTempData = [];

         while((not6char~=6)) 

            temp = cportgetchar(HANDLE,1); 

            if (isempty(temp))

               disp 'WAITING for ATMEL'

               overnow = 0;

               stopnow = now;

               while((stopnow + 1e-5) > overnow)

                  overnow = now;

               end  

            else

               newTempData = [newTempData,temp];

               not6char = size(newTempData,2);

            end

         end

         not6char = 0;

         if(newTempData(1) ~= 't')

            disp 'bitline not alligned'

            newTempData

            stop = 1;

         else

            newTempData=newTempData(2:6);

            newTempData=double(newTempData)-48;  

         end

         if (newTempData(5))

            close all;

            get_threshold;

            uiwait;

            %tell the ATMEL the Thereshold Temp

            temp=cportwrite(HANDLE,newthresh,'');

            gottaG = 0;

            while(~gottaG) 

               temp = cportgetchar(HANDLE,1);

               %wait for the ATMEL to get the data and respond with 'g'

               if (isempty(temp))

                  disp 'WAITING for ATMEL g'

                  overnow = 0;

                  stopnow = now;

                  while((stopnow + 1e-5) > overnow)

                     overnow = now;

                  end  

               else

                  gottaG = (temp(1) =='g');             

               end

            end

         end

         if(~gottaG)

            %updates the HTML file

            UpdateHTML

            %updates the graph and logging of Temperature data

            UpdateTempGraph

         else

            gottaG = 0;

         end

         while (~go)

            %wait for Update functions to finish

         end

      end %while(1)

   catch  %if uart connection is lost or user hits control 'c'

      if(~isempty(lasterr)) %an error occured

         lasterr

         disp 'error occured'

         cportclose(HANDLE);

      end

      stop = 1; % stop the program when one hits ctr c

   end %end catch loop

end %while(~stop) loop , main outsode loop

 

%close serial port

cportclose(HANDLE);