rb_scope 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.txt ADDED
@@ -0,0 +1,26 @@
1
+ ##
2
+ # ni_scope
3
+ #
4
+ # ( C2Ruby wrapper to NI-Scope drivers )
5
+ #
6
+ # version 2.0.1
7
+ # author hugo benichi
8
+ # email hugo.benichi@m4x.org
9
+ # copyright 2012 hugo benichi
10
+ #
11
+ # reproduction
12
+ #
13
+ # You can use this code without restriction if you mention my name in your project.
14
+ # Other than that, you can redistribute and/or modify without restriction.
15
+ #
16
+ # description
17
+ #
18
+ # A Ruby layer wrapping around C calls to the NI-Scope drivers.
19
+ # Most C data manipulation and C calls are done through the FFI gems.
20
+ # In addition a few C methods are provided for improved efficiency when handling data
21
+ #
22
+ # installation
23
+ #
24
+ # gem install ni_scope-x.y.z.gem
25
+ #
26
+ ##
data/compile_cl.bat ADDED
@@ -0,0 +1,17 @@
1
+
2
+ :: Bat script for compilation of the rb_scope.dll using MSVS cl and link.
3
+ :: This script has been automatically generated by rake from the template in rakefile.rb.
4
+ ::
5
+ :: author hugo benichi
6
+ :: email hugo.benichi@gmail.com
7
+ :: copyright 2012 hugo benichi
8
+
9
+ call "C:\Program Files (x86)\MVS10.0\VC\vcvarsall.bat"
10
+
11
+ call ruby -Ilib ext\ni_scope\wrapper_generator.rb > ext\ni_scope\wrapper.c
12
+
13
+ call cl -c .\ext\ni_scope\wrapper.c -I"C:\Program Files (x86)\IVI Foundation\VISA\WinNT\include"
14
+
15
+ call link /DLL /OUT:ext\ni_scope\rb_scope.dll wrapper.obj "C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Lib\msc\niScope.lib"
16
+
17
+
@@ -0,0 +1,68 @@
1
+ require 'rb_scope'
2
+
3
+ # old
4
+ settings = {
5
+ rate: 1000E6,
6
+ bandwidth: 300E6,
7
+ size: 2000,
8
+ packet: 1000,
9
+ shot: 10000,
10
+ tomo: 100000,
11
+ tomo_ohp: 100000,
12
+ tomo_full: 100000,
13
+ byte: 1,
14
+ trig_ref: 62,
15
+ }
16
+
17
+ pxi = Scope::start :pxi5152 do
18
+ self[:timeout] = -1 #= infinity
19
+ return_self false
20
+ config 1
21
+ trigger_edge
22
+ vertical "0", 0.2, settings[:bandwidth], 11
23
+ vertical "1", 1.0, settings[:bandwidth], 11
24
+ horizontal settings[:rate], settings[:size], settings[:shot], settings[:trig_ref]
25
+ set_buffer settings[:packet], settings[:byte]
26
+ end
27
+
28
+ #new change this to
29
+
30
+ settings = {
31
+ rate: 1000E6,
32
+ bandwidth: 300E6,
33
+ size: 1000,
34
+ trig_ref: 62,
35
+ trig_voltage: 1.0,
36
+
37
+ }
38
+
39
+ parameters = {
40
+ rate: 1000000,
41
+ frame_size 500,
42
+ frame_total 1000000,
43
+ channel: [
44
+ {chan_impedance: 50, range: 0.4 },
45
+ {chan_impedance: 50, range: 0.2 }
46
+ ],
47
+
48
+ }
49
+
50
+ session = RbScope.new.configure parameters
51
+
52
+ #old
53
+ pxi[:chan0] = sender[name][:quad]
54
+ pxi[:chan1] = sender[name][:phase]
55
+ pxi.set_num_record settings[name]
56
+
57
+ pxi.acquire
58
+ trigger :on
59
+ pxi.fetch
60
+ trigger :off
61
+
62
+ #new: change this to
63
+ pxi.run conf( records: 10000, chunk: 1000, frame: 500, timeout: 500, chan0: foo, chan1: bar) { trigger :on}
64
+ trigger :off
65
+ # assume foo and bar are object with two
66
+ # methods, :context and :call with return
67
+ # a struct pointer and a function pointer
68
+
@@ -0,0 +1,76 @@
1
+
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include "fetch.h"
5
+ #include "niScope.h"
6
+
7
+ #define prompt(MSG, ...) printf("RbScope::Fetching::fetch.c >> " MSG, __VA_ARGS__)
8
+
9
+ typedef
10
+ struct niScope_wfmInfo
11
+ wfm_info;
12
+
13
+
14
+ DLL int fetch(
15
+ fetch_hlp* parameters
16
+ )
17
+ {
18
+
19
+ unsigned int session = parameters-> session;
20
+ char* address = parameters-> address;
21
+ int frm_siz = parameters-> frm_siz;
22
+ int frm_tot = parameters-> frm_tot;
23
+ int frm_chk = parameters-> frm_chk;
24
+ double timeout = parameters-> timeout;
25
+
26
+ char* buff0 = NULL;
27
+ char* buff1 = NULL;
28
+ buffer_address buf_chan0 = parameters-> chan0;
29
+ buffer_address buf_chan1 = parameters-> chan1;
30
+ void* context0 = parameters-> chan0_context;
31
+ void* context1 = parameters-> chan1_context;
32
+ error_handler err_clbck = parameters-> err_clbck;
33
+
34
+ wfm_info frm_inf;
35
+ wfm_info *frm_inf_p = &frm_inf;
36
+
37
+ int i, rez, records_done;
38
+
39
+ rez = niScope_SetAttributeViBoolean(session, VI_NULL, NISCOPE_ATTR_ALLOW_MORE_RECORDS_THAN_MEMORY, 1); // maybe have to move this before horizontal configuration
40
+ if( rez < 0) err_clbck(rez);
41
+ rez = niScope_SetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_HORZ_RECORD_LENGTH, frm_siz);
42
+ if( rez < 0) err_clbck(rez);
43
+ rez = niScope_SetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_HORZ_NUM_RECORDS, frm_tot);
44
+ if( rez < 0) err_clbck(rez);
45
+ rez = niScope_SetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_FETCH_NUM_RECORDS, frm_chk);
46
+ if( rez < 0) err_clbck(rez);
47
+
48
+ prompt("starting fetching on %s\n", address);
49
+ for (i = 0; i < frm_tot; i += frm_chk)
50
+ {
51
+
52
+ rez = niScope_SetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_FETCH_RECORD_NUMBER, i);
53
+
54
+ if( rez < 0) err_clbck(rez);
55
+ if (buf_chan0 && (buff0 = buf_chan0(context0)) ) /* this call can block ! */
56
+ rez = niScope_FetchBinary8(session, "0", timeout, frm_siz, buff0, frm_inf_p);
57
+
58
+ if( rez < 0) err_clbck(rez);
59
+ if (buf_chan1 && (buff1 = buf_chan1(context1)) ) /* this call can block ! */
60
+ rez = niScope_FetchBinary8(session, "1", timeout, frm_siz, buff1, frm_inf_p);
61
+
62
+ if( rez < 0) err_clbck(rez);
63
+
64
+ rez = niScope_GetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_RECORDS_DONE, &records_done);
65
+ if( rez < 0) err_clbck(rez);
66
+ prompt("%s, frames fetched #%i | measured #%i | left %i\n", address, i, records_done, records_done - i);
67
+ }
68
+
69
+ rez = niScope_GetAttributeViInt32(session, VI_NULL, NISCOPE_ATTR_RECORDS_DONE, &records_done);
70
+ if( rez < 0) err_clbck(rez);
71
+
72
+ prompt("%s, total frames measured per channel: %i\n", address, records_done);
73
+
74
+ return records_done;
75
+
76
+ }
@@ -0,0 +1,38 @@
1
+
2
+
3
+ #ifndef RB_SCOPE_FETCHING_HEADER
4
+ #define RB_SCOPE_FETCHING_HEADER
5
+
6
+ #define DLL extern __declspec(dllexport)
7
+
8
+ typedef
9
+ struct fetch_helper
10
+ fetch_hlp;
11
+
12
+ typedef
13
+ char* (*buffer_address)(void*);
14
+
15
+ typedef
16
+ void (*error_handler)(int);
17
+
18
+ struct
19
+ fetch_helper
20
+ {
21
+ char* address; // physical address of the device
22
+ error_handler err_clbck; // ruby callback to manage errors
23
+ buffer_address chan0; // return the address of the next
24
+ buffer_address chan1; // output buffer for chan0/1
25
+ void* chan0_context; // context for the buffer_address functions
26
+ void* chan1_context; // one per buffer
27
+ double timeout; // in sec; -1 = infinite
28
+ int frm_siz; // numbr of data points per one frame
29
+ int frm_tot; // total number of frm to fetch, per channels all chans one acq
30
+ int frm_chk; // numbr of frames to read in one packet (set by the buffer size)
31
+ unsigned int session; // (ViSession pointer id used by the niScope API)
32
+ };
33
+
34
+ DLL int fetch (fetch_hlp* parameters);
35
+
36
+ #endif
37
+
38
+
@@ -0,0 +1,24 @@
1
+ #!/bin/bash
2
+ #
3
+ # This script takes into input the path to the ivi and niScope headers
4
+ # on the local machine, and use them to generate a text file of pairs
5
+ # name / values.
6
+ #
7
+ # This script is meant to be used in a Unix environment
8
+ # (for example cygwin on windows)
9
+ #
10
+
11
+ path_to_headers=$1
12
+
13
+ ext/rb_scope/generators/extract_names.rb $path_to_headers/niScope.h > ni_scope_names.h
14
+ ext/rb_scope/generators/extract_names.rb $path_to_headers/ivi.h >> ni_scope_names.h
15
+ ext/rb_scope/generators/extract_names.rb $path_to_headers/IviScope.h >> ni_scope_names.h
16
+
17
+ #gcc -E ni_scope_names.h -I$path_to_headers > ni_scope_preprocessed
18
+ #ext/rb_scope/generators/extract_pairs.rb ni_scope_preprocessed > lib/rb_scope/api/niScope_pairs
19
+
20
+ gcc -E ni_scope_names.h -I$path_to_headers > ni_scope_preprocessed
21
+ ext/rb_scope/generators/extract_pairs.rb ni_scope_preprocessed > lib/rb_scope/api/niScope_pairs
22
+
23
+ rm ni_scope_names.h ni_scope_preprocessed
24
+
@@ -0,0 +1,26 @@
1
+ #!/usr/local/bin/ruby
2
+ #
3
+ # This script will extract the #define NAME VALUE pairs from
4
+ # a header file specified in ARGV[0] and write them into ARGV[1]
5
+ # if ARGV[1] is not specified, output to stdin
6
+ #
7
+ # call with extract.rb scope.h filtered_scope.
8
+ #
9
+
10
+ output = ARGV[1] ? File.open(ARGV[1], "w") : $stdout
11
+
12
+ output.puts '#include "niScope.h"'
13
+
14
+ File.open(ARGV[0],'r').readlines.select{ |line|
15
+ line =~ /^#define[\s]+[\w]+[\s]+\S[\s\S]+/
16
+ }.each{ |line|
17
+ #name, val = line.match(/^#define[\s]+([\w]+)[\s]+(\S[\s\S]+)/)[1..2]
18
+ match = line.match(/^#define[\s]+([\w]+)[\s]+(\S[\s\S]+)/)
19
+ name = match[1]
20
+ val = match[2] #
21
+ #output.puts "%s\n %s" % [name, val.gsub( %r|[\s]*/\*[\s\w]*\*/[\s]*|, '')]
22
+ #output.puts "%s\n %s" % [name, val.gsub( %r|[\s]*/\*[\s\S]*?\*/[\s]*|, '')]
23
+ output.puts '"%s"; %s;' % [name, name]
24
+ }
25
+
26
+ output.close unless output == $stdin
@@ -0,0 +1,18 @@
1
+ #!/usr/local/bin/ruby
2
+
3
+ # This script will extract the NAME VALUE pairs file generated by extract_names.rb
4
+ # extract_names.rb and the gcc preprocessor and sligthly reformats the pairs in
5
+ # order for ruby to be able to process them as eval strings.
6
+ #
7
+ # call with extract.rb scope.h filtered_scope.
8
+ #
9
+
10
+ output = ARGV[1] ? File.open(ARGV[1], "w") : $stdout
11
+
12
+ File.open(ARGV[0],'r').readlines.select{ |line|
13
+ line =~ /^"[\w]+";/
14
+ }.each{ |line|
15
+ output.puts line.gsub("L)",")").gsub(/(\d)L/,"\\1").sub(/^"([\w]+)"; /, "\\1\n") [0..-3]
16
+ }
17
+
18
+ output.close unless output == $stdin
@@ -0,0 +1,51 @@
1
+ module RbScope
2
+
3
+ module API
4
+
5
+
6
+ module Wrapper
7
+
8
+ require 'rb_scope/api/template'
9
+
10
+ def self.build file_name=nil
11
+ output = file_name ? File.open(file_name, 'w') : $stdout
12
+ [
13
+ "/*",
14
+ " * This file has been automatically generated",
15
+ " * by rb_scope/ext/rb_scope/#{File.basename __FILE__}",
16
+ " *",
17
+ " * software: rb_scope gem",
18
+ " * author: hugo benichi",
19
+ " * contact: hugo.benichi@m4x.org",
20
+ " *",
21
+ " */",
22
+ "",
23
+ '#include "niScope.h"',
24
+ "",
25
+ "#define DLL extern __declspec(dllexport)",
26
+ "",
27
+ ].each{|line| output.puts line}
28
+ RbScope::API::Template.each do |sig|
29
+ i = 0
30
+ args_chain_decla, args_chain_invoc = *sig[1].map{ |type|
31
+ ctype = RbScope::API::Types[:c][type] || type.to_s
32
+ arg = "arg#{i+=1}"
33
+ [ "%s %s" % [ctype, arg], arg ]
34
+ }.transpose.map{ |chain| chain.join ", "}
35
+ ext_name = sig[0].to_s.sub "niScope", "rbScope" #do not hardcode this
36
+ int_name = sig[0].to_s
37
+ ret_type = RbScope::API::Types[:c][sig[2]] || sig[2].to_s
38
+ substitu = [ret_type, ext_name, args_chain_decla, int_name, args_chain_invoc]
39
+ template = "DLL %s %s(%s)\n{\n return %s(%s);\n}\n\n"
40
+ output.puts template % substitu
41
+ end
42
+ output.close unless output == $stdout
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ RbScope::API::Wrapper.build ARGV[0] #argv[0] is the path to the output file || stdout if nul
@@ -0,0 +1,154 @@
1
+ /*
2
+ * This file has been automatically generated
3
+ * by ni_scope/api/builder.rb
4
+ *
5
+ * software: ni_scope gem
6
+ * author: hugo benichi
7
+ * contact: hugo.benichi@m4x.org
8
+ *
9
+ */
10
+
11
+ #include "niScope.h"
12
+
13
+ #define DLL extern __declspec(dllexport)
14
+
15
+ DLL int rbScope_init(char* arg1, unsigned short arg2, unsigned short arg3, ViUInt32* arg4)
16
+ {
17
+ return niScope_init(arg1, arg2, arg3, arg4);
18
+ }
19
+
20
+ DLL int rbScope_close(unsigned int arg1)
21
+ {
22
+ return niScope_close(arg1);
23
+ }
24
+
25
+ DLL int rbScope_CalSelfCalibrate(unsigned int arg1, char* arg2, int arg3)
26
+ {
27
+ return niScope_CalSelfCalibrate(arg1, arg2, arg3);
28
+ }
29
+
30
+ DLL int rbScope_ConfigureAcquisition(unsigned int arg1, int arg2)
31
+ {
32
+ return niScope_ConfigureAcquisition(arg1, arg2);
33
+ }
34
+
35
+ DLL int rbScope_ConfigureTriggerDigital(unsigned int arg1, char* arg2, int arg3, double arg4, double arg5)
36
+ {
37
+ return niScope_ConfigureTriggerDigital(arg1, arg2, arg3, arg4, arg5);
38
+ }
39
+
40
+ DLL int rbScope_ConfigureTriggerEdge(unsigned int arg1, char* arg2, double arg3, int arg4, int arg5, double arg6, double arg7)
41
+ {
42
+ return niScope_ConfigureTriggerEdge(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
43
+ }
44
+
45
+ DLL int rbScope_ConfigureTriggerImmediate(unsigned int arg1)
46
+ {
47
+ return niScope_ConfigureTriggerImmediate(arg1);
48
+ }
49
+
50
+ DLL int rbScope_ConfigureChanCharacteristics(unsigned int arg1, char* arg2, double arg3, double arg4)
51
+ {
52
+ return niScope_ConfigureChanCharacteristics(arg1, arg2, arg3, arg4);
53
+ }
54
+
55
+ DLL int rbScope_ConfigureVertical(unsigned int arg1, char* arg2, double arg3, double arg4, int arg5, double arg6, unsigned short arg7)
56
+ {
57
+ return niScope_ConfigureVertical(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
58
+ }
59
+
60
+ DLL int rbScope_ConfigureHorizontalTiming(unsigned int arg1, double arg2, int arg3, double arg4, int arg5, unsigned short arg6)
61
+ {
62
+ return niScope_ConfigureHorizontalTiming(arg1, arg2, arg3, arg4, arg5, arg6);
63
+ }
64
+
65
+ DLL int rbScope_ActualRecordLength(unsigned int arg1, ViInt32* arg2)
66
+ {
67
+ return niScope_ActualRecordLength(arg1, arg2);
68
+ }
69
+
70
+ DLL int rbScope_ActualNumWfms(unsigned int arg1, char* arg2, ViInt32* arg3)
71
+ {
72
+ return niScope_ActualNumWfms(arg1, arg2, arg3);
73
+ }
74
+
75
+ DLL int rbScope_GetAttributeViBoolean(unsigned int arg1, char* arg2, unsigned int arg3, unsigned short* arg4)
76
+ {
77
+ return niScope_GetAttributeViBoolean(arg1, arg2, arg3, arg4);
78
+ }
79
+
80
+ DLL int rbScope_GetAttributeViInt32(unsigned int arg1, char* arg2, unsigned int arg3, ViInt32* arg4)
81
+ {
82
+ return niScope_GetAttributeViInt32(arg1, arg2, arg3, arg4);
83
+ }
84
+
85
+ DLL int rbScope_GetAttributeViReal64(unsigned int arg1, char* arg2, unsigned int arg3, double* arg4)
86
+ {
87
+ return niScope_GetAttributeViReal64(arg1, arg2, arg3, arg4);
88
+ }
89
+
90
+ DLL int rbScope_GetAttributeViString(unsigned int arg1, char* arg2, unsigned int arg3, int arg4, char* arg5)
91
+ {
92
+ return niScope_GetAttributeViString(arg1, arg2, arg3, arg4, arg5);
93
+ }
94
+
95
+ DLL int rbScope_SetAttributeViBoolean(unsigned int arg1, char* arg2, unsigned int arg3, unsigned short arg4)
96
+ {
97
+ return niScope_SetAttributeViBoolean(arg1, arg2, arg3, arg4);
98
+ }
99
+
100
+ DLL int rbScope_SetAttributeViInt32(unsigned int arg1, char* arg2, unsigned int arg3, int arg4)
101
+ {
102
+ return niScope_SetAttributeViInt32(arg1, arg2, arg3, arg4);
103
+ }
104
+
105
+ DLL int rbScope_SetAttributeViReal64(unsigned int arg1, char* arg2, unsigned int arg3, double arg4)
106
+ {
107
+ return niScope_SetAttributeViReal64(arg1, arg2, arg3, arg4);
108
+ }
109
+
110
+ DLL int rbScope_SetAttributeViString(unsigned int arg1, char* arg2, unsigned int arg3, char* arg4)
111
+ {
112
+ return niScope_SetAttributeViString(arg1, arg2, arg3, arg4);
113
+ }
114
+
115
+ DLL int rbScope_errorHandler(unsigned int arg1, int arg2, char* arg3, char* arg4)
116
+ {
117
+ return niScope_errorHandler(arg1, arg2, arg3, arg4);
118
+ }
119
+
120
+ DLL int rbScope_GetError(unsigned int arg1, ViInt32* arg2, int arg3, char* arg4)
121
+ {
122
+ return niScope_GetError(arg1, arg2, arg3, arg4);
123
+ }
124
+
125
+ DLL int rbScope_GetErrorMessage(unsigned int arg1, int arg2, int arg3, char* arg4)
126
+ {
127
+ return niScope_GetErrorMessage(arg1, arg2, arg3, arg4);
128
+ }
129
+
130
+ DLL int rbScope_InitiateAcquisition(unsigned int arg1)
131
+ {
132
+ return niScope_InitiateAcquisition(arg1);
133
+ }
134
+
135
+ DLL int rbScope_AcquisitionStatus(unsigned int arg1, ViInt32* arg2)
136
+ {
137
+ return niScope_AcquisitionStatus(arg1, arg2);
138
+ }
139
+
140
+ DLL int rbScope_FetchBinary8(unsigned int arg1, char* arg2, double arg3, int arg4, char* arg5, void* arg6)
141
+ {
142
+ return niScope_FetchBinary8(arg1, arg2, arg3, arg4, arg5, arg6);
143
+ }
144
+
145
+ DLL int rbScope_FetchBinary16(unsigned int arg1, char* arg2, double arg3, int arg4, short* arg5, void* arg6)
146
+ {
147
+ return niScope_FetchBinary16(arg1, arg2, arg3, arg4, arg5, arg6);
148
+ }
149
+
150
+ DLL int rbScope_Abort(unsigned int arg1)
151
+ {
152
+ return niScope_Abort(arg1);
153
+ }
154
+
@@ -0,0 +1,31 @@
1
+ module RbScope
2
+
3
+ # This module acts as the Ruby interface to the C API.
4
+ # It loads the niScope dll library and links itself to the dll functions.
5
+ # It also loads all the macro constants defined in niScope.h for easier
6
+ # scripting of the digitizer devices from Ruby.
7
+ module API
8
+
9
+ require 'ffi'
10
+ require 'rb_scope/api/niScope_template'
11
+ require 'rb_scope/api/niScope_values'
12
+
13
+ extend FFI::Library
14
+
15
+ #begin
16
+ ffi_lib $LOAD_PATH.map{ |path| path+"/rb_scope/rb_scope.dll"}
17
+ Template.each{ |sig|
18
+ puts sig
19
+ attach_function(
20
+ sig[0].to_s.sub("niScope", "rbScope").to_sym,
21
+ sig[1].map{|t| Types[:ruby][t] || t },
22
+ sig[2]
23
+ )
24
+ }
25
+ #rescue
26
+ # RbScope::prompt "could not load rb_scope.dll library"
27
+ #end
28
+
29
+ end
30
+
31
+ end