#!/usr/bin/perl # # master_timer # # Perl control script for X10 Firecracker module # Useful for simple crontab timer applications. Options are overly wordy # to make for easy crontab reading later on. # # Requires the ControlX10::CM17 module and Device::SerialPort module from # CPAN. Not portable to Windows platforms; other portability unknown. # # 18-Mar-2001 by James Mancini - released into public domain use vars qw( $module $secure $action); use Device::SerialPort; use ControlX10::CM17 qw( send_cm17 0.05 ); use Getopt::Long; use strict; my %opt; $opt{fullcmd} = "$0 ".(join " ", map {$_ =~ /[ \[\]\*\{\}\;\>\<\&]/ ? "'$_'" : $_ } @ARGV); options(\%opt); my $port = $opt{port} || "/dev/ttyS0"; my $serial_port = Device::SerialPort->new ($port,1); die "Darn! I Can't open serial port $port: $^E\n" unless ($serial_port); # These are for the pass-through port on the Firecracker $serial_port->databits(8); $serial_port->baudrate(4800); $serial_port->parity("none"); $serial_port->stopbits(1); $serial_port->dtr_active(1); $serial_port->handshake("none"); $serial_port->write_settings || die "Failed to set serial params.\n"; if ($opt{secure}) { my $sleeptime = int(rand (31) * 60); print "\nSleeping $sleeptime seconds..\n" if $opt{verbose}; sleep $sleeptime; } my $action = ""; if ($opt{action} =~ /^off$/) { $action = "K" } elsif ($opt{action} =~ /^dim([+-][\d]+)$/) #dim can be + or - { $action = "${1}" } elsif ($opt{action} =~ /^bright([+-][\d]+)$/) #really the same as dim { $action = "${1}" } elsif ($opt{action} =~ /^dim$/) { $action = "-14" } elsif ($opt{action} =~ /^bright$/) { $action = "+14" } elsif ($opt{action} =~ /^allon$/) # All lights on { $action = "O"; $opt{module}=""; } elsif ($opt{action} =~ /^alloff$/) # All lights off { $action = "N"; $opt{module}=""; } elsif ($opt{action} =~ /^masteroff$/) # All modules off { $action = "P"; $opt{module}=""; } else # Default action = turn module on { $action = "J" } $opt{module} =~ s/^[^0-9a-g]$//i; $opt{hcode} =~ s/^[^a-zA-Z]$|^[\w].+//; $opt{hcode} = "A" unless $opt{hcode}; $opt{hcode} =~ tr/a-z/A-Z/; my $cmd = "$opt{hcode}$opt{module}${action}"; print "\nSending $cmd...\n" if $opt{verbose}; send_cm17($serial_port, $cmd); $serial_port->close || die "\nclose problem with $port\n"; undef $serial_port; sub options () { my $opt = shift; GetOptions( $opt, 'action=s', 'hcode=s', 'module=s', 'secure', 'verbose', 'port=s', ); unless ($opt{module}) #print usage { print <