File Coverage

blib/lib/RPi/DigiPot/MCP4XXXX.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package RPi::DigiPot::MCP4XXXX;
2              
3 1     1   15028 use warnings;
  1         2  
  1         26  
4 1     1   3 use strict;
  1         2  
  1         29  
5              
6             our $VERSION = '2.36.2';
7              
8 1     1   415 use RPi::WiringPi::Constant qw(:all);
  1         736  
  1         170  
9 1     1   169 use WiringPi::API qw(:all);
  0            
  0            
10              
11             sub new {
12             if (@_ !=3 && @_ != 4){
13             die "new() requires \$cs and \$channel at minimum\n";
14             }
15              
16             my ($class, $cs, $channel, $speed) = @_;
17              
18             my $self = bless {}, $class;
19             $self->_cs($cs);
20             $self->{len} = 2;
21              
22             wiringPiSetupGpio();
23              
24             wiringPiSPISetup(
25             $self->_channel($channel),
26             $self->_speed($speed)
27             );
28              
29             pinMode($self->_cs, OUTPUT);
30             digitalWrite($self->_cs, HIGH);
31              
32             return $self;
33             }
34             sub set {
35             my ($self, $data, $pot) = @_;
36              
37             if ($data < 0 || $data > 255){
38             die "set() requires 0-255 as the data param\n";
39             }
40              
41             if (defined $pot){
42             if ($pot !=1 && $pot != 2 && $pot != 3){
43             die "set() \$pot param must be 1-3\n";
44             }
45             }
46            
47             my $cmd = 0x01;
48             $pot = 1 if ! defined $pot;
49              
50             my $bytes = $self->_bytes($cmd, $pot, $data);
51              
52             digitalWrite($self->_cs, LOW);
53             spiDataRW($self->_channel, $bytes, $self->_len);
54             digitalWrite($self->_cs, HIGH);
55             }
56             sub shutdown {
57             my ($self, $pot) = @_;
58              
59             if (defined $pot){
60             if ($pot !=1 && $pot != 2 && $pot != 3){
61             die "set() \$pot param must be 1-3\n";
62             }
63             }
64              
65             my $data = 0;
66             my $cmd = 0x02; # shutdown bit
67             $pot = 1 if ! defined $pot;
68            
69             my $bytes = $self->_bytes($cmd, $pot, $data);
70              
71             spiDataRW($self->_channel, $bytes, $self->_len);
72             }
73             sub _bytes {
74            
75             # calculates and returns an aref of control/data bytes
76              
77             my ($self, $cmd, $chan, $data) = @_;
78              
79             if (! defined $cmd || ! defined $chan || ! defined $data){
80             die "_bytes() requires \$cmd, \$chan (pot) and \$data params\n";
81             }
82              
83             # shift the command byte left to get a nibble,
84             # then OR the channel nibble to it
85              
86             my $cntl = ($cmd << 4) | $chan;
87            
88             return [$cntl, $data];
89             }
90             sub _channel {
91              
92             # sets/gets the SPI channel
93              
94             my ($self, $chan) = @_;
95             $self->{channel} = $chan if defined $chan;
96              
97             if ($self->{channel} != 0 && $self->{channel} != 1){
98             die "\$channel param must be 0 or 1\n";
99             }
100              
101             return $self->{channel};
102             }
103             sub _cs {
104              
105             # sets/gets the chip select (CS) pin
106              
107             my ($self, $pin) = @_;
108              
109             if (defined $pin && ($pin < 0 || $pin > 63)){
110             die "cs() param must be a valid GPIO pin number\n";
111             }
112              
113             $self->{cs} = $pin if defined $pin;
114              
115             if (! defined $self->{cs}){
116             die "cs() can't continue, we're not configured with a pin\n";
117             }
118              
119             return $self->{cs};
120             }
121             sub _len {
122            
123             # returns the number of bytes to send to SPI
124             # this number is hardcoded in new()
125              
126             my $self = shift;
127             return $self->{len};
128             }
129             sub _speed {
130              
131             # sets/gets the SPI bus speed
132              
133             my ($self, $speed) = @_;
134             $self->{speed} = $speed if defined $speed;
135             $self->{speed} = 1000000 if ! defined $self->{speed}; # 1 MHz
136             return $self->{speed};
137             }
138             sub _vim{};
139              
140             1;
141             __END__