Beispiel in Perl: Lauflicht über alle LEDs im manuellen Modus
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3effb23edc
commit
752067240b
3 files changed
+71
No files matched your search
@@ -451,6 +451,7 @@ Makefile Bauen und Flashen mit arduino-cli
|
||||
rainbow_test/ Löttest: alle LEDs statisch im Regenbogen
|
||||
hw_test/ Hardwaretest: Mikrofon und Taster
|
||||
strom_test/ Strommessung: feste Lasten nacheinander
|
||||
examples/ Beispiele für die REST-API (lauflicht.pl: Perl, manueller Modus)
|
||||
ios/ iPhone-App (SwiftUI)
|
||||
case/ Sockel: OpenSCAD-Quelle, STL-Dateien, Vorschau
|
||||
docs/ Hardware-Doku, API-Referenz, Bilder
|
||||
|
||||
@@ -243,6 +243,14 @@ req = urllib.request.Request("http://cube.local/api/pixels", method="PUT",
|
||||
urllib.request.urlopen(req)
|
||||
```
|
||||
|
||||
Ein vollständiges Beispiel in Perl, das jede LED einmal aufleuchten lässt und
|
||||
danach zum letzten Effekt zurückkehrt, steht in
|
||||
[`examples/lauflicht.pl`](../examples/lauflicht.pl) (nur Kernmodule):
|
||||
|
||||
```sh
|
||||
perl examples/lauflicht.pl cube.local 80 # Host, Millisekunden pro LED
|
||||
```
|
||||
|
||||
### `GET /api/pixels`
|
||||
|
||||
```json
|
||||
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/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
|
||||
Reference in new issue
Block a user