% @since: 2014-01-15, @author: roumani@cse.yorku.ca %
classdef KeyPad < c4e.InterfaceListener
   
   % The following attributes hold the object's state.
   % They are available to all methods in this class.
   properties
      timerCallbackDelay = 50; % in ms
      interface, alarm;
      % add your attributes here
      row = 1;
      column = 1;
      pad = ['1', '2', '3'; '4', '5', '6'; '7', '8', '9'; '*', '0', '#'];
      pin = '';
   end
    
   % The following functions exhibit the object's behavior
   methods
   
       function this = KeyPad()  % Object Constructor, rename but do not modify
         pollingRate = 100; % in Hz
         engine = c4e.Engine(pollingRate, this.timerCallbackDelay);
         engine.addPhidgetListener(this);     
         engine.start();
         this.interface = engine.getInterfacePhidget();
         this.alarm = engine;
         this.main();
         engine.stop();
       end   
      
      function main(this) % Called upon object creation
         disp('Press ENTER to start ...'); pause;
         this.alarm.setTimerCallback();
         if (this.interface ~= [] && this.interface.isAttached())
            fprintf('Interfacing Board Detected.\n');
            % Use the interface board, e.g. to turn on digital out #3,
            % write:  this.interface.setOutputState(3, 1);
            for i=1:4
               this.interface.setOutputState(i, 1);
            end
         end
         disp('Press ENTER to terminate ...'); pause;
      end
          
      % ----------------------------------- These are event handlers
      function attached(this, event)
         fprintf('Attach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end
      function detached(this, event)
         fprintf('Detach event at %d for phidget with serial number: %d\n', event.getWhen(), event.getId());
      end
      function diChange(this, event)
         fprintf('Digital input #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end
      function aiChange(this, event)
         %fprintf('Analog input #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end
      function doChange(this, event)
         %fprintf('Digital output #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end

      function timerCallback(this)
         %fprintf('Alarm went off at: %s\n', datestr(now,'dd-mm-yyyy HH:MM:SS FFF'));
         c4e.QU.reset();
         this.alarm.setTimerCallback();
      end
   end
end

