Listing 7.
follow_leader.pl
Home Automation: The X10 Nitty-Gritty
The Perl Journal, Spring 2000
 
#!/usr/bin/perl -w
# USAGE: perl follow_leader.pl [ PORT ]
# same PORT defaults as cm17_bit_toggle.pl
#  output A2 changes when the CM11 reports a change in A1

require 'start_port.pl';
# initialization for real ports

use ControlX10::CM11;
use strict;

my $serial_object = open_port (@ARGV); 

my $DUMMY = 0;  # set true to run without a CM11

my $reps = 30;
my $block = 0;
my $a2_state = 'AK';    # OFF
my $a2_new   = 'AK';    # OFF

while ($reps-- > 0) {
    # loop continuously for 30 seconds
    print ".";
    if (read_cm11($serial_object, $block)) {
        # poll CM11 for "data waiting"
        my $datain = receive_cm11($serial_object);
        if (defined $datain) {
            print "\nReceived $datain\n";
            $a2_new = 'AJ' if ($datain =~ /A1AJ/);
            $a2_new = 'AK' if ($datain =~ /A1AK/);
        }
    }
    
    $a2_new = 'AJ' if ($DUMMY && $reps == 15);

    # A2 follows detected changes in A1
    if ($a2_state ne $a2_new) {
        if ($a2_new eq 'AK') {
          print "\nSending A2 OFF\n";
          $a2_new = $a2_state = 'AK';
        }
      else {
          print "\nSending A2 ON\n";
          $a2_new = $a2_state = 'AJ';
        }
        send_cm11($serial_object, 'A2') unless $DUMMY;
        send_cm11($serial_object, $a2_state) unless $DUMMY;
    }
}
print "\n";

$serial_object->close || die "\nclose problem with port\n";
undef $serial_object;