| 1 | package Linux::IRPulses; | ||||||||||||||||||||||||||||
| 2 | 4 | $Linux::IRPulses::VERSION = '0.6'; | |||||||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use constant DEBUG => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Parse LIRC pulse data | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 14 | as_is => [ 'pulse', 'space', 'pulse_or_space' ], | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub pulse ($) {[ 'pulse', $_[0] ]} | ||||||||||||||||||||||||||
|
Subroutine
pulse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub space ($) {[ 'space', $_[0] ]} | ||||||||||||||||||||||||||
|
Subroutine
space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub pulse_or_space ($) {[ 'either', $_[0] ]} | ||||||||||||||||||||||||||
|
Subroutine
pulse_or_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | has 'fh' => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | 4 | has 'header' => ( | |||||||||||||||||||||||||||
| 26 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | isa => 'ArrayRef[ArrayRef[Str]]', | ||||||||||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||||||||||
| 30 | handles => { | ||||||||||||||||||||||||||||
| 31 | header_length => 'count', | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 4 | has 'zero' => ( | |||||||||||||||||||||||||||
| 35 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | isa => 'ArrayRef[ArrayRef[Str]]', | ||||||||||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||||||||||
| 39 | handles => { | ||||||||||||||||||||||||||||
| 40 | zero_length => 'count', | ||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | 4 | has 'one' => ( | |||||||||||||||||||||||||||
| 44 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => 'ArrayRef[ArrayRef[Str]]', | ||||||||||||||||||||||||||||
| 47 | required => 1, | ||||||||||||||||||||||||||||
| 48 | handles => { | ||||||||||||||||||||||||||||
| 49 | one_length => 'count', | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | 4 | has 'bit_count' => ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | isa => 'Int', | ||||||||||||||||||||||||||||
| 55 | required => 1, | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | 4 | has '_bits' => ( | |||||||||||||||||||||||||||
| 58 | is => 'rw', | ||||||||||||||||||||||||||||
| 59 | isa => 'Int', | ||||||||||||||||||||||||||||
| 60 | default => 0, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | 4 | has 'tolerance' => ( | |||||||||||||||||||||||||||
| 63 | is => 'ro', | ||||||||||||||||||||||||||||
| 64 | isa => 'Num', | ||||||||||||||||||||||||||||
| 65 | required => 1, | ||||||||||||||||||||||||||||
| 66 | default => 0.20, | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | 4 | has 'callback' => ( | |||||||||||||||||||||||||||
| 69 | is => 'ro', | ||||||||||||||||||||||||||||
| 70 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 71 | required => 1, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | 4 | has '_do_close_file' => ( | |||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 76 | default => 0, | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | 4 | has '_do_end_loop' => ( | |||||||||||||||||||||||||||
| 79 | is => 'rw', | ||||||||||||||||||||||||||||
| 80 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 81 | default => 0, | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | 4 | has '_did_see_header' => ( | |||||||||||||||||||||||||||
| 84 | is => 'rw', | ||||||||||||||||||||||||||||
| 85 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 86 | default => 0, | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 4 | has '_header_index' => ( | |||||||||||||||||||||||||||
| 89 | traits => ['Number'], | ||||||||||||||||||||||||||||
| 90 | is => 'rw', | ||||||||||||||||||||||||||||
| 91 | isa => 'Int', | ||||||||||||||||||||||||||||
| 92 | default => 0, | ||||||||||||||||||||||||||||
| 93 | handles => { | ||||||||||||||||||||||||||||
| 94 | _add_header_index => 'add', | ||||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | 4 | has '_bit_count' => ( | |||||||||||||||||||||||||||
| 98 | traits => ['Number'], | ||||||||||||||||||||||||||||
| 99 | is => 'rw', | ||||||||||||||||||||||||||||
| 100 | isa => 'Int', | ||||||||||||||||||||||||||||
| 101 | default => 0, | ||||||||||||||||||||||||||||
| 102 | handles => { | ||||||||||||||||||||||||||||
| 103 | _add_bit_count => 'add', | ||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | 4 | has '_is_maybe_zero' => ( | |||||||||||||||||||||||||||
| 107 | is => 'rw', | ||||||||||||||||||||||||||||
| 108 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 109 | default => 1, | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | 4 | has '_is_maybe_one' => ( | |||||||||||||||||||||||||||
| 112 | is => 'rw', | ||||||||||||||||||||||||||||
| 113 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 114 | default => 1, | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | 4 | has '_zero_index' => ( | |||||||||||||||||||||||||||
| 117 | traits => ['Number'], | ||||||||||||||||||||||||||||
| 118 | is => 'rw', | ||||||||||||||||||||||||||||
| 119 | isa => 'Int', | ||||||||||||||||||||||||||||
| 120 | default => 0, | ||||||||||||||||||||||||||||
| 121 | handles => { | ||||||||||||||||||||||||||||
| 122 | _add_zero_index => 'add', | ||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | 4 | has '_one_index' => ( | |||||||||||||||||||||||||||
| 126 | traits => ['Number'], | ||||||||||||||||||||||||||||
| 127 | is => 'rw', | ||||||||||||||||||||||||||||
| 128 | isa => 'Int', | ||||||||||||||||||||||||||||
| 129 | default => 0, | ||||||||||||||||||||||||||||
| 130 | handles => { | ||||||||||||||||||||||||||||
| 131 | _add_one_index => 'add', | ||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub BUILDARGS | ||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 3 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 3 | ▶ | if( exists $args->{dev_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | my $file = delete $args->{dev_file}; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | open( my $in, '<', $file ) or die "Can't open file '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $args->{fh} = $in; | |||||||||||||||||||||||||||
| 145 | 0 | $args->{'_do_close_file'} = 1; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | return $args; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub run | ||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 2 | my $in = $self->fh; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | while( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->_do_end_loop and my $line = readline $in
MC/DC50%1/2
not $self->_do_end_loop and my $line = readline $in
not $self->_do_end_loop
my $line = readline $in
Truth table66%2/3
not(A) and B
A= not $self->_do_end_loopB= my $line = readline $in
| |||||||||||||||||||||||||||||
| 158 | (! $self->_do_end_loop) | ||||||||||||||||||||||||||||
| 159 | && (my $line = readline($in)) | ||||||||||||||||||||||||||||
| 160 | ) { | ||||||||||||||||||||||||||||
| 161 | 75 | chomp $line; | |||||||||||||||||||||||||||
| 162 | 75 | $self->handle_line( $line ); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | ▶ | close $in if $self->_do_close_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 2 | return; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub end | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 2 | $self->_do_end_loop( 1 ); | |||||||||||||||||||||||||||
| 173 | 2 | return; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub handle_line | ||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 142 | ▶ | my ($self, $line) = @_; | ||||||||||||||||||||||||||
|
Subroutine
handle_line: called CC=14 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 142 | warn "Matching: $line\n" if DEBUG; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 142 | ▶ | if( $self->_did_see_header ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 135 | my $is_matched = 0; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 135 | ▶ | if( $self->_is_maybe_zero() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 135 | ▶ | if( $self->_match_line( $line, $self->zero->[$self->_zero_index] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 68 | $self->_add_zero_index(1); | |||||||||||||||||||||||||||
| 188 | 68 | ▶ | if( $self->_zero_index >= $self->zero_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 4 | warn "\tWe have a complete zero signal\n" if DEBUG; | |||||||||||||||||||||||||||
| 190 | 4 | $self->_zero_index(0); | |||||||||||||||||||||||||||
| 191 | 4 | $self->_one_index(0); | |||||||||||||||||||||||||||
| 192 | 4 | $self->_is_maybe_zero(1); | |||||||||||||||||||||||||||
| 193 | 4 | $self->_is_maybe_one(1); | |||||||||||||||||||||||||||
| 194 | 4 | $self->_add_bit_count(1); | |||||||||||||||||||||||||||
| 195 | 4 | $self->_bits( $self->_bits() << 1 | 0 ); | |||||||||||||||||||||||||||
| 196 | 4 | $is_matched = 1; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||
| 199 | 64 | warn "\tWe might have a zero, but we're not sure so sit tight\n" | |||||||||||||||||||||||||||
| 200 | if DEBUG; | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||||||||||
| 204 | 67 | warn "\tIt's definately not a zero\n" if DEBUG; | |||||||||||||||||||||||||||
| 205 | 67 | $self->_is_maybe_zero( 0 ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 135 | ▶ | if( (! $is_matched) && $self->_is_maybe_one() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $is_matched and $self->_is_maybe_one
MC/DC50%1/2
not $is_matched and $self->_is_maybe_one
not $is_matched
$self->_is_maybe_one
Truth table66%2/3
not(A) and B
A= not $is_matchedB= $self->_is_maybe_one
| |||||||||||||||||||||||||||||
| 210 | 131 | ▶ | if( $self->_match_line( $line, $self->one->[$self->_one_index] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 131 | $self->_add_one_index(1); | |||||||||||||||||||||||||||
| 212 | 131 | ▶ | if( $self->_one_index >= $self->one_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | # We have a complete one signal, reset state | ||||||||||||||||||||||||||||
| 214 | 67 | warn "\tWe have a complete one signal\n" if DEBUG; | |||||||||||||||||||||||||||
| 215 | 67 | $self->_zero_index(0); | |||||||||||||||||||||||||||
| 216 | 67 | $self->_one_index(0); | |||||||||||||||||||||||||||
| 217 | 67 | $self->_is_maybe_zero(1); | |||||||||||||||||||||||||||
| 218 | 67 | $self->_is_maybe_one(1); | |||||||||||||||||||||||||||
| 219 | 67 | $self->_add_bit_count(1); | |||||||||||||||||||||||||||
| 220 | 67 | $self->_bits( $self->_bits() << 1 | 1 ); | |||||||||||||||||||||||||||
| 221 | 67 | $is_matched = 1; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | else { | ||||||||||||||||||||||||||||
| 224 | # Might be a one, but we're not sure yet, so sit tight | ||||||||||||||||||||||||||||
| 225 | 64 | warn "\tWe might have a one, but we're not sure so sit tight\n" | |||||||||||||||||||||||||||
| 226 | if DEBUG; | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | else { | ||||||||||||||||||||||||||||
| 230 | 0 | warn "\tIt's definately not a one\n" if DEBUG; | |||||||||||||||||||||||||||
| 231 | 0 | $self->_is_maybe_one( 0 ); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 135 | ▶ | if( $self->_bit_count >= $self->bit_count ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 236 | 3 | warn "\tWe met our bit count, so call the callback\n" if DEBUG; | |||||||||||||||||||||||||||
| 237 | 3 | $self->callback->({ | |||||||||||||||||||||||||||
| 238 | pulse_obj => $self, | ||||||||||||||||||||||||||||
| 239 | code => $self->_bits | ||||||||||||||||||||||||||||
| 240 | }); | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 3 | $self->_zero_index(0); | |||||||||||||||||||||||||||
| 243 | 3 | $self->_one_index(0); | |||||||||||||||||||||||||||
| 244 | 3 | $self->_is_maybe_zero(1); | |||||||||||||||||||||||||||
| 245 | 3 | $self->_is_maybe_one(1); | |||||||||||||||||||||||||||
| 246 | 3 | $self->_bit_count(0); | |||||||||||||||||||||||||||
| 247 | 3 | $self->_did_see_header(0); | |||||||||||||||||||||||||||
| 248 | 3 | $self->_bits(0); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | ▶ | elsif( (! $self->_is_maybe_zero) && (! $self->_is_maybe_one) ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
$self->_is_maybe_zero || $self->_is_maybe_one
MC/DC0%0/2
$self->_is_maybe_zero || $self->_is_maybe_one
$self->_is_maybe_zero
$self->_is_maybe_one
Truth table33%1/3
A || B
A= $self->_is_maybe_zeroB= $self->_is_maybe_one
| |||||||||||||||||||||||||||||
| 252 | 0 | warn "\tWe've gotten to a bad state where nothing looks right. Resetting.\n" | |||||||||||||||||||||||||||
| 253 | if DEBUG; | ||||||||||||||||||||||||||||
| 254 | 0 | $self->_zero_index(0); | |||||||||||||||||||||||||||
| 255 | 0 | $self->_one_index(0); | |||||||||||||||||||||||||||
| 256 | 0 | $self->_is_maybe_zero(1); | |||||||||||||||||||||||||||
| 257 | 0 | $self->_is_maybe_one(1); | |||||||||||||||||||||||||||
| 258 | 0 | $self->_bit_count(0); | |||||||||||||||||||||||||||
| 259 | 0 | $self->_did_see_header(0); | |||||||||||||||||||||||||||
| 260 | 0 | $self->_bit_count(0); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | else { | ||||||||||||||||||||||||||||
| 264 | 7 | ▶ | if( $self->_match_line( $line, $self->header->[$self->_header_index] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 6 | $self->_add_header_index(1); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 6 | ▶ | if( $self->_header_index >= $self->header_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 3 | warn "\tWe have a complete, valid header\n" if DEBUG; | |||||||||||||||||||||||||||
| 269 | 3 | $self->_did_see_header( 1 ); | |||||||||||||||||||||||||||
| 270 | 3 | $self->_header_index( 0 ); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | else { | ||||||||||||||||||||||||||||
| 273 | 3 | warn "\tHave a partial header, sit tight for now\n" if DEBUG; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||
| 277 | 1 | warn "\tThis isn't the part of the header we were expecting. Reset.\n" if DEBUG; | |||||||||||||||||||||||||||
| 278 | 1 | $self->_did_see_header( 0 ); | |||||||||||||||||||||||||||
| 279 | 1 | $self->_header_index( 0 ); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 142 | return; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | sub _match_line | ||||||||||||||||||||||||||||
| 287 | { | ||||||||||||||||||||||||||||
| 288 | 273 | ▶ | my ($self, $line, $expect) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_match_line: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 289 | 273 | my ($expect_type, $expect_num) = @{ $expect }; | |||||||||||||||||||||||||||
| 290 | 273 | warn "\tMatching '$line', expecting '$expect_type $expect_num'\n" if DEBUG; | |||||||||||||||||||||||||||
| 291 | 273 | my ($type, $num) = $line =~ /\A (pulse|space) \s+ (\d+) /x; | |||||||||||||||||||||||||||
| 292 | 273 | ▶ | $expect_type = $type if $expect_type eq 'either'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | return ( | ||||||||||||||||||||||||||||
| 295 | 273 | ▶ | $self->_is_value_in_range( $num, $expect_num ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_is_value_in_range($num, $expect_num) and $expect_type eq $type
MC/DC50%1/2
$self->_is_value_in_range($num, $expect_num) and $expect_type eq $type
$self->_is_value_in_range($num, $expect_num)
$expect_type eq $type
Truth table66%2/3
A and B
A= $self->_is_value_in_range($num, $expect_num)B= $expect_type eq $type
| |||||||||||||||||||||||||||||
| 296 | && ($expect_type eq $type) | ||||||||||||||||||||||||||||
| 297 | ) ? 1 : 0; | ||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | sub _is_value_in_range | ||||||||||||||||||||||||||||
| 301 | { | ||||||||||||||||||||||||||||
| 302 | 273 | ▶ | my ($self, $val, $target_val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_is_value_in_range: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 303 | 273 | my $tolerance = $self->tolerance; | |||||||||||||||||||||||||||
| 304 | 273 | my $min = $target_val - ($target_val * $tolerance); | |||||||||||||||||||||||||||
| 305 | 273 | my $max = $target_val + ($target_val * $tolerance); | |||||||||||||||||||||||||||
| 306 | 273 | warn "\tMatching $min <= $val <= $max\n" if DEBUG; | |||||||||||||||||||||||||||
| 307 | 273 | ▶ | return (($min <= $val) && ($val <= $max)) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$min <= $val and $val <= $max
MC/DC100%2/2
$min <= $val and $val <= $max
$min <= $val
$val <= $max
Truth table100%3/3
A and B
A= $min <= $valB= $val <= $max
| |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 312 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 313 | 4 | 1; | |||||||||||||||||||||||||||
| 314 | __END__ | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =encoding utf8 | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head1 NAME | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Linux::IRPulses - Parse IR data from LIRC | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | use Linux::IRPulses; # exports pulse(), space(), and pulse_or_space() | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | open( my $in, '-|', 'mode2' ) or die "Can't exec mode2: $!\n"; | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | my $ir = Linux::IRPulses->new({ | ||||||||||||||||||||||||||||
| 330 | fh => $in, | ||||||||||||||||||||||||||||
| 331 | header => [ pulse 9000, space 4500 ], | ||||||||||||||||||||||||||||
| 332 | zero => [ pulse 563, space 563 ], | ||||||||||||||||||||||||||||
| 333 | one => [ pulse 563, space 1688 ], | ||||||||||||||||||||||||||||
| 334 | bit_count => 32, | ||||||||||||||||||||||||||||
| 335 | callback => sub { | ||||||||||||||||||||||||||||
| 336 | my ($args) = @_; | ||||||||||||||||||||||||||||
| 337 | my $ir = $args->{pulse_obj}; | ||||||||||||||||||||||||||||
| 338 | my $code = $args->{code}; | ||||||||||||||||||||||||||||
| 339 | ... | ||||||||||||||||||||||||||||
| 340 | }, | ||||||||||||||||||||||||||||
| 341 | }); | ||||||||||||||||||||||||||||
| 342 | $ir->run; | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Parses the pulse/space data coming from LIRC. Note that this works at a little lower | ||||||||||||||||||||||||||||
| 347 | level down the LIRC stack than usual. LIRC usually works by translating the pulses on | ||||||||||||||||||||||||||||
| 348 | its own, mapping that to a button on a remote, and then mapping that to a command to | ||||||||||||||||||||||||||||
| 349 | execute. If you want that, then look at L<Lirc::Client>. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | This module grabs the pulse data coming out of LIRC and then translates that to binary. | ||||||||||||||||||||||||||||
| 352 | That lets you manipulate the raw encoding. | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head1 HOW IR REMOTES WORK | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Perhaps not surprisingly, every company has their own weird way of encoding IR data. | ||||||||||||||||||||||||||||
| 357 | This usually breaks down to sending a header followed by zeros and ones that are | ||||||||||||||||||||||||||||
| 358 | encoded through sending pulses of different lengths. Everyone also has their own | ||||||||||||||||||||||||||||
| 359 | frequency for sending data, although 36KHz is common. Your IR receiver module needs | ||||||||||||||||||||||||||||
| 360 | to be set to the same frequency. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | The length for encoding pulses has to deal with the fact that in the real world, | ||||||||||||||||||||||||||||
| 363 | the IR emitter and receiver won't shut off at exactly the right time. The pulse will | ||||||||||||||||||||||||||||
| 364 | tend to be a bit longer than specified; I've seen as high as 18%. Parsing must therefore | ||||||||||||||||||||||||||||
| 365 | allow a fudge factor in the exact numbers. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | The exports are to help you build a datastructure that the parser can use. In general, | ||||||||||||||||||||||||||||
| 370 | remotes tend to start with a long header, then a space, then a series of pulses and | ||||||||||||||||||||||||||||
| 371 | spaces. | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | For example, NEC remotes start with a header that pulses (voltage high) for 9000μs, | ||||||||||||||||||||||||||||
| 374 | followed by a space (voltage low) for 4500μs. After that, there are 32 bits. A zero is | ||||||||||||||||||||||||||||
| 375 | sent by a pulse of 563μs followed by a space of 563μs. A one is sent by a pulse of | ||||||||||||||||||||||||||||
| 376 | 563μs followed by a space of 1688μs. We can build this in C<Linux::IRPulses> with: | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | my $ir = Linux::IRPulses->new({ | ||||||||||||||||||||||||||||
| 379 | header => [ pulse 9000, space 4500 ], | ||||||||||||||||||||||||||||
| 380 | zero => [ pulse 563, space 563 ], | ||||||||||||||||||||||||||||
| 381 | one => [ pulse 563, space 1688 ], | ||||||||||||||||||||||||||||
| 382 | ... | ||||||||||||||||||||||||||||
| 383 | }); | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | Notice that the C<pulse()> and C<space()> exports help you to specify the datastructure. | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Another example is EasyRaceLapTimer, which is an IR-based timing system for quadcopter | ||||||||||||||||||||||||||||
| 388 | FPV racing. To save on message time length, it encodes by the time of either the | ||||||||||||||||||||||||||||
| 389 | pulses or the spaces. For example, a 0110 would be sent by a pulse of 300μs, a space | ||||||||||||||||||||||||||||
| 390 | of 600μs, a pulse of 600μs, and a space of 300μs. That is, spaces and pulses always | ||||||||||||||||||||||||||||
| 391 | alternate, and the time of the space or pulse tells you if it's a one or zero. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | To handle this, we use C<pulse_or_space()>: | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | my $ir = Linux::IRPulses->new({ | ||||||||||||||||||||||||||||
| 396 | header => [ pulse 300, space 300 ], | ||||||||||||||||||||||||||||
| 397 | zero => [ pulse_or_space 300 ], | ||||||||||||||||||||||||||||
| 398 | one => [ pulse_or_space 600 ], | ||||||||||||||||||||||||||||
| 399 | ... | ||||||||||||||||||||||||||||
| 400 | }); | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | Which doesn't care if it comes across as a pulse or space, as long as the length is correct. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =head1 METHODS | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =head2 new | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | new({ | ||||||||||||||||||||||||||||
| 409 | fh => $fh, | ||||||||||||||||||||||||||||
| 410 | header => [ pulse 9000, space 4500 ], | ||||||||||||||||||||||||||||
| 411 | zero => [ pulse 563, space 563 ], | ||||||||||||||||||||||||||||
| 412 | one => [ pulse 563, space 1688 ], | ||||||||||||||||||||||||||||
| 413 | bit_count => 32, | ||||||||||||||||||||||||||||
| 414 | callback => sub { | ||||||||||||||||||||||||||||
| 415 | my ($args) = @_; | ||||||||||||||||||||||||||||
| 416 | my $ir = $args->{pulse_obj}; | ||||||||||||||||||||||||||||
| 417 | my $code = $args->{code}; | ||||||||||||||||||||||||||||
| 418 | ... | ||||||||||||||||||||||||||||
| 419 | }, | ||||||||||||||||||||||||||||
| 420 | }); | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | Constructor. The C<fh> argument is a filehandle that will be read for pulse data. In | ||||||||||||||||||||||||||||
| 423 | general, this should be a filehandle open for reading that's piped from LIRC's C<mode2> | ||||||||||||||||||||||||||||
| 424 | program. The C<bit_count> argument is the expected length of each message. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | The C<header>, C<zero>, and C<one> arguments are arrayrefs that specify the format of | ||||||||||||||||||||||||||||
| 427 | the respective datapoint. The first data would match the first entry in C<header>, and | ||||||||||||||||||||||||||||
| 428 | then matching each subsequent entry in turn. Once we reach the end of the C<headers> | ||||||||||||||||||||||||||||
| 429 | list, we start matching C<zero> and C<one> in the same way. We continue matching | ||||||||||||||||||||||||||||
| 430 | zeros and ones until we hit C<bit_count>. At that point, we consider the message complete | ||||||||||||||||||||||||||||
| 431 | and pass the data to the subref in C<callback>. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head2 run | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Starts reading the data from the filehandle. The callback will be hit during this | ||||||||||||||||||||||||||||
| 436 | process. | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =head2 handle_line | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | handle_line( $line ); | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Processes a single line of the forms: | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | pulse 1000 | ||||||||||||||||||||||||||||
| 445 | space 2000 | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Hits the callback if we gathered enough lines to get a full code. Be sure to C<chomp> the | ||||||||||||||||||||||||||||
| 448 | line before passing. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head2 end | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Stops the process of reading from the filehandle, returning to normal execution flow | ||||||||||||||||||||||||||||
| 453 | after the place C<run()> was called. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | Copyright (c) 2016 Timm Murray | ||||||||||||||||||||||||||||
| 458 | All rights reserved. | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | Redistribution and use in source and binary forms, with or without modification, are | ||||||||||||||||||||||||||||
| 461 | permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | * Redistributions of source code must retain the above copyright notice, this list of | ||||||||||||||||||||||||||||
| 464 | conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 465 | * Redistributions in binary form must reproduce the above copyright notice, this list of | ||||||||||||||||||||||||||||
| 466 | conditions and the following disclaimer in the documentation and/or other materials | ||||||||||||||||||||||||||||
| 467 | provided with the distribution. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | ||||||||||||||||||||||||||||
| 470 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 471 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||||||||||||||||||||||||||||
| 472 | COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||||||||||||||||||||||||||||
| 473 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 474 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||||||||||||||||||||||||||
| 475 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||||||||||||||||||||||||||||
| 476 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||||||||||||||||||||||||||||
| 477 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =cut | ||||||||||||||||||||||||||||