From a9261a98661ec0e90de5b58b7654278ab49459b6 Mon Sep 17 00:00:00 2001 From: fladdy <> Date: Tue, 1 Jan 2013 10:04:10 +0000 Subject: [PATCH] Bugfix: negative temp and internal re-design git-svn-id: https://svn.fhem.de/fhem/trunk@2400 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/58_GPIO4.pm | 163 +++++++++++++++++++++++++++------------ 1 file changed, 115 insertions(+), 48 deletions(-) diff --git a/fhem/contrib/58_GPIO4.pm b/fhem/contrib/58_GPIO4.pm index 80f4673be..9cc0227ad 100644 --- a/fhem/contrib/58_GPIO4.pm +++ b/fhem/contrib/58_GPIO4.pm @@ -1,18 +1,27 @@ ############################################################################# -# GPIO4.pm written by Peter J. Flathmann # -# Version 0.3, 2012-12-16 # -# # -# define RPi GPIO4 BUSMASTER # -# # -# All devices should be automatically created with model information: # -# =================================================================== # -# define mysSensor GPIO4 28-000004715a10 # -# attr mySensor model DS18B20 # -# # -# Optional attributes: # -# ==================== # -# attr mySensor pollingInterval 60 (default: 60s) # -# # +# +# $Id$ +# +# Copyright notice +# +# (c) 2012 Copyright: Peter J. Flathmann (peter dot flathmann at web dot de) +# All rights reserved +# +# This script free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# The GNU General Public License can be found at +# http://www.gnu.org/copyleft/gpl.html. +# A copy is found in the textfile GPL.txt and important notices to the license +# from the author is found in LICENSE.txt distributed with these scripts. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# ############################################################################# package main; @@ -27,10 +36,20 @@ sub GPIO4_Initialize($) { $hash->{DefFn} = "GPIO4_Define"; $hash->{UndefFn} = "GPIO4_Undef"; + $hash->{NotifyFn} = "GPIO4_Notify"; $hash->{GetFn} = "GPIO4_Get"; $hash->{AttrList} = "tempOffset pollingInterval model loglevel:0,1,2,3,4,5,6"; } +sub GPIO4_Notify($$) { + my ($hash, $dev) = @_; + return if($hash->{DEF} ne "BUSMASTER" || $dev->{NAME} ne "global" || !grep(m/^INITIALIZED$/, @{$dev->{CHANGED}})); + # check bus for new devices not before fhem.cfg completely loaded to avoid duplicates + GPIO4_GetSlaves($hash); + delete $modules{GPIO4}{NotifyFn}; + return; +} + sub GPIO4_Define($$) { my ($hash, $def) = @_; @@ -39,23 +58,18 @@ sub GPIO4_Define($$) { my @a = split("[ \t][ \t]*", $def); return "syntax: define GPIO4 |BUSMASTER" if (int(@a) != 3); $hash->{NAME} = $a[0]; - $hash->{TYPE} = $a[1]; + $hash->{TYPE} = 'GPIO4'; if ($a[2] eq "BUSMASTER") { $hash->{STATE} = "Initialized"; - - # check connected devices after fhem.cfg completely loaded to avoid duplicates (5s) - InternalTimer(gettimeofday()+5, "GPIO4_GetSlaves", $hash, 0); } else { my ($family, $id) = split('-',$a[2]); if ($family eq "28" || $family eq "10") { - # reset failures counter setReadingsVal($hash,'failures',0,TimeNow()); - - # start polling device after fhem.cfg completely loaded to ensure pollingInterval attribute is assigned (5s) - InternalTimer(gettimeofday()+5, "GPIO4_DeviceUpdateLoop", $hash, 0); + $hash->{fhem}{interfaces} = "temperature"; + GPIO4_DeviceUpdateLoop($hash); } else { return "GPIO4: device family $family not supported"; @@ -88,35 +102,17 @@ sub GPIO_GetSlave($$) { return if (exists $defs{$devicename}{DEF} && $defs{$devicename}{DEF} eq $slave); } - # device does not exist, create it - Log 1, "GPIO4: create $slave"; - CommandDefine(undef,"gpio4_$id GPIO4 $slave"); + # device does not exist, let autocreate create it + my $model; if ($family eq "28") { - $attr{"gpio4_$id"}{model} = "DS18B20"; + $model = "DS18B20"; } - if ($family eq "10") { - $attr{"gpio4_$id"}{model} = "DS1820"; + elsif ($family eq "10") { + $model = "DS1820"; } - $attr{"gpio4_$id"}{room} = "GPIO4"; - $defs{"gpio4_$id"}{MASTER} = $hash->{NAME}; + DoTrigger("global", "UNDEFINED GPIO4_${model}_${id} GPIO4 $slave"); + $attr{"GPIO4_${model}_${id}"}{model} = $model; - # create logfile temp4:Temp - my @logfile = split('/',$attr{autocreate}{filelog}); - pop(@logfile); - my $logdir = join('/',@logfile); - CommandDefine(undef,"FileLog_gpio4_$id FileLog $logdir/gpio4_$id-%Y.log gpio4_$id:T:.*"); - $attr{"FileLog_gpio4_$id"}{room} = "GPIO4"; - $attr{"FileLog_gpio4_$id"}{logtype} = "temp4:Temp,text"; - - # create plot - if ($attr{autocreate}{weblink}) { - CommandDefine(undef,"weblink_gpio4_$id weblink fileplot FileLog_gpio4_$id:temp4:CURRENT"); - $attr{"weblink_gpio4_$id"}{label} = '"'."gpio4_$id: ".'Min $data{min1}, Max $data{max1}, Last $data{currval1}"'; - $attr{"weblink_gpio4_$id"}{room} = $attr{autocreate}{weblink_room} || "GPIO4"; - } - - # save fhem.cfg depending on autocreate autosave - CommandSave(undef, undef) if($attr{autocreate}{autosave}); return; } @@ -134,7 +130,7 @@ sub GPIO4_Get($) { Log 6, "GPIO4: GPIO4_Get($hash->{NAME})"; open DATA, "/sys/bus/w1/devices/$hash->{DEF}/w1_slave"; if ( =~ /YES/) { - =~ /t=(\d+)/; + =~ /t=(-?\d+)/; my $temp = $1/1000.0; if ($attr{$hash->{NAME}}{tempOffset}) { $temp+=$attr{$hash->{NAME}}{tempOffset}; @@ -160,3 +156,74 @@ sub GPIO4_Undef($) { } 1; + +=pod +=begin html + + +

GPIO4

+
    + 1-wire temperature sensors connected to Raspberry Pi's GPIO port 4. +

    + + Define +
      + define <name> GPIO4 ('BUSMASTER'|<id>) +

      + Defines the BUSMASTER or one of the slave devices. The BUSMASTER is necessary + to get the slaves automatically created by autocreate.pm. +

      + Examples: +
        + + define RPi GPIO4 BUSMASTER
        + define mysSensor GPIO4 28-000004715a10
        +
        +
      +
    +
    + + Attributes + +
    +
+ +=end html +=begin html_DE + + +

GPIO4

+
    + 1-wire Temperatursensoren an GPIO Port 4 des Raspberry Pi. +

    + + Define +
      + define <name> GPIO4 ('BUSMASTER'|<id>) +

      + Definiert den BUSMASTER oder die Slave-Devices. Der BUSMASTER ist notwendig, + um die Slave-Devices automatisch via autocreate.pm zu erzeugen. +

      + Examples: +
        + + define RPi GPIO4 BUSMASTER
        + define mysSensor GPIO4 28-000004715a10
        +
        +
      +
    +
    + + Attributes + +
    +
+ +=end html_DE +=cut