File Coverage

blib/lib/Device/Opto22.pm
Criterion Covered Total %
statement 12 112 10.7
branch 0 10 0.0
condition 0 3 0.0
subroutine 4 17 23.5
pod 10 13 76.9
total 26 155 16.7


line stmt bran cond sub pod time code
1             package Device::Opto22;
2              
3 1     1   19020 use 5.008008;
  1         4  
  1         32  
4 1     1   5 use strict;
  1         2  
  1         29  
5 1     1   4 use warnings;
  1         5  
  1         40  
6              
7             require Exporter;
8              
9 1     1   594 use Device::Opto22::Firewire;
  1         16  
  1         13  
10              
11             our @ISA = qw( Exporter Device::Opto22::Firewire );
12              
13              
14             # Items to export into callers namespace by default. Note: do not export
15             # names by default without a very good reason. Use EXPORT_OK instead.
16             # Do not simply export all your public functions/methods/constants.
17              
18             our @EXPORT_OK = qw( send_PUC get_scratchpadint get_scratchpadfloat get_eu_lst get_digital_lst wr_digital_pnt serial_chat );
19              
20             our @EXPORT = qw( );
21              
22             our $VERSION = '0.92';
23              
24             ################################################33
25             # Opto22 Specific commands
26             ################################################33
27              
28             our $timeout = 10;
29              
30             sub new {
31              
32 0     0 1   my $class = shift @_;
33 0           my %args = @_;
34              
35 0           my $PeerAddr = $args{PeerAddr} ;
36 0           my $PeerPort = $args{PeerPort} ;
37              
38 0 0 0       if (not ($PeerAddr && $PeerPort) ) {
39 0           die "Inputs missing in Package $class in Method new";
40             }
41              
42             # This establishes a SENDER socket connection OK
43 0           my $self = new Device::Opto22::Firewire (PeerAddr => $PeerAddr ,
44             PeerPort => $PeerPort );
45              
46 0 0         unless ( $self ) { die "Error connecting in Package $class" ; }
  0            
47              
48 0           bless $self, $class;
49              
50 0           return $self;
51             }
52              
53             sub send_PUC {
54              
55 0     0 1   my ($self) = @_;
56              
57 0           my $packet = $self->bld_wr_quad_packet(0xf0380000,0x00000001);
58 0           my $rsp = $self->chat($packet);
59              
60 0           return ($rsp) ;
61             }
62              
63             # Does not work... not sure what to send ...
64             sub send_MMap_ver {
65              
66 0     0 0   my ($self) = @_;
67              
68 0           my $packet = $self->bld_wr_quad_packet(0xf0300000,0x00000000);
69 0           my $rsp = $self->chat($packet);
70              
71 0           return ($rsp) ;
72             }
73              
74             #-------------------------------------------
75             # Load $len number of elements from the
76             # ScratchPad Integer Table of the brain's
77             # memory map
78             #-------------------------------------------
79             sub get_scratchpadint {
80              
81 0     0 1   my ($self) = shift @_;
82 0           my ($len) = shift @_; # how many ints to get
83            
84 0           my $packet = $self->bld_rd_blk_packet(0xF0D81000,4*$len);
85              
86 0           my $data = $self->chat($packet);
87              
88 0           my @lst = big2little_int($data);
89              
90 0           return (@lst) ;
91              
92             }
93              
94             #-------------------------------------------
95             # Load $len number of elements from the
96             # ScratchPad Float Table of the brain's
97             # memory map
98             #-------------------------------------------
99             sub get_scratchpadfloat {
100              
101 0     0 1   my ($self) = shift @_;
102 0           my ($len) = shift @_; # how many ints to get
103              
104 0           my $packet = $self->bld_rd_blk_packet(0xF0D82000,4*$len);
105              
106 0           my $data = $self->chat($packet);
107              
108 0           my @lst = big2little_fp($data);
109              
110 0           return (@lst) ;
111             }
112              
113             sub get_eu_lst {
114              
115 0     0 1   my ($self) = @_;
116              
117 0           my $packet = $self->bld_rd_blk_packet(0xf0600000,256);
118              
119 0           my $data = $self->chat($packet);
120              
121 0           my @lst = big2little_fp($data);
122              
123 0           return (@lst) ;
124             }
125              
126             sub get_digital_lst {
127              
128 0     0 1   my ($self) = @_;
129              
130 0           my $packet = $self->bld_rd_blk_packet(0xf0400000,8);
131              
132 0           my $data = $self->chat($packet);
133              
134             # Place 0 or 1 in each element of an array
135 0           my @lst = split // , unpack "B64" , $data ;
136              
137 0           @lst = reverse @lst ; # Ain't Perl cool
138              
139 0           return @lst ;
140             }
141              
142             sub wr_digital_pnt {
143              
144 0     0 1   my ($self) = shift @_;
145              
146 0           my ($channel, $data) = @_;
147              
148             # Note: channel is zero based
149 0           my $offset = $channel * 64 ;
150              
151 0           $offset = 0xf0900000 + $offset ;
152              
153             # The set/clr byte are next to each other
154 0 0         if ( not($data) ) { $offset = $offset + 4 ; }
  0            
155              
156 0           my $packet = $self->bld_wr_quad_packet($offset, "1");
157              
158 0           my $rtn = $self->chat($packet);
159              
160 0           return ($rtn) ;
161             }
162              
163              
164             #----------------------------------------------------------
165             # serial_chat() - sends and rcvs on open Opto serial port
166             #
167             # NOTE: The self object must have opened a socket on
168             # a port that maps to a particular Opto serial module.
169             #----------------------------------------------------------
170             sub serial_chat {
171              
172 0     0 1   my ($self) = shift @_;
173              
174 0           my ($data) = @_;
175              
176 0           my $rsp ;
177              
178             my $cnt;
179 0           eval {
180              
181 0           alarm ($timeout) ;
182              
183 0           print $self $data;
184              
185             # Wait for data
186 0           select(undef,undef,undef,0.5);
187              
188 0           $cnt = $self->recv($rsp, 30, 0 ) ;
189              
190 0           alarm(0);
191              
192             };
193              
194 0 0         if (not ($cnt)) {
195 0           ${*$self}->{'error_msg'} = "Nothing returned in Serial Chat";
  0            
196 0           return 0;
197             }else{
198 0           return ($rsp) ;
199             }
200             }
201              
202             #----------------------------------------------------------
203             # serial_send() - sends to an open Opto serial port
204             #
205             # NOTE: The self object must have opened a socket on
206             # a port that maps to a particular Opto serial module.
207             #----------------------------------------------------------
208             sub serial_send {
209              
210 0     0 1   my ($self) = shift @_;
211              
212 0           my ($data) = @_;
213              
214 0           eval {
215              
216 0           alarm ($timeout) ;
217              
218 0           print $self $data;
219              
220 0           alarm(0);
221              
222             };
223              
224 0           return(0);
225             }
226              
227              
228             #----------------------------------------------------------
229             # serial_rcv- rcvs on an open Opto serial port
230             #
231             # NOTE: The self object must have opened a socket on
232             # a port that maps to a particular Opto serial module.
233             #----------------------------------------------------------
234             sub serial_rcv {
235              
236 0     0 1   my ($self) = shift @_;
237              
238 0           my $rsp ;
239              
240 0           eval {
241              
242 0           alarm ($timeout) ;
243              
244 0           $rsp = <$self>; # blocks until newline terminated
245              
246 0           alarm(0);
247              
248             };
249              
250 0 0         if($rsp =~ /^\*/){ # all good data starts with a * (Paroscientific Depth Probe Specific for P3 cmd)
251              
252 0           $rsp =~ s/\*0001(.+)/$1/; # strip off the *0001 leading address info
253              
254 0           return($rsp);
255              
256             }else{
257 0           ${*$self}->{'error_msg'} = "Bad data received in serial_rcv ($rsp)\n$!\n" ;
  0            
258 0           return(0);
259             }
260             }
261              
262              
263             ########################
264             # Private methods
265             ########################
266              
267             sub big2little_fp {
268              
269 0     0 0   my $data = shift @_ ;
270              
271 0           my @lst = () ;
272              
273 0           my $size = length $data ;
274              
275 0           for ( my $j = 0 ; $j < $size ; $j = $j + 4 ) {
276              
277 0           my $quadword = substr $data , $j , 4 ;
278              
279 0           my $reverse_quadword = reverse $quadword ; # Big to Little Endian
280              
281 0           push @lst, unpack( "f", $reverse_quadword );
282              
283             }
284              
285 0           return @lst ;
286              
287             }
288              
289              
290             sub big2little_int {
291              
292 0     0 0   my $data = shift @_ ;
293              
294 0           my @lst = () ;
295              
296 0           my $size = length $data ;
297              
298 0           for (my $j = 0 ; $j < $size ; $j = $j + 4 ) {
299              
300 0           my $quadword = substr $data , $j , 4 ;
301              
302 0           my $reverse_quadword = reverse $quadword ; # Big to Little Endian
303              
304 0           push @lst, unpack( "l", $reverse_quadword );
305              
306             }
307              
308 0           return @lst ;
309              
310             }
311              
312             1;
313             __END__