-
Notifications
You must be signed in to change notification settings - Fork 3
/
fwp_daq.py
1047 lines (837 loc) · 31.7 KB
/
fwp_daq.py
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
# -*- coding: utf-8 -*-
"""
The 'fwp_daq' module is designed to measure with NI USB 6212.
Based on 'fwp_old_daq'.
@author: Vall
"""
from fwp_classes import WrapperDict
import fwp_daq_channels as fch
import fwp_utils as utl
import inspect as spec
import numpy as np
import nidaqmx as nid
import nidaqmx.stream_readers as sr
import nidaqmx.stream_writers as sw
import nidaqmx.system as sys
task_states = nid.constants.TaskMode
continuous = nid.constants.AcquisitionType.CONTINUOUS
single = nid.constants.AcquisitionType.FINITE
#%%
def devices():
"""Returns list of NI Devices.
Parameters
----------
nothing
Returns
-------
devices : list
List of NI Devices' names as string.
"""
devices = []
for dev in sys.System.local().devices:
dev = str(dev)
dev = dev.split('=')[-1].split(')')[0]
devices.append(dev)
return devices
#%%
class DAQ:
"""Class that allows to read and write with a NI USB 6212 DAQ.
Parameters
----------
device : str
NI device's name where analog input channel/s should be put.
print_messages=False : bool, optional
Whether to print messages or not.
test_mode=False : bool, optional
Allows to test classes even if you don't have a real
conection with a NI USB 6212.
Attributes
----------
tasks : fwp_classes.WrapperDict
DAQ channels' manager (contains inputs and outputs' Task object)
inputs : Task
DAQ inputs' manager.
outputs : Task
DAQ outputs' manager.
ninputs : int
Number of DAQ's inputs.
noutputs : int
Number of DAQ's outputs.
reader : nidaqmx.stream_reader
DAQ's reading manager.
writer : nidaqmx.stream_writer
DAQ's writing manager.
Methods
-------
add_analog_inputs(*pins, **kwargs)
Adds analog input channel/s.
add_pwm_outputs(*pins, **kwargs)
Adds digital PWM output channel/s.
inputs.read(nsamples=None, samplerate=None, callback=None)
Reads from input channel/s if this task is meant to read.
outputs.write(status=True, frequency=None, duty_cycle=None)
Writes on output channel/s if this task is meant to write. Up to
now, it's only possible to write on a single PWM channel.
close()
Ends communication with DAQ.
Other Attributes
----------------
test_mode : bool
Whether it's test mode (no real connection) or not.
print : bool
Whether to print inner messages or not.
"""
def __init__(self, device, print_messages=False,
test_mode=False):
# DAQ's general attributes
self.__device = device
# Is there a real DAQ connected or are we just testing?
self.test_mode = test_mode
if test_mode:
self.print = True
else:
self.print = print_messages
# DAQ channels' manager
self.__inputs = Task(self.__device,
mode='r',
print_messages=print_messages,
test_mode=test_mode)
self.__outputs = Task(self.__device,
mode='w',
print_messages=print_messages,
test_mode=test_mode)
self.tasks = WrapperDict(inputs = self.inputs,
outputs = self.outputs)
def __enter__(self):
return self
# return self.__task
def __exit__(self, type, value, traceback):
self.close()
# self.__task.close()
@property
def inputs(self):
return self.__inputs
@inputs.setter
def inputs(self, value):
msg = "Why don't you chek 'add_analog_inputs' instead?"
raise AttributeError(msg)
@property
def outputs(self):
return self.__outputs
@outputs.setter
def outputs(self, value):
msg = "Why don't you chek 'add_pwm_outputs' instead?"
raise AttributeError(msg)
@property
def ninputs(self):
return self.inputs.nchannels
@ninputs.setter
def ninputs(self, value):
raise AttributeError("Can't modify this manually!")
@property
def noutputs(self):
return self.outputs.nchannels
@noutputs.setter
def noutputs(self, value):
raise AttributeError("Can't modify this manually!")
@property
def reader(self):
return self.inputs.streamer
@reader.setter
def reader(self, value):
raise AttributeError("Can't modify this manually!")
@property
def writer(self):
return self.outputs.streamer
@writer.setter
def writer(self, value):
raise AttributeError("Can't modify this manually!")
def add_analog_inputs(self, *pins, **kwargs):
"""Adds analog input channel/s.
Parameters
----------
pins : int, optional
Literally the number/s of DAQ's pins.
Other Parameters
----------------
voltage_range=[-10,10] : list, tuple, optional
Range of the analog input channel. Should be a list or tuple
of length 2 that contains minimum and maximum in V.
configuration='NonRef' : str, optional
Analog input channel terminal configuration.
Returns
-------
nothing
See Also
--------
Task.add_channels
fwp_daq_channels.AnalogInputChannel
"""
self.inputs.add_channels(fch.AnalogInputChannel,
*pins, **kwargs)
def add_pwm_outputs(self, *pins, **kwargs):
"""Adds PWM output channel/s.
Parameters
----------
pins : int, optional
Literally the number/s of DAQ's pins.
Other Parameters
----------------
frequency=100e3 : int, float, optional
PWM's main frequency.
duty_cycle=.5 : int, float, {0 <= duty_cycle <= 1}, optional
PWM's duty cycle, which defines mean value of signal as
'duty_cycle*max' where 'max' is the '+5' voltage.
Returns
-------
nothing
See Also
--------
Task.add_channels
fwp_daq_channels.PWMOutputChannel
"""
self.outputs.add_channels(fch.PWMOutputChannel, *pins, **kwargs)
def close(self):
"""Closes the tasks.
Parameters
----------
none
Returns
-------
nothing
See Also
--------
nidaqmx.Task.close
"""
self.outputs.close()
self.inputs.close()
def __print__(self, message):
"""Only prints if self.print is True.
Parameters
----------
message : str
Message to print
Returns
-------
nothing
Raises
------
print
"""
if self.print:
print(message)
#%%
class Task:
"""Allows whether to read or to write with a NI USB 6212 DAQ.
Parameters
----------
device : str
NI device's name.
mode : str, optional {'r', 'w'}
Says whether this class is meant to read or to write.
print_messages=False : bool, optional
Whether to print messages or not.
test_mode=False : bool, optional
Allows to test classes even if you don't have a real
conection with a NI USB 6212.
Attributes
----------
streamer : nidaqmx.stream_writers or nidaqmx.stream_readers
DAQ task's manager that either writes or reads (but not both).
channels : fwp_classes.WrapperDict
DAQ task's channels, callable by name.
pins : fwp_classes.WrapperDict
DAQ task's channels, callable by pin.
nchannels : int
DAQ task's number of channels.
Methods
-------
add_channels(ChannelClass, *pins, **kwargs)
Adds channel/s of a certain class.
read(nsamples=None, samplerate=None, callback=None)
Reads from input channel/s if this task is meant to read.
write(status=True, frequency=None, duty_cycle=None)
Writes on output channel/s if this task is meant to write. Up to
now, it's only possible to write on a single PWM channel.
stop()
Stops the current task.
close()
Closes communication.
Other Attributes
----------------
write_mode : bool
Whether this is a writing class or a reading one.
test_mode : bool
Whether it's test mode (no real connection) or not.
print : bool
Whether to print inner messages or not.
"""
def __init__(self, device, mode='r',
print_messages=False, test_mode=False):
# DAQ's general attributes
self.__device = device
self.__task = nid.Task()
# Is this task meant to write or to read? Can't do both!
self.__write_mode = 'w' in mode.lower()
# Is there a real DAQ connected or are we just testing?
self.test_mode = test_mode
if test_mode:
self.print = test_mode
else:
self.print = print_messages
# DAQ channels' managers
self.channels = WrapperDict()
self.pins = WrapperDict()
self.__nchannels = 0
# DAQ's reading or writing manager and configuration
self.streamer = None
def __enter__(self):
return self
# return self.__task
def __exit__(self, type, value, traceback):
self.close()
# self.__task.close()
@property
def write_mode(self):
return self.__write_mode
@write_mode.setter
def write_mode(self, value):
msg = "Can't modify this! Must close and open new task"
raise AttributeError(msg)
@property
def nchannels(self):
return self.__nchannels
@nchannels.setter
def nchannels(self, value):
raise AttributeError("Can't modify this manually!")
def add_channels(self, ChannelClass, *pins, **kwargs):
"""Adds channel/s of a certain class.
Parameters
----------
ChannelClass : fwp_daq_channels class
New channel/s' class. Must have a signature given by
"ChannelClass(device, task, streamer, pin, print_messages,
test_mode, **kwargs)".
pins : int, optional
Literally DAQ pin/s' number. Must put at least one.
**kwargs : optional
Optional parameters of new channel/s' class.
Returns
-------
nothing
"""
if not bool(pins):
raise ValueError("Must pass at least one pin number")
# Add channels
new_pins = {}
new_channels = {}
for p in pins:
ch = ChannelClass(
self.__device,
self.__task,
self.__streamer,
p,
**kwargs,
print_messages=self.print,
test_mode=self.test_mode)
new_pins[p] = ch
name = ch.channel.split('/')[1]
new_channels[name] = ch
# Update channels and pins
self.pins.update(new_pins) # holds them by pin
self.channels.update(new_channels) # holds them by channel
self.__dict__.update(new_channels) # attributes by channel name
# Reconfigure
self.__nchannels = self.nchannels + len(new_channels)
self.streamer = None
self.samplerate = None
# self.buffersize = None
@property
def streamer(self):
return self.__streamer
@streamer.setter
def streamer(self, value):
if value is not None:
raise AttributeError("Hey! You can't set this manually!")
# If read mode, StreamReader.
if not self.write_mode:
if self.nchannels > 1:
reader = sr.AnalogMultiChannelReader(
self.__task.in_stream)
else:
reader = sr.AnalogSingleChannelReader(
self.__task.in_stream)
self.__streamer = reader
# Else, StreamWriter for PWM output channel
else:
self.__streamer = sw.CounterWriter(
self.__task.out_stream)
# Reconfigure channels' streamer
if self.nchannels > 0:
try:
self.pins.streamer = self.__streamer
except:
raise AttributeError("Coudn't set streamer to channels")
@property
def samplerate(self):
if self.write_mode:
return TypeError("This task is meant to write!")
else:
return self.__samplerate
@samplerate.setter
def samplerate(self, value):
if self.write_mode:
return TypeError("This task is meant to write!")
else:
# Check if I need to reconfigure
try:
needs_reconfiguration = self.__samplerate != value
except:
needs_reconfiguration = True
# Reconfigure if needed
if needs_reconfiguration:
if value is None: # Default value is maximum value
try:
value = int(400e3/self.nchannels)
except ZeroDivisionError:
value = 400e3
self.__check_samplerate__(value)
if not self.test_mode:
self.__task.timing.cfg_samp_clk_timing(
rate = value)
else:
self.__print__("Should 'task.timing.cgf_samp...'")
self.__samplerate = value
# @property
# def buffersize(self):
# if self.write_mode:
# return TypeError("This task is meant to write!")
# else:
# return self.__buffersize
#
# """Some things we would have liked to do research on:
# task.streamer._in_stream.channels_to_read
# task.streamer._in_stream.logging_file_path
# task.streamer._in_stream.configure_logging
# task.streamer._in_stream.curr_read_pos
# task.streamer._in_stream.read_all_avail_samp
# task.streamer._in_stream.input_onbrd_buf_size
# """
#
# @buffersize.setter
# def buffersize(self, value):
# if self.write_mode:
# return TypeError("This task is meant to write!")
# else:
#
# # Check if I need to reconfigure
# try:
# needs_reconfiguration = self.__buffersize != value
# except:
# needs_reconfiguration = True
#
# # Reconfigure if needed
# if needs_reconfiguration:
# if not self.test_mode:
# if value is None: # Default value is DAQ's one.
# value = self.streamer._in_stream.input_buf_size
# else:
# self.streamer._in_stream.input_buf_size = value
# else:
# self.__print__("Should 'streamer._in_stream.in...'")
# self.__buffersize = value
def read(self, nsamples=None, samplerate=None, duration=None,
callback=None, nsamples_each=200, use_stream=True,
do_return=True):
"""Reads from the input channels.
Parameters
----------
nsamples=None : int, optional
Total number of samples to be acquired from each channel. If
None, the acquisition is continuous and must be stopped by a
KeyboardInterrupt.
samplerate=None : int, float, optional
Samplerate in Hz by channel. If None, samplerate attribute
is used, which is maximum samplerate by default.
duration=None : int, optional
Total duration of the measurement. If nsamples is not
specified but duration is, a new nsamples will be determined
as 'nsamples=duration*samplerate'.
callback=None : function, optional
Callback function. Mustn't return anything. And must either
take in no parameters or either take in only one parameter,
which will be filled with acquired data.
nsamples_each=200 : int, optional
Number of samples acquired by the DAQ before they are passed
to the PC. Same number of samples are collected before
running callback function.
use_stream=True : bool, optional
Whether to use a 'nidaqmx.stream_readers' method if possible
or not.
do_return=True : bool, optional
Whether to return the acquired signal or not.
Returns
-------
signal : np.array
Measured data. If nsamples is not None, it has shape
(self.nchannels, nsamples). If nsamples is None, it has
shape (self.nchannels, i*nsamples_each) where i is an
integer number.
See Also
--------
https://nidaqmx-python.readthedocs.io/en/latest/task.html#
nidaqmx.task.Task.register_every_n_samples_acquired_into_buffer_
event
"""
# INITIAL PARAMETERS
# First, some general configuration
if self.write_mode:
raise TypeError("This task is meant to write!")
if samplerate is None:
samplerate = self.samplerate
else:
self.__check_samplerate__(samplerate)
if nsamples is None and duration is not None:
nsamples = duration * samplerate
# Is callback required?
if nsamples is not None and nsamples < nsamples_each:
callback=None
self.__print__("Callback won't be played because \
'nsamples_each' is smaller than 'nsamples'")
# Does callback take data as a parameter?
if callback is None:
callback_parameters = False
else:
callback_parameters = spec.getfullargspec(callback)[0]
if len(callback_parameters)>1:
raise ValueError("Callback must have only 1 variable")
callback_parameters = bool(callback_parameters)
# Then, get a wrapper callback to wrap the user's
wrapper_callback = self.__choose_wrapper_callback__(
nsamples,
callback,
callback_parameters,
use_stream)
"""There, 'parameters' indicates whether the user's callback
takes in a parameter or not"""
# If wrapper callback needed, configure it.
if wrapper_callback is not None:
if not self.test_mode:
self.__task.register_every_n_samples_acquired_into_buffer_event(
nsamples_each, # call callback every
wrapper_callback)
else:
self.__print__("Should 'task.register_every...'")
# If necessary, set array for the total acquired samples
if do_return and nsamples is not None: # single
signal = utl.zeros((self.nchannels, nsamples),
dtype=np.float64)
elif do_return: # continuous
signal = np.array([], dtype=np.float64)
# Just in case, be ready for measuring in tiny pieces
global each_signal, message, ntimes
each_signal = utl.zeros((self.nchannels,
nsamples_each),
dtype=np.float64)
message = "Number of {}-sized samples' arrays".format(
nsamples_each)
message = message + " read: {}"
ntimes = 0
# SINGLE ACQUISITION
if nsamples is not None:
# Set single reading mode
if not self.test_mode:
self.__task.timing.cfg_samp_clk_timing(
rate=samplerate,
sample_mode=single)
else:
self.__print__("Should 'task.timing.cfg...'")
# According to wrapper callback...
if wrapper_callback is None or not callback_parameters:
if do_return:
# Just measure
if not self.test_mode:
self.__task.start()
if use_stream:
signal = self.__stream_read__(
nsamples,
signal)
else:
signal = self.__read__(nsamples)
self.__task.stop()
else:
self.__print__("Should 'start'+'read_ma...'+'stop'")
return signal
else:
# No need to measure
return
else:
self.__task.start()
while True:
try:
pass
except KeyboardInterrupt:
break
# self.__task.wait_until_done()
self.__task.stop()
if do_return:
return signal
else:
return
# CONTINUOUS ACQUISITION
else:
# Set continuous reading mode
if not self.test_mode:
self.__task.timing.cfg_samp_clk_timing(
rate = samplerate,
sample_mode = continuous
)
else:
self.__print__("Should 'task.timing.cfg...'")
# Start the task
if not self.test_mode:
self.__task.start()
else:
self.__print__("Should run 'task.start'")
print("Acquiring... Press Ctrl+C to stop.")
while True:
try:
pass
except KeyboardInterrupt:
print("Stopped acquiring")
break
# try:
# self.__task.wait_until_done()
# except KeyboardInterrupt:
# pass
self.__task.stop()
if do_return:
return signal
else:
return
def write(self, status=True, frequency=None, duty_cycle=None):
"""Sets the output channels to write.
Up to now, it only allows to turn on/off one digital PWM output.
Parameters
----------
status=True : bool, optional
Whether to turn on or off.
frequency=None : bool, int, float, optional
PWM's pulse frequency. If None, it uses the pre-configured
frequency.
duty_cycle=None : bool, int, float, {0<=duty_cycle<=1}, optional
PWM's pulse duty cycle and therefore mean value. If None, it
uses the pre-configured duty cycle.
Returns
-------
nothing
See Also
--------
fwp_daq_channels.DigitalPWMOutput.status
"""
if not self.write_mode:
raise TypeError("This task is meant to read!")
elif self.nchannels>1:
msg = "This method is only available for 1 PWM Output"
raise IndexError(msg)
# Reconfigure if needed
if frequency is not None:
self.pins.frequency = frequency
if duty_cycle is not None:
self.pins.duty_cycle = duty_cycle
self.pins.status = status
def stop(self):
"""Stops the task.
Parameters
----------
none
Returns
-------
nothing
See Also
--------
nidaqmx.Task.stop
"""
self.__task.stop()
def close(self):
"""Closes the task.
Parameters
----------
none
Returns
-------
nothing
See Also
--------
nidaqmx.Task.close
"""
self.__task.close()
def __read__(self, nsamples):
data = self.__task.read(
number_of_samples_per_channel=int(nsamples))
self.__task.wait_until_done()
# # IS ALL THIS REALLY NECESSARY?
# try:
# len(data[0])
# except:
# return np.array(data)
#
# data = np.array(data).T
# if self.nchannels == 1:
# data = np.expand_dims(data, axis=0).T
#
return np.array(data)
def __stream_read__(self, nsamples, signal):
self.__streamer.read_many_sample(
signal,
number_of_samples_per_channel=int(nsamples),
timeout=20)
return signal
def __choose_wrapper_callback__(self, nsamples, callback,
callback_parameters,
use_stream):
# Now choose the right callback wrapper
if nsamples is not None: # SINGLE ACQUISITION
if callback is None:
return self.__get_wrapper_callback__(0)
elif not callback_parameters:
return self.__get_wrapper_callback__(1)
else:
return self.__get_wrapper_callback__(2)
else: # CONTINUOUS ACQUISITION
if callback is None:
if not use_stream:
return self.__get_wrapper_callback__(3)
else:
return self.__get_wrapper_callback__(4)
elif not callback_parameters:
return self.__get_wrapper_callback__(5)
else:
return self.__get_wrapper_callback__(6)
def __get_wrapper_callback__(self, option):
# These are the possible wrapper callbacks
def no_callback(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that just reads"""
global do_return, nsamples_each
global ntimes, message
global each_signal, signal
if do_return:
each_signal = self.__read__(nsamples_each)
signal = utl.multiappend(signal, each_signal)
ntimes += 1
self.__print__(message.format(ntimes))
return 0
def no_callback_w_stream(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that just reads with read_streamers"""
global do_return, nsamples_each
global ntimes, message
global each_signal, signal
if do_return:
each_signal = self.__stream_read__(nsamples_each,
each_signal)
signal = utl.multiappend(signal, each_signal)
ntimes += 1
self.__print__(message.format(ntimes))
return 0
def wrap_callback(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that just wrapps"""
global callback
callback()
return 0
def noarg_callback(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that just wrapps"""
global callback
global do_return, nsamples_each
global ntimes, message
global each_signal, signal
callback()
if do_return:
each_signal = self.__read__(nsamples_each)
signal = utl.multiappend(signal, each_signal)
ntimes += 1
self.__print__(message.format(ntimes))
return 0
def arg_callback(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that wrapps and reads"""
global callback
global do_return, nsamples_each
global ntimes, message
global each_signal, signal
each_signal = self.__read__(nsamples_each)
callback(each_signal)
if do_return:
signal = utl.multiappend(signal, each_signal)
ntimes += 1
self.__print__(message.format(ntimes))
return 0
def stop_callback(task_handle,
every_n_samples_event_type,
number_of_samples, callback_data):
"""A nidaqmx callback that wrapps, reads and stops"""
global callback, nsamples
global do_return, nsamples_each, nsamples_acquired
global ntimes, message
global each_signal, signal
nsamples_acquired = ntimes * nsamples_each
if nsamples_acquired <= nsamples:
each_signal = self.__read__(nsamples_each)
callback(each_signal)
if do_return:
signal = utl.multiappend(signal, each_signal)
ntimes += 1
self.__print__(message.format(ntimes))
else:
raise KeyboardInterrupt
# self.__task.control(task_states.TASK_STOP)
return 0
# This is the algorithm to choose
# Option must be an int from 0 to 6
wrapper_callback = [None,
wrap_callback,