-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.m
More file actions
207 lines (181 loc) · 6.34 KB
/
Command.m
File metadata and controls
207 lines (181 loc) · 6.34 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
% 8752 command construction
%
% Methods::
% Command Constructor, creates an empty command object
% delete Destructor, clears the command object
%
% addCommand Add a command directly to the command object
%
% clear Clear the command msg
% display Display the command msg (decimal)
%
% Notes::
% - Refer to the NewFocus 8752 manual for a more detailed
% description of the commands.
%
% Example::
% cmd = Command();
% cmd.addLength();
%
% Author: Klaus Hueck (e-mail: khueck (at) physik (dot) uni-hamburg (dot) de)
% Version: 0.0.1alpha
% Changes tracker: 28.01.2016 - First version
% License: GPL v3
classdef Command < handle
properties
msg % message to be sent
Mode = 'r'; % mode of this command ('r' (read) / 'w' (write)) initialize to read
Reply = true; % is a reply requestes (true / false) initialize to true
Sequence = 1; % rolling counter for the sequence byte
end
methods
function cmd = Command(varargin)
% Command.cmd Create an empty command
%
% c = Command(OPTIONS) is an object that represents an C900 command
%
% Example::
% c = Command();
cmd.msg = uint8([]);
end
function delete(cmd)
%Command.delete Clear command
%
% delete(c) clears the command
cmd.msg = '';
end
function addCommand(cmd,c,d)
%Command.addCommand Add a command
%
% Command.addCommand(v) adds a command to the
% command object.
%
% Notes::
% - c is a 2x1 cell which holds the usb command which can be
% found in DLPC900 Programmer's guide
% - d is the usb payload
%
% Example::
% cmd.addCommand({'0x02', '0x05'}, data)
cmd.msg(5) = uint8(sscanf(c{2}, '%x'));
cmd.msg(6) = uint8(sscanf(c{1}, '%x'));
cmd.addData(d);
cmd.addLength;
cmd.addSequence
cmd.addFlagByte;
end
function addData(cmd, d)
%Command.addData Adds data to the command string
%
% Command.addData(d) adds data to the command string this has
% to match the usb command. See DLPC900 Programmer's guide.
%
% Example::
% cmd.addData(d)
d = uint8(bin2dec(d));
cmd.msg(7:7+length(d)-1) = d;
end
function addLength(cmd)
% Command.addLength Add the length
%
% Command.addLength adds the length of the command string set
% via Command.addCommand to the bytes 4 and 4
%
% Example::
% cmd.addLength
len = uint16(length(cmd.msg)-4);
cmd.msg(3:4) = typecast(len, 'uint8');
end
function addFlagByte(cmd)
% Command.addFlagByte Add the flagbyte
%
% Command.addFlagByte adds the flagbyte of the command to be
% executed
%
% Example::
% cmd.addFlagByte
flagstring = '';
switch cmd.Mode
case 'r'
flagstring = [flagstring, '1'];
case 'w'
flagstring = [flagstring, '0'];
otherwise
error(['The mode specified is not valid! Only ''r'' for read operation ', ...
'or ''w'' for write operation is allowed.']);
end
switch cmd.Reply
case true
flagstring = [flagstring, '1'];
case false
flagstring = [flagstring, '0'];
otherwise
error('The reply flag can only be true or false!');
end
flagstring = [flagstring, '000000'];
cmd.msg(1) = bin2dec(flagstring);
end
function addSequence(cmd)
% Command.addSequence Insert the sequence counter
%
% Command.addSequence insert a sequence counter which can be a
% rolling counter and will be set by the C900 identical in the
% response.
%
% Example::
% cmd.addSequence()
cmd.msg(2) = uint8(cmd.Sequence);
end
function clear(cmd)
% Command.clear Clear command
%
% Commad.clear clears the message
%
% Example::
% cmd.clear()
cmd.msg = '';
end
function s = char(cmd)
s = '';
for i=1:length(cmd.msg)
s = [s sprintf(' %d', cmd.msg(i))];
end
end
function s = hex(cmd)
s = '';
for i=1:length(cmd.msg)
s = [s sprintf(' %x', cmd.msg(i))];
end
end
function display(cmd)
% Command.display Display the command message (decimal)
%
% Command.display() prints the command message to the MATALB
% command window in decimal format.
%
% Example::
% cmd.display()
loose = strcmp( get(0, 'FormatSpacing'), 'loose');
if loose
disp(' ');
end
disp([inputname(1), ' = '])
disp( char(cmd) );
end
function displayHex(cmd)
% Command.displayHex Display the command message (hex)
%
% Command.displayHex() prints the command message to the MATLAB
% command window in hexadecimal format.
%
% Example::
% cmd.displayHex()
loose = strcmp( get(0, 'FormatSpacing'), 'loose');
if loose
disp(' ');
end
disp([inputname(1), ' = '])
disp( hex(cmd) );
end
end
end