add blinkenlights

This commit is contained in:
Manawyrm 2023-10-05 21:10:37 +02:00
commit 3fb200fd61
3 changed files with 3470 additions and 0 deletions

3416
blinkenlights.json Normal file

File diff suppressed because it is too large Load Diff

11
blinkenlights.php Normal file
View File

@ -0,0 +1,11 @@
<?php
$frames = json_decode(file_get_contents("blinkenlights.json"), true);
$last_timestamp = 0;
foreach ($frames as $timestamp => $frame)
{
echo chr(27) . $frame;
usleep(($timestamp - $last_timestamp) * 1000);
$last_timestamp = $timestamp;
}

View File

@ -0,0 +1,43 @@
<?php
$start = microtime(true);
$socket = fsockopen("towel.blinkenlights.nl", 23);
$animation = [];
$currentframe = "";
if (!$socket) return;
stream_set_blocking($socket, 0);
stream_set_blocking(STDIN, 0);
do {
$read = array( $socket, STDIN); $write = NULL; $except = NULL;
if(!is_resource($socket)) return;
$num_changed_streams = @stream_select($read, $write, $except, null);
if(feof($socket)) return ;
if($num_changed_streams === 0) continue;
if (false === $num_changed_streams) {
/* Error handling */
var_dump($read);
echo "Continue\n";
die;
} elseif ($num_changed_streams > 0) {
echo "\r";
$data = fread($socket, 16);
if($data !== "")
{
$currentframe .= $data;
if (strpos($currentframe, chr(27)) !== FALSE)
{
$time = (int)((microtime(true) - $start) * 1000.0);
$animation[$time] = explode(chr(27), $currentframe, 2)[0];
$currentframe = explode(chr(27), $currentframe, 2)[1];
file_put_contents("blinkenlights.json", json_encode($animation, JSON_PRETTY_PRINT));
}
}
}
} while(true);