63 lines
2.1 KiB
Perl
Executable File
63 lines
2.1 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
# Lauflicht: jede LED des Würfels leuchtet einmal auf, von unten nach oben,
|
|
# in Regenbogenfarben. Nutzt den manuellen Modus der REST-API (PATCH /api/pixels)
|
|
# und gibt den Würfel am Ende wieder an den letzten Effekt zurück.
|
|
# Braucht nur Perl-Kernmodule.
|
|
#
|
|
# perl examples/lauflicht.pl [Host] [Millisekunden pro LED]
|
|
# perl examples/lauflicht.pl cube.local 80
|
|
|
|
use strict;
|
|
use warnings;
|
|
use HTTP::Tiny;
|
|
use JSON::PP;
|
|
use Socket qw(inet_ntoa);
|
|
use Time::HiRes qw(sleep);
|
|
|
|
my $host = shift // 'cube.local';
|
|
my $delay = (shift // 80) / 1000;
|
|
|
|
# Namen einmal auflösen, und zwar nach IPv4: cube.local per mDNS kostet sonst
|
|
# bei jeder Anfrage Zeit (wie curl ohne -4)
|
|
my $packed = gethostbyname($host) or die "$host nicht gefunden\n";
|
|
my $base = 'http://' . inet_ntoa($packed) . '/api';
|
|
my $http = HTTP::Tiny->new(timeout => 5);
|
|
|
|
sub patch {
|
|
my ($path, $body) = @_;
|
|
my $res = $http->request('PATCH', "$base/$path", {
|
|
headers => { 'Content-Type' => 'application/json' },
|
|
content => encode_json($body),
|
|
});
|
|
die "Fehler $res->{status}: $res->{content}\n" unless $res->{success};
|
|
}
|
|
|
|
# Farbton 0..1 -> "#rrggbb" (volle Sättigung und Helligkeit)
|
|
sub rainbow {
|
|
my ($h) = @_;
|
|
my $i = int($h * 6) % 6;
|
|
my $f = $h * 6 - int($h * 6);
|
|
my @rgb = @{ ([1, $f, 0], [1 - $f, 1, 0], [0, 1, $f], [0, 1 - $f, 1], [$f, 0, 1], [1, 0, 1 - $f])[$i] };
|
|
return sprintf '#%02x%02x%02x', map { int($_ * 255 + 0.5) } @rgb;
|
|
}
|
|
|
|
# Bei Strg-C den Würfel nicht im manuellen Modus stehen lassen
|
|
$SIG{INT} = sub { patch('state', { manual => JSON::PP::false }); exit 1 };
|
|
|
|
# z = Etage von unten, x von hinten nach vorn, y von rechts nach links
|
|
# (vorn = Mikrofon); das ist auch die Reihenfolge der LEDs in der Kette
|
|
my $n = 0;
|
|
for my $z (0 .. 4) {
|
|
for my $y (0 .. 4) {
|
|
for my $x (0 .. 4) {
|
|
patch('pixels', {
|
|
fill => '#000000',
|
|
pixels => [{ x => $x, y => $y, z => $z, color => rainbow($n++ / 125) }],
|
|
});
|
|
sleep $delay;
|
|
}
|
|
}
|
|
}
|
|
|
|
patch('state', { manual => JSON::PP::false }); # zurück zum letzten Effekt
|