lib/RPi/MultiPCA9685.pm

filter: Statementstmt 31.1% 24 / 77 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 32.6% 34 / 104 CC 8 SCAR 24.9
CC
8
Cov
26%
setChannelPWM
30.0
disablePWM
12.0
init_PWM
6.0
CRAP 12.1
↑ Summary script/PCA9685-minimal.pl »
1 package RPi::MultiPCA9685;
2 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $VERSION = '0.08';
4 1 our @ISA = qw();
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX qw/ceil floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use RPi::I2C;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use vars qw/@dev $num_PWMPCBs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = qw(init_PWM setChannelPWM disablePWM);
12  
13 #----------------------------------------------------------
14 # init all PWM PCBs - the number of boards is determined by the number of Servos (Servos /16)
15 #----------------------------------------------------------
16 0 ▶sub init_PWM {
Subroutine init_PWM: not called CC=2 SCAR=17.9
Pod documented
17 0 my $i2cport=shift;
18 0 my $i2c_address=shift;
19 0 my $i2c_freq=shift;
20 0 my $num_servos=shift;
21 0 my $presc=int(25000000/(4096*$i2c_freq));# calc the prescaler value for register FEh(254)
22 0 $num_PWMPCBs=ceil($num_servos/16);
23 0 my $j;
24 0 @dev=();
25 0 ▶ for ($j=0;$j<=$num_PWMPCBs;$j++){ # init all PWM PCBs - every 16 ports switch to the next i2c address
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $RPi::MultiPCA9685::num_PWMPCBs) 0 0 0
26 0 $dev[$j] = RPi::I2C->new($i2c_address+$j,$i2cport);
27 0 $dev[$j]->write_byte(16,0); # allow to program the prescale register (only possible when PWM is inactive)
28 0 $dev[$j]->write_byte($presc,254); # set the calculated prescaler value to achieve the desired frequency
29 0 $dev[$j]->write_block([32,14],0); # init the pca9685 chip to output the PWM
30 }
31 0 return 1;
32 }
33 #----------------------------------------------------------
34 # Disable PWM - powers off all devices
35 #----------------------------------------------------------
36 0 ▶sub disablePWM {
Subroutine disablePWM: not called CC=3 SCAR=24.8
Pod documented
37 0 ▶ return unless defined $num_PWMPCBs; # Only disable if init has been done before
Branch: 1 branch
BranchTrueFalseTotal
unless defined $RPi::MultiPCA9685::num_PWMPCBs 0 0 0
38 0 my $j;
39 0 ▶ for ($j=0;$j<=$num_PWMPCBs;$j++){ # disable all PWM PCBs - every 16 ports switch to the next i2c address
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $RPi::MultiPCA9685::num_PWMPCBs) 0 0 0
40 0 $dev[$j]->write_block([0,0,0,0],250); # disable PWM for all channels via chip Register FAh(250)
41 }
42 0 return 1;
43 }
44 #----------------------------------------------------------
45 # send the pwm values to the PCA9685 chip
46 # use the auto address increment of the chip
47 # Switches to the next chip if channel 15,31,47,63.... is exceeded
48 #----------------------------------------------------------
49 0 ▶sub setChannelPWM {
Subroutine setChannelPWM: not called CC=5 SCAR=34.0
Pod documented
50 0 my $servo=shift; # The first servo we send moves
51 0 my $mref=shift; # contains a reference for an array that contains moves for perhaps several servos
52 0 my @split=();
53 0 my $h; # MSB of the servo move
54 my $l; # LSB of the servo move
55 0 my $i=0; # start with the first array element
56 0 my $rc=0; # set the register count to 0
57 0 my $startreg=6+($servo*4%64); # the first servo register we write to modulo 70 to reset with every new board
58 0 my $numref=$#$mref; # the number of positions to write
59 0 my $splitref=\@split; # RPI::I2C needs all moves in an array reference
60 0 my $brd=int($servo*2/32); # calc the board number - the pwm board to write to
61 0 my $obrd=$brd; # previous board is identical with current
62 0 ▶ while ($i <= $numref){ # $i points onto array element of input array (2 elements per servo)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numref) 0 0 0
63 0 $brd=int(($i+$servo*2)/32); # calc the board number
64 0 ▶ if ($rc > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 15) 0 0 0
65 0 $dev[$obrd]->write_block($splitref,$startreg); # pass all moves of the first block to the PCA9685 chip
66 0 @split=(); # empty, because we switch to a new board
67 0 $rc=0; # continue with register reset to 0
68 0 $startreg+=32; # make sure the next 8 registers will be higher
69 0 ▶ if ($obrd < $brd) { # in addition a board border is reached ?
Branch: 1 branch
BranchTrueFalseTotal
if ($obrd < $brd) 0 0 0
70 0 $obrd=$brd; # note the change
71 0 $startreg=6; # after the chip change start with register 6 of the chip (PWM 0)
72 }
73 }
74 0 ▶ if ($obrd < $brd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obrd < $brd) 0 0 0
75 0 $dev[$obrd]->write_block($splitref,$startreg); # pass all moves of the first block to the PCA9685 chip
76 0 @split=(); # empty, because we switch to a new board
77 0 $obrd=$brd; # note the change
78 0 $startreg=6; # after the chip change start with register 6 of the chip (PWM 0)
79 0 $rc=0; # continue with register reset to 0
80 }
81 0 $h=int($mref->[$i]) >> 8; # calc MSB
82 0 $l=int($mref->[$i]) & 255; # and LSB
83 0 push(@split,($l,$h)); # Put all into an array
84 0 $rc++; # count the registers in buffer (must not exceed 7 since i2c lib can only write 32 byte at once)
85 0 $i++; # point to the next element
86 }
87 0 $dev[$brd]->write_block($splitref,$startreg); # pass remaining moves to the PCA9685 chip
88 0 return 1;
89 }
90 1 1;
91 __END__
↑ Summary script/PCA9685-minimal.pl »