| 1 | |||
| 2 | # Copyright (c) 1999-2017 Rob Fugina <robf@fugina.com> | ||
| 3 | # Distributed under the terms of the GNU Public License, Version 3.0 | ||
| 4 | |||
| 5 | package X10::FireCracker; | ||
| 6 | |||
| 7 | # Perl module for communicating with an X10 FireCracker controller | ||
| 8 | |||
| 9 | use FileHandle; | ||
| 10 | use Device::SerialPort; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | use X10::Controller; | ||
| 15 | |||
| 16 | use vars qw( | ||
| 17 | @ISA | ||
| 18 | $cmd_prefix | ||
| 19 | %house_codes | ||
| 20 | %house_commands | ||
| 21 | %unit_codes | ||
| 22 | %unit_commands | ||
| 23 | $cmd_suffix | ||
| 24 | ); | ||
| 25 | |||
| 26 | @ISA = qw( X10::Controller ); | ||
| 27 | |||
| 28 | $cmd_prefix = 0xd5aa; | ||
| 29 | |||
| 30 | # commands will be assembled as either: | ||
| 31 | # house_code | house_command | ||
| 32 | # OR | ||
| 33 | # house_code | unit_code | unit_command | ||
| 34 | |||
| 35 | %house_codes = ( | ||
| 36 | 'A' => 0x6000, 'B' => 0x7000, 'C' => 0x4000, 'D' => 0x5000, | ||
| 37 | 'E' => 0x8000, 'F' => 0x9000, 'G' => 0xa000, 'H' => 0xb000, | ||
| 38 | 'I' => 0xe000, 'J' => 0xf000, 'K' => 0xc000, 'L' => 0xd000, | ||
| 39 | 'M' => 0x0000, 'N' => 0x1000, 'O' => 0x2000, 'P' => 0x3000, | ||
| 40 | ); | ||
| 41 | %house_commands = ( | ||
| 42 | 'ALL OFF' => 0x0080, | ||
| 43 | 'ALL ON' => 0x0091, | ||
| 44 | 'LIGHTS OFF' => 0x0084, | ||
| 45 | 'LIGHTS ON' => 0x0094, | ||
| 46 | 'DIM' => 0x0098, | ||
| 47 | 'BRIGHT' => 0x0088, | ||
| 48 | ); | ||
| 49 | %unit_codes = ( | ||
| 50 | 1 => 0x0000, 2 => 0x0010, 3 => 0x0008, 4 => 0x0018, | ||
| 51 | 5 => 0x0040, 6 => 0x0050, 7 => 0x0048, 8 => 0x0058, | ||
| 52 | 9 => 0x0400, 10 => 0x0410, 11 => 0x0408, 12 => 0x0418, | ||
| 53 | 13 => 0x0440, 14 => 0x0450, 15 => 0x0448, 16 => 0x0458, | ||
| 54 | ); | ||
| 55 | %unit_commands = ( | ||
| 56 | 'OFF' => 0x0020, | ||
| 57 | 'ON' => 0x0000, | ||
| 58 | ); | ||
| 59 | $cmd_suffix = 0xad; | ||
| 60 | |||
| 61 | |||
| 62 | ### constructors | ||
| 63 | |||
| 64 | sub new | ||
| 65 | { | ||
| 66 | my $type = shift; | ||
| 67 | |||
| 68 | my $self = new X10::Controller( @_ ); | ||
| 69 | bless $self, $type; | ||
| 70 | |||
| 71 | $self->{serial_port} = new Device::SerialPort($self->{port}) | ||
| 72 | || die "Couldn't connect to FireCracker port ", $self->{port}, | ||
| 73 | ": ", $!, "\n"; | ||
| 74 | |||
| 75 | die "Need IOCTL constants for DTR/RTS manipulation -- see docs for Device::SerialPort" | ||
| 76 | unless $self->{serial_port}->can_ioctl; | ||
| 77 | |||
| 78 | $self->{serial_port}->dtr_active(0); | ||
| 79 | $self->{serial_port}->rts_active(0); | ||
| 80 | |||
| 81 | &usleep(100000); | ||
| 82 | |||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | ### public methods (most overriding parent class) | ||
| 88 | |||
| 89 | sub send | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | foreach (@_) | ||
| 93 | { | ||
| 94 | $self->send_one($_); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub send_one | ||
| 99 | { | ||
| 100 | my $self = shift; | ||
| 101 | my $event = shift; | ||
| 102 | |||
| 103 | my $hc = $event->house_code; | ||
| 104 | my $uc = $event->unit_code; | ||
| 105 | my $fn = $event->func; | ||
| 106 | |||
| 107 | # my $house = '[a-p]'; | ||
| 108 | # my $unit = '0?\d|1[01-6]'; | ||
| 109 | |||
| 110 | if ($fn eq 'ON' || $fn eq 'OFF') | ||
| 111 | { | ||
| 112 | $self->send_fcword( | ||
| 113 | $house_codes{$hc} | ||
| 114 | | $unit_codes{$uc} | ||
| 115 | | $unit_commands{$fn} | ||
| 116 | ); | ||
| 117 | |||
| 118 | $self->got_event($event); | ||
| 119 | } | ||
| 120 | elsif ($fn eq 'DIM' || $fn eq 'BRIGHT') | ||
| 121 | { | ||
| 122 | $self->send_fcword( | ||
| 123 | $house_codes{$hc} | ||
| 124 | | $unit_codes{$uc} | ||
| 125 | | $unit_commands{'ON'} | ||
| 126 | ); | ||
| 127 | $self->send_fcword( | ||
| 128 | $house_codes{$hc} | ||
| 129 | | $house_commands{$fn} | ||
| 130 | ); | ||
| 131 | |||
| 132 | $self->got_event($event); | ||
| 133 | } | ||
| 134 | elsif ( ($uc eq 'ALL' && $fn eq 'OFF') | ||
| 135 | || ( $uc eq 'LIGHTS' && $fn eq 'ON' ) ) | ||
| 136 | { | ||
| 137 | $self->send_fcword( | ||
| 138 | $house_codes{$hc} | ||
| 139 | | $house_commands{"$uc $fn"} | ||
| 140 | ); | ||
| 141 | |||
| 142 | $self->got_event($event); | ||
| 143 | } | ||
| 144 | else | ||
| 145 | { | ||
| 146 | warn "Unrecognized event: ", $event->as_string, "\n"; | ||
| 147 | return 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | ### private methods -- don't call these from ouside! | ||
| 153 | |||
| 154 | sub send_fcword | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | my $fcword = shift; | ||
| 158 | |||
| 159 | $self->{serial_port}->dtr_active(1); | ||
| 160 | $self->{serial_port}->rts_active(1); | ||
| 161 | &usleep(200000); | ||
| 162 | |||
| 163 | $self->send_bits($cmd_prefix, 16); | ||
| 164 | $self->send_bits($fcword, 16); | ||
| 165 | $self->send_bits($cmd_suffix, 8); | ||
| 166 | |||
| 167 | # again, should end up this way, but... | ||
| 168 | $self->{serial_port}->dtr_active(1); | ||
| 169 | $self->{serial_port}->rts_active(1); | ||
| 170 | &usleep(200000); | ||
| 171 | |||
| 172 | $self->{serial_port}->dtr_active(0); | ||
| 173 | $self->{serial_port}->rts_active(0); | ||
| 174 | &usleep(100000); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub send_bits | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | |||
| 181 | my $word = shift; | ||
| 182 | |||
| 183 | my $length = shift || 16; | ||
| 184 | |||
| 185 | for (my $i = 1 << ($length-1); $i; $i = $i >> 1) | ||
| 186 | { | ||
| 187 | $self->send_bit($i & $word); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub send_bit | ||
| 192 | { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | return 0 unless exists $self->{serial_port}; | ||
| 196 | |||
| 197 | my $boolean = shift; | ||
| 198 | |||
| 199 | if ($boolean) | ||
| 200 | { | ||
| 201 | # send a 'one' bit | ||
| 202 | $self->{serial_port}->pulse_dtr_off(2); # milliseconds | ||
| 203 | &usleep(1400); | ||
| 204 | } | ||
| 205 | else | ||
| 206 | { | ||
| 207 | # send a 'zero' bit | ||
| 208 | $self->{serial_port}->pulse_rts_off(2); # milliseconds | ||
| 209 | &usleep(1400); | ||
| 210 | } | ||
| 211 | |||
| 212 | # 'clock' | ||
| 213 | &usleep(2000); | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | ### utility functions -- not called as methods | ||
| 218 | |||
| 219 | sub usleep | ||
| 220 | { | ||
| 221 | my $usecs = shift; | ||
| 222 | select(undef, undef, undef, $usecs / 1000000); | ||
| 223 | } | ||
| 224 | |||
| 225 | 1; | ||
| 226 |