forked from nigelrogasch/MAGIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagventure.m
2128 lines (1783 loc) · 86.8 KB
/
magventure.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
classdef magventure < handle
properties (SetAccess = private)
port
connected = 0; %Default value of connected set to 0 to make sure the user connects the port
end
methods
function self = magventure(PortID)
% PortID <char> defines the serail port id on your computer
%% Find All Available etISerial Ports On Your Computer
FoundPorts = instrhwinfo('serial');
ListOfComPorts = FoundPorts.AvailableSerialPorts;
%% Check Input Validity:
if nargin <1
error('Not Enough Input Arguments');
end
if ~(ischar(PortID))
error('The Serial Port ID Must Be a Character Array');
end
if ~any(strcmp(ListOfComPorts,PortID))
error('Invalid Serial Com Port ID');
end
%% Identifing The Port
P = serial(PortID);
P.BaudRate = 38400;
P.DataBits = 8;
P.Parity = 'none';
P.StopBits = 1;
P.InputBufferSize = 19;
P.OutputBufferSize = 512;
self.port = P;
end
end
methods
%% Opening The Desired Port
function [errorOrSuccess, deviceResponse] = connect(self)
%% Check Input Validity
if nargin <1
error('Not Enough Input Arguments');
end
%% Open The Port
fopen(self.port);
if ~(strcmp (self.port.Status,'closed'))
self.connected = 1;
errorOrSuccess = ~ self.connected;
deviceResponse = [];
end
end
%% Closing The Desired Port
function [errorOrSuccess, deviceResponse] = disconnect(self)
%% Check Input Validity
if nargin <1
error('Not Enough Input Arguments');
end
%% Close The Port
fclose(self.port);
if (strcmp (self.port.Status,'closed'))
self.connected = 0;
errorOrSuccess = self.connected;
deviceResponse = [];
end
end
end
methods
%% 0.Getting Status From Device
function [errorOrSuccess, deviceResponse] = getStatus(self)
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Reconnect the stimulator
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
%% Create Control Command
commandLength = '01';
commandID = '00';
commandBytes = commandID;
getResponse = 1;
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,13);
end
%% 1.Setting The Amplitude In Standard Or Twin/Dual Modes
function [errorOrSuccess, deviceResponse] = setAmplitude(self,desiredAmplitudes,varargin)
% Inputs:
% DesiredAmplitudes<double> must be a vector of length 1 indicating A amplitude in
% percentage ; Or of length 2 in Dual/Twin Mode indicating A &
% B amplitudes in percentage respectively
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <2
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <3
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%%
A_Amp =[];
B_Amp =[];
if (length(desiredAmplitudes)~= 1 && length(desiredAmplitudes)~= 2)
error('Invalid Number of Inputs. Number of Inputs Must Be 1 or 2(Dual Mode) In This Control Type');
end
if (length(desiredAmplitudes)==1)
A_Amp = desiredAmplitudes;
if ~isnumeric(A_Amp) || rem(A_Amp,1)~=0 || A_Amp<0 || A_Amp>100
error('Amplitudes Must Be A Whole Numeric Percent value Between 0 and 100');
end
elseif (length(desiredAmplitudes)==2)
A_Amp = desiredAmplitudes(1);
B_Amp = desiredAmplitudes(2);
if ~isnumeric(A_Amp)|| rem(A_Amp,1)~=0 || A_Amp<0 || A_Amp>100
error('Amplitudes Must Be A Whole Numeric Percent value Between 0 and 100');
end
if ~ isnumeric(B_Amp) || rem(B_Amp,1)~=0 || B_Amp<0 || B_Amp>100
error('Amplitudes Must Be A Whole Numeric Percent value Between 0 and 100');
end
end
%% Create Control Command
if (isempty(B_Amp)) %Standard Mode
commandLength = '02';
commandID = '01';
amp = dec2hex(A_Amp,2);
commandBytes = [commandID;amp];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
else %Dual Mode
commandLength = '03';
commandID = '01';
amp1 = dec2hex(A_Amp,2);
amp2 = dec2hex(B_Amp,2);
commandBytes = [commandID;amp1;amp2];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
end
end
%% 2.a Enabling The Desired Port
function [errorOrSuccess, deviceResponse] = arm(self,varargin)
% Inputs:
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity
if nargin <1
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <2
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%% Create Control Command
commandLength = '02';
commandID = '02';
value = '01';
commandBytes = [commandID;value];
[~, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
self.disconnect();
[errorOrSuccess, ~] = self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%% 2.b Disabling The Desired Port
function [errorOrSuccess, deviceResponse] = disarm(self,varargin)
% Inputs:
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity
if nargin <1
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <2
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Create Control Command
commandLength = '02';
commandID = '02';
value = '00';
commandBytes = [commandID;value];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
end
%% 3.Sending A Single Trig To The Specified Port
function [errorOrSuccess, deviceResponse] = fire(self,varargin)
% Inputs:
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity
if nargin <1
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <2
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%% Create Control Command
commandLength = '02';
commandID = '03';
value = '01';
commandBytes = [commandID;value];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
end
%% 4.Sending Train of Pulses To The Specified Port
function [errorOrSuccess, deviceResponse] = sendTrain(self,varargin)
% Inputs:
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <1
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <2
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%% Create Control Command
commandLength = '01';
commandID = '04';
commandBytes = commandID;
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,17);
end
%% 5.Getting Status2 From Device
function [errorOrSuccess, deviceResponse] = getStatus2(self)
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Reconnect the stimulat
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
%% Create Control Command
commandLength = '01';
commandID = '05';
commandBytes = commandID;
getResponse = 1;
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,19);
end
%% 6.Setting Train Parameters
function [errorOrSuccess, deviceResponse] = setTrain(self,repRate,pulses,numberOfTrains,iti,varargin)
% Inputs:
% repRate <int>: represents the number of pulses per second
% pulses <int>: represents the number of pulses in each train
% numberOfTrains <int>: represents the total amount of trains
% arriving in one sequencese
% iti <int>: represents time interval between two trains in seconds, described
% as the time period between the last pulse in the first train to the first pulse in the next train.
% varargin<bool>: refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <5
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <6
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
if (length(repRate)~=1 || length(pulses)~=1 || length(numberOfTrains)~=1 || length(iti)~=1)
error('All Train Parameters Must Be Of length 1');
end
if rem(repRate,1)~=0 || repRate<0
error('The Repitition Rate Must Be A Whole Positive Integer');
end
repRateRange = [0.1:0.1:1 1:1:100];
if ~ismember(repRate,repRateRange)
error('repRate Out Of Bounds');
end
if rem(pulses,1)~=0 || pulses<0
error('The Number of Pulses Must Be A Whole Positive Integer');
end
pulsesRange = [1:1:100 1000:100:2000];
if ~ismember(pulses,pulsesRange)
error('Pulses Out Of Bounds');
end
if rem(numberOfTrains,1)~=0 || numberOfTrains<0
error('The Total Number of Trains Must Be A Whole Positive Integer');
end
numberOfTrainsRange = 1:1:500;
if ~ismember(numberOfTrains,numberOfTrainsRange)
error('numberOfTrains Out Of Bounds');
end
if ~(isnumeric(iti)) || iti<0
error('The Inter Train Interval Must Be A Positive Number');
end
itiRange = 0.1:0.1:120;
if ~ismember(iti,itiRange)
error('iti Out Of Bounds');
end
%% Create Control Command
commandLength = '09';
commandID = '06';
repRate = repRate*10; %Rep Rate is in tenth
repRate = dec2hex(repRate,4); %'4' is because we want all hex numbers to appear in 4 digits
repRate_L = repRate(3:4); %Extracting LSB part
repRate_M = repRate(1:2); %Extracting MSB part
pulses = dec2hex(pulses,4);
pulses_L = pulses(3:4);
pulses_M = pulses(1:2);
numberOfTrains = dec2hex(numberOfTrains,4);
numTrain_L = numberOfTrains(3:4);
numTrain_M = numberOfTrains(1:2);
iti = iti*10; %iti is in tenth
iti = dec2hex(iti,4);
iti_L = iti(3:4);
iti_M = iti(1:2);
commandBytes = [commandID;repRate_M;repRate_L;pulses_M;pulses_L;numTrain_M;numTrain_L;iti_M;iti_L];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
%% Check if device is on train page
end
%% 7.Setting Page
function [errorOrSuccess, deviceResponse] = setPage(self,page,varargin)
% Inputs:
% page <char>: defines the page. Valid inputs in magventure:
% 'Main','Train','Trig','Config','Protocol'
% varargin<bool>: refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <2
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <3
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
if ~(ischar(page))
error('The Page Name Which Shown On magventure Must Be a Character Array');
end
if ~(strcmp(page,'Main') || strcmp(page,'Train')|| strcmp(page,'Trig')|| strcmp(page,'Config')|| strcmp(page,'Protocol'))
error('Invalid Page Name');
end
%% Configuring The Desired Page
switch page
case 'Main'
pageN = '01';
case 'Train'
pageN = '02';
case 'Trig'
pageN = '03';
case 'Config'
pageN = '04';
case 'Protocol'
pageN = '07';
end
%% Create Control Command
commandLength = '03';
commandID = '07';
NAByte = '00';
commandBytes = [commandID;pageN;NAByte];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
end
%% 8.Setting Trig and Charge delay Parameters
function [errorOrSuccess, deviceResponse] = setTrig_ChargeDelay(self,trigInDelay,trigOutDelay,chargeDelay,varargin)
% Inputs:
% trigInDelay <int>: allows setting a delay in milliseconds from the time
% of arrival of an external trigger input to the time for the magnetic stimulation to be provided.
% trigOutDelay <signed int>: allows setting a delay in milliseconds from
% the time of the magnetic stimulation to the time of the external trigger to be provided.
% chargeDelay <int>: defines the time in milliseconds to make the device wait, before
% recharging.
% varargin<bool>: refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <4
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <5
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
%% Reconnect the stimulator
if getResponse ==1
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%%
if (length(trigInDelay)~=1 || length(trigOutDelay)~=1 || length(chargeDelay)~=1)
error('All Trig & Charge Parameters Must Be Of length 1');
end
if rem(trigInDelay,1)~=0 || trigInDelay<0
error('The Delay For Input Trig Must Be A Whole Positive Integer');
end
if (trigInDelay>65535) %Must fit into a four digit hex number
error('repRate Out Of Bounds');
end
if rem(trigOutDelay,1)~=0
error('The Delay For Output Trig Must Be A Whole Integer');
end
if (trigOutDelay>65535)
error('trigOutDelay Out Of Bounds');
end
if rem(chargeDelay,1)~=0 || chargeDelay<0
error('The Charge Delay of Trains Must Be A Whole Positive Integer');
end
if (chargeDelay>65535)
error('chargeDelay Out Of Bounds');
end
%% Create Control Command
commandLength = '09';
commandID = '08';
trigInDelay = trigInDelay*10; %trigInDelay is in tenth
trigInDelay = dec2hex(trigInDelay,4); %'4' is because we want all hex numbers to appear in 4 digits
trigInDelay_L = trigInDelay(3:4); %Extracting LSB part
trigInDelay_M = trigInDelay(1:2); %Extracting MSB part
trigOutDelay = trigOutDelay*10; %trigOutDelay is in tenth
if trigOutDelay>=0 %Output trig delay can be of negative sign
trigOutDelay = dec2hex(trigOutDelay,4);
trigOutDelay_L = trigOutDelay(3:4);
trigOutDelay_M = trigOutDelay(1:2);
else %For negative output trig delay
unsignedvalue = -trigOutDelay;
Complement = hex2dec('FFFF') - unsignedvalue;
trigOutDelay = dec2hex(Complement+1);
trigOutDelay_L = trigOutDelay(3:4);
trigOutDelay_M = trigOutDelay(1:2);
end
chargeDelay = dec2hex(chargeDelay,4);
chargeDelay_L = chargeDelay(3:4);
chargeDelay_M = chargeDelay(1:2);
NAByte1 = '00';
NAByte2 = '00';
commandBytes = [commandID;trigInDelay_M;trigInDelay_L;trigOutDelay_M;trigOutDelay_L;...
chargeDelay_M;chargeDelay_L;NAByte1;NAByte2];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,8);
end
%% 9.a.1 : Setting Mode
function [errorOrSuccess, deviceResponse] = setMode(self, mode, currentDirection, burstPulses,...
ipiValue, BARatioValue, varargin)
% Inputs:
% mode <char>: defines the desired working mode. Valid inputs in magventure:
% 'Standard','Power','Twin','Dual'
% currentDirection <char>: defines current direction of the device in the current status.
% Valid inputs in magventure:'Normal','Reverse'
% burstPulses <int>: Biphasic Burst index in the current status of the device which can
% be 2,3,4, or 5 pulses in each stimulation.
% ipiValue <int>: represents Inter Pulse Interval of the current status of the device
% which defines the duration between the beginning of the first pulse to the beginning of
% the second pulse.
% BARatioValue <int>: when working in Twin Mode, the amplitude of the two pulses A and B are
% controlled in an adjustable ratio between 0.2-5.0. "Pulse B" is now adjusted to a selected
% percent ratio proportional to "Pulse A".
% varargin<bool>: refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <2
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <3
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
if ~(ischar(mode))
error('The Mode Identifier in magventure Must Be a Character Array');
end
if ~(strcmp(mode,'Standard') || strcmp(mode,'Power')|| strcmp(mode,'Twin')|| strcmp(mode,'Dual'))
error('Invalid Mode Identifier');
end
%% Configuring The Desired Mode
switch mode
case 'Standard'
modeN = '00';
case 'Power'
modeN = '01';
case 'Twin'
modeN = '02';
case 'Dual'
modeN = '03';
end
%% Create Control Command
commandLength = '0B';
commandID = '09';
setVal = '01';
%% Creating Output
[errorOrSuccess, rawInfo] = self.getStatusSetGet;
if errorOrSuccess~=0
error('Could Not Retrieve Required Info From the Device');
end
model = rawInfo.Model;
waveform = rawInfo.Waveform;
currentDir = currentDirection;
switch burstPulses
case 2
burstPulseIndex = '03';
case 3
burstPulseIndex = '02';
case 4
burstPulseIndex = '01';
case 5
burstPulseIndex = '00';
end
ipi = dec2hex((ipiValue*10),4);
ipi_M = ipi(1:2);
ipi_L = ipi(3:4);
BARatio = dec2hex((BARatioValue*100),4);
BARatio_M = BARatio(1:2);
BARatio_L = BARatio(3:4);
%% Reconnect the stimulator
if getResponse
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%%
commandBytes = [commandID;setVal;model;modeN;currentDir;waveform;burstPulseIndex;ipi_M;ipi_L;...
BARatio_M;BARatio_L];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,14);
end
%% 9.a.2 : Getting Mode
function [errorOrSuccess, deviceResponse] = getMode(self)
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Reconnect the stimulator
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
%%
%% Check Input Validity:
if nargin <1
error('Not Enough Input Arguments');
end
%% Create Control Command
commandLength = '0B';
commandID = '09';
setVal = '00'; %For get commands
model = '00';
mode = '00';
currentDir = '00';
waveform = '00';
burstPulses = '00';
ipi_M = '00';
ipi_L = '00';
BARatio_M = '00';
BARatio_L = '00';
commandBytes = [commandID;setVal;model;mode;currentDir;waveform;burstPulses;ipi_M;ipi_L;...
BARatio_M;BARatio_L];
getResponse = true;
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,14);
deviceResponse = deviceResponse.Mode;
end
%% 9.b.1 : Setting Waveform
function [errorOrSuccess, deviceResponse] = setWaveform(self,waveform,currentDirection,burstPulses,...
ipiValue,BARatioValue,varargin)
% Inputs:
% waveform <char>: defines the desired waveform. Valid inputs in magventure:
% 'Monophasic','Biphasic','HalfSine','BiphasicBurst'
% currentDirection <char>: defines current direction of the device in the current status.
% Valid inputs in magventure:'Normal','Reverse'
% burstPulses <int>: Biphasic Burst index in the current status of the device which can
% be 2,3,4, or 5 pulses in each stimulation.
% ipiValue <int>: represents Inter Pulse Interval of the current status of the device
% which defines the duration between the beginning of the first pulse to the beginning of
% the second pulse.
% BARatioValue <int>: when working in Twin Mode, the amplitude of the two pulses A and B are
% controlled in an adjustable ratio between 0.2-5.0. "Pulse B" is now adjusted to a selected
% percent ratio proportional to "Pulse A".
% varargin<bool>: refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default value is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Check Input Validity:
if nargin <2
error('Not Enough Input Arguments');
end
if length(varargin)>1
error('Too Many Input Arguments');
end
if nargin <3
getResponse = false ; %Default value Set To 0
else
getResponse = varargin{1};
end
if (getResponse ~= 0 && getResponse ~= 1 )
error('getResponse Must Be A Boolean');
end
if ~(ischar(waveform))
error('The Waveform Name in magventure Must Be a Character Array');
end
if ~(strcmp(waveform,'Monophasic') || strcmp(waveform,'Biphasic')|| strcmp(waveform,'HalfSine')|| strcmp(waveform,'BiphasicBurst'))
error('Invalid Waveform');
end
%% Configuring The Desired Waveform
switch waveform
case 'Monophasic'
waveformN = '00';
case 'Biphasic'
waveformN = '01';
case 'HalfSine'
waveformN = '02';
case 'BiphasicBurst'
waveformN = '03';
end
%% Create Control Command
commandLength = '0B';
commandID = '09';
setVal = '01';
%% Creating Output
[errorOrSuccess, rawInfo] = self.getStatusSetGet;
if errorOrSuccess~=0
error('Could Not Retrieve Required Info From the Device');
end
model = rawInfo.Model;
mode = rawInfo.Mode;
currentDir = currentDirection;
switch burstPulses
case 2
burstPulseIndex = '03';
case 3
burstPulseIndex = '02';
case 4
burstPulseIndex = '01';
case 5
burstPulseIndex = '00';
end
ipi = dec2hex((ipiValue*10),4);
ipi_M = ipi(1:2);
ipi_L = ipi(3:4);
BARatio = dec2hex((BARatioValue*100),4);
BARatio_M = BARatio(1:2);
BARatio_L = BARatio(3:4);
%% Reconnect the stimulator
if getResponse
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
end
%%
commandBytes = [commandID;setVal;model;mode;currentDir;waveformN;burstPulseIndex;ipi_M;ipi_L;BARatio_M;BARatio_L];
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,14);
end
%% 9.b.2 : Getting Waveform
function [errorOrSuccess, deviceResponse] = getWaveform(self)
% Inputs:
% waveform <char> defines the desired waveform. Valid inputs in magventure:
% 'Monophasic','Biphasic','HalfSine','BiphasicBurst'
% varargin<bool> refers to getResponse<bool> that can be True (1) or False (0)
% indicating whether a response from device is required or not.
% The default is set to false.
% Outputs:
% deviceResponse: is the response that is sent back by the
% device to the port indicating current information about the device
% errorOrSuccess: is a boolean value indicating succecc = 0 or error = 1
% in performing the desired task
%% Reconnect the stimulator
self.disconnect();
self.connect();
warning('In case of any problems, try reconnecting the device manually');
%%
%% Check Input Validity:
if nargin <1
error('Not Enough Input Arguments');
end
%% Create Control Command
commandLength = '0B';
commandID = '09';
setVal = '00'; %For get commands
model = '00';
mode = '00';
currentDir = '00';
waveform = '00';
burstPulses = '00';
ipi_M = '00';
ipi_L = '00';
BARatio_M = '00';
BARatio_L = '00';
commandBytes = [commandID;setVal;model;mode;currentDir;waveform;burstPulses;ipi_M;ipi_L;...
BARatio_M;BARatio_L];
getResponse = true;
[errorOrSuccess, deviceResponse] = self.processCommand(commandLength,commandBytes,getResponse,14);
deviceResponse = deviceResponse.Waveform;