Re: Yet another good-night script

Duane wrote:

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

chmod a+x /psp/scripts/goodnight_chumby

AWESOME!!! That was my problem!!!
Works now!

Re: Yet another good-night script

Beastlykings wrote:

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.

Hope this helps.
Here is what I did not using a thumb drive VERY easy once you figure it out:

1. SSH into chumby (see wiki or how to posts)
2. Created the directory /psp/scripts
3. vi goodnight_chumby in /psp/scripts
4. paste in code (from above post)
5. esc (hit escape)
6. type :wq (to save)
7. chmod a+x /psp/scripts/goodnight_chumby
8. type /psp/scripts/goodnight_chumby 1 (to see if it works)
9. rm sleeping
10. vi /psp/crontabs/root and add */10 0-4 * * * /psp/scripts/goodnight_chumby

With VERY little knowledge of what I was doing and with some simple "vi command" google searches and how to posts on here it wasnt that hard once I got all the steps correct.

Again thanks for the GREAT script and to all those who helped out!!!

Re: Yet another good-night script

Beastlykings: define "can't make it work" and i could provide more help.

There're two lines in the script having "warn" and commented out. You can uncomment them by removing the "#" before the warn, to make the script a bit more verbose. Maybe that'll help.

Re: Yet another good-night script

snowcrash: YAY! Glad that worked. And sorry I took it out the important chmod +x part smile

Re: Yet another good-night script

snowcrash: thanks a lot for that guide, I am however experiencing a similar problem to a previous poster, where it simply comes up with the following:

/psp/scripts/goodnight_chumby: line 1: T: not found
/psp/scripts/goodnight_chumby: line 15: my: not found
/psp/scripts/goodnight_chumby: line 16: my: not found
/psp/scripts/goodnight_chumby: line 16: 60: not found
/psp/scripts/goodnight_chumby: line 18: my: not found
/psp/scripts/goodnight_chumby: line 19: syntax error: "(" unexpected

I followed your instructions to the letter, though am not getting any sort of confirmation when i CHMOD, which is making me think that something is going wrong there... any thoughts?

Thanks in advance

Re: Yet another good-night script

velkrosmaak: you are running the script as a shell script, not perl. It's either you are running the script in a wrong way, or didn't copy the first line of the script correctly. The line # 15 my suggests me that you stripped the first line of the script (#!/usr/bin/perl) which is very important.

Re: Yet another good-night script

My sneaking suspicion is that he didn't copy/paste/type all of the comments, including the pound-bang line which is actually functional.

Re: Yet another good-night script

So with the latest firmware update (565) this script stopped working. My script checks to see if the number of btplayd process is 2, to see if chumby is playing any music (and doesn't do anything if it's < 2). Apparently with the latest FW it's always 1, regardless of the status.

Duane: is there a scriptable way to see if chumby is playing any music?

Re: Yet another good-night script

miyagawa wrote:

Duane: is there a scriptable way to see if chumby is playing any music?

/var/run/btplay.state should contain '2' if a track is currently playing.

Re: Yet another good-night script

Thanks Duane! The updated script is available at http://svn.coderepos.org/share/platform … ght_chumby

You can use wget to grab the script. No need to copy/paste via vim smile

Re: Yet another good-night script

I think I must be being severely retarded, as I still can't get this to work, even when wgetting the script from that coderpos.org site (thanks a lot btw miyagawa for hosting that!)

when I run it with the argument of 1,

i.e. /psp/scripts/goodnight_chumby 1

nothing happens. i've got a shoutcast station playing and its in night mode, what's am I doing wrong?

37 (edited by pharaohamps 2008-08-07 17:57:15)

Re: Yet another good-night script

Working for me now.  I'm dumb - I wasn't getting any output from the console,

I use the autodim.sh script to turn off my screen backlight after 1 minute in night mode.  When the screen is off, my /proc/sys/sense1/dimlevel is "2."

I changed:

my $is_night_mode = cat("/proc/sys/sense1/dimlevel") == 1;

to

my $is_night_mode = cat("/proc/sys/sense1/dimlevel") >= 1;

Now my script seems to be working well.  In testing with the cron job scheduled, I see that we're only running it once every 5 minutes.  If I go into sleep mode at 22:01, then the next time the job is run is at 22:05 - this will play music for the ARGV plus around 4 minutes.  Since I want 15 minutes on my sleep timer, I run my script as /psp/scripts/goodnight_chumby 12.  This gives me between 12 and 17 minutes of sleep timer plus the fade-out time.

The last time I ran the script, I went back to the control panel after the script had terminated, and when I hit the mute button, the music started playing again!  I need to watch to see if btplay is killed or not.

Re: Yet another good-night script

I am running the script on this page, and used wget and made no modifications.

It did run last night, and did turn off my music.  However, I had a quick alarm set this morning, and the volume was still muted.  I'm not entirely sure it is the script since I'm running the beta control panel.

I got sound again off the chumby once I clicked the mute on and off.

Any ideas?  I love the idea of the script.

Re: Yet another good-night script

Duane wrote:

My sneaking suspicion is that he didn't copy/paste/type all of the comments, including the pound-bang line which is actually functional.

I thought that pound was a REM statement more or less. Obvoiusly its not. So "# []" and "#! []" are different? Can you pelase explain? I am still very, very green to Linux, I have been tinkering with a stripped version of Red Hat 7.x on a Roku HD1000 box and have cut my teeth on that.

Thanks for your time, Matt

Re: Yet another good-night script

"#!" is called "shebang" - when you try to execute a text file, the operating needs more information about *how* to execute it.

The first line, if it begins with "#!", gives the name of the program that should be used to interpret the rest of the file.

Any other line that begins with "#" is interpreted as a comment (like "REM" in BASIC), if that's the syntax for comments for that language.

For more info, see this.

Re: Yet another good-night script

Questions on the crontabl part.

Here's what I have on the crontab:
*/10 22-23 * * * /psp/scripts/goodnight_chumby

What I want is for volume to start decreasing at 10:00 pm local time and continue to decrease over one hour time.  Is this the right values for that?

Thanks!

42 (edited by pharaohamps 2008-10-14 06:07:57)

Re: Yet another good-night script

kathleen wrote:

Questions on the crontabl part.

Here's what I have on the crontab:
*/10 22-23 * * * /psp/scripts/goodnight_chumby

What I want is for volume to start decreasing at 10:00 pm local time and continue to decrease over one hour time.  Is this the right values for that?

Thanks!

The crontab setting just means that the script runs as specified by the cron table.  In your case, every ten minutes from 10 pm to 11 pm.  If you were to start music playing after 11 pm, the script would not run and your volume would not decrease.  Incidentally, since your script runs only every ten minutes, if you were to start playing music at 10:51, your music would never stop since the last instance of the script would have stopped by then.

The "fade" time is dependent on your initial music volume.  The script decreases the /proc/chumby/audio/mixer/left-speaker/volume and /proc/chumby/audio/mixer/right-speaker/volume by 5 every five seconds.

Look at the script:

sleep $sleep_time * 60;
    my $vol = cat("/proc/chumby/audio/mixer/left-speaker/volume");

sleep (pause) for sleep_time minutes    // sleep is entered in seconds, so x seconds * 60 = x minutes.
set the "vol" variable to the current value of the chumby's speaker volume

    while ($vol > 0) {
        $vol -= 5;
        #warn $vol;
        $vol = 0 if $vol < 0;

as long as the volume is NOT muted (greater than zero,)
reduce the volume by 5.
if reducing the volume by 5 causes the volume to be negative (less than zero,) set the volume to zero.

        for my $lr (qw( left right )) {
            open my $fh, ">", "/proc/chumby/audio/mixer/$lr-speaker/volume";
            print $fh "$vol\n";
            close $fh;
        }
        sleep 5;

this part actually writes the value of the "vol" variable to /proc/chumby/audio/mixer/left-speaker/volume and /proc/chumby/audio/mixer/right-speaker/volume
then wait 5 seconds and return to the top of this block of code (while vol > 0)
Keep executing the code until vol is 0.


So that's basically how the "fade" part works.  If you want a longer fade, you need to either increase the "sleep 5," or decrease the amount taken from the volume each time ($vol -= 5) or both.  Just bear in mind that the script can't take away what's not there - if you go to bed with the volume at 40, then the stock script will take 40 / 5 * 5 = 40 seconds to fade.  If you want an hour, you should start by deciding what approximate volume you'll be using, then adjust accordingly.

# initial volume 60
# one hour fade time
sleep $sleep_time * 60;
    my $vol = cat("/proc/chumby/audio/mixer/left-speaker/volume");
    while ($vol > 0) {
        $vol -= 1;                          
        #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 60;

This takes one hour to fade from volume 60 - it decreases the volume by 1 every 60 seconds.  If your volume is higher, change the ($vol -= 1 ) to a larger number to make your sleep time correct.  To find your sleep volume in numerical terms, SSH into the Chumby, then type:

cat /proc/chumby/audio/mixer/left-speaker/volume

This will return your volume - divide by 60 to find out what your decrement ($vol -= x) amount should be.

Hope this helps.

43 (edited by kathleen 2008-08-19 04:00:49)

Re: Yet another good-night script

Yes, it helps tremendously.

So the crontabl and the script itself are complete independent?  Right now, I have a sleep alarm set for 10:00 pm so I modified the crontabl to fire off every 5 minutes between 22-23, just to make sure I catch Chumby going to sleep or if I decide to fiddle with Chumby before I go to bed.

I also made the modification to have the speaker decrease by an minute since I start with a volume of 127 (I think I'll double check tonight). 

By the way, I loaded the new control panel last night, manually ran the script, and everything worked like a charm.  Chumby got quiet and quit playing music and my morning alarm fired off.

Exactly what I've been wanting in my Chumby!

Re: Yet another good-night script

"crontab" is just that - a table that contains cron jobs.  The settings in the table tell when to run the jobs, and the jobs can be anything executable, a program or script, etc.  We put the goodnight script in the table because we want it to run at certain times independent of any alarms - alarms are the other way of running a script at a certain time.  If we use a post_alarm_action, the script would run once, and if there was no music playing, nothing would happen.

45 (edited by Krazz 2008-10-25 14:59:55)

Re: Yet another good-night script

I updated the script a bit so that it's possible to run it over and over again not having to wait 24 hours. I also tweaked the fading of the volume to my liking and also restored the volume to the previous setting. I see my version as an alternative to miyagawa's version.

If you use this code, don't forget to change the crontab accordingly.

I haven't tested the code that much so there might be bugs. wink

** UPDATE ***
I've been using this script every night since I made this post and it has been running without any problems. I consider it bug free.

#!/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
# (configurable with 1st argument) and decreases the speaker
# volume gradually, then stops the audio.

# HOW TO INSTALL
# Add the following to /psp/crontabs/root
# */5 0-4,* * * * /psp/scripts/goodnight_chumby

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 mode!";
    exit;
}

my $is_night_mode = cat("/proc/sys/sense1/dimlevel") == 1;
my @proc = split /\n/, `ps`;
my $music_playing = cat("/var/run/btplay.state") == 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");
    my $vol_def_r = cat("/proc/chumby/audio/mixer/right-speaker/volume");
    my $vol_def_l = $vol;
    while ($vol > 0) {
        $vol -= 10;
        #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 1;
    }
    
    # I run a loop on the killall command since the player has a tendency to not die. I don't know why that is though!?
    while (cat("/var/run/btplay.state") == 2) {
        system "killall btplayd";
        sleep 10;
    }
    
    # Restoring the original volume settings. Since we can run the script more than once a day now, I thought it would be appropriate to set the volume back.
    for my $lr (qw( left right )) {
        open my $fh, ">", "/proc/chumby/audio/mixer/$lr-speaker/volume";
        if ($lr == "right") {
            print $fh "$vol_def_r\n";
        }
        else {
            print $fh "$vol_def_l\n";
        }
        close $fh;
    }
    
    # Deleting the sleep file. If the sleep file is gone there is no sleep timer in progress; hence no music is playing. ;)
    unlink($sleep_file);
}

sub cat {
    my $f = shift;
    open my $fh, $f or return;
    return join "", <$fh>;
}
My puppies: Night Clock

46 (edited by servalan 2008-11-18 16:09:39)

Re: Yet another good-night script

Krazz wrote:

I updated the script a bit so that it's possible to run it over and over again not having to wait 24 hours. I also tweaked the fading of the volume to my liking and also restored the volume to the previous setting. I see my version as an alternative to miyagawa's version.

It's been a very long time since I touched perl... but following on from this post, I have started to modify your script to use events sent to the Control Panel, which seems a cleaner interface... unless you beat me to it. wink

code fragment:

#!/usr/bin/perl
open my $fh, ">", "/tmp/flashplayer.event";
printf $fh "<event type=\"MusicPlayer\"\ value=\"stop\"\ comment=\"\"/>\n";
close $fh;
system "chumbyflashplayer.x -F1";

chumby:/psp/scripts# ./fred
Adobe FlashLite 3.0.4 Release (Version 8,1,55,0)- Chumby Industries (version 7 build 85-733 [GCC 4.1.2   Microcross GNU X-Tools(tm)])
Built for: ironforge; Features: Video ALSA DMA ARM-optimizations
Build time: Thu 17Jul2008 1235
chumbyflashplayer.x: sending signal 1 (SIGHUP) to running instance 2867
chumbyflashplayer.x: signal sent, process 2867 should still be running

Re: Yet another good-night script

You should probably also consider using the Control Panel events to set the volume rather than bang around /proc.

48 (edited by servalan 2008-11-18 16:24:04)

Re: Yet another good-night script

Duane wrote:

You should probably also consider using the Control Panel events to set the volume rather than bang around /proc.

That's next. I haven't touched perl since 1992... I need to get my creaky brain working & liberate an old O'Reilly zoo book. wink

Re: Yet another good-night script

First stab... comments welcome & many thanks to the orig. authors.

Changes:
* placed the tmp file in a /psp/run directory & made the fn a little more unique to the script name: /psp/run/gnc_sleeping - stop cluttering the /psp directory & increase chances of tmp file surviving ministrations of other script writers - & added a note about deleting this file from debugchumby - just in case of a power out event while the script is half way through
* uses 'chumby_set_volume' with a null argument to find out what volume we're at now, saves it & restores this volume once the stream has been exterminated
* uses the /tmp/flashplayer.event interface to communicate with the control panel

#!/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
# (configurable with 1st argument) and decreases the speaker volume gradually, then stops the audio.

# HOW TO INSTALL
# mkdir /psp/run
# Add the following to /psp/crontabs/root
# */5 0-4,* * * * /psp/scripts/goodnight_chumby
# add 'rm /psp/run/gnc_sleeping' to debugchumby script on startup

my $sleep_file = "/psp/run/gnc_sleeping";
my $sleep_time = shift @ARGV || 5; # minutes

my $now   = time;
my $slept = cat($sleep_file);

if ($slept && $now - $slept < 12 * 60 * 60) {
    warn "chumby is already in sleep mode!";
    exit;
}

my $is_night_mode = cat("/proc/sys/sense1/dimlevel") >= 1;
my @proc = split /\n/, `ps`;
my $music_playing = cat("/var/run/btplay.state") == 2;

# Find out what volume we are at, then decrease it gradually till 0
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 = sprintf("%d", `chumby_set_volume`);
    my $origvol = $vol;
    while ($vol > 0) {
        $vol -= 10;
        $vol = 0 if $vol < 10;
        open my $fh, ">", "/tmp/flashplayer.event";
            printf $fh "<event type=\"MusicPlayer\"\ value=\"setVolume\"\ comment=\"$vol\"/>\n";
        close $fh;
        system "chumbyflashplayer.x -F1";
        sleep 10;
    }

    # Now stop the music completely
    open my $fh, ">", "/tmp/flashplayer.event";
         printf $fh "<event type=\"MusicPlayer\"\ value=\"stop\"\ comment=\"\"/>\n";
    close $fh;
    system "chumbyflashplayer.x -F1";
    sleep 10;

    # Set the volume back to what it was so it's ok when we want to play music again
    system "chumby_set_volume $origvol";

    # Deleting the sleep file. If the sleep file is gone there is no sleep timer in progress; hence no music is playing. ;)
    unlink($sleep_file);
}

sub cat {
    my $f = shift;
    open my $fh, $f or return;
    return join "", <$fh>;
}

Re: Yet another good-night script

Very nice cleanup. I've updated my script and we'll see if it works tonight (I have no doubts though).

My puppies: Night Clock