-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlights
More file actions
executable file
·109 lines (100 loc) · 2.16 KB
/
lights
File metadata and controls
executable file
·109 lines (100 loc) · 2.16 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env perl
## Senseless lights simulation
## Copyright (c) 2008 by Birte Kristina Friesel <derf@derf.homelinux.org>
## I, the copyright holder of this work, hereby release it into the public
## domain. This applies worldwide. In case this is not legally possible, I
## grant any entity the right to use this work for any purpose, without any
## conditions, unless such conditions are required by law.
use feature 'switch';
use strict;
use utf8;
use warnings;
use Curses;
use Time::HiRes 'usleep';
initscr;
noecho;
cbreak;
keypad($stdscr, 1);
curs_set('invisible');
start_color;
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
init_pair(3, COLOR_GREEN, COLOR_BLACK);
my $int = 100;
my $cur = 0;
my $max = 60000;
my $tlight;
my $nextstate;
my $i;
sub light_state($$$) {
my ($i, $state, $char) = @_;
my $ret = ' ';
given($i) {
when(1) {
if ($state < 2) {$ret = $char}
}
when(2) {
if ($state % 2) {$ret = $char}
}
when(3) {
if ($state == 2) {$ret = $char}
}
}
return($ret);
}
sub draw_light($) {
my $light = shift;
my $char = $light->{char} || '●';
my ($y, $x) = @{$light->{position}};
my $i;
addstr($y, $x, '╭─╮');
for ($i = $y+1; $i-$y <= 3; $i++) {
addstr($i, $x, '│');
attron(COLOR_PAIR($i-$y));
addstr($i, $x+1, light_state($i-$y, $light->{state}, $char));
attroff(COLOR_PAIR($i-$y));
addstr($i, $x+2, '│');
}
addstr($y+4, $x, '╰─╯');
refresh;
}
my $lights = [
{
position => [5, 30],
state => 0,
times => [30000, 9500, 10000, 28000],
},
{
position => [5, 27],
state => 0,
times => [59900, 39500, 40000, 58000],
char => '←',
},
{
position => [5, 33],
state => 0,
times => [30000, 4500, 5000, 28000],
char => '→',
},
];
foreach(@$lights) {
draw_light($_);
}
refresh;
while(usleep($int*1000)) {
$cur += $int;
if ($cur > $max) {
$cur = 0;
}
for($i = 0; exists($lights->[$i]); $i++) {
$tlight = $lights->[$i];
$nextstate = $tlight->{state} + 1;
$nextstate = 0 if ($nextstate > 3);
if ($tlight->{times}->[$nextstate] == $cur) {
$tlight->{state} = $nextstate;
draw_light($tlight);
}
$lights->[$i] = $tlight;
}
}
endwin;