-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanGrabber.m
112 lines (97 loc) · 3 KB
/
ScanGrabber.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
classdef ScanGrabber < handle
% reads input
% TODO add cntrl commands
properties
holdCrit %initial time before repeating
repCrit %base pause between keys
blackListInd % XXX NED TO CONSTRUCT THESE
whiteListInd
repWhiteListInd % XXX
scanInd
repTime
holdTime
nKeys
end
properties(Constant)
end
properties(Access=private)
fls
zer
end
events
%KeyPressed % FOR TIME
end
methods
function obj=ScanGrabber(varargin)
obj=Args.parse(obj,ScanGrabber.getP,varargin{:});
obj.fls=false(1,obj.nKeys);
obj.zer=zeros(1,obj.nKeys);
% REP WHITELIST
if isempty(obj.repWhiteListInd)
obj.repWhiteListInd=obj.fls;
elseif ~numel(obj.repWhiteListInd) == obj.nKeys
error('blackListInd is not the correct size');
elseif size(obj.repWhiteListInd,1)==obj.nKeys
obj.repWhiteListInd=obj.repWhiteListInd';
end
% BLACKLIST
if isempty(obj.blackListInd)
obj.whiteListInd=~obj.fls;
elseif numel(obj.blackListInd) == obj.nKeys
obj.whileListInd=obj.blackListInd;
else
error('blackListInd is not the correct size');
end
obj.holdTime=obj.zer;
obj.repTime=obj.zer;
obj.scanInd=obj.fls;
end
function [exitflag,outScanInd,keyTime]=read(obj)
outScanInd=[];
[keyIsDown, keyTime, nScanInd, deltaSecs] = KbCheck(-1);
if ~keyIsDown || ~any(nScanInd(~obj.repWhiteListInd)) % mod keys do nothing by themselves
keyTime=nan;
obj.holdTime=obj.zer;
obj.repTime=obj.zer;
obj.scanInd=obj.fls;
exitflag=1;
return
end
lastScanInd=obj.scanInd;
% FIRST - first keypress
bFirst = nScanInd & ~lastScanInd;
heldInd=lastScanInd & nScanInd;
% Update New Hold
newHoldTime=obj.holdTime;
newHoldTime(heldInd)=newHoldTime(heldInd)+deltaSecs;
newHoldTime(~heldInd)=0;
% SECOND - passing holdCrit
bSecond=(newHoldTime >= obj.holdCrit) & (obj.holdTime < obj.holdCrit);
obj.holdTime=newHoldTime;
heldInd=obj.holdTime >= obj.holdCrit;
% UPDATE REPTIME -- AFTER HOLD
obj.repTime(heldInd)=obj.repTime(heldInd)+deltaSecs;
obj.repTime(~heldInd)=0;
bThird=(obj.repTime > obj.repCrit);
obj.repTime(bThird)=0; % RESET UNTIL NEXT
% COMBINE
outScanInd= (bFirst | bSecond | bThird) | (nScanInd & obj.repWhiteListInd);
obj.scanInd=nScanInd;
exitflag=0;
end
end
methods(Static,Hidden)
function P=getP()