line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
######################################################################################### |
2
|
|
|
|
|
|
|
# Package HiPi::Interface::EnergenieSwitch |
3
|
|
|
|
|
|
|
# Description: Control Energenie OOK switches |
4
|
|
|
|
|
|
|
# Copyright : Copyright (c) 2013-2020 Mark Dootson |
5
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
6
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
7
|
|
|
|
|
|
|
######################################################################################### |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package HiPi::Interface::EnergenieSwitch; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
######################################################################################### |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1005
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
14
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
15
|
1
|
|
|
1
|
|
5
|
use parent qw( HiPi::Interface ); |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
4
|
|
16
|
1
|
|
|
1
|
|
58
|
use HiPi qw( :energenie :rpi ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
386
|
|
17
|
1
|
|
|
1
|
|
9
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
979
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->create_accessors( qw( groupid backend repeat ) ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION ='0.82'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Switch Data |
24
|
|
|
|
|
|
|
# $data = $switchmask->[$socketnum - 1]->[$offon]; |
25
|
|
|
|
|
|
|
# where $socketnum == 0 | 1 | 2 | 3 | 4 and $offon == 0|1; |
26
|
|
|
|
|
|
|
# when $socketnum == 0 then $offon is applied to all sockets |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $_switchdata = [ |
29
|
|
|
|
|
|
|
[ 0b1100, 0b1101 ], # off / on all sockets |
30
|
|
|
|
|
|
|
[ 0b1110, 0b1111 ], # off / on socket 1 |
31
|
|
|
|
|
|
|
[ 0b0110, 0b0111 ], # off / on socket 2 |
32
|
|
|
|
|
|
|
[ 0b1010, 0b1011 ], # off / on socket 3 |
33
|
|
|
|
|
|
|
[ 0b0010, 0b0011 ], # off / on socket 4 |
34
|
|
|
|
|
|
|
]; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
0
|
|
|
0
|
0
|
|
my( $class, %userparams ) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my %params = ( |
40
|
|
|
|
|
|
|
backend => 'ENER314_RT', |
41
|
|
|
|
|
|
|
groupid => 0x6C6C6, |
42
|
|
|
|
|
|
|
device => undef, |
43
|
|
|
|
|
|
|
devicename => '/dev/spidev0.1', |
44
|
|
|
|
|
|
|
repeat => ENERGENIE_TXOOK_REPEAT_RATE, |
45
|
|
|
|
|
|
|
reset_gpio => RPI_PIN_22, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
foreach my $key (sort keys(%userparams)) { |
49
|
0
|
|
|
|
|
|
$params{$key} = $userparams{$key}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
unless( defined($params{device}) ) { |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ( $params{backend} eq 'ENER314_RT' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Two way configurable board |
57
|
0
|
|
|
|
|
|
require HiPi::Energenie::ENER314_RT; |
58
|
|
|
|
|
|
|
my $dev = HiPi::Energenie::ENER314_RT->new( |
59
|
|
|
|
|
|
|
led_on => 0, |
60
|
|
|
|
|
|
|
devicename => $params{devicename}, |
61
|
|
|
|
|
|
|
reset_gpio => $params{reset_gpio}, |
62
|
0
|
|
|
|
|
|
); |
63
|
0
|
|
|
|
|
|
$params{device} = $dev; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} elsif( $params{backend} eq 'RF69HW' ) { |
66
|
|
|
|
|
|
|
# Two way high powered module |
67
|
0
|
|
|
|
|
|
require HiPi::Energenie::ENER314_RT; |
68
|
|
|
|
|
|
|
my $dev = HiPi::Energenie::ENER314_RT->new( |
69
|
|
|
|
|
|
|
led_on => 0, |
70
|
|
|
|
|
|
|
devicename => $params{devicename}, |
71
|
|
|
|
|
|
|
reset_gpio => $params{reset_gpio}, |
72
|
0
|
|
|
|
|
|
rf_high_power => 1, |
73
|
|
|
|
|
|
|
); |
74
|
0
|
|
|
|
|
|
$params{device} = $dev; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} elsif( $params{backend} eq 'ENER314' ) { |
77
|
|
|
|
|
|
|
# simple 1 way single group board |
78
|
0
|
|
|
|
|
|
require HiPi::Energenie::ENER314; |
79
|
0
|
|
|
|
|
|
my $dev = HiPi::Energenie::ENER314->new(); |
80
|
0
|
|
|
|
|
|
$params{device} = $dev; |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
croak qq(Invalid backend $params{backend} specified); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(%params); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $self; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub pair_socket { |
92
|
0
|
|
|
0
|
0
|
|
my($self, $socket, $seconds) = @_; |
93
|
0
|
0
|
|
|
|
|
croak(qq(Invalid socket $socket)) unless $socket =~ /^1|2|3|4$/; |
94
|
0
|
|
0
|
|
|
|
$seconds ||= 10; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# broadcast for $seconds seconds; |
97
|
0
|
|
|
|
|
|
my $endtime = time() + $seconds; |
98
|
0
|
|
|
|
|
|
my $data = $_switchdata->[$socket]->[0]; # broadcast 'off' message for socket |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
while ( $endtime >= time() ) { |
101
|
0
|
|
|
|
|
|
$self->device->switch_ook_socket( $self->groupid, $data, $self->repeat ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
return; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub switch_socket { |
108
|
0
|
|
|
0
|
0
|
|
my($self, $socket, $offon) = @_; |
109
|
0
|
0
|
|
|
|
|
croak(qq(Invalid socket $socket)) unless $socket =~ /^0|1|2|3|4$/; |
110
|
0
|
0
|
|
|
|
|
$offon = ( $offon ) ? 1 : 0; |
111
|
0
|
|
|
|
|
|
my $data = $_switchdata->[$socket]->[$offon]; |
112
|
0
|
|
|
|
|
|
$self->device->switch_ook_socket( $self->groupid, $data, $self->repeat ); |
113
|
0
|
|
|
|
|
|
return; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# test what we actually send |
117
|
|
|
|
|
|
|
sub dump_message { |
118
|
0
|
|
|
0
|
0
|
|
my($self, $socket, $offon) = @_; |
119
|
0
|
0
|
|
|
|
|
croak(q(Method requires backend 'ENER314_RT')) if $self->backend !~ /^ENER314_RT|RF69HW$/; |
120
|
0
|
0
|
|
|
|
|
croak(qq(Invalid socket $socket)) unless $socket =~ /^0|1|2|3|4$/; |
121
|
0
|
0
|
|
|
|
|
$offon = ( $offon ) ? 1 : 0; |
122
|
0
|
|
|
|
|
|
my $data = $_switchdata->[$socket]->[$offon]; |
123
|
0
|
|
|
|
|
|
my @tvals = $self->device->make_ook_message( $self->groupid, $data ); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# print preamble |
126
|
0
|
|
|
|
|
|
print sprintf("preamble : 0x%x, 0x%x, 0x%x, 0x%x\n", @tvals[0..3]); |
127
|
|
|
|
|
|
|
# print group id |
128
|
0
|
|
|
|
|
|
print sprintf("group id : 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", @tvals[4..13]); |
129
|
|
|
|
|
|
|
# print data |
130
|
0
|
|
|
|
|
|
print sprintf("set data : 0x%x, 0x%x\n", @tvals[14..15]); |
131
|
0
|
|
|
|
|
|
return; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |