% @since: 2014-01-15, @author: roumani@cse.yorku.ca %
classdef Password < c4e.InterfaceListener & c4e.RfidListener
   
   % The following attributes hold the object's state.
   % They are available to all methods in this class.
   properties
      timerCallbackDelay = 50; % in ms
      interface, rfid, alarm, database;
      % add your attributes here
      row = 1;
      column = 1;
      pad = ['1', '2', '3'; '4', '5', '6'; '7', '8', '9'; '*', '0', '#'];
      pin = '';
      tag = '';
   end
    
   % The following functions exhibit the object's behavior
   methods
   
       function this = Password()  % 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.rfid = engine.getRFIDPhidget();
         this.alarm = engine;
         this.database = c4e.simpleDB(); % c4e.simpleDB() eventually
         this.main();
         engine.stop();
       end   
      
      function main(this) % Called upon object creation
         fprintf('reading in the database\n');
         fid = fopen('database.csv');
         file = textscan(fid, '%s %s', 'Delimiter', ',');
         rfids = cellstr(file{1});
         passwds = cellstr(file{2});
         fclose(fid);
         npasswds = size(rfids);
         npasswds = npasswds(1);
         for i=1:npasswds
	         this.database.put(char(rfids(i)), char(passwds(i)));
         end
         this.database.print();
         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);
         end
         if (this.rfid ~= [] && this.rfid.isAttached())
             disp('rfid board attached');
            this.rfid.setAntennaOn(true);
            % Use the rfid board, e.g. to turn on its LED
            this.rfid.setLEDOn(false);
         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());
         if (event.getValue() == 1)
            disp(this.pad(this.row,event.getId()));
            this.interface.setOutputState(7, 1);
         end   
      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 rfidLoss(this, event)
      end
      
      function rfidGain(this, event)
          this.tag = char(event.getTag());
          this.pin = '';
          fprintf('we got an rfid tag %s\n', this.tag);
      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();
         this.interface.setOutputState(7,0);
      end
   end
end

