Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.20 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function [ dx, dy, sx, sy ] = RawMouse2(devIndex)
  2. % Parse raw HID datastream from a mouse to implement
  3. % raw mouse movement from HID reports.
  4. %
  5. % This is just an example! The data format is mouse
  6. % dependent, this is for a DELL LaserMouse.
  7. %
  8. % Must be run "sudo" root unless proper udev rules
  9. % are defined to allow non-root raw access to mouse
  10. % on your system.
  11. %
  12. % Will (violently) detach the mouse from the GUI,
  13. % making it unuseable for regular "mousing" until
  14. % mouse is unplugged and replugged.
  15. %
  16. % Should work on Linux and probably OS/X. Will not
  17. % work on Windows, as MS forbids low-level HID access to
  18. % mouse and keyboard.
  19. %
  20. % You should disable mouse queries after use via
  21. % PsychHID('ReceiveReportsStop', devIndex); and/or
  22. % flush the buffer via a PsychHID('GiveMeReports', devIndex);
  23. % This is not shown in this demo...
  24.  
  25. persistent sx;
  26. persistent sy;
  27.  
  28. % devIndex must be found out by proper matching against
  29. % output of dev = PsychHID('Devices').
  30. if nargin < 1 || isempty(devIndex)
  31.    devIndex = 1;
  32. end
  33.  
  34. if isempty(sx)
  35.   sx = 0;
  36.   sy = 0;
  37.  
  38.   % Process at most 2 msecs per 'ReceiveReports' call, so we
  39.   % don't block script execution for too long.
  40.   options.secs = 0.002;
  41.   PsychHID('ReceiveReports', devIndex, options);
  42. end
  43.  
  44. dx = 0;
  45. dy = 0;
  46.  
  47. % Fetch a bunch of HID-Reports from the HID device into
  48. % PsychHID internal buffer:
  49. PsychHID('ReceiveReports', devIndex);
  50.  
  51. % Retrieve them from PsychHID buffer:
  52. reps = PsychHID('GiveMeReports', devIndex);
  53.  
  54. % Parse them:
  55. for i=1:numel(reps)
  56.   rep = reps(i).report;
  57.   if ~isempty(rep)
  58.     % DELL Lasermouse encodes x,y motion as two 12 bit numbers,
  59.     % packed into bytes 2, 3 and 4: Decode...
  60.  
  61.     % Byte2[0:7] = 8 LSB of x, Byte 3[0:3] = 4 MSB of x:
  62.     ix = bitshift(bitand(int32(rep(3)), 1+2+4+8), 8) + int32(rep(2));
  63.     % Byte3[4:7] = 4 LSB of y, Byte 4[0:7] = 8 MSB of x:
  64.     iy = bitand(bitshift(int32(rep(3)), -4), 1+2+4+8) + bitshift(int32(rep(4)), 4);
  65.  
  66.     % 12th bit (aka Bit 11) encodes sign: 1 (aka dx >= 2048) -> Negative dx:
  67.     dx = double(ix);
  68.     if dx >= 2048
  69.       dx = dx - 4096;
  70.     end
  71.  
  72.     dy = double(iy);
  73.     if dy >= 2048
  74.       dy = dy - 4096;
  75.     end
  76.  
  77.     % (dx, dy) is mouse delta. Accumulate for abs position:
  78.     sx = sx + dx;
  79.     sy = sy + dy;
  80.   end
  81. end
  82.  
  83. return;