-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhidgetInterface.m
executable file
·146 lines (128 loc) · 4.13 KB
/
PhidgetInterface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
%
% PhidgetInterface.m
%
% Created by Léa Strobino.
% Copyright 2016 hepia. All rights reserved.
%
classdef PhidgetInterface < matlab.mixin.SetGet
properties (SetAccess = private)
IDN
DeviceName
DeviceVersion
ClassVersion
AnalogInputCount
DigitalInputCount
DigitalOutputCount
DataRate
end
properties
AnalogReference
end
properties (Access = private) %#ok<*MCSUP>
ptr
async
end
properties (Constant, Access = private)
PHIDGET_SAMPLING_PERIOD = 8E-3; % 8 ms = 125 Hz
end
methods
function this = PhidgetInterface(IDN)
if ~nargin || isempty(IDN)
IDN = -1;
end
try
this.ptr = phidget21interface('open',IDN);
catch e
s = '';
if ischar(IDN)
s = [' "' IDN '"'];
end
e = addCause(MException('PhidgetInterface:CommunicationError',...
'Unable to open the Phidget%s.',s),e);
throw(e);
end
[this.DeviceName,IDN,this.DeviceVersion,this.ClassVersion,...
this.AnalogInputCount,this.DigitalInputCount,this.DigitalOutputCount,...
dataRateMin,dataRateMax] = phidget21interface('getInfo',this.ptr);
this.IDN = sprintf('%d',IDN);
this.DataRate = 1E3./double([dataRateMin dataRateMax]);
this.async.timer = timer('ExecutionMode','SingleShot',...
'Name','PhidgetInterface::getAnalogInputAsync','TimerFcn',@this.asyncTimerFcn);
this.AnalogReference = 'VCC';
end
function delete(this)
waitfor(this.async.timer);
delete(this.async.timer);
phidget21interface('close',this.ptr);
end
function this = set.AnalogReference(this,reference)
if strcmpi(reference,'internal')
this.AnalogReference = 'internal';
phidget21interface('setRatiometric',this.ptr,0);
else
this.AnalogReference = 'VCC';
phidget21interface('setRatiometric',this.ptr,1);
end
end
function a = getAnalogInput(this,index)
a = zeros(1,length(index));
for i = 1:length(index)
v = phidget21interface('getSensorRawValue',this.ptr,index(i)-1);
a(i) = double(v)/4095;
end
if strcmp(this.AnalogReference,'internal')
a = 5*a;
end
end
function getAnalogInputAsync(this,index,duration,frequency,callbackFcn)
if strcmpi(this.async.timer.running,'off')
dt = 1/min([max([frequency this.DataRate(1)]) this.DataRate(2)]);
if dt > this.PHIDGET_SAMPLING_PERIOD
dt = this.PHIDGET_SAMPLING_PERIOD*round(dt/this.PHIDGET_SAMPLING_PERIOD);
else
[~,k] = min(abs(2.^(0:3)-1E3*dt));
dt = 1E-3*2^(k-1);
end
n = uint32(max([duration/dt 0])+1);
phidget21interface('startCapture',this.ptr,n,1E3*dt);
this.async.index = index;
this.async.t = 0:dt:double(n-1)*dt;
this.async.callbackFcn = callbackFcn;
this.async.timer.startDelay = 0.1+this.async.t(end);
start(this.async.timer);
else
error('PhidgetInterface:AsyncAlreadyRunning',...
'Asynchronous acquisition is already running.');
end
end
function d = getDigitalInput(this,index)
d = false(1,length(index));
for i = 1:length(index)
d(i) = phidget21interface('getInputState',this.ptr,index(i)-1);
end
end
function setDigitalOutput(this,index,state)
state = logical(state);
for i = 1:length(index)
phidget21interface('setOutputState',this.ptr,index(i)-1,state(i));
end
end
end
methods (Access = private)
function asyncTimerFcn(this,~,~)
stop(this.async.timer);
for i = this.async.index
a = phidget21interface('getCapturedData',this.ptr,i-1);
a = double(a)/1000;
if strcmp(this.AnalogReference,'internal')
a = 5*a;
end
try
this.async.callbackFcn(i,this.async.t,a);
catch e
warning(e.identifier,e.message);
end
end
end
end
end