blib/lib/Device/Chip/PCA9685.pm

filter: Statementstmt 92.6% 76 / 82 Branchbran 37.5% 3 / 8 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 95.8% 23 / 24 Podpod 87.5% 7 / 8 totaltotal 85.3% 111 / 130 CC 7 SCAR 8.2
CC
7
Cov
85%
set_channel_value
7.2
set_frequency
3.0
set_default_mode
2.0
CRAP 2.3
↑ Summary
1 package Device::Chip::PCA9685;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = 'v0.9';
7  
8 4 ▶use base qw/Device::Chip/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Time::HiRes q/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 C<Device::Chip::PCA9685> - A C<Device::Chip> implementation for the PCA9685 chip
16  
17 =head1 DESCRIPTION
18  
19 This class implements a L<Device::Chip> interface for the PCA9685 chip, a 12-bit 16 channel PWM driver.
20  
21 =head1 SYNOPSIS
22  
23 use Device::Chip::PCA9685;
24 use Device::Chip::Adapter;
25  
26 my $adapter = Device::Chip::Adapter->new_from_description("LinuxKernel");
27  
28 my $chip = Device::Chip::PCA9685->new();
29 # This is the i2c bus on an RPI 2 B+
30 $chip->mount($adapter, bus => '/dev/i2c-1')->get;
31
32 $chip->enable()->get;
33 $chip->set_frequency(400)->get; # 400 Hz
34
35 $chip->set_channel_value(10, 1024)->get; # Set channel 10 to 25% (1024/4096)
36
37 $chip->set_channel_full_value(10, 1024, 3192)->get; # Set channel 10 to ON at COUNTER=1024, and OFF at COUNTER=3192 (50% duty cycle, with 25% phase difference)
38  
39 =head1 METHODS
40  
41 =cut
42  
43 4 my %REGS = (
44 MODE1 => {addr => 0},
45 MODE2 => {addr => 1},
46 SUBADR1 => {addr => 2},
47 SUBADR2 => {addr => 3},
48 SUBADR3 => {addr => 4},
49 ALLCALLADR => {addr => 5},
50 ALL_CHAN_ON => {addr => 0xFA}, # 16bit
51 ALL_CHAN_OFF => {addr => 0xFC}, # 16bit
52 PRE_SCALE => {addr => 0xFE},
53 TEST_MODE => {addr => 0xFF},
54 );
55  
56 4 for my $n (0..15) {
57 64 $REGS{"CHAN${n}_ON"} = {addr => 0x06 + $n * 4}; # 16bit
58 64 $REGS{"CHAN${n}_OFF"} = {addr => 0x08 + $n * 4}; # 16bit
59 }
60  
61 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 4 ▶use constant PROTOCOL => "I2C";
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 ▶sub _read_u8 {
Subroutine _read_u8: called CC=1 SCAR=0.0
66 1 my $self = shift;
67 1 my ($register) = @_;
68
69 1 my $regv = $REGS{$register}{addr};
70
71 1 ▶ $self->protocol->write_then_read("\0", 1)->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 my ($value) = @_;
73 1 return Future->done(unpack("C", $value));
74 1 });
75 }
76  
77 5 ▶sub _write_u8 {
Subroutine _write_u8: called CC=1 SCAR=0.0
78 5 my $self = shift;
79 5 my ($register, $value) = @_;
80  
81 5 my $regv = $REGS{$register}{addr};
82  
83 5 $self->protocol->write(pack("C C", $regv, $value));
84 }
85  
86 3 ▶sub _write_u16 {
Subroutine _write_u16: called CC=1 SCAR=0.0
87 3 my $self = shift;
88 3 my ($register, @values) = @_;
89  
90 3 my $regv = $REGS{$register}{addr};
91  
92 3 $self->protocol->write(pack("C (S<)*", $regv, @values));
93 }
94  
95 3 ▶sub I2C_options {my $self = shift; return (addr => 0x40, @_)}; # pass it through, but allow the address to change if passed in, should use a constructor instead
Subroutine I2C_options: called CC=1 SCAR=0.0
Pod undocumented
96  
97 =head2 set_channel_value
98  
99 $chip->set_channel_value($channel, $time_on, $offset = 0)->get
100
101 Sets a channel PWM time based on a single value from 0-4095. Starts the channel to turn on at COUNTER = 0, and off at $time_on.
102 C<$offset> lets you stagger the time that the channel comes on and off. This lets you vary the times that channels go on and off
103 to reduce noise effects and power supply issues from large loads all coming on at once.
104  
105 C<$on_time> := 0; C<$off_time> := $time_on;
106  
107 =cut
108  
109 1 ▶sub set_channel_value {
Subroutine set_channel_value: called CC=5 SCAR=19.7
Pod documented
110 1 my $self = shift;
111 1 my ($chan, $time_on, $offset) = @_;
112 1 ▶ $offset //= 0;
Condition50%1/2
$offset //= 0
l!l
01
MC/DC0%0/1
$offset //= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
113
114 # set the high parts first, we shouldn't ever have backtracking then
115  
116 1 ▶ if ($time_on < 0 || $time_on >= 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_on < 0 or $time_on >= 4096) 0 1 1
Condition33%1/3
$time_on < 0 or $time_on >= 4096
l!l&&r!l&&!r
001
MC/DC0%0/2
$time_on < 0 or $time_on >= 4096
$time_on < 0 $time_on >= 4096
Truth table33%1/3
A or B
A= $time_on < 0B= $time_on >= 4096
ABresult
1X1
011
000
117 0 ▶ $time_on = $time_on >= 4096 ? 4095 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$time_on >= 4096 ? : 0 0 0
118 0 warn "Channel outside allowed value, clamping: $chan, $time_on\n";
119 }
120  
121 1 $offset %= 4096; # wrap the offset around, that way you can increment it by any amount and have it work as expected
122 1 $time_on = ($time_on + $offset) % 4096; # wrap it around based on the offset.
123
124 1 $self->set_channel_full_value($chan, $offset, $time_on);
125 }
126  
127 =head2 set_channel_full_value
128  
129 $chip->set_channel_full_value($channel, $on_time, $off_time)->get
130
131 Set a channel value, on and off time. Lets you control the full on and off time based on the 12 bit counter on the device.
132  
133 =cut
134  
135 1 ▶sub set_channel_full_value {
Subroutine set_channel_full_value: called CC=1 SCAR=0.0
Pod documented
136 1 my ($self, $chan, $on_t, $off_t) = @_;
137  
138 1 $self->_write_u16("CHAN${chan}_ON" => ($on_t & 0x0FFF), ($off_t & 0x0FFF));
139 }
140  
141 =head2 set_channel_on
142  
143 $chip->set_channel_on($channel)->get
144
145 Set a channel to full on. No off time at all.
146  
147 =cut
148  
149 1 ▶sub set_channel_on {
Subroutine set_channel_on: called CC=1 SCAR=0.0
Pod documented
150 1 my ($self, $chan) = @_;
151
152 # Set bit 4 of ON high, this is the bit that sets the channel to full on
153 1 $self->_write_u16("CHAN${chan}_ON" => 0x1000, 0x0000);
154 }
155  
156 =head2 set_channel_off
157  
158 $chip->set_channel_off($channel)->get
159
160 Set a channel to full off. No on time at all.
161  
162 =cut
163  
164 1 ▶sub set_channel_off {
Subroutine set_channel_off: called CC=1 SCAR=0.0
Pod documented
165 1 my ($self, $chan) = @_;
166
167 # Set bit 4 of OFF high, this is the bit that sets the channel to full off
168 1 $self->_write_u16("CHAN${chan}_ON" => 0x0000, 0x1000);
169 }
170  
171 =head2 set_default_mode
172  
173 $chip->set_default_mode()->get
174
175 Reset the default mode back to the PCA9685.
176  
177 =cut
178  
179 0 ▶sub set_default_mode {
Subroutine set_default_mode: not called CC=1 SCAR=6.9
Pod documented
180 0 my $self = shift;
181 # Sets all the mode registers to the chip defaults
182 0 Future->needs_all(
183 $self->_write_u8(MODE1 => 0b0000_0001),
184 $self->_write_u8(MODE2 => 0b000_00100),
185 );
186 }
187  
188 =head2 set_frequency
189  
190 $chip->set_frequency()
191
192 Set the prescaler to the desired frequency for PWM. Returns the real frequency due to rounding.
193  
194 =cut
195  
196 1 ▶sub set_frequency {
Subroutine set_frequency: called CC=3 SCAR=11.1
Pod documented
197 1 my $self = shift;
198 1 my ($freq) = @_;
199 4 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 1 my $divisor = int( ( 25000000 / ( 4096 * $freq ) ) + 0.5 ) - 1;
202 0 ▶ if ($divisor < 3) { die "PCA9685 forces the scaler to be at least >= 3 (1526 Hz)." };
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor < 3) 0 1 1
203 0 ▶ if ($divisor > 255) { die "PCA9685 forces the scaler to be <= 255 (24Hz)." };
Branch: 1 branch
BranchTrueFalseTotal
if ($divisor > 255) 0 1 1
204  
205 1 my $realfreq = 25000000 / (($divisor + 1)*(4096));
206
207 1 my $old_mode1;
208 1 ▶ $self->_read_u8("MODE1")->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
209 1 ( $old_mode1 ) = @_;
210  
211 1 my $new_mode1 = ($old_mode1 & 0x7f) | 0x10; # Set the chip to sleep, make sure reset is disabled while we do this to avoid noise/phase differences
212
213 1 $self->_write_u8(MODE1 => $new_mode1);
214 1 ▶ })->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 1 Future->needs_all(
216 $self->_write_u8(PRE_SCALE => $divisor),
217 $self->_write_u8(MODE1 => $old_mode1),
218 );
219 1 ▶ })->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
220 1 usleep(5000);
221 1 $self->_write_u8(MODE1 => $old_mode1 | 0x80); # turn on the external clock, should this be optional?
222 1 ▶ })->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 1 return Future->done( $realfreq );
224 1 });
225 }
226  
227 =head2 enable
228  
229 $chip->enable()->get
230  
231 Enable the device. Must be the first thing done with the device.
232  
233 =cut
234  
235 1 ▶sub enable {
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
236 1 my $self = shift;
237  
238 # 0x20 == AI, auto-increment addresses during register transfer
239 # Useful for 16bit read/write
240 1 $self->_write_u8(MODE1 => 0x20);
241 }
242  
243 =head1 AUTHOR
244  
245 Ryan Voots, <simcop2387@simcop2387.info>
246 Paul 'LeoNerd' Evans <leonerd@leonerd.org.uk>
247  
248 =cut
249  
250 4 1;
↑ Summary