Topic: Yet another good-night script
Here's a script that I wrote, to detect a) if chumby is playing any audio and b) the screen is set to low brightness or night mode, and in that case, sleeps for 60 minutes (confugrable) and automatically decreases the volume gradually down to zero, and then stop the music.
It'd be useful if you listen to music or podcast at night with chumby, and want it to auto-stop when you're in sleep. Save this as /psp/scripts/goodnight_chumby and install it via crontab.
#!/usr/bin/perl
# WHAT IS THIS:
# checks if the screen is in night mode or low brightness
# and if chumby is playing any audio. If so, wait for 60 minutes
# (confugrable with 1st argument) and decreases the speaker
# volume gradually, then stop the audio.
# HOW TO INSTALL
# Add the following to /psp/crontabs/root
# 0,30 0-4,22-23 * * * /psp/scripts/goodnight_chumby
# NOTE
# This script works only once per day. remove /psp/sleeping if you
# want to reset this good-night script more than once.
my $sleep_file = "/psp/sleeping";
my $sleep_time = shift @ARGV || 60; # minutes
my $now = time;
my $slept = cat($sleep_file);
if ($slept && $now - $slept < 12 * 60 * 60) {
# warn "chumby is already in sleep tonight!";
exit;
}
my $is_night_mode = cat("/proc/sys/sense1/dimlevel") == 1;
my @proc = split /\n/, `ps`;
my $music_playing = ((grep /btplayd/, @proc) >= 2);
if ($is_night_mode && $music_playing) {
open my $out, ">", $sleep_file or die $!;
print $out $now;
close $out;
#warn "entering sleep mode. Sleep in $sleep_time minutes.";
sleep $sleep_time * 60;
my $vol = cat("/proc/chumby/audio/mixer/left-speaker/volume");
while ($vol > 0) {
$vol -= 5;
#warn $vol;
$vol = 0 if $vol < 0;
for my $lr (qw( left right )) {
open my $fh, ">", "/proc/chumby/audio/mixer/$lr-speaker/volume";
print $fh "$vol\n";
close $fh;
}
sleep 5;
}
system "killall btplayd";
}
sub cat {
my $f = shift;
open my $fh, $f or return;
return join "", <$fh>;
}