#!/usr/bin/perl # control system for RGB Xmas lights # see: http://staticfree.info/projects/rgb-xmas/ # Copyright (C) 2002 Steve Pomeroy # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # $Id: setrgb 13 2002-12-09 17:24:14Z steve $ # $Log$ # Revision 1.4 2002/12/09 17:24:14 steve # cleaner temp file deletion and URL update # # Revision 1.3 2002/09/03 20:17:48 steve # minor bugfix with color listing # # Revision 1.2 2002/09/03 20:02:04 steve # added GPL header # # Revision 1.1 2002/09/03 19:58:36 steve # Initial revision # use Getopt::Std; use Graphics::ColorNames qw( hex2tuple tuple2hex ); ###################################################### ######## settings ########## ###################################################### ### # ## these need to be set to the names of the X10 units used ## for each light strand. Order red, green, blue my @rgbX10 = ("C1","C2","C3"); # set the command to run here. use the placeholders, UNIT and VALUE # for the unit number and dim level respectively my $cmd = "heyu turn UNIT"; my $dim = "dim VALUE"; my $bright = "bright VALUE"; my $on = "on"; my $off = "off"; # set the max value that a unit can be dimmed my $maxlvl = 23; # this is to compensate for green being brighter than the rest # adjust according to lights my @maxbright = ( $maxlvl, $maxlvl - 6, $maxlvl ); # temp file for my $tmp = "/var/tmp/rgbold"; # end configurable ###################################################### # mode: (a)bsolute or (r)elative brightness my $mode = "a"; tie %ColorTable, 'Graphics::ColorNames', 'X'; getopts("apdl"); if( $opt_d ){ unlink $tmp; exit; } #remove the tmp file usage() unless defined $ARGV[0] || ($opt_p || $opt_l); my @oldrgb; my $oldcolor; # grab the previous color from the temp file if it's there if(open OLD, "$tmp"){ $oldcolor = ; close OLD; chomp $oldcolor; @oldrgb = hex2tuple($oldcolor); $mode = "r"; } # print the color and quit if( $opt_p ){ print "#$oldcolor" if $oldcolor ne ""; exit; } # list all the known colors if( $opt_l ){ foreach ( sort keys %ColorTable ){ print "$_\n" } exit; } my $color = join / /, @ARGV; # if $color isn't a hex value, look it up if( $color !~ /^\#?[\dabcdef]{6,6}$/i ){ if( exists $ColorTable{ lc($color) } ){ $color = $ColorTable{ lc($color) }; }else{ print STDERR "Error: Cannot find color \"$color\"\n"; exit; } } $mode = "a" if $opt_a; my @rgb = hex2tuple($color); open OLD, ">$tmp" or die "aie, captn'! $tmp hates me! $!"; print OLD $color; close OLD; for (my $i = 0; $i < 3; $i++){ my $command = $cmd; $command =~ s/UNIT/$rgbX10[$i]/g; if($mode eq 'a'){ # absolute setting when the dim levels are unknown my $val = int($maxlvl - ($rgb[$i]/255) * $maxbright[$i]); my $dimcmd = $dim; $dimcmd =~ s/VALUE/$val/g; system("$command $off"); system("$command $dimcmd") if $val != 23; }else{ # relative setting... my $val = int((abs($rgb[$i] - $oldrgb[$i])/255) * $maxbright[$i]); my $dimcmd; if(( $rgb[$i] - $oldrgb[$i]) > 0){ $dimcmd = $bright; }else{ $dimcmd = $dim; } $dimcmd =~ s/VALUE/$val/g; system("$command $dimcmd") if $val != 0; } } sub usage{ die< 9/3/2002 EOF }