File Coverage

blib/lib/Device/MindWave/Utils.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition 3 3 100.0
subroutine 8 8 100.0
pod 3 3 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package Device::MindWave::Utils;
2              
3 14     14   22541 use strict;
  14         30  
  14         589  
4 14     14   71 use warnings;
  14         21  
  14         388  
5              
6 14     14   73 use List::Util qw(sum);
  14         30  
  14         1838  
7 14     14   134 use Scalar::Util qw(blessed);
  14         26  
  14         2227  
8              
9 14     14   82 use base qw(Exporter);
  14         25  
  14         6105  
10             our @EXPORT_OK = qw(checksum
11             packet_isa
12             packet_to_bytes);
13              
14             sub checksum
15             {
16 181     181 1 2030 my ($bytes) = @_;
17              
18 181         193 my $sum = sum(0, @{$bytes});
  181         526  
19 181         326 my $byte = $sum & 0xFF;
20 181         399 return ((~$byte) & 0xFF);
21             }
22              
23             sub packet_isa
24             {
25 223     223 1 916 my ($packet, $suffix) = @_;
26              
27             return
28 223   100     3235 ((blessed $packet)
29             and ($packet->isa('Device::MindWave::Packet::'.$suffix)));
30             }
31              
32             sub packet_to_bytes
33             {
34 88     88 1 128 my ($packet) = @_;
35              
36 88         414 my $bytes = $packet->as_bytes();
37 88         153 my $checksum = checksum($bytes);
38              
39 88         97 return [ 0xAA, 0xAA, scalar @{$bytes}, @{$bytes}, $checksum ];
  88         101  
  88         359  
40             }
41              
42             1;
43              
44             __END__