% -----------------------------------------------------------------------
% First.m
% A test program for MATLAB and the Phidgets Interface Board.
%
% This is written in an "object-oriented" style.
% Original programming by Dr. Hamzeh Roumani, 2014.
% -----------------------------------------------------------------------

% -------------------------------------------------------------
% The "class" definition, it's based on c4e.InterfaceListener
% [Don't worry about this for now.]
% -------------------------------------------------------------
classdef First < c4e.InterfaceListener
   
    % --------------------------------------------------
    % The properties used internally in the code.
    % [Don't worry too much about this for now].
    % ---------------------------------------------------
   properties
      interface, timerCallbackDelay = 100;
      count = 0;
   end
    
   % -------------------------------------------------------------
   % Methods is where all the real work happens.
   % [Not terribly important for you to know about this in detail.]
   % -------------------------------------------------------------
   methods 
      function this = First()
         pollingRate = 125;                                             % How often MATLAB talks to the Phidgets over the USB
         engine = c4e.Engine(pollingRate, this.timerCallbackDelay);     % Set up the "engine", the MATLAB<->Phidget communication routine
         engine.addPhidgetListener(this);                               % Let MATLAB "listen" to data stream from Phidget board.
         engine.start();                                                % Start the listening process.
         this.interface = engine.getInterfacePhidget();                 % "this" is a copy of the "engine" process
         this.main();                                                   % Call the "main" function
         engine.stop();                                                 % When main function terminates, stop the engine process.
      end   
      
      % ----------------------------------- 
      % This is the main function
      % [Here is where things start to get interesting.]
      % -----------------------------------
      function main(this)
         disp('Press ENTER to start ...'); pause;                     % Wait for user input.
         %this.interface.setOutputState(3, 1);                        % Set output pin 3 to value Logic value 1  
         %pause(2);                                                   % Wait for two seconds after changing the digital output.
         disp('Press ENTER to terminate ...'); pause;                 % Wait for user input.
         %fprintf('Digital output changed %d times.', this.count);    % Print a message about the digital output.
      end

      % ----------------------------------- 
      % These are event handlers: 
      %     do one something in MATLAB when something occurs with the
      %     Phidgets board.
      %     Because the main function has "this" as an argument, and so
      %     do these event handlers then actions can be taken by the envent
      %     handlers when main is running.
      % -----------------------------------
      
      % Event handler 1: Attach
      function attached(this, event)
         fprintf('Attach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end   % END of function attached
      
      % Event handler 2: Detach
      function detached(this, event)
         fprintf('Detach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end   % END of function detached
      
      % Event handler  3: Digital input changed
      function diChange(this, event)
         fprintf('Digital input #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());     
      end   % END of function diChange
      
      % Event handler 4: Analog input changed
      function aiChange(this, event)
         fprintf('Analog input #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end   % END of function aiChange
      
      % Event handler 5: Digital ouput changed
      function doChange(this, event)
         this.count = this.count + 1;
         fprintf('Digital output #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end  % End of function doChange.
      
      % -- No more event handlers. --

   end  % END of methods
   
end  % END of class definition, classdef First < c4e.InterfaceListener

