| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # (C) Paul Evans, 2018-2023 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use v5.26; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Object::Pad 0.800; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | package Device::Chip::MCP3221 0.16; | ||||||||||||
| 11 | class Device::Chip::MCP3221 | ||||||||||||
| 12 | :isa(Device::Chip); | ||||||||||||
| 13 | |||||||||||||
| 14 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use constant PROTOCOL => "I2C"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | =encoding UTF-8 | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 NAME | ||||||||||||
| 21 | |||||||||||||
| 22 | C<Device::Chip::MCP3221> - chip driver for F<MCP3221> | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||
| 25 | |||||||||||||
| 26 | use Device::Chip::MCP3221; | ||||||||||||
| 27 | use Future::AsyncAwait; | ||||||||||||
| 28 | |||||||||||||
| 29 | my $chip = Device::Chip::MCP3221->new; | ||||||||||||
| 30 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||
| 31 | |||||||||||||
| 32 | printf "The reading is %d\n", await $chip->read_adc; | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||
| 35 | |||||||||||||
| 36 | This L<Device::Chip> subclass provides specific communications to a | ||||||||||||
| 37 | F<Microchip> F<MCP3221> chip. | ||||||||||||
| 38 | |||||||||||||
| 39 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||
| 40 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||
| 41 | concepts or features, only the use of this module to access them. | ||||||||||||
| 42 | |||||||||||||
| 43 | =cut | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 MOUNT PARAMETERS | ||||||||||||
| 46 | |||||||||||||
| 47 | =head2 addr | ||||||||||||
| 48 | |||||||||||||
| 49 | The I²C address of the device. Can be specified in decimal, octal or hex with | ||||||||||||
| 50 | leading C<0> or C<0x> prefixes. | ||||||||||||
| 51 | |||||||||||||
| 52 | =cut | ||||||||||||
| 53 | |||||||||||||
| 54 | 1 | ▶ | sub I2C_options ( $, %params ) | ||||||||||
|
Subroutine
I2C_options: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||
| 55 | { | ||||||||||||
| 56 | 1 | ▶ | my $addr = delete $params{addr} // 0x4D; | ||||||||||
|
Condition50%1/2
delete $params{"addr"} // 77
MC/DC0%0/1
delete $params{"addr"} // 77
delete $params{"addr"}
Truth table50%1/2
A
A= delete $params{"addr"}
| |||||||||||||
| 57 | 1 | ▶ | $addr = oct $addr if $addr =~ m/^0/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | |||||||||||||
| 59 | return ( | ||||||||||||
| 60 | 1 | addr => $addr, | |||||||||||
| 61 | max_bitrate => 400E3, | ||||||||||||
| 62 | ); | ||||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 METHODS | ||||||||||||
| 66 | |||||||||||||
| 67 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||
| 68 | instances. | ||||||||||||
| 69 | |||||||||||||
| 70 | =cut | ||||||||||||
| 71 | |||||||||||||
| 72 | # Chip has no config registers | ||||||||||||
| 73 | 0 | ▶ | async method read_config () { return {} } | ||||||||||
|
Subroutine
read_config: not called CC=1 SCAR=4.6
Pod
undocumented
| |||||||||||||
| 74 | 0 | ▶ | async method change_config (%) { } | ||||||||||
|
Subroutine
change_config: not called CC=1 SCAR=4.1
Pod
undocumented
| |||||||||||||
| 75 | |||||||||||||
| 76 | =head2 read_adc | ||||||||||||
| 77 | |||||||||||||
| 78 | $value = await $chip->read_adc; | ||||||||||||
| 79 | |||||||||||||
| 80 | Performs a conversion and returns the result as a plain unsigned 12-bit | ||||||||||||
| 81 | integer. | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 2 | ▶ | async method read_adc () | ||||||||||
|
Subroutine
read_adc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 86 | 2 | { | |||||||||||
| 87 | 2 | my $buf = await $self->protocol->read( 2 ); | |||||||||||
| 88 | |||||||||||||
| 89 | 2 | return unpack "S>", $buf; | |||||||||||
| 90 | 2 | } | |||||||||||
| 91 | |||||||||||||
| 92 | =head2 read_adc_ratio | ||||||||||||
| 93 | |||||||||||||
| 94 | $ratio = await $chip->read_adc_ratio; | ||||||||||||
| 95 | |||||||||||||
| 96 | Performs a conversion and returns the result as a floating-point number | ||||||||||||
| 97 | between 0 and 1. | ||||||||||||
| 98 | |||||||||||||
| 99 | =cut | ||||||||||||
| 100 | |||||||||||||
| 101 | 1 | ▶ | async method read_adc_ratio () | ||||||||||
|
Subroutine
read_adc_ratio: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 102 | 1 | { | |||||||||||
| 103 | # MCP3221 is 12-bit | ||||||||||||
| 104 | 1 | return ( await $self->read_adc ) / 2**12; | |||||||||||
| 105 | 2 | } | |||||||||||
| 106 | |||||||||||||
| 107 | =head1 AUTHOR | ||||||||||||
| 108 | |||||||||||||
| 109 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | 2 | 0x55AA; | |||||||||||