blib/lib/RPi/UnicornHatHD.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package RPi::UnicornHatHD;
2 #
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #
7 our $VERSION = "0.07";
8 #
9 0 ▶use WiringPi::API;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 #
11 my $spi_channel = 0;
12 my $spi_max_speed_hz = 9000000;
13 my $sof = 0x72;
14  
15 # _DELAY = 1.0/120 # Leave this to the user
16 #
17 sub BUILD {
18 my ($self, $args) = @_;
19 if ((WiringPi::API::spi_setup($spi_channel, $spi_max_speed_hz) < 0)) {
20 die "failed to open the SPI bus...\n";
21 }
22 }
23 has brightness => (is => 'rw', default => sub {.5});
24 has rotation => (is => 'rw', default => sub {0});
25 has matrix => (is => 'lazy', clearer => 'clear');
26  
27 sub _build_matrix {
28 [map {
29 [map { [0, 0, 0] } 1 .. 16]
30 } 1 .. 16
31 ];
32 }
33  
34 sub show {
35 my @buffer = $_[0]->_rotate;
36 my $brightness = $_[0]->brightness;
37 WiringPi::API::spiDataRW(0,
38 [$sof, map { $_ * $brightness } @buffer],
39 1 + scalar @buffer);
40 }
41  
42 sub off {
43 my $s = shift;
44 $s->clear();
45 $s->show();
46 }
47  
48 sub set_all {
49 my $s = shift;
50 my ($r, $g, $b)
51 = $#_ == 2 ?
52 @_
53 : (map { hex $_ } $_[0] =~ m[#(\w{2})(\w{2})(\w{2})]);
54 for my $x (0 .. 15) {
55 for my $y (0 .. 15) {
56 $s->matrix->[$x][$y] = [$r, $g, $b];
57 }
58 }
59 }
60  
61 sub set_pixel {
62 my $s = shift;
63 my ($x, $y, $r, $g, $b)
64 = $#_ == 4 ?
65 @_
66 : (@_[0 .. 1], map { hex $_ } $_[2] =~ m[#(\w{2})(\w{2})(\w{2})]);
67 $s->matrix->[$x][$y] = [$r, $g, $b];
68 }
69  
70 sub get_pixel {
71 my $s = shift;
72 my ($x, $y) = @_;
73 $s->matrix->[$x][$y];
74 }
75  
76 sub _rotate {
77 my $s = shift;
78 my $in = $s->matrix;
79 for my $rot (0 .. (($s->rotation / 90) % 4 - 1)) {
80 my @out = ();
81 my $rows = scalar @$in;
82 for my $row (0 .. $rows - 1) {
83 $out[$row] = [map { $_->[$row] } reverse @$in];
84 }
85  
86 #wantarray ? @out : \@out;
87 $in = \@out;
88 }
89 __flatten($in);
90 }
91  
92 sub __flatten {
93 map { ref $_ ? __flatten(@{$_}) : $_ } @_;
94 }
95 1;
96 __END__
↑ Summary