1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#!/usr/bin/perl
use strict;
use warnings;
use HiPi::Interface::MicroDotPHAT;
use Time::HiRes ();
my $phat = HiPi::Interface::MicroDotPHAT->new();
print q(Fading Text
Uses the brightness control to fade between messages.
Press Ctrl+C to exit
);
my $speed = 5;
my @strings = ("One", "Two", "Three", "Four");
my $string = 0;
my $shown = 1;
$phat->show();
# Start time. Phase offset
my $start = Time::HiRes::time();
my $offsetx = 0;
my $offsety = 0;
my $kerning = 0;
while(1) {
my $b = (sin((Time::HiRes::time() - $start) * $speed) + 1) / 2;
$phat->set_brightness($b);
if($b < 0.002 && $shown ) {
$phat->clear();
$phat->write_string($strings[$string], $offsetx, $offsety, $kerning);
$string += 1;
$string %= scalar @strings;
$phat->show();
$shown = 0;
}
# At maximum brightness, confirm the string has been shown
if ( $b > 0.998 ) {
$shown = 1;
}
# Sleep a bit to save resources, this wont affect the fading speed
$phat->sleep_milliseconds( 10 );
}
1;
|