| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Device::PWMGenerator::PCA9685; | ||||||||||
| 5 | |||||||||||
| 6 | # PODNAME: Device::PWMGenerator::PCA9685 | ||||||||||
| 7 | # ABSTRACT: I2C interface to PWM Generator on PCA9685 using Device::SMBus | ||||||||||
| 8 | # | ||||||||||
| 9 | # This file is part of Device-PCA9685 | ||||||||||
| 10 | # | ||||||||||
| 11 | # This software is copyright (c) 2016 by Shantanu Bhadoria. | ||||||||||
| 12 | # | ||||||||||
| 13 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 14 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 15 | # | ||||||||||
| 16 | 2 | our $VERSION = '0.004'; # VERSION | |||||||||
| 17 | |||||||||||
| 18 | # Dependencies | ||||||||||
| 19 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use POSIX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 2 | ▶ | use Time::HiRes qw(usleep); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | extends 'Device::SMBus'; | |||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | MODE1 => 0x00, | |||||||||
| 30 | |||||||||||
| 31 | SUBADR1 => 0x02, | ||||||||||
| 32 | SUBADR2 => 0x03, | ||||||||||
| 33 | SUBADR3 => 0x04, | ||||||||||
| 34 | |||||||||||
| 35 | PRESCALE => 0xFE, | ||||||||||
| 36 | |||||||||||
| 37 | LED0_ON_L => 0x06, | ||||||||||
| 38 | LED0_ON_H => 0x07, | ||||||||||
| 39 | LED0_OFF_L => 0x08, | ||||||||||
| 40 | LED0_OFF_H => 0x09, | ||||||||||
| 41 | 2 | }; | |||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | 2 | has '+I2CDeviceAddress' => ( | |||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | default => 0x40, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | 2 | has frequency => ( | |||||||||
| 51 | is => 'rw', | ||||||||||
| 52 | required => 1, | ||||||||||
| 53 | trigger => \&_frequencySet, | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | 2 | has debug => ( | |||||||||
| 57 | is => 'ro', | ||||||||||
| 58 | default => 0, | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub _frequencySet { | ||||||||
|
Subroutine
_frequencySet: not called CC=4 SCAR=30.0
| |||||||||||
| 62 | 0 | my ( $self, $newFrequency, $oldFrequency ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | # PCA9685 allows you to set the frequency using a register it calls PRESCALE | ||||||||||
| 65 | 0 | ▶ | say "Making PCA9685 go to sleep" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | my $oldMode = $self->readByteData(MODE1); | |||||||||
| 67 | 0 | my $newMode = ( $oldMode & 0x7f ) | 0x10 | |||||||||
| 68 | ; #set sleep bit while preserving other bits and ensuring that restart bit is set to 0 when writing to register | ||||||||||
| 69 | 0 | $self->writeByteData( MODE1, $newMode ); | |||||||||
| 70 | |||||||||||
| 71 | # Calculate Prescale from Frequency. Clock is 25 MHz on PCA8695 | ||||||||||
| 72 | 0 | my $prescale = floor( ( 25000000 / ( 4096 * $newFrequency ) ) + 0.5 ) - 1; | |||||||||
| 73 | 0 | ▶ | say | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | "Setting frequency to $newFrequency Hz. Calcualting prescale value \nprescale=round( 25000000/(4096 * frequency)) - 1 \nfrom frequency as $prescale(PCA9685 clock is at 25MHz or 25000000 Hz)" | ||||||||||
| 75 | if $self->debug; | ||||||||||
| 76 | 0 | ▶ | if ( $prescale >= 0x03 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | $self->writeByteData( PRESCALE, $prescale ); | |||||||||
| 78 | } | ||||||||||
| 79 | else { | ||||||||||
| 80 | 0 | die | |||||||||
| 81 | "Bad frequency value. Can't set prescale less than 3. reduce your frequency value"; | ||||||||||
| 82 | } | ||||||||||
| 83 | 0 | $self->writeByteData( MODE1, $oldMode ); | |||||||||
| 84 | 0 | usleep(5000); | |||||||||
| 85 | 0 | $self->writeByteData( MODE1, $oldMode | 0x80 ); # set extclk bit | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | sub enable { | ||||||||
|
Subroutine
enable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 90 | 0 | my ($self) = @_; | |||||||||
| 91 | 0 | ▶ | say "Setting up PCA9685" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | $self->writeByteData( MODE1, 0b00000000 ); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub setChannelPWM { | ||||||||
|
Subroutine
setChannelPWM: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 97 | 0 | my ( $self, $channel, $on, $off ) = @_; | |||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | say | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | "Setting Channel $channel PWM on at $on step, off at $off step in 0 to 4095 steps at " | ||||||||||
| 101 | . $self->frequency . "Hz" | ||||||||||
| 102 | if $self->debug; | ||||||||||
| 103 | 0 | $self->writeByteData( LED0_ON_L + ( 4 * $channel ), $on & 0xff ); | |||||||||
| 104 | 0 | $self->writeByteData( LED0_ON_H + ( 4 * $channel ), $on >> 8 ); | |||||||||
| 105 | |||||||||||
| 106 | 0 | $self->writeByteData( LED0_OFF_L + ( 4 * $channel ), $off & 0xff ); | |||||||||
| 107 | 0 | $self->writeByteData( LED0_OFF_H + ( 4 * $channel ), $off >> 8 ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 2 | 1; | |||||||||
| 111 | |||||||||||
| 112 | __END__ | ||||||||||