blib/lib/RF/HC12.pm

filter: Statementstmt 17.1% 17 / 99 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 23.8% 5 / 21 Podpod 100% 11 / 11 totaltotal 19.3% 33 / 171 CC 21 SCAR 23.2
CC
21
Cov
12%
config
20.0
_fetch_control
20.0
baud
12.0
CRAP 10.2
↑ Summary
1 package RF::HC12;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use RPi::Serial;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our $VERSION = '0.01';
10  
11 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 COMM_BAUD => 9600,
13 EOL => 0x0A,
14 DEBUG_FETCH => 0,
15 10 };
16  
17 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
18 0 my ($class, $dev) = @_;
19  
20 0 ▶ croak "new() requires a serial device path sent in" if ! defined $dev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dev 0 0 0
21  
22 0 my $self = bless {}, $class;
23  
24 0 $self->_serial($dev, COMM_BAUD);
25  
26 0 return $self;
27 }
28 0 ▶sub test {
Subroutine test: not called CC=1 SCAR=6.9
Pod documented
29 0 my ($self) = @_;
30 0 my $cmd = 'AT';
31 0 return $self->_fetch_control($cmd);
32 }
33 0 ▶sub baud {
Subroutine baud: not called CC=3 SCAR=24.8
Pod documented
34 0 my ($self, $baud) = @_;
35  
36 0 ▶ if (defined $baud){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $baud) 0 0 0
37 0 ▶ if (! $self->_baud_rates($baud)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_baud_rates($baud)) 0 0 0
38 0 croak "baud rate '$baud' is invalid. See the documentation";
39 }
40 0 my $cmd = "AT+B$baud";
41 0 $self->_set_control($cmd);
42 }
43 0 return $self->_fetch_control('AT+RB');
44 }
45 0 ▶sub power {
Subroutine power: not called CC=3 SCAR=24.8
Pod documented
46 0 my ($self, $tp) = @_;
47  
48 0 ▶ if (defined $tp){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tp) 0 0 0
49 0 ▶ if (! $self->_power_rates($tp)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_power_rates($tp)) 0 0 0
50 0 croak "transmit power '$tp' is invalid. See the documentation";
51 }
52 0 my $cmd = "AT+P$tp";
53 0 $self->_set_control($cmd);
54 }
55 0 return $self->_fetch_control('AT+RP');
56 }
57 0 ▶sub mode {
Subroutine mode: not called CC=3 SCAR=24.8
Pod documented
58 0 my ($self, $mode) = @_;
59  
60 0 ▶ if (defined $mode){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 0 0 0
61 0 ▶ if (! grep {$mode == $_} (1, 2, 3)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$mode == $_;} 1, 2, 3) 0 0 0
62 0 croak "functional mode'$mode' is invalid. Values are 1, 2 or 3";
63 }
64 0 my $cmd = "AT+FU$mode";
65 0 $self->_set_control($cmd);
66 }
67 0 return $self->_fetch_control('AT+RF');
68 }
69 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self) = @_;
71 0 return $self->_fetch_control('AT+V');
72 }
73 0 ▶sub channel {
Subroutine channel: not called CC=3 SCAR=24.8
Pod documented
74 0 my ($self, $channel) = @_;
75  
76 0 ▶ if (defined $channel){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel) 0 0 0
77 0 ▶ if (! grep {$channel == $_} (1..127)){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$channel == $_;} 1..127) 0 0 0
78 0 croak "channel '$channel' is invalid. Values are 1-127";
79 }
80  
81 # pad out zeros to the left to make 3 chars
82 0 my $pad = 3 - length($channel);
83 0 $channel = 0 x $pad . $channel;
84  
85 0 my $cmd = "AT+C$channel";
86 0 $self->_set_control($cmd);
87 }
88 0 return $self->_fetch_control('AT+RC');
89 }
90 0 ▶sub config {
Subroutine config: not called CC=4 SCAR=30.0
Pod documented
91 0 my ($self) = @_;
92  
93 0 $self->_serial->puts('AT+RX');
94  
95 0 my ($read, $line_count);
96  
97 0 while (1){
98 0 ▶ if ($self->_serial->avail){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_serial->avail) 0 0 0
99 0 my $char = $self->_serial->getc;
100  
101 0 $read .= chr $char;
102 0 ▶ if (hex(sprintf("%x", $char)) == 0x0A){
Branch: 1 branch
BranchTrueFalseTotal
if (hex sprintf("%x", $char) == 10) 0 0 0
103 0 $line_count++;
104 0 ▶ return $read if $line_count == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $line_count == 4 0 0 0
105 }
106  
107 }
108 }
109 }
110 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self) = @_;
112 0 $self->_fetch_control('AT+DEFAULT');
113 }
114 0 ▶sub sleep {
Subroutine sleep: not called CC=1 SCAR=6.9
Pod documented
115 0 return $_[0]->_fetch_control('AT+SLEEP');
116 }
117 0 ▶sub wake {
Subroutine wake: not called CC=1 SCAR=6.9
Pod documented
118 0 return $_[0]->_fetch_control('AT');
119 }
120  
121 0 ▶sub _serial {
Subroutine _serial: not called CC=2 SCAR=17.9
122 0 my ($self, $dev, $comm_baud) = @_;
123 0 ▶ return $self->{serial} if exists $self->{serial};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"serial"} 0 0 0
124 0 $self->{serial} = RPi::Serial->new($dev, $comm_baud);
125 0 return $self->{serial};
126 }
127 0 ▶sub _set_control {
Subroutine _set_control: not called CC=1 SCAR=6.9
128 0 my ($self, $control) = @_;
129 0 $self->_serial->puts($control);
130 }
131 0 ▶sub _fetch_control {
Subroutine _fetch_control: not called CC=4 SCAR=30.0
132 0 my ($self, $control) = @_;
133  
134 0 $self->_serial->puts($control);
135  
136 0 my $read;
137  
138 0 while (1){
139 0 ▶ if ($self->_serial->avail){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_serial->avail) 0 0 0
140 0 my $char = $self->_serial->getc;
141  
142 0 ▶ if (hex(sprintf("%x", $char)) == 0x0D){
Branch: 1 branch
BranchTrueFalseTotal
if (hex sprintf("%x", $char) == 13) 0 0 0
143 0 next;
144 }
145  
146 0 ▶ if (hex(sprintf("%x", $char)) == 0x0A){
Branch: 1 branch
BranchTrueFalseTotal
if (hex sprintf("%x", $char) == 10) 0 0 0
147 0 return $read;
148 }
149  
150 0 $read .= chr $char;
151 }
152 }
153 }
154 0 ▶sub _baud_rates {
Subroutine _baud_rates: not called CC=3 SCAR=24.8
155 0 my ($self, $baud) = @_;
156  
157 0 my $baud_rates = {
158 1200 => 1,
159 2400 => 1,
160 4800 => 1,
161 9600 => 1,
162 19200 => 1,
163 38400 => 1,
164 57600 => 1,
165 115200 => 1,
166 };
167  
168 0 ▶ return sort {$a <=> $b}(keys(%$baud_rates)) if ! defined $baud;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $baud 0 0 0
169  
170 0 ▶ return $baud_rates->{$baud} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$baud_rates->{$baud} ? : 0 0 0
171 }
172 0 ▶sub _power_rates {
Subroutine _power_rates: not called CC=3 SCAR=24.8
173 0 my ($self, $rate) = @_;
174  
175 0 my $power_rates = {
176 1 => -1,
177 2 => 2,
178 3 => 5,
179 4 => 8,
180 5 => 11,
181 6 => 14,
182 7 => 17,
183 8 => 20
184 };
185  
186 0 ▶ return %$power_rates if ! defined $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate 0 0 0
187 0 ▶ return $power_rates->{$rate} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$power_rates->{$rate} ? : 0 0 0
188 }
189  
190 10 1;
191 __END__
↑ Summary