NXT/MacBlueComm.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 79 CC 5 SCAR 19.5
CC
5
Cov
0%
do_cmd
12.0
myread
12.0
new
2.0
CRAP 7.0
1 package LEGO::NXT::MacBlueComm;
2  
3 use Device::SerialPort;
4 use LEGO::NXT::Constants;
5 use strict;
6  
7 =head1 NAME
8  
9 LEGO::NXT::MacBlueComm - The Mac Bluetooth Communication Module For the NXT
10  
11 =head1 SYNOPSIS
12  
13 use LEGO::NXT:MacBlueComm;
14  
15 $comm = new LEGO::NXT::MacBlueComm();
16  
17 =head1 DESCRIPTION
18  
19 Presents a bluetooth comm interface to the LEGO NXT on the Mac for internal use in the L<LEGO::NXT> module.
20  
21 =cut
22  
23 my $DEFAULT_DEVICE = '/dev/tty.NXT-DevB-1';
24  
25 =head1 METHODS
26  
27 =head2 new
28  
29 $serial_port = '/dev/tty.NXT-DevB-1';
30 $comm = new LEGO::NXT:MacBlueComm($serial_port)
31 $nxt = LEGO::NXT->new( $comm );
32  
33 Creates a new Mac Bluetooth comm object based on a Mac Bluetooth serial port.
34  
35 =cut
36  
37 sub new
38 {
39 my ($pkg,$device) = @_;
40  
41 my $this = {
42 'device' => $device || $DEFAULT_DEVICE,
43 'fh' => undef ,
44 'status' => 0
45 };
46  
47 bless $this, $pkg;
48 $this;
49 }
50  
51  
52 sub connect
53 {
54 my ($this) = @_;
55
56 my $dev = new Device::SerialPort( $this->{device}, 0 )
57 || die "Can't open ".$this->{device}.": $!\n";
58  
59 $dev->baudrate(9600);
60 $dev->parity("none");
61 $dev->databits(8);
62 $dev->stopbits(1);
63 $dev->handshake("none");
64 $dev->write_settings || die "fail write setting";
65  
66 $|=1; #HOT PIPES
67 $this->{fh} = $dev;
68 $dev;
69 }
70  
71 sub do_cmd
72 {
73 my ($this,$request,$needsret) = @_;
74  
75 $this->connect unless $this->{fh};
76  
77 my $dev = $this->{fh};
78  
79 my $count = $dev->write( $request );
80  
81 return if( $needsret == $NXT_NORET );
82  
83 $|=1;
84  
85 my $msgbytes = $this->myread(2);
86 my ( $msgsz ) = unpack('v',$msgbytes);
87 my $msg = $this->myread( $msgsz );
88  
89 return $msgbytes.$msg;
90 }
91  
92 sub myread
93 {
94 my ( $this, $br ) = @_;
95 my $msgbytes = '';
96 my $dev = $this->{fh};
97  
98 return if $br < 1;
99  
100 while( my ($tc,$tbytes) = $dev->read( $br ) )
101 {
102 $br -= $tc;
103 $msgbytes .= $tbytes;
104 last if $br < 1;
105 }
106  
107 return $msgbytes;
108 }
109  
110  
111 sub type { 'blue' }
112  
113 1;