line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package RPi::UnicornHatHD; |
2
|
|
|
|
|
|
|
# |
3
|
1
|
|
|
1
|
|
871
|
use Moo; |
|
1
|
|
|
|
|
7501
|
|
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
1296
|
use strictures 2; |
|
1
|
|
|
|
|
1131
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
423
|
use namespace::clean; |
|
1
|
|
|
|
|
9298
|
|
|
1
|
|
|
|
|
9
|
|
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
8
|
|
|
|
|
|
|
# |
9
|
1
|
|
|
1
|
|
561
|
use WiringPi::API; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
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__ |