diff --git a/97_PiXtendV2.pm b/97_PiXtendV2.pm new file mode 100644 index 0000000000000000000000000000000000000000..f8d02b05d00338dc1faec53c43abfeffdc7fdf5e --- /dev/null +++ b/97_PiXtendV2.pm @@ -0,0 +1,2036 @@ +############################################################################## +# $Id$ +# 97_PiXtendV2.pm +# +# Modul to control PiXtendV2 +# +# define <name> PiXtendV2 <optional> +# +############################################################################## +# +# For more information about PiXtend(R) and this program, +# see <https://www.pixtend.de> - <support@pixtend.de> +# +# Copyright (C) 2021 Kontron Electronics GmbH +# Max-Planck-Str. 6 +# 72636 Frickenhausen, Germany +# +# This program is 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 3 of the License, or +# (at your option) any later version. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +#Der Einstieg in die FHEM-Modul Programmierung ist das folgende Tutorial, bzw. die folgende Beschreibung: +#https://wiki.fhem.de/wiki/DevelopmentModuleIntro + +package main; +use strict; #perl pragma um debugen zu erleichtern +use warnings; #perl pragma um debugen zu erleichtern +use Time::HiRes qw(gettimeofday usleep); #Um feiner zu warten, als mit sleep() + +my $modul_ver = "1.3"; #FHEM Modul-Version + + +#Zur Erweiterung für neue Modelle müssen die folgenden beiden Arrays und die beiden Funktionen kopiert und angepasst werden. +#Zudem muss in der Funktion "PiXtendV2_Define" die Abfrage für das Modell eingefügt werden. + +##################################### +# Configuration for Model -S- +##################################### + +#Nach einem Doppelpunkt folgen die Einstellmöglichkeiten, bzw ein Widget (Textfeld, Slider, ...) +#Für alle möglichen Widgets: http://fhem.de/commandref.html#widgetOverride +my @PiXtendV2S_Set = ( + #entspricht CODESYS-Paramtern: + "_JumperSettingAI0:10V,5V", + "_JumperSettingAI1:10V,5V", + "_GPIO0Ctrl:input,output,DHT11,DHT22", + "_GPIO1Ctrl:input,output,DHT11,DHT22", + "_GPIO2Ctrl:input,output,DHT11,DHT22", + "_GPIO3Ctrl:input,output,DHT11,DHT22", + "_GPIOPullupsEnable:no,yes", + "_WatchdogEnable:disabled,125ms,1s,8s", + "_StateLEDDisable:no,yes", + + #entspricht CODESYS-Variablen: + "Reset:noArg", + "SafeState:noArg", + "RetainCopy:off,on", + "RetainEnable:off,on", + "DigitalDebounce01:textField", + "DigitalDebounce23:textField", + "DigitalDebounce45:textField", + "DigitalDebounce67:textField", + "DigitalOut0:on,off,toggle", + "DigitalOut1:on,off,toggle", + "DigitalOut2:on,off,toggle", + "DigitalOut3:on,off,toggle", + "RelayOut0:on,off,toggle", + "RelayOut1:on,off,toggle", + "RelayOut2:on,off,toggle", + "RelayOut3:on,off,toggle", + "GPIOOut0:on,off,toggle", + "GPIOOut1:on,off,toggle", + "GPIOOut2:on,off,toggle", + "GPIOOut3:on,off,toggle", + "GPIODebounce01:textField", + "GPIODebounce23:textField", + "PWM0Ctrl0:textField", + "PWM0Ctrl1:slider,0,1,65535", + "PWM0A:slider,0,1,65535", + "PWM0B:slider,0,1,65535", + "PWM1Ctrl0:textField", + "PWM1Ctrl1:slider,0,1,255", + "PWM1A:slider,0,1,255", + "PWM1B:slider,0,1,255", + "AnalogOut0:slider,0.00,0.01,10.00,1", #...,1 damit floats nicht gerundet werden + "AnalogOut1:slider,0.00,0.01,10.00,1", + "RetainDataOut:textField" +); + +my @PiXtendV2S_Get = ( + "Version:noArg", + "SysState:noArg", + "UCState:noArg", + "UCWarnings:noArg", + "DigitalIn0:noArg", + "DigitalIn1:noArg", + "DigitalIn2:noArg", + "DigitalIn3:noArg", + "DigitalIn4:noArg", + "DigitalIn5:noArg", + "DigitalIn6:noArg", + "DigitalIn7:noArg", + "AnalogIn0:noArg", + "AnalogIn1:noArg", + "GPIOIn0:noArg", + "GPIOIn1:noArg", + "GPIOIn2:noArg", + "GPIOIn3:noArg", + "Sensor0:temperature,humidity", + "Sensor1:temperature,humidity", + "Sensor2:temperature,humidity", + "Sensor3:temperature,humidity", + "RetainDataIn:textField" +); + +sub PiXtendV2_BufferWrite_S($$) { + my ($buffer, $hash) = @_; + + #Der Aufbau "if() {}" wird benötigt, um in Perl Warnungen zu verhindern, dass ein nicht initialisierter Wert verwendet wird, wenn bisher noch nichts an die Stellen im Hash geschrieben wurde + if($hash->{DataOut}{DigitalDebounce01}) {$buffer->[9] = ($hash->{DataOut}{DigitalDebounce01});} + if($hash->{DataOut}{DigitalDebounce23}) {$buffer->[10] = ($hash->{DataOut}{DigitalDebounce23});} + if($hash->{DataOut}{DigitalDebounce45}) {$buffer->[11] = ($hash->{DataOut}{DigitalDebounce45});} + if($hash->{DataOut}{DigitalDebounce67}) {$buffer->[12] = ($hash->{DataOut}{DigitalDebounce67});} + if($hash->{DataOut}{DigitalOut}) {$buffer->[13] = ($hash->{DataOut}{DigitalOut});} + if($hash->{DataOut}{RelayOut}) {$buffer->[14] = ($hash->{DataOut}{RelayOut});} + if($hash->{DataOut}{GPIOCtrl0}) {$buffer->[15] = ($hash->{DataOut}{GPIOCtrl0});} #Falls mehr als 4 GPIOs bereitstehen, ist auch GPIOCtrl1 verfügbar + if($hash->{DataOut}{GPIOOut}) {$buffer->[16] = ($hash->{DataOut}{GPIOOut});} + if($hash->{DataOut}{GPIODebounce01}) {$buffer->[17] = ($hash->{DataOut}{GPIODebounce01});} + if($hash->{DataOut}{GPIODebounce23}) {$buffer->[18] = ($hash->{DataOut}{GPIODebounce23});} + + if($hash->{DataOut}{PWMCtrl00}) {$buffer->[19] = ($hash->{DataOut}{PWMCtrl00});} + if($hash->{DataOut}{PWMCtrl01}) {$buffer->[20] = (($hash->{DataOut}{PWMCtrl01}) & 0xFF); + $buffer->[21] = (($hash->{DataOut}{PWMCtrl01}) >> 8);} + if($hash->{DataOut}{PWM0a}) {$buffer->[22] = (($hash->{DataOut}{PWM0a}) & 0xFF); + $buffer->[23] = (($hash->{DataOut}{PWM0a}) >> 8);} + if($hash->{DataOut}{PWM0b}) {$buffer->[24] = (($hash->{DataOut}{PWM0b}) & 0xFF); + $buffer->[25] = (($hash->{DataOut}{PWM0b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl10}) {$buffer->[26] = ($hash->{DataOut}{PWMCtrl10});} + if($hash->{DataOut}{PWMCtrl11}) {$buffer->[27] = (($hash->{DataOut}{PWMCtrl11}) & 0xFF); + $buffer->[28] = (($hash->{DataOut}{PWMCtrl11}) >> 8);} + if($hash->{DataOut}{PWM1a}) {$buffer->[29] = (($hash->{DataOut}{PWM1a}) & 0xFF); + $buffer->[30] = (($hash->{DataOut}{PWM1a}) >> 8);} + if($hash->{DataOut}{PWM1b}) {$buffer->[31] = (($hash->{DataOut}{PWM1b}) & 0xFF); + $buffer->[32] = (($hash->{DataOut}{PWM1b}) >> 8);} + #Retain beschreiben + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + if($hash->{DataOut}{"RetainDataOut".$i}) {$buffer->[33+$i] = ($hash->{DataOut}{"RetainDataOut".$i});} + } +} + +sub PiXtendV2_BufferRead_S($$) { + my ($buffer, $hash) = @_; + my $str; + + readingsBeginUpdate($hash); #Wird benötigt um das Update der Readings zu starten + + readingsBulkUpdateIfChanged($hash, "Firmware", $buffer->[0], 0); #letzter Parameter: 0-Kein Update in WebGui oder notify, 1- Update + readingsBulkUpdateIfChanged($hash, "Hardware", $buffer->[1], 0); + readingsBulkUpdateIfChanged($hash, "Model", pack("C",$buffer->[2]), 0); + readingsBulkUpdateIfChanged($hash, "UCState", $buffer->[3], 1); + readingsBulkUpdateIfChanged($hash, "UCWarnings", $buffer->[4], 1); + + #DigitalIn + for(my $i=0; $i < 8; $i++){ + if($buffer->[9] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + + #AnalogIn + $str = sprintf("%.2f", (($buffer->[10] | ($buffer->[11] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn0", $str, 1); + $str = sprintf("%.2f", (($buffer->[12] | ($buffer->[13] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn1", $str, 1); + + #GPIOIn + for(my $i=0; $i < 4; $i++){ + if($buffer->[14] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "off", 1);} + } + + #DHTs + for(my $i=0; $i < 4; $i++){ + my $temp = ($buffer->[15+(4*$i)] | ($buffer->[16+(4*$i)] << 8)); + my $humi = ($buffer->[17+(4*$i)] | ($buffer->[18+(4*$i)] << 8)); + if($temp == 65535 && $humi == 65535) + { + $temp = "Sensor not connected"; + $humi = $temp; + } + else + { + if($hash->{DataOut}{"GPIO".$i."DHT"}) #Prüfen, ob Wert definiert + { + if(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht11") + { + $temp = sprintf("%.1f", ($temp/256)); + $humi = sprintf("%.1f", ($humi/256)); + + } + elsif(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht22") + { + if($temp & (1<<15)) + { + $temp &= ~(1<<15); + $temp /= (-10); + } + else + { + $temp /= 10; + } + $temp = sprintf("%.1f", $temp); + $humi = sprintf("%.1f", ($humi/10)); + } + } + else + { + $temp = "Function not enabled"; + $humi = $temp; + } + } + readingsBulkUpdateIfChanged($hash, "Sensor".$i."T", $temp, 1); + readingsBulkUpdateIfChanged($hash, "Sensor".$i."H", $humi, 1); + } + + #Retain lesen + $str = ""; + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + $str .= $buffer->[33+$i]." "; + } + readingsBulkUpdateIfChanged($hash, "RetainDataIn", $str, 1); + + readingsEndUpdate($hash, 1); #Wird benötigt um das Update der Readings zu beenden. Die "1" gibt an, dass alle Ereignisse ausgelöst werden sollen (für "notify") +} + +##################################### +# Configuration for Model -L- +##################################### + +my @PiXtendV2L_Set = ( + #entspricht CODESYS-Paramtern: + "_JumperSettingAI0:10V,5V", + "_JumperSettingAI1:10V,5V", + "_JumperSettingAI2:10V,5V", + "_JumperSettingAI3:10V,5V", + "_GPIO0Ctrl:input,output,DHT11,DHT22", + "_GPIO1Ctrl:input,output,DHT11,DHT22", + "_GPIO2Ctrl:input,output,DHT11,DHT22", + "_GPIO3Ctrl:input,output,DHT11,DHT22", + "_GPIOPullupsEnable:no,yes", + "_WatchdogEnable:disabled,125ms,1s,8s", + "_StateLEDDisable:no,yes", + + #entspricht CODESYS-Variablen: + "Reset:noArg", + "SafeState:noArg", + "RetainCopy:off,on", + "RetainEnable:off,on", + "DigitalDebounce01:textField", + "DigitalDebounce23:textField", + "DigitalDebounce45:textField", + "DigitalDebounce67:textField", + "DigitalDebounce89:textField", + "DigitalDebounce1011:textField", + "DigitalDebounce1213:textField", + "DigitalDebounce1415:textField", + "DigitalOut0:on,off,toggle", + "DigitalOut1:on,off,toggle", + "DigitalOut2:on,off,toggle", + "DigitalOut3:on,off,toggle", + "DigitalOut4:on,off,toggle", + "DigitalOut5:on,off,toggle", + "DigitalOut6:on,off,toggle", + "DigitalOut7:on,off,toggle", + "DigitalOut8:on,off,toggle", + "DigitalOut9:on,off,toggle", + "DigitalOut10:on,off,toggle", + "DigitalOut11:on,off,toggle", + "RelayOut0:on,off,toggle", + "RelayOut1:on,off,toggle", + "RelayOut2:on,off,toggle", + "RelayOut3:on,off,toggle", + "GPIOOut0:on,off,toggle", + "GPIOOut1:on,off,toggle", + "GPIOOut2:on,off,toggle", + "GPIOOut3:on,off,toggle", + "GPIODebounce01:textField", + "GPIODebounce23:textField", + "PWM0Ctrl0:textField", + "PWM0Ctrl1:slider,0,1,65535", + "PWM0A:slider,0,1,65535", + "PWM0B:slider,0,1,65535", + "PWM1Ctrl0:textField", + "PWM1Ctrl1:slider,0,1,65535", + "PWM1A:slider,0,1,65535", + "PWM1B:slider,0,1,65535", + "PWM2Ctrl0:textField", + "PWM2Ctrl1:slider,0,1,65535", + "PWM2A:slider,0,1,65535", + "PWM2B:slider,0,1,65535", + "AnalogOut0:slider,0.00,0.01,10.00,1", #...,1 damit floats nicht gerundet werden + "AnalogOut1:slider,0.00,0.01,10.00,1", + "RetainDataOut:textField" +); + +my @PiXtendV2L_Get = ( + "Version:noArg", + "SysState:noArg", + "UCState:noArg", + "UCWarnings:noArg", + "DigitalIn0:noArg", + "DigitalIn1:noArg", + "DigitalIn2:noArg", + "DigitalIn3:noArg", + "DigitalIn4:noArg", + "DigitalIn5:noArg", + "DigitalIn6:noArg", + "DigitalIn7:noArg", + "DigitalIn8:noArg", + "DigitalIn9:noArg", + "DigitalIn10:noArg", + "DigitalIn11:noArg", + "DigitalIn12:noArg", + "DigitalIn13:noArg", + "DigitalIn14:noArg", + "DigitalIn15:noArg", + "AnalogIn0:noArg", + "AnalogIn1:noArg", + "AnalogIn2:noArg", + "AnalogIn3:noArg", + "AnalogIn4:noArg", + "AnalogIn5:noArg", + "GPIOIn0:noArg", + "GPIOIn1:noArg", + "GPIOIn2:noArg", + "GPIOIn3:noArg", + "Sensor0:temperature,humidity", + "Sensor1:temperature,humidity", + "Sensor2:temperature,humidity", + "Sensor3:temperature,humidity", + "RetainDataIn:textField" +); + +sub PiXtendV2_BufferWrite_L($$) { + my ($buffer, $hash) = @_; + + #Der Aufbau "if() {}" wird benötigt, um in Perl Warnungen zu verhindern, dass ein nicht initialisierter Wert verwendet wird, wenn bisher noch nichts an die Stellen im Hash geschrieben wurde + if($hash->{DataOut}{DigitalDebounce01}) {$buffer->[9] = ($hash->{DataOut}{DigitalDebounce01});} + if($hash->{DataOut}{DigitalDebounce23}) {$buffer->[10] = ($hash->{DataOut}{DigitalDebounce23});} + if($hash->{DataOut}{DigitalDebounce45}) {$buffer->[11] = ($hash->{DataOut}{DigitalDebounce45});} + if($hash->{DataOut}{DigitalDebounce67}) {$buffer->[12] = ($hash->{DataOut}{DigitalDebounce67});} + if($hash->{DataOut}{DigitalDebounce89}) {$buffer->[13] = ($hash->{DataOut}{DigitalDebounce89});} + if($hash->{DataOut}{DigitalDebounce1011}) {$buffer->[14] = ($hash->{DataOut}{DigitalDebounce1011});} + if($hash->{DataOut}{DigitalDebounce1213}) {$buffer->[15] = ($hash->{DataOut}{DigitalDebounce1213});} + if($hash->{DataOut}{DigitalDebounce1415}) {$buffer->[16] = ($hash->{DataOut}{DigitalDebounce1415});} + if($hash->{DataOut}{DigitalOut}) {$buffer->[17] = (($hash->{DataOut}{DigitalOut}) & 0xFF); + $buffer->[18] = (($hash->{DataOut}{DigitalOut}) >> 8);} + if($hash->{DataOut}{RelayOut}) {$buffer->[19] = ($hash->{DataOut}{RelayOut});} + if($hash->{DataOut}{GPIOCtrl0}) {$buffer->[20] = ($hash->{DataOut}{GPIOCtrl0});} #Falls mehr als 4 GPIOs bereitstehen, ist auch GPIOCtrl1 verfügbar + if($hash->{DataOut}{GPIOOut}) {$buffer->[21] = ($hash->{DataOut}{GPIOOut});} + if($hash->{DataOut}{GPIODebounce01}) {$buffer->[22] = ($hash->{DataOut}{GPIODebounce01});} + if($hash->{DataOut}{GPIODebounce23}) {$buffer->[23] = ($hash->{DataOut}{GPIODebounce23});} + + if($hash->{DataOut}{PWMCtrl00}) {$buffer->[24] = ($hash->{DataOut}{PWMCtrl00});} + if($hash->{DataOut}{PWMCtrl01}) {$buffer->[25] = (($hash->{DataOut}{PWMCtrl01}) & 0xFF); + $buffer->[26] = (($hash->{DataOut}{PWMCtrl01}) >> 8);} + if($hash->{DataOut}{PWM0a}) {$buffer->[27] = (($hash->{DataOut}{PWM0a}) & 0xFF); + $buffer->[28] = (($hash->{DataOut}{PWM0a}) >> 8);} + if($hash->{DataOut}{PWM0b}) {$buffer->[29] = (($hash->{DataOut}{PWM0b}) & 0xFF); + $buffer->[30] = (($hash->{DataOut}{PWM0b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl10}) {$buffer->[31] = ($hash->{DataOut}{PWMCtrl10});} + if($hash->{DataOut}{PWMCtrl11}) {$buffer->[32] = (($hash->{DataOut}{PWMCtrl11}) & 0xFF); + $buffer->[33] = (($hash->{DataOut}{PWMCtrl11}) >> 8);} + if($hash->{DataOut}{PWM1a}) {$buffer->[34] = (($hash->{DataOut}{PWM1a}) & 0xFF); + $buffer->[35] = (($hash->{DataOut}{PWM1a}) >> 8);} + if($hash->{DataOut}{PWM1b}) {$buffer->[36] = (($hash->{DataOut}{PWM1b}) & 0xFF); + $buffer->[37] = (($hash->{DataOut}{PWM1b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl20}) {$buffer->[38] = ($hash->{DataOut}{PWMCtrl20});} + if($hash->{DataOut}{PWMCtrl21}) {$buffer->[39] = (($hash->{DataOut}{PWMCtrl21}) & 0xFF); + $buffer->[40] = (($hash->{DataOut}{PWMCtrl21}) >> 8);} + if($hash->{DataOut}{PWM2a}) {$buffer->[41] = (($hash->{DataOut}{PWM2a}) & 0xFF); + $buffer->[42] = (($hash->{DataOut}{PWM2a}) >> 8);} + if($hash->{DataOut}{PWM2b}) {$buffer->[43] = (($hash->{DataOut}{PWM2b}) & 0xFF); + $buffer->[44] = (($hash->{DataOut}{PWM2b}) >> 8);} + #Retain beschreiben + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + if($hash->{DataOut}{"RetainDataOut".$i}) {$buffer->[45+$i] = ($hash->{DataOut}{"RetainDataOut".$i});} + } +} + +sub PiXtendV2_BufferRead_L($$) { + my ($buffer, $hash) = @_; + my $str; + + readingsBeginUpdate($hash); #Wird benötigt um das Update der Readings zu starten + + readingsBulkUpdateIfChanged($hash, "Firmware", $buffer->[0], 0); #letzter Parameter: 0-Kein Update in WebGui oder notify, 1- Update + readingsBulkUpdateIfChanged($hash, "Hardware", $buffer->[1], 0); + readingsBulkUpdateIfChanged($hash, "Model", pack("C",$buffer->[2]), 0); + readingsBulkUpdateIfChanged($hash, "UCState", $buffer->[3], 1); + readingsBulkUpdateIfChanged($hash, "UCWarnings", $buffer->[4], 1); + + #DigitalIn + for(my $i=0; $i < 8; $i++){ + if($buffer->[9] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + for(my $i=8; $i < 16; $i++){ + if($buffer->[10] & (1<<($i-8))) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + + #AnalogIn + $str = sprintf("%.2f", (($buffer->[11] | ($buffer->[12] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn0", $str, 1); + $str = sprintf("%.2f", (($buffer->[13] | ($buffer->[14] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn1", $str, 1); + + $str = sprintf("%.2f", (($buffer->[15] | ($buffer->[16] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn2", $str, 1); + $str = sprintf("%.2f", (($buffer->[17] | ($buffer->[18] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn3", $str, 1); + + $str = sprintf("%.2f", (($buffer->[19] | ($buffer->[20] << 8))*0.02015840023)); + readingsBulkUpdateIfChanged($hash, "AnalogIn4", $str, 1); + $str = sprintf("%.2f", (($buffer->[21] | ($buffer->[22] << 8))*0.02015840023)); + readingsBulkUpdateIfChanged($hash, "AnalogIn5", $str, 1); + + #GPIOIn + for(my $i=0; $i < 4; $i++){ + if($buffer->[23] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "off", 1);} + } + + #DHTs + for(my $i=0; $i < 4; $i++){ + my $temp = ($buffer->[24+(4*$i)] | ($buffer->[25+(4*$i)] << 8)); + my $humi = ($buffer->[26+(4*$i)] | ($buffer->[27+(4*$i)] << 8)); + if($temp == 65535 && $humi == 65535) + { + $temp = "Sensor not connected"; + $humi = $temp; + } + else + { + if($hash->{DataOut}{"GPIO".$i."DHT"}) #Prüfen, ob Wert definiert + { + if(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht11") + { + $temp = sprintf("%.1f", ($temp/256)); + $humi = sprintf("%.1f", ($humi/256)); + + } + elsif(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht22") + { + if($temp & (1<<15)) + { + $temp &= ~(1<<15); + $temp /= (-10); + } + else + { + $temp /= 10; + } + $temp = sprintf("%.1f", $temp); + $humi = sprintf("%.1f", ($humi/10)); + } + } + else + { + $temp = "Function not enabled"; + $humi = $temp; + } + } + readingsBulkUpdateIfChanged($hash, "Sensor".$i."T", $temp, 1); + readingsBulkUpdateIfChanged($hash, "Sensor".$i."H", $humi, 1); + } + + #Retain lesen + $str = ""; + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + $str .= $buffer->[45+$i]." "; + } + readingsBulkUpdateIfChanged($hash, "RetainDataIn", $str, 1); + + readingsEndUpdate($hash, 1); #Wird benötigt um das Update der Readings zu beenden. Die "1" gibt an, dass alle Ereignisse ausgelöst werden sollen (für "notify") +} + + + +##################################### +# General Functions +##################################### + +##################################### +sub PiXtendV2_Initialize($) { #Die Initialize-Subroutine muss auf jeden Fall in einem Modul definiert sein. Der Suffix entspricht dabei dem Datei-Namen + my ($hash) = @_; #my = variable + + $hash->{DefFn} = 'PiXtendV2_Define'; #Bekanntmachung der anderen Funktionen des Moduls. Die Keys {DefFn} sind von FHEM vorgegeben + $hash->{UndefFn} = 'PiXtendV2_Undef'; + $hash->{SetFn} = 'PiXtendV2_Set'; + $hash->{GetFn} = 'PiXtendV2_Get'; + #$hash->{DeleteFn} = 'PiXtendV2_Undef'; + $hash->{AttrFn} = 'PiXtendV2_Attr'; + #$hash->{ReadFn} = 'PiXtendV2_Read'; + #$hash->{Shutdown} = 'PiXtendV2_Undef'; + + $hash->{AttrList} = "PiXtend_GetFormat:text,value PiXtend_Parameter ".$readingFnAttributes; #Eigene Attribute + Readings Attribute +} + +##################################### +sub PiXtendV2_Define($$) { + my ($hash, $def) = @_; #@_ ist ein "Sonderzeichen" und entspricht den Funktionsparametern. Parameter sind immer der lokale Hash und alle weiteren, die bei define angegeben wurden. + my @param = split("[ \t][ \t]*", $def); #Teilt die weiteren Paramter auf und speichert sie in einem Array (@param). [0] = name, [1] = PiXtendV2, [2] = optional + + if(scalar(@param) < 2 or scalar(@param) > 3) #2 oder 3 Define-Parameter erlauben + { + return "Wrong syntax: use define <name> PiXtendV2 <optional>"; + } + + eval{ require "sys/ioctl.ph" }; #Prüfen, ob ioctl.ph existiert. + if($@){ return "ioctl.ph is not available but needed"; } #Fehler zurückgeben, falls es Probleme gab. + + if(scalar(@param) eq 2 or $param[2] eq "S") #Default oder Model S + { + $hash->{Set} = \@PiXtendV2S_Set; #Refrenez(\) auf das Array. Referenzen werden wohl nicht in der Oberfläche angezeigt, da normalerweiße alle Elemente der ersten Hash-Ebene dort angezeigt werden + $hash->{Get} = \@PiXtendV2S_Get; + $hash->{SPI}{Write} = \&PiXtendV2_BufferWrite_S; #Referenz auf die Funktionen um den Puffer zu füllen und zu leeren + $hash->{SPI}{Read} = \&PiXtendV2_BufferRead_S; + $hash->{SPI}{Length} = 67; #Anzahl Bytes + $hash->{SPI}{Speed} = 1100000; #Frequenz, entspricht tatsächlich aber nur ~62,5% davon. + $hash->{SPI}{Dev0} = "/dev/spidev0.0"; #Kommunikation mit dem PiXtend + $hash->{SPI}{Dev1} = "/dev/spidev0.1"; #Kommunikation mit dem DAC + $hash->{SPI}{Enable} = "/sys/class/gpio/"; #Grundpfad zu den GPIOs + $hash->{SPI}{Model} = 83; #Hardware-Modell + $hash->{RetainSize} = 32; #Anzahl der Bytes des Retain-Speichers + + #Default-Werte setzen (hier nur die Jumper-Stellungen) + $hash->{DataOut}{JumperAI0} = 10; + $hash->{DataOut}{JumperAI1} = 10; + } + elsif($param[2] eq "L") #Model L + { + $hash->{Set} = \@PiXtendV2L_Set; #Refrenez(\) auf das Array. Referenzen werden wohl nicht in der Oberfläche angezeigt, da normalerweiße alle Elemente der ersten Hash-Ebene dort angezeigt werden + $hash->{Get} = \@PiXtendV2L_Get; + $hash->{SPI}{Write} = \&PiXtendV2_BufferWrite_L; #Referenz auf die Funktionen um den Puffer zu füllen und zu leeren + $hash->{SPI}{Read} = \&PiXtendV2_BufferRead_L; + $hash->{SPI}{Length} = 111; #Anzahl Bytes + $hash->{SPI}{Speed} = 1100000; #Frequenz, entspricht tatsächlich aber nur ~62,5% davon. + $hash->{SPI}{Dev0} = "/dev/spidev0.0"; #Kommunikation mit dem PiXtend + $hash->{SPI}{Dev1} = "/dev/spidev0.1"; #Kommunikation mit dem DAC + $hash->{SPI}{Enable} = "/sys/class/gpio/"; #Grundpfad zu den GPIOs + $hash->{SPI}{Model} = 76; #Hardware-Modell + $hash->{RetainSize} = 64; #Anzahl der Bytes des Retain-Speichers + + #Default-Werte setzen (hier nur die Jumper-Stellungen) + $hash->{DataOut}{JumperAI0} = 10; + $hash->{DataOut}{JumperAI1} = 10; + $hash->{DataOut}{JumperAI2} = 10; + $hash->{DataOut}{JumperAI3} = 10; + + $hash->{DataIn}{UnitAI4} = "mA"; #Wenn die Einheit nicht explizit angegeben ist (Eingänge 0-3) wird der Wert in Volt ausgegeben + $hash->{DataIn}{UnitAI5} = "mA"; + } + #elsif($param[2] eq "M") #Model M + #{ + #} + else + { + return "Parameter <$param[2]> not supported"; + } + + my $ret = undef; + $ret = PiXtendV2_Check_Device($hash->{SPI}{Dev0}); #Prüfen, ob User "fhem" in der Gruppe "spi" ist + if($ret){ return "$ret"; } + $ret = PiXtendV2_Check_Device($hash->{SPI}{Enable}."export"); #Prüfen, ob User "fhem" in der Gruppe "gpio" ist + if($ret){ return "$ret"; } + + $hash->{STATE} = "defined"; #Status aktualisieren + PiXtendV2_SPI_Transfer($hash); #SPI Transfer starten + + $attr{$hash->{NAME}}{icon} = 'RPi'; + $attr{$hash->{NAME}}{devStateIcon} = 'defined:rc_YELLOW active:rc_GREEN error:rc_RED'; + + return undef; +} + +##################################### +sub PiXtendV2_Undef($$) { + my ($hash, $arg) = @_; + + RemoveInternalTimer($hash, "PiXtendV2_SPI_Transfer"); #Zyklischen Timer stoppen + PiXtendV2_SPI_Disable($hash); #SPI-Enable Pin auf low + PiXtendV2_UC_Reset($hash); #Mikrocontroller zurücksetzen + + return undef; +} + +##################################### +sub PiXtendV2_Attr ($$$$) +{ + my ( $cmd, $name, $attrName, $attrValue ) = @_; + my $hash = $defs{$name}; + + if($cmd eq "set") + { + #GetFormat + if($attrName eq "PiXtend_GetFormat") + { + if($attrValue eq "text" or $attrValue eq "value") + { + } + else + { + return "Unknown value $attrValue for $attrName, choose one of: text, value"; + } + } + + #Parameter + if($attrName eq "PiXtend_Parameter") + { + my @coms = split(/ /, $attrValue); + for(my $i=0; $i < @coms; $i++) + { + my @param = split(/:/, $coms[$i]); + if(index($param[0], "_") != -1) + { + my $ret = PiXtendV2_Set($hash, $name, $param[0], $param[1]); + if(defined($ret)) + { + return $ret; + } + } + else + { + return "Unknown command $param[0]. Only set commands with a leading '_' sign can be used."; + } + } + } + } + return undef; +} + +##################################### +sub PiXtendV2_Get($$@) { + my ($hash, $name, $cmd, @param) = @_; + + my @InList = @{$hash->{Get}}; #kopiert die OutputListe lokal + foreach my $c (@InList) #für jeden string c in inlist + { + if(index($c, ":") != -1) #falls : vorkommt + { + $c = substr($c,0,index($c,":")); #den substring nur bis dahin nehmen + } + $c = lc($c); #zu lower case machen + } + $cmd = lc($cmd); #verändert usereingabe auch zu lower case: + if(grep($_ eq $cmd, @InList) > 0) #Prüft ob Array "outList" den Wert von "opt" enthält; auf Lower-Case-Ebene => case insensitive! || experimentelle Alternative (schlecht!): ($opt ~~ @InList) + { + my $str = undef; + my $val = undef; + + #Version + if($cmd eq "version") + { + $val = ReadingsVal($name, "Model", "?")."-".ReadingsVal($name, "Hardware", "?")."-".ReadingsVal($name, "Firmware", "?"); + $str = "FHEM-Modul-Version [$modul_ver], PiXtend-Version [$val]"; + } + + #SysState + elsif($cmd eq "sysstate") + { + $val = $hash->{STATE}; + $str = "System state is [$val]"; + } + + #UCState + elsif($cmd eq "ucstate") + { + $val = ReadingsVal($name, "UCState", "?"); + $str = "Microcontroller state is [$val]"; + } + + #UCWarnings + elsif($cmd eq "ucwarnings") + { + $val = ReadingsVal($name, "UCWarnings", "?"); + $str = "Microcontroller warnings are [$val]"; + } + + #DigitalIn + elsif(index($cmd, "digitalin") != -1) + { + $cmd =~ s/digitalin//; + $val = ReadingsVal($name, "DigitalIn$cmd", undef); + if(defined($val)) + { + $str = "DigitalIn$cmd is [$val]"; + } + } + + #AnalogIn + elsif(index($cmd, "analogin") != -1) + { + $cmd =~ s/analogin//; + $val = ReadingsVal($name, "AnalogIn$cmd", undef); + if(defined($val)) + { + if($hash->{DataIn}{"UnitAI$cmd"}) + { + my $unit = $hash->{DataIn}{"UnitAI$cmd"}; + $str = "AnalogIn$cmd is [$val] $unit"; + } + else + { + $str = "AnalogIn$cmd is [$val] V"; + } + } + } + + #GPIOIn + elsif(index($cmd, "gpioin") != -1) + { + $cmd =~ s/gpioin//; + $val = ReadingsVal($name, "GPIOIn$cmd", undef); + if(defined($val)) + { + $str = "GPIOIn$cmd is [$val]"; + } + } + + #DHTs + elsif(index($cmd, "sensor") != -1) + { + $cmd =~ s/sensor//; + + if(lc($param[0]) eq "temperature") + { + $val = ReadingsVal($name, "Sensor".$cmd."T", undef); + if(defined($val) && index($val, "not") == -1) + { + $str = "For Sensor$cmd temperature is [$val] °C"; + } + elsif(index($val, "not") != -1) + { + $str = "$val for Sensor$cmd"; + } + } + elsif(lc($param[0]) eq "humidity") + { + $val = ReadingsVal($name, "Sensor".$cmd."H", undef); + if(defined($val) && index($val, "not") == -1) + { + $str = "For Sensor$cmd humidity is [$val] %"; + } + elsif(index($val, "not") != -1) + { + $str = "$val for Sensor$cmd"; + } + } + else + { + $val = "Unknown value $param[0] for Sensorx, choose one of: temperature, humidity"; + $str = $val; + } + } + + #Retain + elsif($cmd eq "retaindatain") + { + $str = ReadingsVal($name, "RetainDataIn", undef); + if(defined($str)) + { + if(int($param[0]) >= 0 && int($param[0]) < ($hash->{RetainSize})) + { + my @vals = split(/ /, $str); + $val = $vals[$param[0]]; + $str = "Value for RetainDataIn$param[0] is [$val]"; + } + else + { + $val = "Unknown Index $param[0] for RetainDataIn, index must be between 0 and ($hash->{RetainSize}-1)"; + $str = $val; + } + } + } + + if (defined($str) && defined($val)) + { + if(AttrVal($name, "PiXtend_GetFormat", "?") eq "value") + { + return $val; + } + else + { + return $str; + } + } + } + else + { + return "Unknown argument $cmd, choose one of ". join(" ", @{$hash->{Get}}); # eine solche zeile muss enthalten sein, da fhem hier die möglichen werte raus liest + } + return undef; +} + +##################################### +sub PiXtendV2_Set($$@){ + my ($hash, $name, $cmd, @param) = @_; + + my @outList = @{$hash->{Set}}; #kopiert die OutputListe lokal + foreach my $c (@outList) #für jeden string c in outlist + { + if(index($c, ":") != -1) #falls ":" vorkommt + { + $c = substr($c,0,index($c,":")); #den substring nur bis dahin nehmen + } + $c = lc($c); #zu lower case machen + } + $cmd = lc($cmd); #verändert usereingabe auch zu lower case + + if(grep($_ eq $cmd, @outList) > 0) #Prüft ob Array "outList" den Wert von "cmd" enthält; auf Lower-Case-Ebene => case insensitive! || experimentelle Alternative (schlecht!): ($cmd ~~ @InList) + { + #Reset + if(index($cmd, "reset") != -1) + { + PiXtendV2_UC_Reset($hash); + } + + #Parameter + ############### + elsif(index($cmd, "_jumpersettingai") != -1) #Prüft ob $cmd "_jumpersettingai" als substring hat + { + $cmd =~ s/_jumpersettingai//; #löscht den substring _jumpersettingai + if(lc($param[0]) eq "10v") + { + ($hash->{DataOut}{"JumperAI".$cmd}) = 10; + } + elsif(lc($param[0]) eq "5v") + { + ($hash->{DataOut}{"JumperAI".$cmd}) = 5; + } + else + { + return "Unknown value $param[0] for JumperSetting, choose one of: 10V, 5V"; + } + } + + elsif(index($cmd, "_gpio") != -1 and index($cmd, "ctrl") != -1) + { + $cmd =~ s/_gpio//; + $cmd =~ s/ctrl//; + my $no = 0; + if($cmd < 4){$no = 0;} + elsif($cmd < 8){$no = 1; $cmd -= 4;} + else{ return "Unknown number for GPIOxCtrl";} + + if(lc($param[0]) eq "input") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~ (1<<($cmd+4)); #DHT auf 0 + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~(1<<$cmd); #Eingang (clear) + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = undef; #DHT-Modus zurücksetzen + } + elsif(lc($param[0]) eq "output") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~ (1<<($cmd+4)); #DHT auf 0 + ($hash->{DataOut}{"GPIOCtrl".$no}) |= (1<<$cmd); #Ausgang (set) + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = undef; #DHT-Modus zurücksetzen + } + elsif(lc($param[0]) eq "dht11" or lc($param[0]) eq "dht22") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) |= (1<<($cmd+4)); #DHT auf 1 + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = lc($param[0]); #DHT-Modus für den Pin speichern + } + else + { + return "Unknown value $param[0] for GPIOxCtrl, choose one of: input, output, DHT11, DHT22"; + } + } + + elsif(index($cmd, "_gpiopullupsenable") != -1) + { + if(lc($param[0]) eq "no") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<4); + } + elsif(lc($param[0]) eq "yes") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<4); + } + else + { + return "Unknown value $param[0] for GPIOPullupsEnable, choose one of: no, yes"; + } + } + + elsif(index($cmd, "_watchdogenable") != -1) + { + if(lc($param[0]) eq "disabled") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 0; + } + elsif(lc($param[0]) eq "125ms") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 4; + } + elsif(lc($param[0]) eq "1s") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 7; + } + elsif(lc($param[0]) eq "8s") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 10; + } + else + { + return "Unknown value $param[0] for WatchdogEnable, choose one of: disabled, 125ms, 1s, 8s"; + } + } + + elsif(index($cmd, "_stateleddisable") != -1) + { + if(lc($param[0]) eq "no") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<3); + } + elsif(lc($param[0]) eq "yes") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<3); + } + else + { + return "Unknown value $param[0] for StateLEDDisable, choose one of: no, yes"; + } + } + + elsif(index($cmd, "safestate") != -1) + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<0); #wird beim SPI-Transfer automatisch zurückgesetzt! + } + + elsif(index($cmd, "retaincopy") != -1) + { + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<1); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<1); + } + else + { + return "Unknown value $param[0] for RetainCopy, choose one of: off, on"; + } + } + + elsif(index($cmd, "retainenable") != -1) + { + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<2); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<2); + } + else + { + return "Unknown value $param[0] for RetainEnable, choose one of: off, on"; + } + } + + ############### + #RelayOut + elsif(index($cmd, "relayout") != -1) #Prüft ob $cmd "relayout" als substring hat + { + $cmd =~ s/relayout//; #löscht den substring relayout + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{RelayOut}) &= ~(1<<$cmd); #Bitweise Verknüpfung => löschen + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{RelayOut}) |= (1<<$cmd); #setzen + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{RelayOut}) ^= (1<<$cmd); #toggeln + } + else + { + return "Unknown value $param[0] for RelayOut, choose one of: on, off, toggle"; + } + } + + #DigitalOut + elsif(index($cmd, "digitalout") != -1) + { + $cmd =~ s/digitalout//; + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{DigitalOut}) &= ~(1<<$cmd); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{DigitalOut}) |= (1<<$cmd); + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{DigitalOut}) ^= (1<<$cmd); + } + else + { + return "Unknown value $param[0] for DigitalOutput, choose one of: on, off, toggle"; + } + } + + #DigitalDebounce + elsif(index($cmd, "digitaldebounce") != -1) + { + $cmd =~ s/digitaldebounce//; + $hash->{DataOut}{"DigitalDebounce".$cmd} = PiXtendV2_Check_Range(0,255,$param[0]); + } + + #GPIODebounce + elsif(index($cmd, "gpiodebounce") != -1) + { + $cmd =~ s/gpiodebounce//; + $hash->{DataOut}{"GPIODebounce".$cmd} = PiXtendV2_Check_Range(0,255,$param[0]); + } + + #GPIOOut + elsif(index($cmd, "gpioout") != -1) + { + $cmd =~ s/gpioout//; + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{GPIOOut}) &= ~(1<<$cmd); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{GPIOOut}) |= (1<<$cmd); + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{GPIOOut}) ^= (1<<$cmd); + } + else + { + return "Unknown value $param[0] for GPIOOut, choose one of: on, off, toggle"; + } + } + + #AnalogOut + elsif(index($cmd, "analogout") != -1) + { + $cmd =~ s/analogout//; + if(index($param[0], ".") != -1) #Abfrage ob float (Spannung) oder int (RAW) + { + if($param[0] >= 0.00 && $param[0] <= 10.00) + { + PiXtendV2_DAC_Transfer($hash, ($param[0]*1023/10), $cmd); + } + else + { + return "Unknown value $param[0] for AnalogOut, choose a voltage between 0.00 and 10.00"; + } + } + else #Eingabe als RAW-Wert + { + if(int($param[0]) >= 0 && int($param[0]) <= 1023) + { + PiXtendV2_DAC_Transfer($hash, $param[0], $cmd); + } + else + { + return "Unknown value $param[0] for AnalogOut, choose a number between 0 and 1023"; + } + } + } + + #PWM + elsif(index($cmd, "pwm") != -1) + { + $cmd =~ s/pwm//; + if(index($cmd, "ctrl") != -1) + { + $cmd =~ s/ctrl//; + $hash->{DataOut}{"PWMCtrl".$cmd} = PiXtendV2_Check_Range(0,65535,$param[0]); + } + elsif(index($cmd, "a") != -1 or index($cmd, "b") != -1) + { + $hash->{DataOut}{"PWM".$cmd} = PiXtendV2_Check_Range(0,65535,$param[0]); + } + else + { + return "Unknown command for PWM"; + } + } + + #Retain + elsif(index($cmd, "retaindataout") != -1) + { + if(@param == 2) #Falls genau zwei Parameter übergeben wurden + { + if(int($param[0]) < ($hash->{RetainSize})) + { + if(int($param[1]) >= 0 && int($param[1]) <= 255) + { + $hash->{DataOut}{"RetainDataOut".$param[0]} = $param[1]; + } + else + { + return "Unknown value $param[1] for RetainDataOut, choose a number between 0 and 255"; + } + } + else + { + return "Unknown Index $param[0] for RetainDataOut, index must be between 0 and ($hash->{RetainSize}-1)"; + } + } + else + { + return "Too few parameters for RetainDataOut, 2 are expected"; + } + } + } + else + { + return "Unknown argument $cmd, choose one of ". join(" ", @{$hash->{Set}}); #eine Zeile dieser Art muss enthalten sein, da FHEM hier die möglichen Werte für die Oberfläche herausliest + } + return undef; +} + + +##################################### +sub PiXtendV2_Check_Range ($$$) { + my ($min, $max, $val) = @_; + if($val < $min){return $min;} + if($val > $max){return $max;} + else{return $val;} +} + +##################################### +sub PiXtendV2_Check_Device ($) { #Überprüft, ob das Gerät exisitiert und ob darauf zugegriffen werden kann. + my ($dev) = @_; + my $ret = undef; + if(-e $dev) { + if(-r $dev) { + unless(-w $dev) { + $ret = ': Error! Device not writable: '.$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot).'; + } + } else { + $ret = ': Error! Device not readable: '.$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot).'; + } + } else { + $ret = ': Error! Device not found: ' .$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot) or check if kernelmodules must be loaded.'; + } + return $ret; +} + +##################################### +sub PiXtendV2_SPI_GetIOCMD ($) { #entspricht SPI_IOC_MESsAGE(N) + my $arg = (1 << 30); #Modus des Kommandos: 1-Schreiben, 2-Lesen + $arg |= (($_[0]*32) << 16); #Legt die Größe der zu übertragenden Daten fest. Ein "struct spi_io_transfer" entspricht 32 Bytes + $arg |= (107 << 8); #Kommando-ID. Für SPI "k" (107). + return $arg; +} + +##################################### +sub PiXtendV2_calc_crc16($$$){ #entspricht dem Ablauf der CRC-Berechnung aus der Mikrocontroller-Bibliothek + my ($beg, $end, $buff) = @_; + my $crc = 0xFFFF; #Startwert der CRC-Berechnung + for(my $i=$beg; $i <= $end; $i++){ + $crc ^= $$buff[$i]; #zusätzliches "$" ist zum Dereferenzieren + for(my $k=0; $k < 8; ++$k) + { + if($crc & 1) + { + $crc = ($crc >> 1) ^ 0xA001; + } + else + { + $crc = ($crc >> 1); + } + } + } + return $crc; +} + +##################################### +sub PiXtendV2_DAC_Transfer ($) { + my ($hash, $val, $chn) = @_; + $val = ($val<<2); #Wert verschieben + $val |= (1<<12); #SHDN deaktivieren + $val |= ($chn<<15); #Kanal auswählen + $val = pack("n", $val); #Als big Endian unsigned short (16Bit) packen + my $freq = $hash->{SPI}{Speed}; + $freq = pack("L", $freq); #Frequenz als Long (32Bit) packen + my $device = $hash->{SPI}{Dev1}; + my $handler = undef; + my $ret = sysopen($handler, $device, O_RDWR); + if($ret == 1){ + ioctl($handler, 0x40046b04, $freq); #Übertragungsfrequenz einstellen + syswrite($handler, $val); #Daten schreiben + close($handler); + return undef; + } + else + { + return "error"; #Error File Handling + } +} + +##################################### +sub PiXtendV2_SPI_Transfer ($) { + my ($hash) = @_; + my $len = $hash->{SPI}{Length}; + my $freq = $hash->{SPI}{Speed}; + my $device = $hash->{SPI}{Dev0}; + my $handler = undef; + my $ret = sysopen($handler, $device, O_RDWR); + if($ret == 1){ + PiXtendV2_SPI_Enable($hash); + my @buffer = (0) x $len; #Puffer mit 0 initialisieren + $buffer[0] = ($hash->{SPI}{Model}); #Setzt den ersten Wert des Puffers auf die Model-Nummer + if($hash->{DataOut}{UCCtrl0}){$buffer[2] = ($hash->{DataOut}{UCCtrl0});} + if($hash->{DataOut}{UCCtrl1}){$buffer[3] = ($hash->{DataOut}{UCCtrl1});} + ($buffer[7], $buffer[8]) = unpack("C*", pack("S", PiXtendV2_calc_crc16(0,6, \@buffer))); #CRC-HeaderOut berechnen + + #Puffer füllen + $hash->{SPI}{Write}->(\@buffer, $hash); #Aufruf der Funktion, um den Puffer "buffer" zu füllen. Der Hash enthält an der Stelle SPI-Write eine Referenz auf die auszuführende Funktion. Die Werte in Klammern () sind die Funktionsparameter + ($buffer[$len-2], $buffer[$len-1]) = unpack("C*", pack("S", PiXtendV2_calc_crc16(9,($len-3), \@buffer))); #CRC-DataOut berechnen + if($hash->{DataOut}{UCCtrl1}){($hash->{DataOut}{UCCtrl1}) &= ~(1<<0);} #SafeState-Bit zurücksetzen, damit der Mikrocontroller nach einem Reset nicht wieder sofort in den SafeState übergeht + + #Zusammenbau der "struct spi_ioc_transfer". Vgl. Wissensdatenbank. + my $struct; + for(my $i=0; $i < $len; $i++){ + $buffer[$i] = pack("Q", $buffer[$i]); #Wandeln der Daten in das entsprechende Format + my $ptr = unpack("L", pack("P", $buffer[$i])); #Pointer muss L (32-Bit) sein, da ein 32Bit Betriebssystem vorliegt + if($i == $len-1) + { + $struct .= pack("QQLLSCCL", $ptr, $ptr, 1, $freq, 10, 8, 0, 0); #10µs CS-Delay nach dem letzten übertragenen Byte + } + else + { + $struct .= pack("QQLLSCCL", $ptr, $ptr, 1, $freq, 0, 8, 0, 0); + } + } + + my $iocmd = PiXtendV2_SPI_GetIOCMD($len); #IOCTL-Kommando für die Übertragung berechnen + ioctl($handler, $iocmd, $struct); #Kommando ausführen. $struct wird intern als Pointer übergeben + for(my $i=0; $i < $len; $i++) {$buffer[$i] = unpack("Q", $buffer[$i]);} #Umwandeln der empfangenen Daten, damit in FHEM der einfache Zugriff möglich ist + + #Überprüfen der empfangenen Daten + if(unpack("S", pack("C2", $buffer[7], $buffer[8])) eq PiXtendV2_calc_crc16(0,6, \@buffer) and #CRC HeaderIn stimmt + unpack("S", pack("C2", $buffer[$len-2], $buffer[$len-1])) eq PiXtendV2_calc_crc16(9,($len-3), \@buffer)) #CRC DataIn stimmt + { + if($buffer[2] eq $hash->{SPI}{Model}) #Model stimmt überein + { + $hash->{SPI}{Read}->(\@buffer, $hash); #Aufruf der Funktion, um den Puffer "buffer" zu lesen. Funktion ist als Referenz im Hash gespeichert. Parameter werden in Klammern () übergeben + $hash->{STATE} = "active"; + $hash->{ErrorMsg} = "-"; + } + else + { + $hash->{STATE} = "error"; #Error Model-Abgleich + $hash->{ErrorMsg} = "Wrong model selected"; + } + } + else + { + $hash->{STATE} = "error"; #Error CRC-Abgleich + $hash->{ErrorMsg} = "SPI-Transfer error"; + } + } + else + { + $hash->{STATE} = "error"; #Error File Handling + $hash->{ErrorMsg} = "Couldn't open SPI device"; + } + + InternalTimer(gettimeofday()+0.1, "PiXtendV2_SPI_Transfer", $hash); #Timer neustarten, um 0,1 Sekunde + return undef; +} + +##################################### +sub PiXtendV2_SPI_Enable($){ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."export", O_WRONLY); + syswrite($handler, 24); + + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio24/direction", O_WRONLY); + syswrite($handler, "out", 3); + + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio24/value", O_WRONLY); + syswrite($handler, "1", 1); + close($handler); + return 1; +} + +##################################### +sub PiXtendV2_SPI_Disable(){ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."unexport", O_WRONLY); + syswrite($handler, 24); + close($handler); + return 1; +} + +##################################### +sub PiXtendV2_UC_Reset() +{ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."export", O_WRONLY); + syswrite($handler, 23); + #close($handler); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/direction", O_WRONLY); + syswrite($handler, "out", 3); + #close($handler); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/value", O_WRONLY); + syswrite($handler, "1", 1); + #close($handler); + usleep(1000); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/value", O_WRONLY); + syswrite($handler, "0", 1); + close($handler); + return 1; +} + + + +1; + +=pod +=item device +=item summary Allows to access the PiXtendV2 (PLC). +=item summary_DE Ermöglicht den Zugriff auf den PiXtendV2 (SPS) +=begin html + +<a name="PiXtendV2"></a> +<h3>PiXtendV2</h3> +<ul> + + PiXtend is a programmable logic controller (PLC) based on the Raspberry Pi. + This FHEM-module allows to access the functions of the PiXtendV2-Board through the FHEM interface. + PiXtend offers a variety of digital and analog inputs and outputs which are designed for industry standards + and safe connections and thus is ideally suited for home automation. + For more information about PiXtend(R) and this FHEM-module, see + <a href="https://www.pixtend.de" target="_blank">www.pixtend.de</a>. + + <br><br> + + + <a name="PiXtendV2Define"></a> + <b>Define</b> + <ul> + <code>define <name> PiXtendV2 <optional></code> + <br><br> + The parameter "optional" is used to determine the hardware model, e.g. S or L. If the parameter is not used a FHEM device for model S is created automatically. + <br><br> + Example: + <ul> + <code>define pix PiXtendV2</code>    => creates a FHEM device for model S<br> + <code>define pix PiXtendV2 S</code>   => creates a FHEM device for model S<br> + <code>define pix PiXtendV2 L</code>   => creates a FHEM device for model L<br> + </ul> + </ul> + <br> + + + <a name="PiXtendV2Set"></a> + <b>Set</b> + <ul> + Commands to configure the basic settings of the PiXtend start with an "_".<br> + If a command supports multiple channels the "#"-sign has to be replaced with the channel number.<br> + All Set-commands are case insensitive to guarantee an easy use.<br> + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br> + Example: + <ul> + <code>set pix relayOut0 on</code><br> + <code>set pix Relayout0 On</code><br> + <code>set pix rElAyoUT0 oFf</code><br> + </ul> + <br><br> + + <li>_GPIO#Ctrl [input,output,DHT11,DHT22]<br> + With this setting the GPIO can be configured as [input], [output] or as [DHT11] or [DHT22] as well, if a DHT sensor is connected to this GPIO. + If a DHT is selected and connected, the GPIO can't simultaniously be used as a normal GPIO but the temperatur and humidity is read automatically. + <br><br></li> + + <li>_GPIOPullupsEnable [yes,no]<br> + This setting enables [yes] or disables [no] the possibility to set the internal pullup resistors via GPIOOut setting for all GPIOs. + <br><br></li> + + <li>_JumperSettingAI# [5V,10V]<br> + This setting affects the calculation of the voltage on the analog inputs and refers to the actual setting of the physical jumper on the PiXtend-Board [5V,10V]. + The default value is [10V] if no jumper is used. + <br><br></li> + + <li>_StateLEDDisable [yes,no]<br> + This setting disables [yes] or enables [no] the state LED on the PiXtend. If the LED is disabled it won't light up in case of an error. + <br><br></li> + + <li>_WatchdogEnable [disable,125ms,1s,8s]<br> + This setting allows to configure the watchdog timer. If the watchdog is configured, the PiXtend will go to safe state if no valid data transfer has + occured within the selected timeout and thus can't be accessed anymore without a reset. + <br><br></li> + + <li>AnalogOut# []<br> + Sets the analog output to the selected voltage. The value can be a voltage between 0 V and 10 V + or a raw value between 0 and 1023. To set the value to a voltage the value has to include a + "." even if it is an even number. + <br><br> + Example: + <ul> + <code>set pix analogout0 2.5</code>   => sets analog output 0 to 2.5 V<br> + <code>set pix analogout0 4.0</code>   => sets analog output 0 to 4 V<br> + <code>set pix analogout0 223</code>   => sets analog output 0 to 10*(233/1024) = 1.09 V + </ul> + <br><br></li> + + <li>DigitalDebounce# [0-255]<br> + Allows to debounce the digital inputs. A setting always affects two channels. So DigitalDebounce01 affects DigitalIn0 and DigitalIn1. + The resulting delay is calculated by (selected value)*(100 ms). The selected value can be any number between 0 and 255. + Debouncing can be useful if a switch or button is connected to this input. + <br><br> + Example: + <ul> + <code>set pix digitaldebounce01 20</code>   => debounces DigitalIn0 and DigitalIn1 over (20*100ms) = 2s + </ul> + <br><br></li> + + <li>DigitalOut# [on,off,toggle]<br> + Set the digital output HIGH [on] or LOW [off] or [toggle]s it. + <br><br></li> + + <li>GPIODebounce# [0-255]<br> + Allows to debounce the GPIO inputs. A setting always affects two channels. So GPIODebounce01 affects GPIOIn0 and GPIOIn1. + The resulting delay is calculated by (selected value)*(100 ms). The selected value can be any number between 0 and 255. + Debouncing can be useful if a switch or button is connected to this input. + <br><br> + Example: + <ul> + <code>set pix gpiodebounce23 33</code>   => debounces GPIOIn2 and GPIOIn3 over (33*100ms) = 3.3s + </ul> + <br><br></li> + + <li>GPIOOut# [on,off,toggle]<br> + Set the GPIO to HIGH [on] or LOW [off] or [toggle]s it, if it is configured as an output. + If it is configured as an input, this command can enable [on] or disable [off] or [toggle] the internal pullup resistor for that GPIO, + but therefore pullups have to be enabled globally via _GPIOPullupsEnable. + <br><br></li> + + <li>PWM<br> + The PiXtendV2 supports various PWM-Modes, which can be configured with this settings. + For example a servo-mode to control servo-motors, a frequency-mode or a duty-cycle-mode are supported. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br> + PWM#Ctrl0 needs a value between 0 and 255<br> + PWM#Ctrl1 needs a value between 0 and 65535 (or a value between 0 and 255, see exception for model -S-)<br> + PWM#A/B needs a value between 0 and 65535 (or a value between 0 and 255, see exception for model -S-) + <br><br></li> + + <li>RelayOut# [on,off,toggle]<br> + Set the relay to HIGH [on] or LOW [off] or [toggle]s it. + <br><br></li> + + <li>Reset<br> + Resets the controller on the PiXtend, for example if it is in safe state and allows to access it again. + <br><br></li> + + <li>RetainCopy [on,off]<br> + If RetainCopy is enabled [on] the RetainDataOut that is written to the PiXtend will be received in RetainDataIn again. + This can be useful in some situations to see which data was send to the PiXtend. + If it is disabled [off] the last stored data will be received. + <br><br></li> + + <li>RetainDataOut [0-(RetainSize-1)] [0-255]<br> + The PiXtendV2 supports the storage of retain data. If enabled, this data is stored in case of a power failure or if it is triggered by a watchdog timeout or the safe state command. + The retain data is organized in bytes and each byte can be written individually with a value between 0 and 255.<br> + As first parameter the command needs the byte index which is between 0 and the (RetainSize-1). The RetainSize is shown in the "Internals". + As the second parameter a value is expected which should be stored. + <br><br> + Example: + <ul> + <code>set pix retaindataout 0 34</code>    => stores 34 in retain-data-byte 0<br> + <code>set pix retaindataout 30 222</code>   => stores 222 in retain-data-byte 30 + </ul> + <br><br></li> + + <li>RetainEnable [on,off]<br> + The function of storing retain data on the PiXtend has to be enabled [on], otherwise no data is stored [off]. + The memory in which the data is stored supports 10.000 write-cycles. + So the storage of retain data should only be used if it is really necessary. + <br><br></li> + + <li>SafeState<br> + This setting allows to force the PiXtend to enter the safe state . If retain storage is enabled the data will be stored. In safe state the PiXtend won't communicate with FHEM and can't be configured anymore. + To restart the PiXtend a reset has to be done. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Get"></a> + <b>Get</b> + <ul> + If a command supports multiple channels the "#"-sign has to be replaced with the channel number.<br> + All Get-commands are case insensitive to guarantee an easy use.<br> + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + The values can be returned as a string where the actual value is inside squared brackets or as raw values. + To change the returned value the attribute "PiXtend_GetFormat" has to be set. + <br><br> + + <li>AnalogIn#<br> + Returns the Value of the selected analog input. + The result depends on the selected _JumperSettingAI# and the actual physical jumper position on the board, as well as the kind of measurement. + For example AnalogIn4 and AnalogIn5 on model -L- are used to measure a current instead of a voltage. + <br><br></li> + + <li>DigitalIn#<br> + Returns the state on (HIGH) or off (LOW) of the digital input. + <br><br></li> + + <li>GPIOIn#<br> + Returns the state on (HIGH) or off (LOW) of the GPIO, independant of its configuration (input, output, ..). + <br><br></li> + + <li>RetainDataIn [0-(RetainSize-1)]<br> + Returns the value of the selected RetainDataIn-byte. + <br><br></li> + + <li>Sensor# [temperature,humidity]<br> + If a DHT-Sensor is connected to the corresponding GPIO and the _GPIO#Ctrl is set to DHT11 or DHT22 the + temperature and humidity are measured and can be read. + <br><br> + Example: + <ul> + <code>set pix _GPIO0Ctrl DHT11</code><br> + <code>get pix Sensor0 temperature</code> + </ul> + <br><br></li> + + <li>SysState<br> + Returns the system state [defined, active, error] of the FHEM module. + <br><br></li> + + <li>UCState<br> + Returns the state of the PiXtend. If it is 1 everything is fine. If it is greater than 1 an error occured or is present and the PiXtend can't be configured. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>UCWarnings<br> + Returns a value that represents the PiXtend warnings. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>Version<br> + Returns the FHEM-module version and the microcontroller version [Model-Hardware-Firmware]. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Readings"></a> + <b>Readings</b> + <ul> + The FHEM-module for PiXtendV2 supports multiple readings from which most of them trigger an event when they have changed. + The meaning of the readings is similar to the Get-commands. + <br><br> + + <li>AnalogIn#<br> + Shows the result of the measurment on the analog inputs in V or mA. + <br><br></li> + + <li>DigitalIn#<br> + Shows the state on (HIGH) or off (LOW) of the digital inputs. + <br><br></li> + + <li>Firmware<br> + Shows the firmware version. + <br><br></li> + + <li>GPIOIn#<br> + Shows the state on (HIGH) or off (LOW) of the GPIOs, independant of their configuration (input, output, ..). + <br><br></li> + + <li>Hardware<br> + Shows the hardware version. + <br><br></li> + + <li>Model<br> + Shows the model. + <br><br></li> + + <li>RetainDataIn<br> + Shows the values of the RetainDataIn. + The values of RetainDataIn are combined in one row, whereby the most left value represents Byte0 / RetainDataIn0. + Each value is seperated by an " " and thus can be parsed very easy in perl: + <br><br> + Example: + <ul> + <code>my ($str) = ReadingsVal(pix, "RetainDataIn", "?")</code><br> + <code>if($str ne "?"){</code><br> +  <code>my @val = split(/ /, $str);</code>   => $val[0] contains Byte0, $val[1] Byte1, ...<br> +  <code>...</code><br> + <code>}</code> + </ul> + <br><br></li> + + <li>Sensor#T/H<br> + Shows the temperature (T) in °C and the humidity (H) in % of the sensor that is connected to the corresponding GPIO. + <br><br></li> + + <li>UCState<br> + Shows the state of the PiXtend. If it is 1 everything is fine. If it is greater than 1 an error occured or is present and the PiXtend can't be configured. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>UCWarnings<br> + Shows a value that represents the PiXtend warnings. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + </ul> + <br> + + <a name="PiXtendV2Attr"></a> + <b>Attributes</b> + <ul> + The attribute name is case-sensitive. + <br><br> + + <li>PiXtend_GetFormat [text,value]<br> + Changes the style in which the values of the Get commands are returned. They can be returned as a message [text] or as a raw [value]. + Default is the presentation as a message. + <br><br></li> + + <li>PiXtend_Parameter<br> + With this attribute the base configuration (Set commands with a leading "_") can be saved as an attribute. + Attributes are stored in the config file. Single commands are seperated with a space " " and each value is seperated by a ":". + <br><br> + Example: + <ul> + <code>attr pix PiXtend_Parameter _gpio0ctrl:dht11 _gpio3ctrl:dht22</code> + </ul> + <br><br></li> + + </ul> + <br> + +</ul> + +=end html + +=begin html_DE + +<a name="PiXtendV2"></a> +<h3>PiXtendV2</h3> +<ul> + + PiXtend ist eine speicherprogrammierbare Steuerung auf Basis des Raspberry Pi. + Dieses FHEM-Modul ermöglicht dabei den Zugriff auf die Funktionen des PiXtendV2-Boards in der FHEM-Oberfläche. + Der PiXtend bietet dabei eine Vielzahl an digitalen und analogen Ein- und Ausgängen, die nach Industrie-Standards ausgelegt sind + und ist aufgrund der sicheren Anschlüsse auch ideal für die Hausautomatisierung geeignet. + Für mehr Informationen über PiXtend(R) und das FHEM-Modul besuchen Sie unsere Website + <a href="https://www.pixtend.de" target="_blank">www.pixtend.de</a>. + + <br><br> + + + <a name="PiXtendV2Define"></a> + <b>Define</b> + <ul> + <code>define <name> PiXtendV2 <optional></code> + <br><br> + Der Parameter "optional" bestimmt für welches Hardware-Modell ein FHEM-Gerät angelegt werden soll, z.B. S oder L. + Wird der Parameter weggelassen, wird automatisch ein FHEM-Gerät für Model -S- angelegt. + <br><br> + Beispiel: + <ul> + <code>define pix PiXtendV2</code>    => erzeugt ein FHEM-Gerät für Model S<br> + <code>define pix PiXtendV2 S</code>   => erzeugt ein FHEM-Gerät für Model S<br> + <code>define pix PiXtendV2 L</code>   => erzeugt ein FHEM-Gerät für Model L<br> + </ul> + </ul> + <br> + + + <a name="PiXtendV2Set"></a> + <b>Set</b> + <ul> + Kommandos um die Basiskonfiguration für den PiXtend durchzuführen, beginnen mit einem "_".<br> + Unterstützt ein Kommando mehrere Kanäle, muss das "#"-Zeichen durch die Kanal-Nummer ersetzt werden.<br> + Alle Set-Kommandos sind unabhängig von der Groß-/Kleinschreibung um die einfache Benutzung zu ermöglichen.<br> + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br> + Beispiel: + <ul> + <code>set pix relayOut0 on</code><br> + <code>set pix Relayout0 On</code><br> + <code>set pix rElAyoUT0 oFf</code><br> + </ul> + <br><br> + + <li>_GPIO#Ctrl [input,output,DHT11,DHT22]<br> + Mit dieser Einstellung kann die Funktion des GPIO eingestellt werden. [input], [output] oder [DHT11] und [DHT22] wenn ein DHT-Sensor an den GPIO angeschlossen ist. + Wenn ein DHT-Sensor angeschlossen ist und verwendet wird, kann die normale Funktion des GPIO als Eingang/Ausgang nicht gleichzeitig verwendet werden. + <br><br></li> + + <li>_GPIOPullupsEnable [yes,no]<br> + Diese Einstellung aktiviert [yes] oder deaktiviert [no] für alle GPIOs die Möglichkeit die internen PullUp-Widerstände durch GPIOOut zu setzen. + <br><br></li> + + <li>_JumperSettingAI# [5V,10V]<br> + Diese Einstellung beeinflusst die Berechnung der Spannung durch die analogen Eingänge und bezieht sich dabei auf die tatsächliche Position des Jumpers + auf dem PiXtend-Board [5V,10V]. Wenn kein Jumper verwendet wird, entspricht das der Standardeinstellung von [10V]. + <br><br></li> + + <li>_StateLEDDisable [yes,no]<br> + Diese Einstellung deaktiviert [yes] oder aktiviert [no] die Status-LED auf dem PiXtend. Wenn die LED deaktiviert ist, leuchtet sie im Fehlerfall nicht auf. + <br><br></li> + + <li>_WatchdogEnable [disable,125ms,1s,8s]<br> + Diese Einstellung ermöglicht die Konfiguration des Watchdog-Timers. Wenn der Watchdog konfiguriert ist, geht der PiXtend in den Sicheren Zustand + über, falls innerhalb der eingestellten Zeit keine gültige Übertragung zwischen PiXtend und Raspberry Pi stattgefunden hat. + Im Sicheren Zustand kann der PiXtend erst wieder angesprochen werden, nachdem ein Reset des PiXtend durchgeführt wurde. + <br><br></li> + + <li>AnalogOut# []<br> + Stellt am analogen Ausgang eine Spannung ein. Der übergebene Wert kann eine Spannung zwischen 0 V und 10 V + oder ein Rohwert zwischen 0 und 1023 sein. Um den Wert als Spannung zu übergeben, muss der Wert ein "." enthalten, + auch wenn der Wert ganzzahlig ist. + <br><br> + Beispiel: + <ul> + <code>set pix analogout0 2.5</code>   => Setzt den analogen Ausgang 0 auf 2,5 V<br> + <code>set pix analogout0 4.0</code>   => Setzt den analogen Ausgang 0 auf 4 V<br> + <code>set pix analogout0 223</code>   => Setzt den analogen Ausgang 0 auf 10*(233/1024) = 1,09 V + </ul> + <br><br></li> + + <li>DigitalDebounce# [0-255]<br> + Ermöglicht das Entprellen der digitalen Eingänge. Die Einstellung beeinflusst dabei immer zwei Kanäle. + DigitalDebounce01 beeinflusst somit DigitalIn0 und DigitalIn1. + Die resultierende Verzögerung berechnet sich dabei durch (eingestellten Wert)*(100 ms). + Der übergebene Wert kann eine beliebige Zahl zwischen 0 und 255 sein. + Entprellen kann sinnvoll sein, falls an den Eingängen Schalter oder Taster angeschlossen sind. + <br><br> + Beispiel: + <ul> + <code>set pix digitaldebounce01 20</code>   => entprellt DigitalIn0 und DigitalIn1 über (20*100ms) = 2s + </ul> + <br><br></li> + + <li>DigitalOut# [on,off,toggle]<br> + Setzt den digitalen Ausgang auf HIGH [on] oder LOW [off] oder [toggle]t ihn. + <br><br></li> + + <li>GPIODebounce# [0-255]<br> + Ermöglicht das Entprellen der GPIO Eingänge. Die Einstellung beeinflusst dabei immer zwei Kanäle. + GPIODebounce01 beeinflusst somit GPIOIn0 und GPIOIn1. + Die resultierende Verzögerung berechnet sich dabei durch (eingestellten Wert)*(100 ms). + Der übergebene Wert kann eine beliebige Zahl zwischen 0 und 255 sein. + Entprellen kann sinnvoll sein, falls an den Eingängen Schalter oder Taster angeschlossen sind. + <br><br> + Beispiel: + <ul> + <code>set pix gpiodebounce23 33</code>   => entprellt GPIOIn2 und GPIOIn3 über (33*100ms) = 3,3s + </ul> + <br><br></li> + + <li>GPIOOut# [on,off,toggle]<br> + Setzt den GPIO auf HIGH [on] oder LOW [off] oder [toggle]t ihn, falls er als Ausgang konfiguriert ist. + Wenn der GPIO als Eingang konfiguriert ist, kann mit diesem Kommando der interne PullUp-Widerstand aktiviert [on], deaktiviert [off] oder + ge[toggle]t werden. Dazu muss die Möglichkeit allerdings global durch _GPIOPullupsEnable aktiviert werden. + <br><br></li> + + <li>PWM<br> + PiXtendV2 unterstützt mehrere PWM-Modi, die mit diesen Einstellungen konfiguriert werden können. + Zum Beispiel wird ein Servo-Mode um Modellbau-Servomotoren anzusteuern, ein Frequency-Mode oder ein Duty-Cycle-Mode unterstüzt. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="http://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br> + PWM#Ctrl0 benötigt einen Wert zwischen 0 und 255<br> + PWM#Ctrl1 benötigt einen Wert zwischen 0 und 65535 (oder einen Wert zwischen 0 und 255, siehe Ausnahme Model -S-)<br> + PWM#A/B benötigt einen Wert zwischen 0 und 65535 (oder einen Wert zwischen 0 und 255, siehe Ausnahme Model -S-) + <br><br></li> + + <li>RelayOut# [on,off,toggle]<br> + Setzt das Relay auf HIGH [on] oder LOW [off] oder [toggle]t es. + <br><br></li> + + <li>Reset<br> + Setzt den Controller auf dem PiXtend zurück, z.B. wenn er sich im Sicheren Zustand befindet, um ihn erneut konfigurieren zu können. + <br><br></li> + + <li>RetainCopy [on,off]<br> + Wenn RetainCopy aktiviert [on] ist, werden die geschriebenen Daten RetainDataOut vom PiXtend in RetainDataIn zurückgegeben. + Die Aktivierung kann in Situationen sinnvoll sein, wenn überprüft werden soll, welche Daten an den PiXtend geschickt wurden. + Ist die Funktion deaktiviert [off] werden die zuletzt gespeicherten Daten in RetainDataIn zurückgegeben. + <br><br></li> + + <li>RetainDataOut [0-(RetainSize-1)] [0-255]<br> + Der PiXtendV2 unterstüzt die Speicherung remanenter/persistenter Daten - auch Retain genannt. Diese Daten werden im Falle + einer Betribsspannungsunterbrechung, beim Auslösen des Watchdog-Timers oder beim Entritt in den Sicheren Zustand gespeichert, + sofern diese Funktion aktiviert wurde. Die Retain-Daten sind dabei in Bytes organisiert, wobei jedes Byte + individuell mit einem Wert zwischen 0 und 255 beschrieben werden kann.<br> + Als ersten Parameter erwartet das Kommando den Index des Bytes, der zwischen 0 und (RetainSize-1) liegt. RetainSize ist in den "Internals" zu finden. + Als zweiter Parameter wird der Wert erwartet, der gespeichert werden soll. + <br><br> + Beispiel: + <ul> + <code>set pix retaindataout 0 34</code>    => speichert 34 in Retain-Data-Byte 0<br> + <code>set pix retaindataout 30 222</code>   => speichert 222 in Retain-Data-Byte 30 + </ul> + <br><br></li> + + <li>RetainEnable [on,off]<br> + Die Funktion um Retain-Daten auf dem PiXtend zu speichern muss erst aktiviert [on] werden. Andernfalls [off] werden keine Daten gespeichert. + Es ist zu beachten, dass für den Retain-Speicherbereich 10.000 Schreibzyklen unterstützt werden. Dementsprechend + sollte die Funktion nur aktiviert werden, wenn sie tatsächlich benötigt wird. + <br><br></li> + + <li>SafeState<br> + Mit dieser Einstellung kann der PiXtend in den Sicheren Zustand versetzt werden. Wenn die Retain-Speicherung aktiviert ist, werden die Daten gesichert. + Im Sicheren Zustand kommuniziert der PiXtend nicht mehr mit FHEM. Um den PiXtend neuzustarten muss ein Reset durchgeführt werden. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Get"></a> + <b>Get</b> + <ul> + Unterstützt ein Kommando mehrere Kanäle, muss das "#"-Zeichen durch die Kanal-Nummer ersetzt werden.<br> + Alle Get-Kommandos sind unabhängig von der Groß-/Kleinschreibung um die einfache Benutzung zu ermöglichen.<br> + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach.<br> + Die Werte können als Text, wobei die Werte in eckigen Klammern stehen oder als rohe Werte zurückgegeben werden. + Die Einstellung für das Format ist in den Attributen ("PiXtend_GetFormat") zu finden. + <br><br> + + <li>AnalogIn#<br> + Gibt den Wert des ausgewählten analogen Eingangs zurück. + Der Wert hängt dabei von der Einstellung _JumperSettingAI# und der tatsächlichen Jumper-Position auf dem Board ab, sowie der Messmethode des Kanals ab. + AnalogIn4 und AnalogIn5 bei Modell -L- sind z.B. Stromeingänge. + <br><br></li> + + <li>DigitalIn#<br> + Gibt den Status on (HIGH) oder off (LOW) des digitalen Eingangs zurück. + <br><br></li> + + <li>GPIOIn#<br> + Gibt den Status on (HIGH) oder off (LOW) des GPIOs zurück, unabhängig von der Konfiguration (input, output, ..). + <br><br></li> + + <li>RetainDataIn [0-(RetainSize-1)]<br> + Gibt den Wert des ausgewählten RetainDataIn-Bytes zurück. + <br><br></li> + + <li>Sensor# [temperature,humidity]<br> + Wenn ein DHT-Sensor an den entsprechenden GPIO angeschlossen ist und _GPIO#Ctrl auf DHT11 oder DHT22 gesetzt ist + wird die Temperatur und Luftfeuchtigkeit gemessen und kann ausgelesen werden. + <br><br> + Beispiel: + <ul> + <code>set pix _GPIO0Ctrl DHT11</code><br> + <code>get pix Sensor0 temperature</code> + </ul> + <br><br></li> + + <li>SysState<br> + Gibt den Systemstatus [defined, active, error] des FHEM-Moduls zurück. + <br><br></li> + + <li>UCState<br> + Gibt den Status des PiXtend zurück. Ist der Status 1, ist alles in Ordnung. Ist der Status allerdings größer als 1 ist ein Fehler aufgetreten + oder steht noch an. In diesem Fall kann der PiXtend nicht konfiguriert werden. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="http://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>UCWarnings<br> + Der zurückgegebene Wert repräsentiert die Warnungen des PiXtendV2. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>Version<br> + Gibt die Version des FHEM-Moduls sowie die PiXtend-Version [Model-Hardware-Firmware] zurück. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Readings"></a> + <b>Readings</b> + <ul> + Das FHEM-Modul des PiXtend unterstüzt mehrere Readings, von denen die meisten ein Event auslösen, sobald sie sich ändern. + Die Bedeutung der Readings ist ähnlich zu den Get-Kommandos. + <br><br> + + <li>AnalogIn#<br> + Zeigt das Ergebnis der Messungen der analogen Eingänge in V beziehungsweise in mA an. + <br><br></li> + + <li>DigitalIn#<br> + Zeigt den Status on (HIGH) oder off (LOW) der digitalen Eingänge an. + <br><br></li> + + <li>Firmware<br> + Zeigt die Firmware-Version an. + <br><br></li> + + <li>GPIOIn#<br> + Zeigt den Status on (HIGH) oder off (LOW) der GPIOs, unabhängig von deren Konfiguration (input, output, ..). + <br><br></li> + + <li>Hardware<br> + Zeigt die Hardware-Version an. + <br><br></li> + + <li>Model<br> + Zeigt das Model an. + <br><br></li> + + <li>RetainDataIn<br> + Zeigt die Werte von RetainDataIn an. Die Werte von RetainDataIn sind dabei in einer Zeile + zusammengefasst. Der am weitsten links stehende Wert entspricht Byte0 / RetainDataIn0. + Die Werte sind durch ein Leerzeichen " " voneinander getrennt und können somit einfach in Perl ausgewertet werden: + <br><br> + Beispiel: + <ul> + <code>my ($str) = ReadingsVal(pix, "RetainDataIn", "?")</code><br> + <code>if($str ne "?"){</code><br> +  <code>my @val = split(/ /, $str);</code>   => $val[0] enthält nun Byte0, $val[1] Byte1, usw<br> +  <code>...</code><br> + <code>}</code> + </ul> + <br><br></li> + + <li>Sensor#T/H<br> + Zeigt die Temperatur (T) in °C und die Luftfeuchtigkeit (H) in % des Sensors an, der an den entsprechenden GPIO angeschlossen ist. + <br><br></li> + + <li>UCState<br> + Zeigt den Status des PiXtend an. Ist der Status 1, ist alles in Ordnung. Ist der Status allerdings größer als 1 ist ein Fehler aufgetreten + oder steht noch an. In diesem Fall kann der PiXtend nicht konfiguriert werden. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>UCWarnings<br> + Der angezeigte Wert repräsentiert die Warnungen des PiXtendV2. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + </ul> + <br> + + <a name="PiXtendV2Attr"></a> + <b>Attributes</b> + <ul> + Für den Attribut-Namen muss die Groß-/Kleinschreibung beachtet werden. + <br><br> + + <li>PiXtend_GetFormat [text,value]<br> + Ändert die Darstellung, wie die Werte durch die Get-Kommandos zurückgegeben werden. Die Werte können entweder in einer Nachricht [text] oder als rohe Werte [value] zurückgegeben werden. + Standard ist die Ausgabe als Text. + <br><br></li> + + <li>PiXtend_Parameter<br> + Dieses Attribut kann verwendet werden, um die Einstellungen zur Basiskonfiguration (Set-Kommandos beginnend mit "_") als Attribut zu speichern. Attribute werden im Gegensatz zu Set-Kommandos in der Config-Datei gespeichert.<br> + Einzelne Kommandos werden durch ein Leerzeichen voneinander getrennt und erhalten ihre Werte nach einem Doppelpunkt. + <br><br> + Beispiel: + <ul> + <code>attr pix PiXtend_Parameter _gpio0ctrl:dht11 _gpio3ctrl:dht22</code> + </ul> + <br><br></li> + + </ul> + <br> + +</ul> + +=end html_DE + +=cut diff --git a/97_PiXtendV2_hipi.pm b/97_PiXtendV2_hipi.pm new file mode 100644 index 0000000000000000000000000000000000000000..b63131a6a34927836a0e19b306a450249cdf7e79 --- /dev/null +++ b/97_PiXtendV2_hipi.pm @@ -0,0 +1,2081 @@ +############################################################################## +# $Id$ +# 97_PiXtendV2.pm +# +# Modul to control PiXtendV2 +# +# define <name> PiXtendV2 <optional> +# +############################################################################## +# +# For more information about PiXtend(R) and this program, +# see <https://www.pixtend.de> - <support@pixtend.de> +# +# Copyright (C) 2021 Kontron Electronics GmbH +# Max-Planck-Str. 6 +# 72636 Frickenhausen, Germany +# +# This program is 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 3 of the License, or +# (at your option) any later version. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +# History +# 2017 First Release +# 2018 Added PiXtend V2 -L- +# 2021 Change to HiPi module, direct use of ioctl in perl since kernel 5.5 no longer works. +# Communication via SPI bus using ioctl always results in return value undef. +# The HiPi module uses a C class on the back end where ioctl still works as expected. +# Changed some sub names to include PiXtendV2 as prefix, +# to potential avoid collisions with other modules and sub. +# Also see this forum post: https://forum.fhem.de/index.php?topic=116308.msg1154062#msg1154062 + +#-------------------------- +#HiPi for perl installation +#-------------------------- +# pi@raspberry:~ $ wget http://hipi.znix.com/download/libhipi-perl_0.85-1_armhf.deb +# pi@raspberry:~ $ sudo apt-get update +# pi@raspberry:~ $ sudo dpkg --install libhipi-perl_0.85-1_armhf.deb +# pi@raspberry:~ $ sudo apt-get -y -f install + + +#Der Einstieg in die FHEM-Modul Programmierung ist das folgende Tutorial, bzw. die folgende Beschreibung: +#https://wiki.fhem.de/wiki/DevelopmentModuleIntro + +package main; +use strict; #perl pragma um debugen zu erleichtern +use warnings; #perl pragma um debugen zu erleichtern +use Time::HiRes qw(gettimeofday usleep); #Um feiner zu warten, als mit sleep() +use HiPi qw( :rpi ); #RPi Konstanten für HiPi +use HiPi qw( :spi ); #SPI Konstanten für HiPi +use HiPi::Device::SPI; #SPI Gerät von HiPi + +my $modul_ver = "1.3"; #FHEM Modul-Version + + +#Zur Erweiterung für neue Modelle müssen die folgenden beiden Arrays und die beiden Funktionen kopiert und angepasst werden. +#Zudem muss in der Funktion "PiXtendV2_Define" die Abfrage für das Modell eingefügt werden. + +##################################### +# Configuration for Model -S- +##################################### + +#Nach einem Doppelpunkt folgen die Einstellmöglichkeiten, bzw ein Widget (Textfeld, Slider, ...) +#Für alle möglichen Widgets: http://fhem.de/commandref.html#widgetOverride +my @PiXtendV2S_Set = ( + #entspricht CODESYS-Paramtern: + "_JumperSettingAI0:10V,5V", + "_JumperSettingAI1:10V,5V", + "_GPIO0Ctrl:input,output,DHT11,DHT22", + "_GPIO1Ctrl:input,output,DHT11,DHT22", + "_GPIO2Ctrl:input,output,DHT11,DHT22", + "_GPIO3Ctrl:input,output,DHT11,DHT22", + "_GPIOPullupsEnable:no,yes", + "_WatchdogEnable:disabled,125ms,1s,8s", + "_StateLEDDisable:no,yes", + + #entspricht CODESYS-Variablen: + "Reset:noArg", + "SafeState:noArg", + "RetainCopy:off,on", + "RetainEnable:off,on", + "DigitalDebounce01:textField", + "DigitalDebounce23:textField", + "DigitalDebounce45:textField", + "DigitalDebounce67:textField", + "DigitalOut0:on,off,toggle", + "DigitalOut1:on,off,toggle", + "DigitalOut2:on,off,toggle", + "DigitalOut3:on,off,toggle", + "RelayOut0:on,off,toggle", + "RelayOut1:on,off,toggle", + "RelayOut2:on,off,toggle", + "RelayOut3:on,off,toggle", + "GPIOOut0:on,off,toggle", + "GPIOOut1:on,off,toggle", + "GPIOOut2:on,off,toggle", + "GPIOOut3:on,off,toggle", + "GPIODebounce01:textField", + "GPIODebounce23:textField", + "PWM0Ctrl0:textField", + "PWM0Ctrl1:slider,0,1,65535", + "PWM0A:slider,0,1,65535", + "PWM0B:slider,0,1,65535", + "PWM1Ctrl0:textField", + "PWM1Ctrl1:slider,0,1,255", + "PWM1A:slider,0,1,255", + "PWM1B:slider,0,1,255", + "AnalogOut0:slider,0.00,0.01,10.00,1", #...,1 damit floats nicht gerundet werden + "AnalogOut1:slider,0.00,0.01,10.00,1", + "RetainDataOut:textField" +); + +my @PiXtendV2S_Get = ( + "Version:noArg", + "SysState:noArg", + "UCState:noArg", + "UCWarnings:noArg", + "DigitalIn0:noArg", + "DigitalIn1:noArg", + "DigitalIn2:noArg", + "DigitalIn3:noArg", + "DigitalIn4:noArg", + "DigitalIn5:noArg", + "DigitalIn6:noArg", + "DigitalIn7:noArg", + "AnalogIn0:noArg", + "AnalogIn1:noArg", + "GPIOIn0:noArg", + "GPIOIn1:noArg", + "GPIOIn2:noArg", + "GPIOIn3:noArg", + "Sensor0:temperature,humidity", + "Sensor1:temperature,humidity", + "Sensor2:temperature,humidity", + "Sensor3:temperature,humidity", + "RetainDataIn:textField" +); + +sub PiXtendV2_BufferWrite_S($$) { + my ($buffer, $hash) = @_; + + #Der Aufbau "if() {}" wird benötigt, um in Perl Warnungen zu verhindern, dass ein nicht initialisierter Wert verwendet wird, wenn bisher noch nichts an die Stellen im Hash geschrieben wurde + if($hash->{DataOut}{DigitalDebounce01}) {$buffer->[9] = ($hash->{DataOut}{DigitalDebounce01});} + if($hash->{DataOut}{DigitalDebounce23}) {$buffer->[10] = ($hash->{DataOut}{DigitalDebounce23});} + if($hash->{DataOut}{DigitalDebounce45}) {$buffer->[11] = ($hash->{DataOut}{DigitalDebounce45});} + if($hash->{DataOut}{DigitalDebounce67}) {$buffer->[12] = ($hash->{DataOut}{DigitalDebounce67});} + if($hash->{DataOut}{DigitalOut}) {$buffer->[13] = ($hash->{DataOut}{DigitalOut});} + if($hash->{DataOut}{RelayOut}) {$buffer->[14] = ($hash->{DataOut}{RelayOut});} + if($hash->{DataOut}{GPIOCtrl0}) {$buffer->[15] = ($hash->{DataOut}{GPIOCtrl0});} #Falls mehr als 4 GPIOs bereitstehen, ist auch GPIOCtrl1 verfügbar + if($hash->{DataOut}{GPIOOut}) {$buffer->[16] = ($hash->{DataOut}{GPIOOut});} + if($hash->{DataOut}{GPIODebounce01}) {$buffer->[17] = ($hash->{DataOut}{GPIODebounce01});} + if($hash->{DataOut}{GPIODebounce23}) {$buffer->[18] = ($hash->{DataOut}{GPIODebounce23});} + + if($hash->{DataOut}{PWMCtrl00}) {$buffer->[19] = ($hash->{DataOut}{PWMCtrl00});} + if($hash->{DataOut}{PWMCtrl01}) {$buffer->[20] = (($hash->{DataOut}{PWMCtrl01}) & 0xFF); + $buffer->[21] = (($hash->{DataOut}{PWMCtrl01}) >> 8);} + if($hash->{DataOut}{PWM0a}) {$buffer->[22] = (($hash->{DataOut}{PWM0a}) & 0xFF); + $buffer->[23] = (($hash->{DataOut}{PWM0a}) >> 8);} + if($hash->{DataOut}{PWM0b}) {$buffer->[24] = (($hash->{DataOut}{PWM0b}) & 0xFF); + $buffer->[25] = (($hash->{DataOut}{PWM0b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl10}) {$buffer->[26] = ($hash->{DataOut}{PWMCtrl10});} + if($hash->{DataOut}{PWMCtrl11}) {$buffer->[27] = (($hash->{DataOut}{PWMCtrl11}) & 0xFF); + $buffer->[28] = (($hash->{DataOut}{PWMCtrl11}) >> 8);} + if($hash->{DataOut}{PWM1a}) {$buffer->[29] = (($hash->{DataOut}{PWM1a}) & 0xFF); + $buffer->[30] = (($hash->{DataOut}{PWM1a}) >> 8);} + if($hash->{DataOut}{PWM1b}) {$buffer->[31] = (($hash->{DataOut}{PWM1b}) & 0xFF); + $buffer->[32] = (($hash->{DataOut}{PWM1b}) >> 8);} + #Retain beschreiben + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + if($hash->{DataOut}{"RetainDataOut".$i}) {$buffer->[33+$i] = ($hash->{DataOut}{"RetainDataOut".$i});} + } +} + +sub PiXtendV2_BufferRead_S($$) { + my ($buffer, $hash) = @_; + my $str; + + readingsBeginUpdate($hash); #Wird benötigt um das Update der Readings zu starten + + readingsBulkUpdateIfChanged($hash, "Firmware", $buffer->[0], 0); #letzter Parameter: 0-Kein Update in WebGui oder notify, 1- Update + readingsBulkUpdateIfChanged($hash, "Hardware", $buffer->[1], 0); + readingsBulkUpdateIfChanged($hash, "Model", pack("C",$buffer->[2]), 0); + readingsBulkUpdateIfChanged($hash, "UCState", $buffer->[3], 1); + readingsBulkUpdateIfChanged($hash, "UCWarnings", $buffer->[4], 1); + + #DigitalIn + for(my $i=0; $i < 8; $i++){ + if($buffer->[9] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + + #AnalogIn + $str = sprintf("%.2f", (($buffer->[10] | ($buffer->[11] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn0", $str, 1); + $str = sprintf("%.2f", (($buffer->[12] | ($buffer->[13] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn1", $str, 1); + + #GPIOIn + for(my $i=0; $i < 4; $i++){ + if($buffer->[14] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "off", 1);} + } + + #DHTs + for(my $i=0; $i < 4; $i++){ + my $temp = ($buffer->[15+(4*$i)] | ($buffer->[16+(4*$i)] << 8)); + my $humi = ($buffer->[17+(4*$i)] | ($buffer->[18+(4*$i)] << 8)); + if($temp == 65535 && $humi == 65535) + { + $temp = "Sensor not connected"; + $humi = $temp; + } + else + { + if($hash->{DataOut}{"GPIO".$i."DHT"}) #Prüfen, ob Wert definiert + { + if(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht11") + { + $temp = sprintf("%.1f", ($temp/256)); + $humi = sprintf("%.1f", ($humi/256)); + + } + elsif(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht22") + { + if($temp & (1<<15)) + { + $temp &= ~(1<<15); + $temp /= (-10); + } + else + { + $temp /= 10; + } + $temp = sprintf("%.1f", $temp); + $humi = sprintf("%.1f", ($humi/10)); + } + } + else + { + $temp = "Function not enabled"; + $humi = $temp; + } + } + readingsBulkUpdateIfChanged($hash, "Sensor".$i."T", $temp, 1); + readingsBulkUpdateIfChanged($hash, "Sensor".$i."H", $humi, 1); + } + + #Retain lesen + $str = ""; + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + $str .= $buffer->[33+$i]." "; + } + readingsBulkUpdateIfChanged($hash, "RetainDataIn", $str, 1); + + readingsEndUpdate($hash, 1); #Wird benötigt um das Update der Readings zu beenden. Die "1" gibt an, dass alle Ereignisse ausgelöst werden sollen (für "notify") +} + +##################################### +# Configuration for Model -L- +##################################### + +my @PiXtendV2L_Set = ( + #entspricht CODESYS-Paramtern: + "_JumperSettingAI0:10V,5V", + "_JumperSettingAI1:10V,5V", + "_JumperSettingAI2:10V,5V", + "_JumperSettingAI3:10V,5V", + "_GPIO0Ctrl:input,output,DHT11,DHT22", + "_GPIO1Ctrl:input,output,DHT11,DHT22", + "_GPIO2Ctrl:input,output,DHT11,DHT22", + "_GPIO3Ctrl:input,output,DHT11,DHT22", + "_GPIOPullupsEnable:no,yes", + "_WatchdogEnable:disabled,125ms,1s,8s", + "_StateLEDDisable:no,yes", + + #entspricht CODESYS-Variablen: + "Reset:noArg", + "SafeState:noArg", + "RetainCopy:off,on", + "RetainEnable:off,on", + "DigitalDebounce01:textField", + "DigitalDebounce23:textField", + "DigitalDebounce45:textField", + "DigitalDebounce67:textField", + "DigitalDebounce89:textField", + "DigitalDebounce1011:textField", + "DigitalDebounce1213:textField", + "DigitalDebounce1415:textField", + "DigitalOut0:on,off,toggle", + "DigitalOut1:on,off,toggle", + "DigitalOut2:on,off,toggle", + "DigitalOut3:on,off,toggle", + "DigitalOut4:on,off,toggle", + "DigitalOut5:on,off,toggle", + "DigitalOut6:on,off,toggle", + "DigitalOut7:on,off,toggle", + "DigitalOut8:on,off,toggle", + "DigitalOut9:on,off,toggle", + "DigitalOut10:on,off,toggle", + "DigitalOut11:on,off,toggle", + "RelayOut0:on,off,toggle", + "RelayOut1:on,off,toggle", + "RelayOut2:on,off,toggle", + "RelayOut3:on,off,toggle", + "GPIOOut0:on,off,toggle", + "GPIOOut1:on,off,toggle", + "GPIOOut2:on,off,toggle", + "GPIOOut3:on,off,toggle", + "GPIODebounce01:textField", + "GPIODebounce23:textField", + "PWM0Ctrl0:textField", + "PWM0Ctrl1:slider,0,1,65535", + "PWM0A:slider,0,1,65535", + "PWM0B:slider,0,1,65535", + "PWM1Ctrl0:textField", + "PWM1Ctrl1:slider,0,1,65535", + "PWM1A:slider,0,1,65535", + "PWM1B:slider,0,1,65535", + "PWM2Ctrl0:textField", + "PWM2Ctrl1:slider,0,1,65535", + "PWM2A:slider,0,1,65535", + "PWM2B:slider,0,1,65535", + "AnalogOut0:slider,0.00,0.01,10.00,1", #...,1 damit floats nicht gerundet werden + "AnalogOut1:slider,0.00,0.01,10.00,1", + "RetainDataOut:textField" +); + +my @PiXtendV2L_Get = ( + "Version:noArg", + "SysState:noArg", + "UCState:noArg", + "UCWarnings:noArg", + "DigitalIn0:noArg", + "DigitalIn1:noArg", + "DigitalIn2:noArg", + "DigitalIn3:noArg", + "DigitalIn4:noArg", + "DigitalIn5:noArg", + "DigitalIn6:noArg", + "DigitalIn7:noArg", + "DigitalIn8:noArg", + "DigitalIn9:noArg", + "DigitalIn10:noArg", + "DigitalIn11:noArg", + "DigitalIn12:noArg", + "DigitalIn13:noArg", + "DigitalIn14:noArg", + "DigitalIn15:noArg", + "AnalogIn0:noArg", + "AnalogIn1:noArg", + "AnalogIn2:noArg", + "AnalogIn3:noArg", + "AnalogIn4:noArg", + "AnalogIn5:noArg", + "GPIOIn0:noArg", + "GPIOIn1:noArg", + "GPIOIn2:noArg", + "GPIOIn3:noArg", + "Sensor0:temperature,humidity", + "Sensor1:temperature,humidity", + "Sensor2:temperature,humidity", + "Sensor3:temperature,humidity", + "RetainDataIn:textField" +); + +sub PiXtendV2_BufferWrite_L($$) { + my ($buffer, $hash) = @_; + + #Der Aufbau "if() {}" wird benötigt, um in Perl Warnungen zu verhindern, dass ein nicht initialisierter Wert verwendet wird, wenn bisher noch nichts an die Stellen im Hash geschrieben wurde + if($hash->{DataOut}{DigitalDebounce01}) {$buffer->[9] = ($hash->{DataOut}{DigitalDebounce01});} + if($hash->{DataOut}{DigitalDebounce23}) {$buffer->[10] = ($hash->{DataOut}{DigitalDebounce23});} + if($hash->{DataOut}{DigitalDebounce45}) {$buffer->[11] = ($hash->{DataOut}{DigitalDebounce45});} + if($hash->{DataOut}{DigitalDebounce67}) {$buffer->[12] = ($hash->{DataOut}{DigitalDebounce67});} + if($hash->{DataOut}{DigitalDebounce89}) {$buffer->[13] = ($hash->{DataOut}{DigitalDebounce89});} + if($hash->{DataOut}{DigitalDebounce1011}) {$buffer->[14] = ($hash->{DataOut}{DigitalDebounce1011});} + if($hash->{DataOut}{DigitalDebounce1213}) {$buffer->[15] = ($hash->{DataOut}{DigitalDebounce1213});} + if($hash->{DataOut}{DigitalDebounce1415}) {$buffer->[16] = ($hash->{DataOut}{DigitalDebounce1415});} + if($hash->{DataOut}{DigitalOut}) {$buffer->[17] = (($hash->{DataOut}{DigitalOut}) & 0xFF); + $buffer->[18] = (($hash->{DataOut}{DigitalOut}) >> 8);} + if($hash->{DataOut}{RelayOut}) {$buffer->[19] = ($hash->{DataOut}{RelayOut});} + if($hash->{DataOut}{GPIOCtrl0}) {$buffer->[20] = ($hash->{DataOut}{GPIOCtrl0});} #Falls mehr als 4 GPIOs bereitstehen, ist auch GPIOCtrl1 verfügbar + if($hash->{DataOut}{GPIOOut}) {$buffer->[21] = ($hash->{DataOut}{GPIOOut});} + if($hash->{DataOut}{GPIODebounce01}) {$buffer->[22] = ($hash->{DataOut}{GPIODebounce01});} + if($hash->{DataOut}{GPIODebounce23}) {$buffer->[23] = ($hash->{DataOut}{GPIODebounce23});} + + if($hash->{DataOut}{PWMCtrl00}) {$buffer->[24] = ($hash->{DataOut}{PWMCtrl00});} + if($hash->{DataOut}{PWMCtrl01}) {$buffer->[25] = (($hash->{DataOut}{PWMCtrl01}) & 0xFF); + $buffer->[26] = (($hash->{DataOut}{PWMCtrl01}) >> 8);} + if($hash->{DataOut}{PWM0a}) {$buffer->[27] = (($hash->{DataOut}{PWM0a}) & 0xFF); + $buffer->[28] = (($hash->{DataOut}{PWM0a}) >> 8);} + if($hash->{DataOut}{PWM0b}) {$buffer->[29] = (($hash->{DataOut}{PWM0b}) & 0xFF); + $buffer->[30] = (($hash->{DataOut}{PWM0b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl10}) {$buffer->[31] = ($hash->{DataOut}{PWMCtrl10});} + if($hash->{DataOut}{PWMCtrl11}) {$buffer->[32] = (($hash->{DataOut}{PWMCtrl11}) & 0xFF); + $buffer->[33] = (($hash->{DataOut}{PWMCtrl11}) >> 8);} + if($hash->{DataOut}{PWM1a}) {$buffer->[34] = (($hash->{DataOut}{PWM1a}) & 0xFF); + $buffer->[35] = (($hash->{DataOut}{PWM1a}) >> 8);} + if($hash->{DataOut}{PWM1b}) {$buffer->[36] = (($hash->{DataOut}{PWM1b}) & 0xFF); + $buffer->[37] = (($hash->{DataOut}{PWM1b}) >> 8);} + + if($hash->{DataOut}{PWMCtrl20}) {$buffer->[38] = ($hash->{DataOut}{PWMCtrl20});} + if($hash->{DataOut}{PWMCtrl21}) {$buffer->[39] = (($hash->{DataOut}{PWMCtrl21}) & 0xFF); + $buffer->[40] = (($hash->{DataOut}{PWMCtrl21}) >> 8);} + if($hash->{DataOut}{PWM2a}) {$buffer->[41] = (($hash->{DataOut}{PWM2a}) & 0xFF); + $buffer->[42] = (($hash->{DataOut}{PWM2a}) >> 8);} + if($hash->{DataOut}{PWM2b}) {$buffer->[43] = (($hash->{DataOut}{PWM2b}) & 0xFF); + $buffer->[44] = (($hash->{DataOut}{PWM2b}) >> 8);} + #Retain beschreiben + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + if($hash->{DataOut}{"RetainDataOut".$i}) {$buffer->[45+$i] = ($hash->{DataOut}{"RetainDataOut".$i});} + } +} + +sub PiXtendV2_BufferRead_L($$) { + my ($buffer, $hash) = @_; + my $str; + + readingsBeginUpdate($hash); #Wird benötigt um das Update der Readings zu starten + + readingsBulkUpdateIfChanged($hash, "Firmware", $buffer->[0], 0); #letzter Parameter: 0-Kein Update in WebGui oder notify, 1- Update + readingsBulkUpdateIfChanged($hash, "Hardware", $buffer->[1], 0); + readingsBulkUpdateIfChanged($hash, "Model", pack("C",$buffer->[2]), 0); + readingsBulkUpdateIfChanged($hash, "UCState", $buffer->[3], 1); + readingsBulkUpdateIfChanged($hash, "UCWarnings", $buffer->[4], 1); + + #DigitalIn + for(my $i=0; $i < 8; $i++){ + if($buffer->[9] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + for(my $i=8; $i < 16; $i++){ + if($buffer->[10] & (1<<($i-8))) { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "DigitalIn$i", "off", 1);} + } + + #AnalogIn + $str = sprintf("%.2f", (($buffer->[11] | ($buffer->[12] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn0", $str, 1); + $str = sprintf("%.2f", (($buffer->[13] | ($buffer->[14] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn1", $str, 1); + + $str = sprintf("%.2f", (($buffer->[15] | ($buffer->[16] << 8))*($hash->{DataOut}{JumperAI0})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn2", $str, 1); + $str = sprintf("%.2f", (($buffer->[17] | ($buffer->[18] << 8))*($hash->{DataOut}{JumperAI1})/1024)); + readingsBulkUpdateIfChanged($hash, "AnalogIn3", $str, 1); + + $str = sprintf("%.2f", (($buffer->[19] | ($buffer->[20] << 8))*0.02015840023)); + readingsBulkUpdateIfChanged($hash, "AnalogIn4", $str, 1); + $str = sprintf("%.2f", (($buffer->[21] | ($buffer->[22] << 8))*0.02015840023)); + readingsBulkUpdateIfChanged($hash, "AnalogIn5", $str, 1); + + #GPIOIn + for(my $i=0; $i < 4; $i++){ + if($buffer->[23] & (1<<$i)) { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "on", 1);} + else { readingsBulkUpdateIfChanged($hash, "GPIOIn$i", "off", 1);} + } + + #DHTs + for(my $i=0; $i < 4; $i++){ + my $temp = ($buffer->[24+(4*$i)] | ($buffer->[25+(4*$i)] << 8)); + my $humi = ($buffer->[26+(4*$i)] | ($buffer->[27+(4*$i)] << 8)); + if($temp == 65535 && $humi == 65535) + { + $temp = "Sensor not connected"; + $humi = $temp; + } + else + { + if($hash->{DataOut}{"GPIO".$i."DHT"}) #Prüfen, ob Wert definiert + { + if(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht11") + { + $temp = sprintf("%.1f", ($temp/256)); + $humi = sprintf("%.1f", ($humi/256)); + + } + elsif(($hash->{DataOut}{"GPIO".$i."DHT"}) eq "dht22") + { + if($temp & (1<<15)) + { + $temp &= ~(1<<15); + $temp /= (-10); + } + else + { + $temp /= 10; + } + $temp = sprintf("%.1f", $temp); + $humi = sprintf("%.1f", ($humi/10)); + } + } + else + { + $temp = "Function not enabled"; + $humi = $temp; + } + } + readingsBulkUpdateIfChanged($hash, "Sensor".$i."T", $temp, 1); + readingsBulkUpdateIfChanged($hash, "Sensor".$i."H", $humi, 1); + } + + #Retain lesen + $str = ""; + for(my $i=0; $i < ($hash->{RetainSize}); $i++){ + $str .= $buffer->[45+$i]." "; + } + readingsBulkUpdateIfChanged($hash, "RetainDataIn", $str, 1); + + readingsEndUpdate($hash, 1); #Wird benötigt um das Update der Readings zu beenden. Die "1" gibt an, dass alle Ereignisse ausgelöst werden sollen (für "notify") +} + + + +##################################### +# General Functions +##################################### + +##################################### +sub PiXtendV2_Initialize($) { #Die Initialize-Subroutine muss auf jeden Fall in einem Modul definiert sein. Der Suffix entspricht dabei dem Datei-Namen + my ($hash) = @_; #my = variable + + $hash->{DefFn} = 'PiXtendV2_Define'; #Bekanntmachung der anderen Funktionen des Moduls. Die Keys {DefFn} sind von FHEM vorgegeben + $hash->{UndefFn} = 'PiXtendV2_Undef'; + $hash->{SetFn} = 'PiXtendV2_Set'; + $hash->{GetFn} = 'PiXtendV2_Get'; + #$hash->{DeleteFn} = 'PiXtendV2_Undef'; + $hash->{AttrFn} = 'PiXtendV2_Attr'; + #$hash->{ReadFn} = 'PiXtendV2_Read'; + #$hash->{Shutdown} = 'PiXtendV2_Undef'; + + $hash->{AttrList} = "PiXtend_GetFormat:text,value PiXtend_Parameter ".$readingFnAttributes; #Eigene Attribute + Readings Attribute +} + +##################################### +sub PiXtendV2_Define($$) { + my ($hash, $def) = @_; #@_ ist ein "Sonderzeichen" und entspricht den Funktionsparametern. Parameter sind immer der lokale Hash und alle weiteren, die bei define angegeben wurden. + my @param = split("[ \t][ \t]*", $def); #Teilt die weiteren Paramter auf und speichert sie in einem Array (@param). [0] = name, [1] = PiXtendV2, [2] = optional + + if(scalar(@param) < 2 or scalar(@param) > 3) #2 oder 3 Define-Parameter erlauben + { + return "Wrong syntax: use define <name> PiXtendV2 <optional>"; + } + + eval{ require "sys/ioctl.ph" }; #Prüfen, ob ioctl.ph existiert. + if($@){ return "ioctl.ph is not available but needed"; } #Fehler zurückgeben, falls es Probleme gab. + + if(scalar(@param) eq 2 or $param[2] eq "S") #Default oder Model S + { + $hash->{Set} = \@PiXtendV2S_Set; #Refrenez(\) auf das Array. Referenzen werden wohl nicht in der Oberfläche angezeigt, da normalerweiße alle Elemente der ersten Hash-Ebene dort angezeigt werden + $hash->{Get} = \@PiXtendV2S_Get; + $hash->{SPI}{Write} = \&PiXtendV2_BufferWrite_S; #Referenz auf die Funktionen um den Puffer zu füllen und zu leeren + $hash->{SPI}{Read} = \&PiXtendV2_BufferRead_S; + $hash->{SPI}{Length} = 67; #Anzahl Bytes + $hash->{SPI}{Speed} = 700000; #Frequenz, 700 kHz + $hash->{SPI}{Dev0} = "/dev/spidev0.0"; #Kommunikation mit dem PiXtend + $hash->{SPI}{Dev1} = "/dev/spidev0.1"; #Kommunikation mit dem DAC + $hash->{SPI}{Enable} = "/sys/class/gpio/"; #Grundpfad zu den GPIOs + $hash->{SPI}{Model} = 83; #Hardware-Modell + $hash->{RetainSize} = 32; #Anzahl der Bytes des Retain-Speichers + + #Default-Werte setzen (hier nur die Jumper-Stellungen) + $hash->{DataOut}{JumperAI0} = 10; + $hash->{DataOut}{JumperAI1} = 10; + } + elsif($param[2] eq "L") #Model L + { + $hash->{Set} = \@PiXtendV2L_Set; #Refrenez(\) auf das Array. Referenzen werden wohl nicht in der Oberfläche angezeigt, da normalerweiße alle Elemente der ersten Hash-Ebene dort angezeigt werden + $hash->{Get} = \@PiXtendV2L_Get; + $hash->{SPI}{Write} = \&PiXtendV2_BufferWrite_L; #Referenz auf die Funktionen um den Puffer zu füllen und zu leeren + $hash->{SPI}{Read} = \&PiXtendV2_BufferRead_L; + $hash->{SPI}{Length} = 111; #Anzahl Bytes + $hash->{SPI}{Speed} = 700000; #Frequenz, 700 kHz. + $hash->{SPI}{Dev0} = "/dev/spidev0.0"; #Kommunikation mit dem PiXtend + $hash->{SPI}{Dev1} = "/dev/spidev0.1"; #Kommunikation mit dem DAC + $hash->{SPI}{Enable} = "/sys/class/gpio/"; #Grundpfad zu den GPIOs + $hash->{SPI}{Model} = 76; #Hardware-Modell + $hash->{RetainSize} = 64; #Anzahl der Bytes des Retain-Speichers + + #Default-Werte setzen (hier nur die Jumper-Stellungen) + $hash->{DataOut}{JumperAI0} = 10; + $hash->{DataOut}{JumperAI1} = 10; + $hash->{DataOut}{JumperAI2} = 10; + $hash->{DataOut}{JumperAI3} = 10; + + $hash->{DataIn}{UnitAI4} = "mA"; #Wenn die Einheit nicht explizit angegeben ist (Eingänge 0-3) wird der Wert in Volt ausgegeben + $hash->{DataIn}{UnitAI5} = "mA"; + } + #elsif($param[2] eq "M") #Model M + #{ + #} + else + { + return "Parameter <$param[2]> not supported"; + } + + my $ret = undef; + $ret = PiXtendV2_Check_Device($hash->{SPI}{Dev0}); #Prüfen, ob User "fhem" in der Gruppe "spi" ist + if($ret){ return "$ret"; } + $ret = PiXtendV2_Check_Device($hash->{SPI}{Enable}."export"); #Prüfen, ob User "fhem" in der Gruppe "gpio" ist + if($ret){ return "$ret"; } + + $hash->{STATE} = "defined"; #Status aktualisieren + PiXtendV2_SPI_Transfer($hash); #SPI Transfer starten + + $attr{$hash->{NAME}}{icon} = 'RPi'; + $attr{$hash->{NAME}}{devStateIcon} = 'defined:rc_YELLOW active:rc_GREEN error:rc_RED'; + + return undef; +} + +##################################### +sub PiXtendV2_Undef($$) { + my ($hash, $arg) = @_; + + RemoveInternalTimer($hash, "PiXtendV2_SPI_Transfer"); #Zyklischen Timer stoppen + PiXtendV2_SPI_Disable($hash); #SPI-Enable Pin auf low + PiXtendV2_UC_Reset($hash); #Mikrocontroller zurücksetzen + + return undef; +} + +##################################### +sub PiXtendV2_Attr ($$$$) +{ + my ( $cmd, $name, $attrName, $attrValue ) = @_; + my $hash = $defs{$name}; + + if($cmd eq "set") + { + #GetFormat + if($attrName eq "PiXtend_GetFormat") + { + if($attrValue eq "text" or $attrValue eq "value") + { + } + else + { + return "Unknown value $attrValue for $attrName, choose one of: text, value"; + } + } + + #Parameter + if($attrName eq "PiXtend_Parameter") + { + my @coms = split(/ /, $attrValue); + for(my $i=0; $i < @coms; $i++) + { + my @param = split(/:/, $coms[$i]); + if(index($param[0], "_") != -1) + { + my $ret = PiXtendV2_Set($hash, $name, $param[0], $param[1]); + if(defined($ret)) + { + return $ret; + } + } + else + { + return "Unknown command $param[0]. Only set commands with a leading '_' sign can be used."; + } + } + } + } + return undef; +} + +##################################### +sub PiXtendV2_Get($$@) { + my ($hash, $name, $cmd, @param) = @_; + + my @InList = @{$hash->{Get}}; #kopiert die OutputListe lokal + foreach my $c (@InList) #für jeden string c in inlist + { + if(index($c, ":") != -1) #falls : vorkommt + { + $c = substr($c,0,index($c,":")); #den substring nur bis dahin nehmen + } + $c = lc($c); #zu lower case machen + } + $cmd = lc($cmd); #verändert usereingabe auch zu lower case: + if(grep($_ eq $cmd, @InList) > 0) #Prüft ob Array "outList" den Wert von "opt" enthält; auf Lower-Case-Ebene => case insensitive! || experimentelle Alternative (schlecht!): ($opt ~~ @InList) + { + my $str = undef; + my $val = undef; + + #Version + if($cmd eq "version") + { + $val = ReadingsVal($name, "Model", "?")."-".ReadingsVal($name, "Hardware", "?")."-".ReadingsVal($name, "Firmware", "?"); + $str = "FHEM-Modul-Version [$modul_ver], PiXtend-Version [$val]"; + } + + #SysState + elsif($cmd eq "sysstate") + { + $val = $hash->{STATE}; + $str = "System state is [$val]"; + } + + #UCState + elsif($cmd eq "ucstate") + { + $val = ReadingsVal($name, "UCState", "?"); + $str = "Microcontroller state is [$val]"; + } + + #UCWarnings + elsif($cmd eq "ucwarnings") + { + $val = ReadingsVal($name, "UCWarnings", "?"); + $str = "Microcontroller warnings are [$val]"; + } + + #DigitalIn + elsif(index($cmd, "digitalin") != -1) + { + $cmd =~ s/digitalin//; + $val = ReadingsVal($name, "DigitalIn$cmd", undef); + if(defined($val)) + { + $str = "DigitalIn$cmd is [$val]"; + } + } + + #AnalogIn + elsif(index($cmd, "analogin") != -1) + { + $cmd =~ s/analogin//; + $val = ReadingsVal($name, "AnalogIn$cmd", undef); + if(defined($val)) + { + if($hash->{DataIn}{"UnitAI$cmd"}) + { + my $unit = $hash->{DataIn}{"UnitAI$cmd"}; + $str = "AnalogIn$cmd is [$val] $unit"; + } + else + { + $str = "AnalogIn$cmd is [$val] V"; + } + } + } + + #GPIOIn + elsif(index($cmd, "gpioin") != -1) + { + $cmd =~ s/gpioin//; + $val = ReadingsVal($name, "GPIOIn$cmd", undef); + if(defined($val)) + { + $str = "GPIOIn$cmd is [$val]"; + } + } + + #DHTs + elsif(index($cmd, "sensor") != -1) + { + $cmd =~ s/sensor//; + + if(lc($param[0]) eq "temperature") + { + $val = ReadingsVal($name, "Sensor".$cmd."T", undef); + if(defined($val) && index($val, "not") == -1) + { + $str = "For Sensor$cmd temperature is [$val] °C"; + } + elsif(index($val, "not") != -1) + { + $str = "$val for Sensor$cmd"; + } + } + elsif(lc($param[0]) eq "humidity") + { + $val = ReadingsVal($name, "Sensor".$cmd."H", undef); + if(defined($val) && index($val, "not") == -1) + { + $str = "For Sensor$cmd humidity is [$val] %"; + } + elsif(index($val, "not") != -1) + { + $str = "$val for Sensor$cmd"; + } + } + else + { + $val = "Unknown value $param[0] for Sensorx, choose one of: temperature, humidity"; + $str = $val; + } + } + + #Retain + elsif($cmd eq "retaindatain") + { + $str = ReadingsVal($name, "RetainDataIn", undef); + if(defined($str)) + { + if(int($param[0]) >= 0 && int($param[0]) < ($hash->{RetainSize})) + { + my @vals = split(/ /, $str); + $val = $vals[$param[0]]; + $str = "Value for RetainDataIn$param[0] is [$val]"; + } + else + { + $val = "Unknown Index $param[0] for RetainDataIn, index must be between 0 and ($hash->{RetainSize}-1)"; + $str = $val; + } + } + } + + if (defined($str) && defined($val)) + { + if(AttrVal($name, "PiXtend_GetFormat", "?") eq "value") + { + return $val; + } + else + { + return $str; + } + } + } + else + { + return "Unknown argument $cmd, choose one of ". join(" ", @{$hash->{Get}}); # eine solche zeile muss enthalten sein, da fhem hier die möglichen werte raus liest + } + return undef; +} + +##################################### +sub PiXtendV2_Set($$@){ + my ($hash, $name, $cmd, @param) = @_; + + my @outList = @{$hash->{Set}}; #kopiert die OutputListe lokal + foreach my $c (@outList) #für jeden string c in outlist + { + if(index($c, ":") != -1) #falls ":" vorkommt + { + $c = substr($c,0,index($c,":")); #den substring nur bis dahin nehmen + } + $c = lc($c); #zu lower case machen + } + $cmd = lc($cmd); #verändert usereingabe auch zu lower case + + if(grep($_ eq $cmd, @outList) > 0) #Prüft ob Array "outList" den Wert von "cmd" enthält; auf Lower-Case-Ebene => case insensitive! || experimentelle Alternative (schlecht!): ($cmd ~~ @InList) + { + #Reset + if(index($cmd, "reset") != -1) + { + PiXtendV2_UC_Reset($hash); + } + + #Parameter + ############### + elsif(index($cmd, "_jumpersettingai") != -1) #Prüft ob $cmd "_jumpersettingai" als substring hat + { + $cmd =~ s/_jumpersettingai//; #löscht den substring _jumpersettingai + if(lc($param[0]) eq "10v") + { + ($hash->{DataOut}{"JumperAI".$cmd}) = 10; + } + elsif(lc($param[0]) eq "5v") + { + ($hash->{DataOut}{"JumperAI".$cmd}) = 5; + } + else + { + return "Unknown value $param[0] for JumperSetting, choose one of: 10V, 5V"; + } + } + + elsif(index($cmd, "_gpio") != -1 and index($cmd, "ctrl") != -1) + { + $cmd =~ s/_gpio//; + $cmd =~ s/ctrl//; + my $no = 0; + if($cmd < 4){$no = 0;} + elsif($cmd < 8){$no = 1; $cmd -= 4;} + else{ return "Unknown number for GPIOxCtrl";} + + if(lc($param[0]) eq "input") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~ (1<<($cmd+4)); #DHT auf 0 + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~(1<<$cmd); #Eingang (clear) + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = undef; #DHT-Modus zurücksetzen + } + elsif(lc($param[0]) eq "output") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) &= ~ (1<<($cmd+4)); #DHT auf 0 + ($hash->{DataOut}{"GPIOCtrl".$no}) |= (1<<$cmd); #Ausgang (set) + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = undef; #DHT-Modus zurücksetzen + } + elsif(lc($param[0]) eq "dht11" or lc($param[0]) eq "dht22") + { + ($hash->{DataOut}{"GPIOCtrl".$no}) |= (1<<($cmd+4)); #DHT auf 1 + ($hash->{DataOut}{"GPIO".($no*4+$cmd)."DHT"}) = lc($param[0]); #DHT-Modus für den Pin speichern + } + else + { + return "Unknown value $param[0] for GPIOxCtrl, choose one of: input, output, DHT11, DHT22"; + } + } + + elsif(index($cmd, "_gpiopullupsenable") != -1) + { + if(lc($param[0]) eq "no") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<4); + } + elsif(lc($param[0]) eq "yes") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<4); + } + else + { + return "Unknown value $param[0] for GPIOPullupsEnable, choose one of: no, yes"; + } + } + + elsif(index($cmd, "_watchdogenable") != -1) + { + if(lc($param[0]) eq "disabled") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 0; + } + elsif(lc($param[0]) eq "125ms") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 4; + } + elsif(lc($param[0]) eq "1s") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 7; + } + elsif(lc($param[0]) eq "8s") + { + ($hash->{DataOut}{UCCtrl0}) = (($hash->{DataOut}{UCCtrl0}) & 0xF0) | 10; + } + else + { + return "Unknown value $param[0] for WatchdogEnable, choose one of: disabled, 125ms, 1s, 8s"; + } + } + + elsif(index($cmd, "_stateleddisable") != -1) + { + if(lc($param[0]) eq "no") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<3); + } + elsif(lc($param[0]) eq "yes") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<3); + } + else + { + return "Unknown value $param[0] for StateLEDDisable, choose one of: no, yes"; + } + } + + elsif(index($cmd, "safestate") != -1) + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<0); #wird beim SPI-Transfer automatisch zurückgesetzt! + } + + elsif(index($cmd, "retaincopy") != -1) + { + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<1); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<1); + } + else + { + return "Unknown value $param[0] for RetainCopy, choose one of: off, on"; + } + } + + elsif(index($cmd, "retainenable") != -1) + { + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{UCCtrl1}) &= ~(1<<2); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{UCCtrl1}) |= (1<<2); + } + else + { + return "Unknown value $param[0] for RetainEnable, choose one of: off, on"; + } + } + + ############### + #RelayOut + elsif(index($cmd, "relayout") != -1) #Prüft ob $cmd "relayout" als substring hat + { + $cmd =~ s/relayout//; #löscht den substring relayout + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{RelayOut}) &= ~(1<<$cmd); #Bitweise Verknüpfung => löschen + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{RelayOut}) |= (1<<$cmd); #setzen + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{RelayOut}) ^= (1<<$cmd); #toggeln + } + else + { + return "Unknown value $param[0] for RelayOut, choose one of: on, off, toggle"; + } + } + + #DigitalOut + elsif(index($cmd, "digitalout") != -1) + { + $cmd =~ s/digitalout//; + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{DigitalOut}) &= ~(1<<$cmd); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{DigitalOut}) |= (1<<$cmd); + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{DigitalOut}) ^= (1<<$cmd); + } + else + { + return "Unknown value $param[0] for DigitalOutput, choose one of: on, off, toggle"; + } + } + + #DigitalDebounce + elsif(index($cmd, "digitaldebounce") != -1) + { + $cmd =~ s/digitaldebounce//; + $hash->{DataOut}{"DigitalDebounce".$cmd} = PiXtendV2_Check_Range(0,255,$param[0]); + } + + #GPIODebounce + elsif(index($cmd, "gpiodebounce") != -1) + { + $cmd =~ s/gpiodebounce//; + $hash->{DataOut}{"GPIODebounce".$cmd} = PiXtendV2_Check_Range(0,255,$param[0]); + } + + #GPIOOut + elsif(index($cmd, "gpioout") != -1) + { + $cmd =~ s/gpioout//; + if(lc($param[0]) eq "off") + { + ($hash->{DataOut}{GPIOOut}) &= ~(1<<$cmd); + } + elsif(lc($param[0]) eq "on") + { + ($hash->{DataOut}{GPIOOut}) |= (1<<$cmd); + } + elsif(lc($param[0]) eq "toggle") + { + ($hash->{DataOut}{GPIOOut}) ^= (1<<$cmd); + } + else + { + return "Unknown value $param[0] for GPIOOut, choose one of: on, off, toggle"; + } + } + + #AnalogOut + elsif(index($cmd, "analogout") != -1) + { + $cmd =~ s/analogout//; + if(index($param[0], ".") != -1) #Abfrage ob float (Spannung) oder int (RAW) + { + if($param[0] >= 0.00 && $param[0] <= 10.00) + { + PiXtendV2_DAC_Transfer($hash, ($param[0]*1023/10), $cmd); + } + else + { + return "Unknown value $param[0] for AnalogOut, choose a voltage between 0.00 and 10.00"; + } + } + else #Eingabe als RAW-Wert + { + if(int($param[0]) >= 0 && int($param[0]) <= 1023) + { + PiXtendV2_DAC_Transfer($hash, $param[0], $cmd); + } + else + { + return "Unknown value $param[0] for AnalogOut, choose a number between 0 and 1023"; + } + } + } + + #PWM + elsif(index($cmd, "pwm") != -1) + { + $cmd =~ s/pwm//; + if(index($cmd, "ctrl") != -1) + { + $cmd =~ s/ctrl//; + $hash->{DataOut}{"PWMCtrl".$cmd} = PiXtendV2_Check_Range(0,65535,$param[0]); + } + elsif(index($cmd, "a") != -1 or index($cmd, "b") != -1) + { + $hash->{DataOut}{"PWM".$cmd} = PiXtendV2_Check_Range(0,65535,$param[0]); + } + else + { + return "Unknown command for PWM"; + } + } + + #Retain + elsif(index($cmd, "retaindataout") != -1) + { + if(@param == 2) #Falls genau zwei Parameter übergeben wurden + { + if(int($param[0]) < ($hash->{RetainSize})) + { + if(int($param[1]) >= 0 && int($param[1]) <= 255) + { + $hash->{DataOut}{"RetainDataOut".$param[0]} = $param[1]; + } + else + { + return "Unknown value $param[1] for RetainDataOut, choose a number between 0 and 255"; + } + } + else + { + return "Unknown Index $param[0] for RetainDataOut, index must be between 0 and ($hash->{RetainSize}-1)"; + } + } + else + { + return "Too few parameters for RetainDataOut, 2 are expected"; + } + } + } + else + { + return "Unknown argument $cmd, choose one of ". join(" ", @{$hash->{Set}}); #eine Zeile dieser Art muss enthalten sein, da FHEM hier die möglichen Werte für die Oberfläche herausliest + } + return undef; +} + + +##################################### +sub PiXtendV2_Check_Range ($$$) { + my ($min, $max, $val) = @_; + if($val < $min){return $min;} + if($val > $max){return $max;} + else{return $val;} +} + +##################################### +sub PiXtendV2_Check_Device ($) { #Überprüft, ob das Gerät exisitiert und ob darauf zugegriffen werden kann. + my ($dev) = @_; + my $ret = undef; + if(-e $dev) { + if(-r $dev) { + unless(-w $dev) { + $ret = ': Error! Device not writable: '.$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot).'; + } + } else { + $ret = ': Error! Device not readable: '.$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot).'; + } + } else { + $ret = ': Error! Device not found: ' .$dev . '. Please change access rights for fhem user (sudo adduser fhem gpio; sudo adduser fhem spi; sudo reboot) or check if kernelmodules must be loaded.'; + } + return $ret; +} + +##################################### +#sub SPI_GetIOCMD ($) { #entspricht SPI_IOC_MESsAGE(N) +# my $arg = (1 << 30); #Modus des Kommandos: 1-Schreiben, 2-Lesen +# $arg |= (($_[0]*32) << 16); #Legt die Größe der zu übertragenden Daten fest. Ein "struct spi_io_transfer" entspricht 32 Bytes +# $arg |= (107 << 8); #Kommando-ID. Für SPI "k" (107). +# return $arg; +#} + +##################################### +sub PiXtendV2_calc_crc16($$$){ #entspricht dem Ablauf der CRC-Berechnung aus der Mikrocontroller-Bibliothek + my ($beg, $end, $buff) = @_; + my $crc = 0xFFFF; #Startwert der CRC-Berechnung + for(my $i=$beg; $i <= $end; $i++){ + $crc ^= $$buff[$i]; #zusätzliches "$" ist zum Dereferenzieren + for(my $k=0; $k < 8; ++$k) + { + if($crc & 1) + { + $crc = ($crc >> 1) ^ 0xA001; + } + else + { + $crc = ($crc >> 1); + } + } + } + return $crc; +} + +##################################### +sub PiXtendV2_DAC_Transfer ($) { + my ($hash, $val, $chn) = @_; + $val = ($val<<2); #Wert verschieben + $val |= (1<<12); #SHDN deaktivieren + $val |= ($chn<<15); #Kanal auswählen + $val = pack("n", $val); #Als big Endian unsigned short (16Bit) packen + my $freq = $hash->{SPI}{Speed}; #Geschwindigkeit normalerweise 700 kHz + my $device = $hash->{SPI}{Dev1}; #Der DAC hängt am spidev0.1 + my $dev = undef; + my $ret = 1; + my @buffer = (0) x 2; #Der DAC erwartet 2 Bytes + + #SPI Device öffnen und prüfen ob es ging ... + eval{ + $dev = HiPi::Device::SPI->new( + devicename => $device, + speed => $freq, + bitsperword => 8, + delay => 0, + ); + } + or do { + # SPI Device konnte nicht geöffnet werden + $ret = 0; + }; + + if($ret == 1){ + # Variable auf 2 Bytes aufteilen + ($buffer[0], $buffer[1]) = unpack("C2", $val); + # Do data transfer + my @readbytes = $dev->transfer_byte_array( @buffer ); + + return undef; + } + else + { + return "error"; #Fehler + } +} + +##################################### +sub PiXtendV2_SPI_Transfer ($) { + my ($hash) = @_; + my $len = $hash->{SPI}{Length}; + my $freq = $hash->{SPI}{Speed}; + my $device = $hash->{SPI}{Dev0}; + my $retval = -1; + my $ret = 1; + my $dev = undef; + + #SPI Kommunikation erlauben, GPIO24 setzen... + PiXtendV2_SPI_Enable($hash); + + #SPI Device öffnen und prüfen ob es ging ... + eval{ + $dev = HiPi::Device::SPI->new( + devicename => $device, + speed => $freq, + bitsperword => 8, + delay => 0, + ); + } + or do { + # SPI Device konnte nicht geöffnet werden + $ret = 0; + }; + + if($ret == 1){ + my @buffer = (0) x $len; #Puffer mit 0 initialisieren + $buffer[0] = ($hash->{SPI}{Model}); #Setzt den ersten Wert des Puffers auf die Model-Nummer + if($hash->{DataOut}{UCCtrl0}){$buffer[2] = ($hash->{DataOut}{UCCtrl0});} + if($hash->{DataOut}{UCCtrl1}){$buffer[3] = ($hash->{DataOut}{UCCtrl1});} + ($buffer[7], $buffer[8]) = unpack("C*", pack("S", PiXtendV2_calc_crc16(0,6, \@buffer))); #CRC-HeaderOut berechnen + + #Puffer füllen + $hash->{SPI}{Write}->(\@buffer, $hash); #Aufruf der Funktion, um den Puffer "buffer" zu füllen. Der Hash enthält an der Stelle SPI-Write eine Referenz auf die auszuführende Funktion. Die Werte in Klammern () sind die Funktionsparameter + + ($buffer[$len-2], $buffer[$len-1]) = unpack("C*", pack("S", PiXtendV2_calc_crc16(9,($len-3), \@buffer))); #CRC-DataOut berechnen + if($hash->{DataOut}{UCCtrl1}){($hash->{DataOut}{UCCtrl1}) &= ~(1<<0);} #SafeState-Bit zurücksetzen, damit der Mikrocontroller nach einem Reset nicht wieder sofort in den SafeState übergeht + + # Do data transfer + my @readbytes = $dev->transfer_byte_array( @buffer ); + + #Überprüfen der empfangenen Daten + if(unpack("S", pack("C2", $readbytes[7], $readbytes[8])) eq PiXtendV2_calc_crc16(0,6, \@readbytes) and #CRC HeaderIn stimmt + unpack("S", pack("C2", $readbytes[$len-2], $readbytes[$len-1])) eq PiXtendV2_calc_crc16(9,($len-3), \@readbytes)) #CRC DataIn stimmt + { + if($readbytes[2] eq $hash->{SPI}{Model}) #Model stimmt überein + { + $hash->{SPI}{Read}->(\@readbytes, $hash); #Aufruf der Funktion, um den Puffer "readbytes" zu lesen. Funktion ist als Referenz im Hash gespeichert. Parameter werden in Klammern () übergeben + $hash->{STATE} = "active"; + $hash->{ErrorMsg} = "-"; + } + else + { + $hash->{STATE} = "error"; #Error Model-Abgleich + $hash->{ErrorMsg} = "Wrong model selected"; + Debug("PiXtendV2 - DEBUG: Wrong model selected"); + } + } + else + { + $hash->{STATE} = "error"; #Error CRC-Abgleich + $hash->{ErrorMsg} = "SPI-Transfer error"; + Debug("PiXtendV2 - DEBUG: SPI-Transfer error"); + } + } + else + { + $hash->{STATE} = "error"; #Error File Handling + $hash->{ErrorMsg} = "Couldn't open SPI device"; + Debug("PiXtendV2 - DEBUG: Couldn't open SPI device"); + } + + InternalTimer(gettimeofday()+0.1, "PiXtendV2_SPI_Transfer", $hash); #Timer neustarten, um 0,1 Sekunde + return undef; +} + +##################################### +sub PiXtendV2_SPI_Enable($){ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."export", O_WRONLY); + syswrite($handler, 24); + + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio24/direction", O_WRONLY); + syswrite($handler, "out", 3); + + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio24/value", O_WRONLY); + syswrite($handler, "1", 1); + close($handler); + return 1; +} + +##################################### +sub PiXtendV2_SPI_Disable(){ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."unexport", O_WRONLY); + syswrite($handler, 24); + close($handler); + return 1; +} + +##################################### +sub PiXtendV2_UC_Reset() +{ + my ($hash) = @_; + my ($handler) = undef; + my $ret = sysopen($handler, $hash->{SPI}{Enable}."export", O_WRONLY); + syswrite($handler, 23); + #close($handler); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/direction", O_WRONLY); + syswrite($handler, "out", 3); + #close($handler); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/value", O_WRONLY); + syswrite($handler, "1", 1); + #close($handler); + usleep(1000); + $ret = sysopen($handler, $hash->{SPI}{Enable}."gpio23/value", O_WRONLY); + syswrite($handler, "0", 1); + close($handler); + return 1; +} + + + +1; + +=pod +=item device +=item summary Allows to access the PiXtendV2 (PLC). +=item summary_DE Ermöglicht den Zugriff auf den PiXtendV2 (SPS) +=begin html + +<a name="PiXtendV2"></a> +<h3>PiXtendV2</h3> +<ul> + + PiXtend is a programmable logic controller (PLC) based on the Raspberry Pi. + This FHEM-module allows to access the functions of the PiXtendV2-Board through the FHEM interface. + PiXtend offers a variety of digital and analog inputs and outputs which are designed for industry standards + and safe connections and thus is ideally suited for home automation. + For more information about PiXtend(R) and this FHEM-module, see + <a href="https://www.pixtend.de" target="_blank">www.pixtend.de</a>. + + <br><br> + + + <a name="PiXtendV2Define"></a> + <b>Define</b> + <ul> + <code>define <name> PiXtendV2 <optional></code> + <br><br> + The parameter "optional" is used to determine the hardware model, e.g. S or L. If the parameter is not used a FHEM device for model S is created automatically. + <br><br> + Example: + <ul> + <code>define pix PiXtendV2</code>    => creates a FHEM device for model S<br> + <code>define pix PiXtendV2 S</code>   => creates a FHEM device for model S<br> + <code>define pix PiXtendV2 L</code>   => creates a FHEM device for model L<br> + </ul> + </ul> + <br> + + + <a name="PiXtendV2Set"></a> + <b>Set</b> + <ul> + Commands to configure the basic settings of the PiXtend start with an "_".<br> + If a command supports multiple channels the "#"-sign has to be replaced with the channel number.<br> + All Set-commands are case insensitive to guarantee an easy use.<br> + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br> + Example: + <ul> + <code>set pix relayOut0 on</code><br> + <code>set pix Relayout0 On</code><br> + <code>set pix rElAyoUT0 oFf</code><br> + </ul> + <br><br> + + <li>_GPIO#Ctrl [input,output,DHT11,DHT22]<br> + With this setting the GPIO can be configured as [input], [output] or as [DHT11] or [DHT22] as well, if a DHT sensor is connected to this GPIO. + If a DHT is selected and connected, the GPIO can't simultaniously be used as a normal GPIO but the temperatur and humidity is read automatically. + <br><br></li> + + <li>_GPIOPullupsEnable [yes,no]<br> + This setting enables [yes] or disables [no] the possibility to set the internal pullup resistors via GPIOOut setting for all GPIOs. + <br><br></li> + + <li>_JumperSettingAI# [5V,10V]<br> + This setting affects the calculation of the voltage on the analog inputs and refers to the actual setting of the physical jumper on the PiXtend-Board [5V,10V]. + The default value is [10V] if no jumper is used. + <br><br></li> + + <li>_StateLEDDisable [yes,no]<br> + This setting disables [yes] or enables [no] the state LED on the PiXtend. If the LED is disabled it won't light up in case of an error. + <br><br></li> + + <li>_WatchdogEnable [disable,125ms,1s,8s]<br> + This setting allows to configure the watchdog timer. If the watchdog is configured, the PiXtend will go to safe state if no valid data transfer has + occured within the selected timeout and thus can't be accessed anymore without a reset. + <br><br></li> + + <li>AnalogOut# []<br> + Sets the analog output to the selected voltage. The value can be a voltage between 0 V and 10 V + or a raw value between 0 and 1023. To set the value to a voltage the value has to include a + "." even if it is an even number. + <br><br> + Example: + <ul> + <code>set pix analogout0 2.5</code>   => sets analog output 0 to 2.5 V<br> + <code>set pix analogout0 4.0</code>   => sets analog output 0 to 4 V<br> + <code>set pix analogout0 223</code>   => sets analog output 0 to 10*(233/1024) = 1.09 V + </ul> + <br><br></li> + + <li>DigitalDebounce# [0-255]<br> + Allows to debounce the digital inputs. A setting always affects two channels. So DigitalDebounce01 affects DigitalIn0 and DigitalIn1. + The resulting delay is calculated by (selected value)*(100 ms). The selected value can be any number between 0 and 255. + Debouncing can be useful if a switch or button is connected to this input. + <br><br> + Example: + <ul> + <code>set pix digitaldebounce01 20</code>   => debounces DigitalIn0 and DigitalIn1 over (20*100ms) = 2s + </ul> + <br><br></li> + + <li>DigitalOut# [on,off,toggle]<br> + Set the digital output HIGH [on] or LOW [off] or [toggle]s it. + <br><br></li> + + <li>GPIODebounce# [0-255]<br> + Allows to debounce the GPIO inputs. A setting always affects two channels. So GPIODebounce01 affects GPIOIn0 and GPIOIn1. + The resulting delay is calculated by (selected value)*(100 ms). The selected value can be any number between 0 and 255. + Debouncing can be useful if a switch or button is connected to this input. + <br><br> + Example: + <ul> + <code>set pix gpiodebounce23 33</code>   => debounces GPIOIn2 and GPIOIn3 over (33*100ms) = 3.3s + </ul> + <br><br></li> + + <li>GPIOOut# [on,off,toggle]<br> + Set the GPIO to HIGH [on] or LOW [off] or [toggle]s it, if it is configured as an output. + If it is configured as an input, this command can enable [on] or disable [off] or [toggle] the internal pullup resistor for that GPIO, + but therefore pullups have to be enabled globally via _GPIOPullupsEnable. + <br><br></li> + + <li>PWM<br> + The PiXtendV2 supports various PWM-Modes, which can be configured with this settings. + For example a servo-mode to control servo-motors, a frequency-mode or a duty-cycle-mode are supported. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br> + PWM#Ctrl0 needs a value between 0 and 255<br> + PWM#Ctrl1 needs a value between 0 and 65535 (or a value between 0 and 255, see exception for model -S-)<br> + PWM#A/B needs a value between 0 and 65535 (or a value between 0 and 255, see exception for model -S-) + <br><br></li> + + <li>RelayOut# [on,off,toggle]<br> + Set the relay to HIGH [on] or LOW [off] or [toggle]s it. + <br><br></li> + + <li>Reset<br> + Resets the controller on the PiXtend, for example if it is in safe state and allows to access it again. + <br><br></li> + + <li>RetainCopy [on,off]<br> + If RetainCopy is enabled [on] the RetainDataOut that is written to the PiXtend will be received in RetainDataIn again. + This can be useful in some situations to see which data was send to the PiXtend. + If it is disabled [off] the last stored data will be received. + <br><br></li> + + <li>RetainDataOut [0-(RetainSize-1)] [0-255]<br> + The PiXtendV2 supports the storage of retain data. If enabled, this data is stored in case of a power failure or if it is triggered by a watchdog timeout or the safe state command. + The retain data is organized in bytes and each byte can be written individually with a value between 0 and 255.<br> + As first parameter the command needs the byte index which is between 0 and the (RetainSize-1). The RetainSize is shown in the "Internals". + As the second parameter a value is expected which should be stored. + <br><br> + Example: + <ul> + <code>set pix retaindataout 0 34</code>    => stores 34 in retain-data-byte 0<br> + <code>set pix retaindataout 30 222</code>   => stores 222 in retain-data-byte 30 + </ul> + <br><br></li> + + <li>RetainEnable [on,off]<br> + The function of storing retain data on the PiXtend has to be enabled [on], otherwise no data is stored [off]. + The memory in which the data is stored supports 10.000 write-cycles. + So the storage of retain data should only be used if it is really necessary. + <br><br></li> + + <li>SafeState<br> + This setting allows to force the PiXtend to enter the safe state . If retain storage is enabled the data will be stored. In safe state the PiXtend won't communicate with FHEM and can't be configured anymore. + To restart the PiXtend a reset has to be done. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Get"></a> + <b>Get</b> + <ul> + If a command supports multiple channels the "#"-sign has to be replaced with the channel number.<br> + All Get-commands are case insensitive to guarantee an easy use.<br> + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + The values can be returned as a string where the actual value is inside squared brackets or as raw values. + To change the returned value the attribute "PiXtend_GetFormat" has to be set. + <br><br> + + <li>AnalogIn#<br> + Returns the Value of the selected analog input. + The result depends on the selected _JumperSettingAI# and the actual physical jumper position on the board, as well as the kind of measurement. + For example AnalogIn4 and AnalogIn5 on model -L- are used to measure a current instead of a voltage. + <br><br></li> + + <li>DigitalIn#<br> + Returns the state on (HIGH) or off (LOW) of the digital input. + <br><br></li> + + <li>GPIOIn#<br> + Returns the state on (HIGH) or off (LOW) of the GPIO, independant of its configuration (input, output, ..). + <br><br></li> + + <li>RetainDataIn [0-(RetainSize-1)]<br> + Returns the value of the selected RetainDataIn-byte. + <br><br></li> + + <li>Sensor# [temperature,humidity]<br> + If a DHT-Sensor is connected to the corresponding GPIO and the _GPIO#Ctrl is set to DHT11 or DHT22 the + temperature and humidity are measured and can be read. + <br><br> + Example: + <ul> + <code>set pix _GPIO0Ctrl DHT11</code><br> + <code>get pix Sensor0 temperature</code> + </ul> + <br><br></li> + + <li>SysState<br> + Returns the system state [defined, active, error] of the FHEM module. + <br><br></li> + + <li>UCState<br> + Returns the state of the PiXtend. If it is 1 everything is fine. If it is greater than 1 an error occured or is present and the PiXtend can't be configured. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>UCWarnings<br> + Returns a value that represents the PiXtend warnings. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>Version<br> + Returns the FHEM-module version and the microcontroller version [Model-Hardware-Firmware]. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Readings"></a> + <b>Readings</b> + <ul> + The FHEM-module for PiXtendV2 supports multiple readings from which most of them trigger an event when they have changed. + The meaning of the readings is similar to the Get-commands. + <br><br> + + <li>AnalogIn#<br> + Shows the result of the measurment on the analog inputs in V or mA. + <br><br></li> + + <li>DigitalIn#<br> + Shows the state on (HIGH) or off (LOW) of the digital inputs. + <br><br></li> + + <li>Firmware<br> + Shows the firmware version. + <br><br></li> + + <li>GPIOIn#<br> + Shows the state on (HIGH) or off (LOW) of the GPIOs, independant of their configuration (input, output, ..). + <br><br></li> + + <li>Hardware<br> + Shows the hardware version. + <br><br></li> + + <li>Model<br> + Shows the model. + <br><br></li> + + <li>RetainDataIn<br> + Shows the values of the RetainDataIn. + The values of RetainDataIn are combined in one row, whereby the most left value represents Byte0 / RetainDataIn0. + Each value is seperated by an " " and thus can be parsed very easy in perl: + <br><br> + Example: + <ul> + <code>my ($str) = ReadingsVal(pix, "RetainDataIn", "?")</code><br> + <code>if($str ne "?"){</code><br> +  <code>my @val = split(/ /, $str);</code>   => $val[0] contains Byte0, $val[1] Byte1, ...<br> +  <code>...</code><br> + <code>}</code> + </ul> + <br><br></li> + + <li>Sensor#T/H<br> + Shows the temperature (T) in °C and the humidity (H) in % of the sensor that is connected to the corresponding GPIO. + <br><br></li> + + <li>UCState<br> + Shows the state of the PiXtend. If it is 1 everything is fine. If it is greater than 1 an error occured or is present and the PiXtend can't be configured. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + <li>UCWarnings<br> + Shows a value that represents the PiXtend warnings. + For more information see the manuel for PiXtendV2 in the + <a href="https://www.pixtend.de/downloads/" target="_blank">download section</a> + of our homepage. + <br><br></li> + + </ul> + <br> + + <a name="PiXtendV2Attr"></a> + <b>Attributes</b> + <ul> + The attribute name is case-sensitive. + <br><br> + + <li>PiXtend_GetFormat [text,value]<br> + Changes the style in which the values of the Get commands are returned. They can be returned as a message [text] or as a raw [value]. + Default is the presentation as a message. + <br><br></li> + + <li>PiXtend_Parameter<br> + With this attribute the base configuration (Set commands with a leading "_") can be saved as an attribute. + Attributes are stored in the config file. Single commands are seperated with a space " " and each value is seperated by a ":". + <br><br> + Example: + <ul> + <code>attr pix PiXtend_Parameter _gpio0ctrl:dht11 _gpio3ctrl:dht22</code> + </ul> + <br><br></li> + + </ul> + <br> + +</ul> + +=end html + +=begin html_DE + +<a name="PiXtendV2"></a> +<h3>PiXtendV2</h3> +<ul> + + PiXtend ist eine speicherprogrammierbare Steuerung auf Basis des Raspberry Pi. + Dieses FHEM-Modul ermöglicht dabei den Zugriff auf die Funktionen des PiXtendV2-Boards in der FHEM-Oberfläche. + Der PiXtend bietet dabei eine Vielzahl an digitalen und analogen Ein- und Ausgängen, die nach Industrie-Standards ausgelegt sind + und ist aufgrund der sicheren Anschlüsse auch ideal für die Hausautomatisierung geeignet. + Für mehr Informationen über PiXtend(R) und das FHEM-Modul besuchen Sie unsere Website + <a href="https://www.pixtend.de" target="_blank">www.pixtend.de</a>. + + <br><br> + + + <a name="PiXtendV2Define"></a> + <b>Define</b> + <ul> + <code>define <name> PiXtendV2 <optional></code> + <br><br> + Der Parameter "optional" bestimmt für welches Hardware-Modell ein FHEM-Gerät angelegt werden soll, z.B. S oder L. + Wird der Parameter weggelassen, wird automatisch ein FHEM-Gerät für Model -S- angelegt. + <br><br> + Beispiel: + <ul> + <code>define pix PiXtendV2</code>    => erzeugt ein FHEM-Gerät für Model S<br> + <code>define pix PiXtendV2 S</code>   => erzeugt ein FHEM-Gerät für Model S<br> + <code>define pix PiXtendV2 L</code>   => erzeugt ein FHEM-Gerät für Model L<br> + </ul> + </ul> + <br> + + + <a name="PiXtendV2Set"></a> + <b>Set</b> + <ul> + Kommandos um die Basiskonfiguration für den PiXtend durchzuführen, beginnen mit einem "_".<br> + Unterstützt ein Kommando mehrere Kanäle, muss das "#"-Zeichen durch die Kanal-Nummer ersetzt werden.<br> + Alle Set-Kommandos sind unabhängig von der Groß-/Kleinschreibung um die einfache Benutzung zu ermöglichen.<br> + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br> + Beispiel: + <ul> + <code>set pix relayOut0 on</code><br> + <code>set pix Relayout0 On</code><br> + <code>set pix rElAyoUT0 oFf</code><br> + </ul> + <br><br> + + <li>_GPIO#Ctrl [input,output,DHT11,DHT22]<br> + Mit dieser Einstellung kann die Funktion des GPIO eingestellt werden. [input], [output] oder [DHT11] und [DHT22] wenn ein DHT-Sensor an den GPIO angeschlossen ist. + Wenn ein DHT-Sensor angeschlossen ist und verwendet wird, kann die normale Funktion des GPIO als Eingang/Ausgang nicht gleichzeitig verwendet werden. + <br><br></li> + + <li>_GPIOPullupsEnable [yes,no]<br> + Diese Einstellung aktiviert [yes] oder deaktiviert [no] für alle GPIOs die Möglichkeit die internen PullUp-Widerstände durch GPIOOut zu setzen. + <br><br></li> + + <li>_JumperSettingAI# [5V,10V]<br> + Diese Einstellung beeinflusst die Berechnung der Spannung durch die analogen Eingänge und bezieht sich dabei auf die tatsächliche Position des Jumpers + auf dem PiXtend-Board [5V,10V]. Wenn kein Jumper verwendet wird, entspricht das der Standardeinstellung von [10V]. + <br><br></li> + + <li>_StateLEDDisable [yes,no]<br> + Diese Einstellung deaktiviert [yes] oder aktiviert [no] die Status-LED auf dem PiXtend. Wenn die LED deaktiviert ist, leuchtet sie im Fehlerfall nicht auf. + <br><br></li> + + <li>_WatchdogEnable [disable,125ms,1s,8s]<br> + Diese Einstellung ermöglicht die Konfiguration des Watchdog-Timers. Wenn der Watchdog konfiguriert ist, geht der PiXtend in den Sicheren Zustand + über, falls innerhalb der eingestellten Zeit keine gültige Übertragung zwischen PiXtend und Raspberry Pi stattgefunden hat. + Im Sicheren Zustand kann der PiXtend erst wieder angesprochen werden, nachdem ein Reset des PiXtend durchgeführt wurde. + <br><br></li> + + <li>AnalogOut# []<br> + Stellt am analogen Ausgang eine Spannung ein. Der übergebene Wert kann eine Spannung zwischen 0 V und 10 V + oder ein Rohwert zwischen 0 und 1023 sein. Um den Wert als Spannung zu übergeben, muss der Wert ein "." enthalten, + auch wenn der Wert ganzzahlig ist. + <br><br> + Beispiel: + <ul> + <code>set pix analogout0 2.5</code>   => Setzt den analogen Ausgang 0 auf 2,5 V<br> + <code>set pix analogout0 4.0</code>   => Setzt den analogen Ausgang 0 auf 4 V<br> + <code>set pix analogout0 223</code>   => Setzt den analogen Ausgang 0 auf 10*(233/1024) = 1,09 V + </ul> + <br><br></li> + + <li>DigitalDebounce# [0-255]<br> + Ermöglicht das Entprellen der digitalen Eingänge. Die Einstellung beeinflusst dabei immer zwei Kanäle. + DigitalDebounce01 beeinflusst somit DigitalIn0 und DigitalIn1. + Die resultierende Verzögerung berechnet sich dabei durch (eingestellten Wert)*(100 ms). + Der übergebene Wert kann eine beliebige Zahl zwischen 0 und 255 sein. + Entprellen kann sinnvoll sein, falls an den Eingängen Schalter oder Taster angeschlossen sind. + <br><br> + Beispiel: + <ul> + <code>set pix digitaldebounce01 20</code>   => entprellt DigitalIn0 und DigitalIn1 über (20*100ms) = 2s + </ul> + <br><br></li> + + <li>DigitalOut# [on,off,toggle]<br> + Setzt den digitalen Ausgang auf HIGH [on] oder LOW [off] oder [toggle]t ihn. + <br><br></li> + + <li>GPIODebounce# [0-255]<br> + Ermöglicht das Entprellen der GPIO Eingänge. Die Einstellung beeinflusst dabei immer zwei Kanäle. + GPIODebounce01 beeinflusst somit GPIOIn0 und GPIOIn1. + Die resultierende Verzögerung berechnet sich dabei durch (eingestellten Wert)*(100 ms). + Der übergebene Wert kann eine beliebige Zahl zwischen 0 und 255 sein. + Entprellen kann sinnvoll sein, falls an den Eingängen Schalter oder Taster angeschlossen sind. + <br><br> + Beispiel: + <ul> + <code>set pix gpiodebounce23 33</code>   => entprellt GPIOIn2 und GPIOIn3 über (33*100ms) = 3,3s + </ul> + <br><br></li> + + <li>GPIOOut# [on,off,toggle]<br> + Setzt den GPIO auf HIGH [on] oder LOW [off] oder [toggle]t ihn, falls er als Ausgang konfiguriert ist. + Wenn der GPIO als Eingang konfiguriert ist, kann mit diesem Kommando der interne PullUp-Widerstand aktiviert [on], deaktiviert [off] oder + ge[toggle]t werden. Dazu muss die Möglichkeit allerdings global durch _GPIOPullupsEnable aktiviert werden. + <br><br></li> + + <li>PWM<br> + PiXtendV2 unterstützt mehrere PWM-Modi, die mit diesen Einstellungen konfiguriert werden können. + Zum Beispiel wird ein Servo-Mode um Modellbau-Servomotoren anzusteuern, ein Frequency-Mode oder ein Duty-Cycle-Mode unterstüzt. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br> + PWM#Ctrl0 benötigt einen Wert zwischen 0 und 255<br> + PWM#Ctrl1 benötigt einen Wert zwischen 0 und 65535 (oder einen Wert zwischen 0 und 255, siehe Ausnahme Model -S-)<br> + PWM#A/B benötigt einen Wert zwischen 0 und 65535 (oder einen Wert zwischen 0 und 255, siehe Ausnahme Model -S-) + <br><br></li> + + <li>RelayOut# [on,off,toggle]<br> + Setzt das Relay auf HIGH [on] oder LOW [off] oder [toggle]t es. + <br><br></li> + + <li>Reset<br> + Setzt den Controller auf dem PiXtend zurück, z.B. wenn er sich im Sicheren Zustand befindet, um ihn erneut konfigurieren zu können. + <br><br></li> + + <li>RetainCopy [on,off]<br> + Wenn RetainCopy aktiviert [on] ist, werden die geschriebenen Daten RetainDataOut vom PiXtend in RetainDataIn zurückgegeben. + Die Aktivierung kann in Situationen sinnvoll sein, wenn überprüft werden soll, welche Daten an den PiXtend geschickt wurden. + Ist die Funktion deaktiviert [off] werden die zuletzt gespeicherten Daten in RetainDataIn zurückgegeben. + <br><br></li> + + <li>RetainDataOut [0-(RetainSize-1)] [0-255]<br> + Der PiXtendV2 unterstüzt die Speicherung remanenter/persistenter Daten - auch Retain genannt. Diese Daten werden im Falle + einer Betribsspannungsunterbrechung, beim Auslösen des Watchdog-Timers oder beim Entritt in den Sicheren Zustand gespeichert, + sofern diese Funktion aktiviert wurde. Die Retain-Daten sind dabei in Bytes organisiert, wobei jedes Byte + individuell mit einem Wert zwischen 0 und 255 beschrieben werden kann.<br> + Als ersten Parameter erwartet das Kommando den Index des Bytes, der zwischen 0 und (RetainSize-1) liegt. RetainSize ist in den "Internals" zu finden. + Als zweiter Parameter wird der Wert erwartet, der gespeichert werden soll. + <br><br> + Beispiel: + <ul> + <code>set pix retaindataout 0 34</code>    => speichert 34 in Retain-Data-Byte 0<br> + <code>set pix retaindataout 30 222</code>   => speichert 222 in Retain-Data-Byte 30 + </ul> + <br><br></li> + + <li>RetainEnable [on,off]<br> + Die Funktion um Retain-Daten auf dem PiXtend zu speichern muss erst aktiviert [on] werden. Andernfalls [off] werden keine Daten gespeichert. + Es ist zu beachten, dass für den Retain-Speicherbereich 10.000 Schreibzyklen unterstützt werden. Dementsprechend + sollte die Funktion nur aktiviert werden, wenn sie tatsächlich benötigt wird. + <br><br></li> + + <li>SafeState<br> + Mit dieser Einstellung kann der PiXtend in den Sicheren Zustand versetzt werden. Wenn die Retain-Speicherung aktiviert ist, werden die Daten gesichert. + Im Sicheren Zustand kommuniziert der PiXtend nicht mehr mit FHEM. Um den PiXtend neuzustarten muss ein Reset durchgeführt werden. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Get"></a> + <b>Get</b> + <ul> + Unterstützt ein Kommando mehrere Kanäle, muss das "#"-Zeichen durch die Kanal-Nummer ersetzt werden.<br> + Alle Get-Kommandos sind unabhängig von der Groß-/Kleinschreibung um die einfache Benutzung zu ermöglichen.<br> + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach.<br> + Die Werte können als Text, wobei die Werte in eckigen Klammern stehen oder als rohe Werte zurückgegeben werden. + Die Einstellung für das Format ist in den Attributen ("PiXtend_GetFormat") zu finden. + <br><br> + + <li>AnalogIn#<br> + Gibt den Wert des ausgewählten analogen Eingangs zurück. + Der Wert hängt dabei von der Einstellung _JumperSettingAI# und der tatsächlichen Jumper-Position auf dem Board ab, sowie der Messmethode des Kanals ab. + AnalogIn4 und AnalogIn5 bei Modell -L- sind z.B. Stromeingänge. + <br><br></li> + + <li>DigitalIn#<br> + Gibt den Status on (HIGH) oder off (LOW) des digitalen Eingangs zurück. + <br><br></li> + + <li>GPIOIn#<br> + Gibt den Status on (HIGH) oder off (LOW) des GPIOs zurück, unabhängig von der Konfiguration (input, output, ..). + <br><br></li> + + <li>RetainDataIn [0-(RetainSize-1)]<br> + Gibt den Wert des ausgewählten RetainDataIn-Bytes zurück. + <br><br></li> + + <li>Sensor# [temperature,humidity]<br> + Wenn ein DHT-Sensor an den entsprechenden GPIO angeschlossen ist und _GPIO#Ctrl auf DHT11 oder DHT22 gesetzt ist + wird die Temperatur und Luftfeuchtigkeit gemessen und kann ausgelesen werden. + <br><br> + Beispiel: + <ul> + <code>set pix _GPIO0Ctrl DHT11</code><br> + <code>get pix Sensor0 temperature</code> + </ul> + <br><br></li> + + <li>SysState<br> + Gibt den Systemstatus [defined, active, error] des FHEM-Moduls zurück. + <br><br></li> + + <li>UCState<br> + Gibt den Status des PiXtend zurück. Ist der Status 1, ist alles in Ordnung. Ist der Status allerdings größer als 1 ist ein Fehler aufgetreten + oder steht noch an. In diesem Fall kann der PiXtend nicht konfiguriert werden. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>UCWarnings<br> + Der zurückgegebene Wert repräsentiert die Warnungen des PiXtendV2. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>Version<br> + Gibt die Version des FHEM-Moduls sowie die PiXtend-Version [Model-Hardware-Firmware] zurück. + <br><br></li> + + </ul> + <br> + + + <a name="PiXtendV2Readings"></a> + <b>Readings</b> + <ul> + Das FHEM-Modul des PiXtend unterstüzt mehrere Readings, von denen die meisten ein Event auslösen, sobald sie sich ändern. + Die Bedeutung der Readings ist ähnlich zu den Get-Kommandos. + <br><br> + + <li>AnalogIn#<br> + Zeigt das Ergebnis der Messungen der analogen Eingänge in V beziehungsweise in mA an. + <br><br></li> + + <li>DigitalIn#<br> + Zeigt den Status on (HIGH) oder off (LOW) der digitalen Eingänge an. + <br><br></li> + + <li>Firmware<br> + Zeigt die Firmware-Version an. + <br><br></li> + + <li>GPIOIn#<br> + Zeigt den Status on (HIGH) oder off (LOW) der GPIOs, unabhängig von deren Konfiguration (input, output, ..). + <br><br></li> + + <li>Hardware<br> + Zeigt die Hardware-Version an. + <br><br></li> + + <li>Model<br> + Zeigt das Model an. + <br><br></li> + + <li>RetainDataIn<br> + Zeigt die Werte von RetainDataIn an. Die Werte von RetainDataIn sind dabei in einer Zeile + zusammengefasst. Der am weitsten links stehende Wert entspricht Byte0 / RetainDataIn0. + Die Werte sind durch ein Leerzeichen " " voneinander getrennt und können somit einfach in Perl ausgewertet werden: + <br><br> + Beispiel: + <ul> + <code>my ($str) = ReadingsVal(pix, "RetainDataIn", "?")</code><br> + <code>if($str ne "?"){</code><br> +  <code>my @val = split(/ /, $str);</code>   => $val[0] enthält nun Byte0, $val[1] Byte1, usw<br> +  <code>...</code><br> + <code>}</code> + </ul> + <br><br></li> + + <li>Sensor#T/H<br> + Zeigt die Temperatur (T) in °C und die Luftfeuchtigkeit (H) in % des Sensors an, der an den entsprechenden GPIO angeschlossen ist. + <br><br></li> + + <li>UCState<br> + Zeigt den Status des PiXtend an. Ist der Status 1, ist alles in Ordnung. Ist der Status allerdings größer als 1 ist ein Fehler aufgetreten + oder steht noch an. In diesem Fall kann der PiXtend nicht konfiguriert werden. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + <li>UCWarnings<br> + Der angezeigte Wert repräsentiert die Warnungen des PiXtendV2. + Für mehr Informationen sehen Sie bitte im Handbuch für den PiXtendV2 im + <a href="https://www.pixtend.de/downloads/" target="_blank">Downloadbereich</a> + unserer Hompage nach. + <br><br></li> + + </ul> + <br> + + <a name="PiXtendV2Attr"></a> + <b>Attributes</b> + <ul> + Für den Attribut-Namen muss die Groß-/Kleinschreibung beachtet werden. + <br><br> + + <li>PiXtend_GetFormat [text,value]<br> + Ändert die Darstellung, wie die Werte durch die Get-Kommandos zurückgegeben werden. Die Werte können entweder in einer Nachricht [text] oder als rohe Werte [value] zurückgegeben werden. + Standard ist die Ausgabe als Text. + <br><br></li> + + <li>PiXtend_Parameter<br> + Dieses Attribut kann verwendet werden, um die Einstellungen zur Basiskonfiguration (Set-Kommandos beginnend mit "_") als Attribut zu speichern. Attribute werden im Gegensatz zu Set-Kommandos in der Config-Datei gespeichert.<br> + Einzelne Kommandos werden durch ein Leerzeichen voneinander getrennt und erhalten ihre Werte nach einem Doppelpunkt. + <br><br> + Beispiel: + <ul> + <code>attr pix PiXtend_Parameter _gpio0ctrl:dht11 _gpio3ctrl:dht22</code> + </ul> + <br><br></li> + + </ul> + <br> + +</ul> + +=end html_DE + +=cut diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..9692a6b5e9c50f8b16a499b1cf79a23bc70835b2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,34 @@ +Changelog +============== + +1.3 (2021-10-26) +------------------ +* Changed company address in license header +* Adjusted version number for both files to 1.3 + +1.22 (2021-05-07) +------------------ + +* 97_PiXtendV2_hipi.pm +* Added 97_PiXtendV2_hipi.pm as an alternative version of the 97_PiXtendV2.pm which uses the HiPi library to access the SPI bus on the Raspberry Pi. +* Changed some sub names to potentially avoid collision with other subs in the main context. Also see this post for more information on this version of the module: https://forum.fhem.de/index.php?topic=116308.msg1154062#msg1154062 + +1.03 (2021-05-05) +------------------ +* 97_PiXtendV2.pm +* Changed some sub names to potentially avoid collision with other subs in the main context. If this main version does not work with your kernel, please see this post for an alternate version of this module: https://forum.fhem.de/index.php?topic=116308.msg1154062#msg1154062 + +1.02 (2021-03-01) +------------------ +* 97_PiXtendV2.pm +* Changed company name to Kontron Electronics GmbH + +1.01 (2018-08-01) +------------------ +* 97_PiXtendV2.pm +* Added functionality to the module to support the PiXtend V2 -L- + +1.0 (2017-10-01) +------------------ +* 97_PiXtendV2.pm +* First Release diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..20d40b6bceca3a6c0237d7455ebf1820aeff3680 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is 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 3 of the License, or + (at your option) any later version. + + This program 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<http://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<http://www.gnu.org/philosophy/why-not-lgpl.html>. \ No newline at end of file diff --git a/README.md b/README.md index 5a545136b8f93fee063ee7a9a29754859197bcda..de2522705c188ef1c06aadd7314b6d7a05afd103 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,30 @@ -# fhem +# FHEM + +This FHEM module gives users full access to the functions of the PiXtend V2 devices within the FHEM environment (www.fhem.de). + +For more Information, please visit our homepage and download the software manual: https://www.pixtend.de/downloads/ + +There are 2 versions of the PiXtend V2 FHEM module: + +* 97_PiXtendV2.pm + * The PiXtend V2 driver/module for FHEM, the original version using the ioctl function +* 97_PiXtendV2_hipi.pm + * The PiXtend V2 driver/module for FHEM modified to use the HiPi library to access the SPI bus on the Raspberry Pi, if a kernel version newer then 5.5 is used. Starting with this version the ioctl function in perl no longer works as before. To use this module with FHEM, delete the 97_PiXtendV2.pm file, rename this file to "97_PiXtendV2.pm" and replace the one in your FHEM module folder, then install the HiPi library. + +### Installing the HiPi library + +To be able to use the PiXtend V2 FHEM module based on the HiPi library, the perl HiPi library has to be manually installed on the Raspberry Pi. + +Use these steps to install the library: + +```shell +wget http://hipi.znix.com/download/libhipi-perl_0.85-1_armhf.deb +sudo apt-get update +sudo dpkg --install libhipi-perl_0.85-1_armhf.deb +sudo apt-get -y -f install +``` + + + +Also see this forum post for more information on this topic (German): https://forum.fhem.de/index.php?topic=116308.msg1154062#msg1154062 -This FHEM module gives users full access to the functions of the PiXtend V2 devices within the FHEM environment. \ No newline at end of file