classdef First < c4e.InterfaceListener
   
   properties
      interface, timerCallbackDelay = 100;
      count = 0;
   end
    
   methods 
      function this = First()
         pollingRate = 125;
         engine = c4e.Engine(pollingRate, this.timerCallbackDelay);
         engine.addPhidgetListener(this);     
         engine.start();
         this.interface = engine.getInterfacePhidget();
         this.main();
         engine.stop();
      end   
      
      function main(this)
         disp('Press ENTER to start ...'); pause;
         % this.interface.setOutputState(3, 1);
         disp('Press ENTER to terminate ...'); pause;
         % fprintf('Digital output changed %d times.', this.count);
      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)
         this.count = this.count + 1;
         fprintf('Digital output #%d became %g at %d\n', event.getId(), event.getValue(), event.getWhen());
      end

   end
   
end
