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>;
}

Re: Yet another good-night script

nice thanks!

Re: Yet another good-night script

I watch BOL though and I never know juat how long it'll be, personally it'd just be an annoyance to me. I'll keep it in mind though THANKS!

Re: Yet another good-night script

Can you explain a little more how to get this on my lil chumby? I can SSH fine, but try to write and save this with vi and get write error read only messages. Not that great with linux or anything, but can get my way around. Also I noticed the scripts were located urs/chumby/scripts is that the same??

Been searching and reading the wikki all night and cant figure out for the life of me how to get this on there and this would be something that i would LOVE to use!!!

Thanks!

Re: Yet another good-night script

Beastlykings: do you mean Buzz Out Loud by BOL? Yeah it's a podcast of indeterminent length, but you know, it's not longer than 45 minutes these days smile

Re: Yet another good-night script

snowcrash: /usr on chumby is a read only file system. you should copy this to /psp/scripts/goodnight_chumby (actually whatever you like, under /psp). And then vi /psp/crontabs/root to install this script to cron, something like "*/10 0-4 * * * /psp/scripts/goodnight_chumby", and reboot your chumby.

Re: Yet another good-night script

miyagawa wrote:

snowcrash: /usr on chumby is a read only file system. you should copy this to /psp/scripts/goodnight_chumby (actually whatever you like, under /psp). And then vi /psp/crontabs/root to install this script to cron, something like "*/10 0-4 * * * /psp/scripts/goodnight_chumby", and reboot your chumby.

Sweet! Think I got it to work!!!
I will know if music is playing tomorrow when I wake up hahaha!

Awesome script though. I'm kind of surprised it didn't come with a sleep function anyway.

Re: Yet another good-night script

Damn! It didnt work?? I dont know what I did wrong. I have the script saved, added the stuff to crontabs but for some reason its not sleeping when I play my music??

From reading the script shouldnt I get some txt saying its in sleep mode as well? That never popped up either. Not sure what went wrong everything looks correct.

Re: Yet another good-night script

snowcrash wrote:

Damn! It didnt work?? I dont know what I did wrong. I have the script saved, added the stuff to crontabs but for some reason its not sleeping when I play my music??

From reading the script shouldnt I get some txt saying its in sleep mode as well? That never popped up either. Not sure what went wrong everything looks correct.

I'm having the same problem. Everything is exactly the same and still no change in volume. Does anyone have any thoughts?

Re: Yet another good-night script

ok guys, before installing it to crontab, run the script manually via the shell. Play some music and make your chumby a night mode, and then run like "> /psp/scripts/goodnight_chumby 1". If it's working correctly, the script will turn down the volume in 1 minute and finally stops the music. Check out if /psp/sleeping file is there (that file should contain the timestamp).

If that works correctly, install the script by editing /psp/crontabs/root and remove /psp/sleeping (because that's the file to make it not run multiple times per day) and reboot your chumby, to reflect the crontab changes.

Re: Yet another good-night script

Miyagawa, thanks for the help. I will try this later this weekend. From my little knowledge of scripts and stuff it looks like a great one though. And VERY useful for someone who loves to crash while listening to tunes! Appreciate the time ya took to write it and help me out.

Re: Yet another good-night script

miyagawa, yeah, its a great podcast imho.
Hmm, well in that case I think I might give it a try, now to adjust how long it takes.. do I just change all the '60's in the script to '45's? or what?

Re: Yet another good-night script

Beastlykings: yes you can change the 60 to 45, or give it a command line argument on crontab, like /psp/scripts/goodnight_chumby 45.

Re: Yet another good-night script

miyagawa wrote:

ok guys, before installing it to crontab, run the script manually via the shell. Play some music and make your chumby a night mode, and then run like "> /psp/scripts/goodnight_chumby 1". If it's working correctly, the script will turn down the volume in 1 minute and finally stops the music. Check out if /psp/sleeping file is there (that file should contain the timestamp).

If that works correctly, install the script by editing /psp/crontabs/root and remove /psp/sleeping (because that's the file to make it not run multiple times per day) and reboot your chumby, to reflect the crontab changes.

Still cant get it to work sad

Re: Yet another good-night script

This looks fantastic, and something I would love to use on mine, but... I have no idea whatsoever on how to use these scripts. I've searched the Wiki and the forums and can't seem to find anything on the subject of actually installing these. Can somebody point me in the right direction?

Also, do you think it would be possible to have a script that does kinda the opposite, i.e. when the alarm goes off in the morning, fades the music in over a predefined period? Would be nicer than waking up to a sudden loud noise...

thanks!

Re: Yet another good-night script

velkrosmaak, I can walk you through it if you would like? Its real easy once you get going.

Re: Yet another good-night script

Beastlykings wrote:

velkrosmaak, I can walk you through it if you would like? Its real easy once you get going.

Could you help me with that as well?
Thx

Re: Yet another good-night script

Beastlykings wrote:

velkrosmaak, I can walk you through it if you would like? Its real easy once you get going.

Yeah that would be much appreciated. Perhaps a guide or something will get stickied on the forum here, as I imagine I'm not the only newb who was confused by this...? At least I hope not. Hehe.

Re: Yet another good-night script

You don't need a guide or anything like that... There's nothing necessary besides what I've been writing here.

1. Enable SSH on your chumby
2. SSH to your chumby and copy the script to /psp/scripts/goodnight_chumby (you should create a directory /psp/scripts first). Copying via USB thumb drive would be handy if you're not getting used to vi
3. Play some music and turn your chumby night mode
4. on your SSH shell, run /psp/scripts/goodnight_chumby with an argument 1 i.e.) /psp/scripts/goodnight_chumby 1
5. Wait for 1 minute
6. See if the volume is gradually decreased and finally stops the music.
7. rm /psp/sleeping
8. vi /psp/crontabs/root and add the following entry
*/10 0-4 * * * /psp/scripts/goodnight_chumby
(This means the script should run every 10 minutes between 0-4 am. If you sleep earlier, change the 2nd column appropriately)
9. reboot your chumby

And you're all set. If you're unfamiliar with one of these, go look at chumby Wiki or see Linux how-tos.

20 (edited by Beastlykings 2008-03-18 18:49:41)

Re: Yet another good-night script

Hmm.. It won't let me post such a long post.. That really sucks.

21 (edited by snowcrash 2008-03-17 23:10:07)

Re: Yet another good-night script

I tried this again and again but everytime i try to run it i get:

-sh: /psp/scripts/goodnight_chumby: line 16: my: not found
-sh: /psp/scripts/goodnight_chumby: line 17: my: not found
-sh: /psp/scripts/goodnight_chumby: line 17: 60: not found
-sh: /psp/scripts/goodnight_chumby: line 19: my: not found
-sh: /psp/scripts/goodnight_chumby: line 20: syntax error: "(" unexpected


Here is what I did:
SSH into chumby
Created the directory /psp/scripts
vi goodnight_chumby
paste in code
:wq
. /psp/scripts/goodnight_chumby 1
and it returns those errors?

Am I doing something wrong by just doing a copy/paste of the code in vi? I tried this on both my MAC with terminal and also on an XP box with PuTTY I think something is getting messed up with the paste or something??

Thanks for all the help everyone btw.

Re: Yet another good-night script

snowcrash: you don't need a dot (".") before the script when you execute the program. "." on bash means "execute this script as a shell script" but it's actually a perl script. So just type "/psp/scripts/goodnight_chumby 1", without the double quotes.

Re: Yet another good-night script

miyagawa wrote:

snowcrash: you don't need a dot (".") before the script when you execute the program. "." on bash means "execute this script as a shell script" but it's actually a perl script. So just type "/psp/scripts/goodnight_chumby 1", without the double quotes.

Thx but now i get:
-sh: /psp/scripts/goodnight_chumby: Permission denied

I know there is something I'm doing wrong but I've done the walk through a couple times and I'm sure its something small but is there something I'm over looking?

Re: Yet another good-night script

You need to mark the script as executable (not needed on a dongle).

chmod a+x /psp/scripts/goodnight_chumby

Re: Yet another good-night script

I think we need a better walkthrough than I got because even trying all these steps I can't make it work. I'm a little frustrated now.. Any more help would be appreciated.