| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- ##############################################
- # $Id: 52_I2C_MCP342x.pm 13424 2017-02-16 22:06:16Z klausw $
- package main;
- use strict;
- use warnings;
- use Time::HiRes qw(usleep);
- use Scalar::Util qw(looks_like_number);
- #use Error qw(:try);
- use constant {
- MCP3422_I2C_ADDRESS => '0x68',
- };
- ##################################################
- # Forward declarations
- #
- sub I2C_MCP342x_Initialize($);
- sub I2C_MCP342x_Define($$);
- sub I2C_MCP342x_Attr(@);
- sub I2C_MCP342x_Poll($);
- sub I2C_MCP342x_Set($@);
- sub I2C_MCP342x_Undef($$);
- my %resols = (
- '12' => {
- code => 0b00000000,
- delay => 5690,
- lsb => 1000,
- },
- '14' => {
- code => 0b00000100,
- delay => 22730,
- lsb => 250,
- },
- '16' => {
- code => 0b00001000,
- delay => 90910,
- lsb => 62.5,
- },
- '18' => {
- code => 0b00001100,
- delay => 363640,
- lsb => 15.625,
- },
- );
- my %gains = (
- '1' => 0b00000000,
- '2' => 0b00000001,
- '4' => 0b00000010,
- '8' => 0b00000011,
- );
- sub I2C_MCP342x_Initialize($) {
- my ($hash) = @_;
- $hash->{DefFn} = 'I2C_MCP342x_Define';
- $hash->{InitFn} = 'I2C_MCP342x_Init';
- $hash->{AttrFn} = 'I2C_MCP342x_Attr';
- $hash->{GetFn} = 'I2C_MCP342x_Get';
- $hash->{UndefFn} = 'I2C_MCP342x_Undef';
- $hash->{I2CRecFn} = 'I2C_MCP342x_I2CRec';
- $hash->{AttrList} = 'IODev do_not_notify:0,1 showtime:0,1 poll_interval:1,2,5,10,20,30 ' .
- 'ch1roundDecimal:0,1,2,3 ch1gain:1,2,4,8 ch1resolution:12,14,16,18 ch1factor '.
- 'ch2roundDecimal:0,1,2,3 ch2gain:1,2,4,8 ch2resolution:12,14,16,18 ch2factor '.
- 'ch3roundDecimal:0,1,2,3 ch3gain:1,2,4,8 ch3resolution:12,14,16,18 ch3factor '.
- 'ch4roundDecimal:0,1,2,3 ch4gain:1,2,4,8 ch4resolution:12,14,16,18 ch4factor '.
- $readingFnAttributes;
- }
- sub I2C_MCP342x_Define($$) {
- my ($hash, $def) = @_;
- my @a = split('[ \t][ \t]*', $def);
-
- $hash->{STATE} = "defined";
- if ($main::init_done) {
- eval { I2C_MCP342x_Init( $hash, [ @a[ 2 .. scalar(@a) - 1 ] ] ); };
- return I2C_MCP342x_Catch($@) if $@;
- }
- return undef;
- }
- sub I2C_MCP342x_Init($$) {
- my ( $hash, $args ) = @_;
-
- my $name = $hash->{NAME};
- Log3 $hash, 5, "$hash->{NAME}: Init Argumente1: $args";
- if (defined $args && int(@$args) < 1)
- {
- Log3 $hash, 0, "Define: Wrong syntax. Usage:\n" .
- "define <name> MCP342x [<i2caddress>] [<type>]";
- }
-
- if (defined (my $address = shift @$args)) {
- $hash->{I2C_Address} = $address =~ /^0x.*$/ ? oct($address) : $address;
- Log3 $hash, 0, "$name: I2C Address not valid" unless ($hash->{I2C_Address} < 128 && $hash->{I2C_Address} > 3);
- } else {
- $hash->{I2C_Address} = hex(MCP3422_I2C_ADDRESS);
- }
-
- if (defined (my $channels = shift @$args)) {
- $hash->{channels} = ($channels == 4 ? 4 : 2);
- } else {
- $hash->{channels} = 2;
- }
- my $msg = '';
- # create default attributes
- if (AttrVal($name, 'poll_interval', '?') eq '?') {
- $msg = CommandAttr(undef, $name . ' poll_interval 5');
- if ($msg) {
- Log3 ($hash, 1, $msg);
- return $msg;
- }
- }
- AssignIoPort($hash);
- $hash->{STATE} = 'Initialized';
- # my %sendpackage = ( i2caddress => $hash->{I2C_Address}, direction => "i2cread" );
- # $sendpackage{reg} = hex("AA");
- # $sendpackage{nbyte} = 22;
- # return "$name: no IO device defined" unless ($hash->{IODev});
- # my $phash = $hash->{IODev};
- # my $pname = $phash->{NAME};
- # CallFn($pname, "I2CWrtFn", $phash, \%sendpackage);
- return undef;
- }
- sub I2C_MCP342x_Catch($) {
- my $exception = shift;
- if ($exception) {
- $exception =~ /^(.*)( at.*FHEM.*)$/;
- return $1;
- }
- return undef;
- }
- sub I2C_MCP342x_Attr (@) {# hier noch Werteueberpruefung einfuegen
- my ($command, $name, $attr, $val) = @_;
- my $hash = $defs{$name};
- my $msg = '';
- if ($command && $command eq "set" && $attr && $attr eq "IODev") {
- if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $val)) {
- main::AssignIoPort($hash,$val);
- my @def = split (' ',$hash->{DEF});
- I2C_MCP342x_Init($hash,\@def) if (defined ($hash->{IODev}));
- }
- }
- if ($attr && $attr eq 'poll_interval') {
- #my $pollInterval = (defined($val) && looks_like_number($val) && $val > 0) ? $val : 0;
- if (!defined($val) ) {
- RemoveInternalTimer($hash);
- } elsif ($val > 0) {
- RemoveInternalTimer($hash);
- InternalTimer(1, 'I2C_MCP342x_Poll', $hash, 0);
- } else {
- $msg = 'Wrong poll intervall defined. poll_interval must be a number > 0';
- }
- } elsif ($attr eq 'roundDecimal') {
- $msg = 'Wrong $attr defined. Use one of 0, 1, 2' if defined($val) && $val <= 0 && $val >= 3 ;
- } elsif ($attr eq 'gain') {
- foreach (split (/,/,$val)) {
- my @pair = split (/=/,$_);
- $msg = "wrong value: $_ for \"attr $hash->{NAME} $attr\" use comma separated <channel>=1|2|4|8 where <channel> = 1-$hash->{channels}"
- unless ( ( scalar(@pair) == 2 &&
- $pair[0] =~ m/^[1-4]$/i && $pair[0] <= $hash->{channels} &&
- $pair[1] =~ m/^(1|2|4|8)$/i ) ||
- $val =~ m/^(1|2|4|8)$/i);
- }
- } elsif ($attr eq 'resolution') {
- foreach (split (/,/,$val)) {
- my @pair = split (/=/,$_);
- $msg = "wrong value: $_ for \"attr $hash->{NAME} $attr\" use comma separated <channel>=12|14|16|18 where <channel> = 1-$hash->{channels}"
- unless ( ( scalar(@pair) == 2 &&
- $pair[0] =~ m/^[1-4]$/i &&
- $pair[1] =~ m/^1(2|4|6|8)$/i ) &&
- $val =~ m/^1(2|4|6|8)$/i );
- }
- }
-
- return ($msg) ? $msg : undef;
- }
- sub I2C_MCP342x_Poll($) {
- my ($hash) = @_;
- my $name = $hash->{NAME};
-
- # Read values
- I2C_MCP342x_Get($hash, $name);
-
- my $pollInterval = AttrVal($hash->{NAME}, 'poll_interval', 0);
- if ($pollInterval > 0) {
- InternalTimer(gettimeofday() + ($pollInterval * 60), 'I2C_MCP342x_Poll', $hash, 0);
- }
- }
- sub I2C_MCP342x_Get($@) {
- my ($hash, @a) = @_;
- my $name = $a[0];
- my $cmd = $a[1];
-
- my $rex = "^[1-" . $hash->{channels} . "]\$";
- if (defined $cmd && $cmd =~ m/$rex/i) {
- my $resol = defined $a[2] ? $a[2] : AttrVal($hash->{NAME},("ch" . $cmd . "resolution"),"12");
- return "Wrong resolution, use 12, 14, 16 or 18" unless $resol =~ m/^1(2|4|6|8)$/i;
- my $gain = defined $a[3] ? $a[3] : AttrVal($hash->{NAME},("ch" . $cmd . "gain"),"1");
- return "Wrong gain, use 1, 2, 4 or 8" unless $gain =~ m/^(1|2|4|8)$/i;
- my $ts = ReadingsTimestamp($hash->{NAME},("Channel".$cmd),0);
- I2C_MCP342x_readvoltage($hash,$cmd,$resol,$gain);
- foreach (1..400) { #max 2s warten
- usleep 5000;
- return ReadingsVal($hash->{NAME},("Channel".$cmd),undef) if $ts ne ReadingsTimestamp($hash->{NAME},("Channel".$cmd),0);
- }
- } else {
- foreach (1..$hash->{channels}) {
- my $resol = defined $a[3] ? $a[3] : AttrVal($hash->{NAME},("ch" . $_ . "resolution"),"12");
- return "Wrong resolution, use 12, 14, 16 or 18" unless $resol =~ m/^1(2|4|6|8)$/i;
- my $gain = defined $a[4] ? $a[4] : AttrVal($hash->{NAME},("ch" . $_ . "gain"),"1");
- return "Wrong gain, use 1, 2, 4 or 8" unless $gain =~ m/^(1|2|4|8)$/i;
- I2C_MCP342x_readvoltage($hash,$_,$resol,$gain);
- }
- my @gets = ('1', '2');
- push(@gets,('3', '4')) if $hash->{channels} == 4;
- return 'Unknown argument' . (defined $cmd ? (" " . $cmd) : "" ) . ', choose one of ' . join(' ', @gets)
- }
- }
- sub I2C_MCP342x_Undef($$) {
- my ($hash, $arg) = @_;
- RemoveInternalTimer($hash);
- return undef;
- }
- sub I2C_MCP342x_I2CRec ($$) {
- my ($hash, $clientmsg) = @_;
- my $name = $hash->{NAME};
- my $phash = $hash->{IODev};
- my $pname = $phash->{NAME};
- while ( my ( $k, $v ) = each %$clientmsg ) { #erzeugen von Internals fuer alle Keys in $clientmsg die mit dem physical Namen beginnen
- $hash->{$k} = $v if $k =~ /^$pname/ ;
- }
- #my $ankommen = "$hash->{NAME}: vom physical empfangen";
- # foreach my $av (keys %{$clientmsg}) { $ankommen .= "|" . $av . ": " . $clientmsg->{$av}; }
- #Log3 $hash, 1, $ankommen;
- if ($clientmsg->{direction} && $clientmsg->{$pname . "_SENDSTAT"} && $clientmsg->{$pname . "_SENDSTAT"} eq "Ok") {
- readingsSingleUpdate($hash,"state", "Ok", 1);
- if ( $clientmsg->{direction} eq "i2cread" && defined($clientmsg->{received}) ) {###hier noch normal read rein,wenn alles wieder ok
- #Log3 $hash, 1, "empfangen: $clientmsg->{received}";
- I2C_MCP342x_GetVoltage ($hash, $clientmsg->{received}); # if $clientmsg->{type} eq "temp" && $clientmsg->{nbyte} == 2;
- }
- } else {
- readingsSingleUpdate($hash,"state", "transmission error", 1);
- Log3 $hash, 3, "$name: failurei in message from $pname";
- Log3 $hash, 3,(defined($clientmsg->{direction}) ? "Direction: " . $clientmsg->{direction} : "Direction: undef").
- (defined($clientmsg->{i2caddress}) ? " I2Caddress: " . sprintf("0x%.2X", $clientmsg->{i2caddress}) : " I2Caddress: undef").
- (defined($clientmsg->{reg}) ? " Register: " . sprintf("0x%.2X", $clientmsg->{reg}) : " Register: undef").
- (defined($clientmsg->{data}) ? " Data: " . sprintf("0x%.2X", $clientmsg->{data}) : " Data: undef").
- (defined($clientmsg->{received}) ? " received: " . sprintf("0x%.2X", $clientmsg->{received}) : " received: undef");
- }
- }
- sub I2C_MCP342x_GetVoltage ($$) {
- my ($hash, $rawdata) = @_;
- my @raw = split(" ",$rawdata);
- if ( defined($raw[4]) ) {
- if ( ($raw[4] & 0b10000000) == 0 ) {
- my $channel = 1 + (($raw[4] & 0b01100000) >> 5 );
- my $resol = 2 * (($raw[4] & 0b00001100) >> 2 ) + 12;
- my $gain = 2 ** ($raw[4] & 0b00000011);
- my $rawvolt;
- if ($resol == 18) {
- $rawvolt = ($raw[0] & 0b00000011) << 16 | $raw[1] << 8 | $raw[2];
- } elsif ($resol == 14) {
- $rawvolt = ($raw[0] & 0b00111111) << 8 | $raw[1];
- } elsif ($resol == 12) {
- $rawvolt = ($raw[0] & 0b00001111) << 8 | $raw[1];
- } else {
- $rawvolt = $raw[0] << 8 | $raw[1];
- }
- Log3 $hash, 4, "Kanal: $channel, rawvolt: $rawvolt, Aufloesung: $resol, Gain: $gain, LSB: $resols{$resol}{lsb}";
- $rawvolt -= (1 << $resol) if $rawvolt >= (1 << ($resol - 1));
- Log3 $hash, 4, "Kanal: $channel, Signedrawvolt: $rawvolt";
-
- my $voltage = ( $rawvolt * $resols{$resol}{lsb} ) / $gain ;
- $voltage /= 1000000; # LSB Werte in µV
- $voltage *= AttrVal($hash->{NAME},("ch" . $channel . "factor"),"1");
- $voltage = sprintf(
- '%.' . AttrVal($hash->{NAME}, ('ch' . $channel . 'roundDecimal'), 3) . 'f',
- $voltage
- );
- $voltage .= " overflow" if ( $rawvolt == ( (1<<($resol-1)) - 1) || $rawvolt == (1<<($resol-1)) );
- readingsSingleUpdate($hash,"Channel$channel", $voltage, 1);
- } else {
- Log3 $hash, 3, $hash->{NAME} . " error, output conversion not finished";
- }
- }
- }
- sub I2C_MCP342x_readvoltage($@) {
- my ($hash, $channel, $resol, $gain) = @_;
- my $name = $hash->{NAME};
- return "$name: no IO device defined" unless ($hash->{IODev});
- my $phash = $hash->{IODev};
- my $pname = $phash->{NAME};
- #0b10010000
- my $confreg = 1 << 7; # 1|| |||| Initiate a new conversion
- $confreg |= ($channel - 1) << 5; # 11 |||| Channel Selection Bits
- $confreg |= $resols{$resol}{code}; # 11|| Sample Rate Selection Bit
- $confreg |= $gains{$gain}; # 11 PGA Gain Selection Bits
- #Log3 $hash, 1, "confinhalt: " . sprintf ('0b%08b', $confreg);
-
- # Write CONFIGURATION REGISTER to device. This requests a conversion process
- my $i2creq = { i2caddress => $hash->{I2C_Address}, direction => "i2cwrite" };
- $i2creq->{data} = $confreg;
- CallFn($pname, "I2CWrtFn", $phash, $i2creq);
- usleep($resols{$resol}{delay}); #Verzoegerung
- # Read the result from device
- my $i2cread = { i2caddress => $hash->{I2C_Address}, direction => "i2cread" };
- $i2cread->{nbyte} = 5;
- #$i2cread->{type} = "temp";
- CallFn($pname, "I2CWrtFn", $phash, $i2cread);
-
- return;
- }
- 1;
- =pod
- =item device
- =item summary reads the analog inputs from an via I2C connected MCP342x
- =item summary_DE lesen der Analogeingänge eines über I2C angeschlossenen MCP342x
- =begin html
- <a name="I2C_MCP342x"></a>
- <h3>I2C_MCP342x</h3>
- (en | <a href="commandref_DE.html#I2C_MCP342x">de</a>)
- <ul>
- <a name="I2C_MCP342x"></a>
- Provides an interface to the MCP3422/3/4 A/D converter.
- The I2C messages are send through an I2C interface module like <a href="#RPII2C">RPII2C</a>, <a href="#FRM">FRM</a>
- or <a href="#NetzerI2C">NetzerI2C</a> so this device must be defined first.<br>
- <b>attribute IODev must be set</b><br>
- <a name="I2C_MCP342xDefine"></a><br>
- <b>Define</b>
- <ul>
- <code>define <name> I2C_MCP342x [[<I2C Address>] <n channels>]</code><br>
- where <I2C Address> is without direction bit and <n channels> is the number of A/D channels<br><br>
- </ul>
- <a name="I2C_MCP342xSet"></a>
- <b>Get</b>
- <ul>
- <code>get <name> [[[<channel>] <resolution> ] <gain>]</code><br>
- Returns the level on specific <channel>. <resolution> and <gain> will override attibutes for actual operation.
- Without attributes only the readings will be refreshed.<br><br>
- </ul>
- <a name="I2C_MCP342xAttr"></a>
- <b>Attributes</b>
- <ul>
- <li>poll_interval<br>
- Set the polling interval in minutes to query data from sensor<br>
- Default: 5, valid values: 1,2,5,10,20,30<br><br>
- </li>
- Following attributes are separate for all channels.<br><br>
- <li>ch1resolution<br>
- resolution settings<br>
- the bigger the resolution the longer the conversion time.<br>
- Default: 12, valid values: 12,14,16,18<br><br>
- </li>
- <li>ch1gain<br>
- gain setting<br>
- Important: the gain setting will reduce the measurement range an may produce an overflow. In this case "overflow" will be added to reading<br>
- Default: 1, valid values: 1,2,4,8<br><br>
- </li>
- <li>ch1factor<br>
- correction factor (will be mutiplied to channel value)<br>
- Default: 1, valid values: number<br><br>
- </li>
- <li>ch1roundDecimal<br>
- Number of decimal places for value<br>
- Default: 3, valid values: 0,1,2,3<br><br>
- </li>
- <li><a href="#IODev">IODev</a></li>
- <li><a href="#do_not_notify">do_not_notify</a></li>
- <li><a href="#showtime">showtime</a></li>
- </ul><br>
- </ul>
- =end html
- =begin html_DE
- <a name="I2C_MCP342x"></a>
- <h3>I2C_MCP342x</h3>
- (<a href="commandref.html#I2C_MCP342x">en</a> | de)
- <ul>
- <a name="I2C_MCP342x"></a>
- Ermöglicht die Verwendung eines MCP3422/3/4 I2C A/D Wandler.
- I2C-Botschaften werden über ein I2C Interface Modul wie beispielsweise das <a href="#RPII2C">RPII2C</a>, <a href="#FRM">FRM</a>
- oder <a href="#NetzerI2C">NetzerI2C</a> gesendet. Daher muss dieses vorher definiert werden.<br>
- <b>Das Attribut IODev muss definiert sein.</b><br>
- <a name="I2C_MCP342xDefine"></a><br>
- <b>Define</b>
- <ul>
- <code>define <name> I2C_MCP342x [[<I2C Address>] <n channels>]</code><br>
- Der Wert <code><I2C Address></code> ist die I2C Adresse ohne Richtungsbit und <n channels> die Anzahl der A/D Kanäle.<br>
- </ul>
- <a name="I2C_MCP342xGet"></a>
- <b>Get</b>
- <ul>
- <code>get <name> [[[<channel>] <resolution> ] <gain>]</code><br>
- Aktuelle Werte vom entstrechenden <channel> lesen. <resolution> und <gain> überschreiben die entsprechenden Attribute für diesen Lesevorgang<br><br>
- </ul>
- <a name="I2C_MCP342xAttr"></a>
- <b>Attribute</b>
- <ul>
- <li>poll_interval<br>
- Aktualisierungsintervall aller Werte in Minuten.<br>
- Standard: 5, gültige Werte: 1,2,5,10,20,30<br><br>
- </li>
- Folgende Attribute existieren separat für alle Kanäle.<br><br>
- <li>ch1resolution<br>
- Auflösung des Kanals<br>
- Je größer die Auflösung desto länger die Lesezeit.<br>
- Standard: 12, gültige Werte: 12,14,16,18<br><br>
- </li>
- <li>ch1gain<br>
- Verstärkungsfaktor<br>
- Wichtig: Der Verstärkungsfaktor verringert den Messbereich entsprechend und kann zu einem Überlauf führen. In diesem Fall wird "overflow" an das reading angehängt.<br>
- Standard: 1, gültige Werte: 1,2,4,8<br><br>
- </li>
- <li>ch1factor<br>
- Korrekturfaktor (Wird zum Kanalwert multipliziert.)<br>
- Standard: 1, gültige Werte: Zahl<br><br>
- </li>
- <li>ch1roundDecimal<br>
- Anzahl Dezimalstellen für den Messwert<br>
- Standard: 3, gültige Werte: 0,1,2,3<br><br>
- </li>
- <li><a href="#IODev">IODev</a></li>
- <li><a href="#do_not_notify">do_not_notify</a></li>
- <li><a href="#showtime">showtime</a></li>
- </ul><br>
- </ul>
- =end html_DE
- =cut
|