mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-02-01 01:09:47 +00:00
changed access to gpio via userspace by default (for BBB and Cubie)
access via gpio utility as fallback updated commandref git-svn-id: https://svn.fhem.de/fhem/trunk@6207 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
fc130a6a18
commit
766f77e8f3
@ -8,7 +8,7 @@
|
||||
# define <name> RPI_GPIO <Pin>
|
||||
# where <Pin> is one of RPi's GPIO
|
||||
#
|
||||
# contributed by Klaus Wittstock (2013) email: klauswittstock bei gmail punkt com
|
||||
# contributed by Klaus Wittstock (2013) email: klauswittstock bei gmail
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
@ -20,6 +20,8 @@ use Scalar::Util qw(looks_like_number);
|
||||
use IO::File;
|
||||
use SetExtensions;
|
||||
|
||||
sub RPI_GPIO_fileaccess($$;$);
|
||||
|
||||
sub RPI_GPIO_Initialize($) {
|
||||
my ($hash) = @_;
|
||||
$hash->{DefFn} = "RPI_GPIO_Define";
|
||||
@ -33,7 +35,7 @@ sub RPI_GPIO_Initialize($) {
|
||||
" direction:input,output pud_resistor:off,up,down" .
|
||||
" interrupt:none,falling,rising,both" .
|
||||
" toggletostate:no,yes active_low:no,yes" .
|
||||
" debounce_in_ms restoreOnStartup:no,yes" .
|
||||
" debounce_in_ms restoreOnStartup:no,yes,on,off,last" .
|
||||
" longpressinterval " .
|
||||
"$readingFnAttributes";
|
||||
}
|
||||
@ -62,7 +64,7 @@ sub RPI_GPIO_Define($$) {
|
||||
"define <name> RPI_GPIO <GPIO>";
|
||||
}
|
||||
|
||||
#Prüfen, ob GPIO bereits verwendet
|
||||
#Pruefen, ob GPIO bereits verwendet
|
||||
foreach my $dev (devspec2array("TYPE=$hash->{TYPE}")) {
|
||||
if ($args[2] eq InternalVal($dev,"RPI_pin","")) {
|
||||
return "GPIO $args[2] already used by $dev";
|
||||
@ -71,17 +73,38 @@ sub RPI_GPIO_Define($$) {
|
||||
|
||||
my $name = $args[0];
|
||||
$hash->{RPI_pin} = $args[2];
|
||||
$hash->{dir_not_set} = 1;
|
||||
|
||||
#export Pin alte Version -> wird jetzt über direction gemacht (WiringPi Programm GPIO)
|
||||
#my $exp = IO::File->new("> /sys/class/gpio/export");
|
||||
#print $exp "$hash->{RPI_pin}";
|
||||
#$exp->close;
|
||||
#select(undef, undef, undef, 0.4); #kurz warten bis Verzeichnis angelegt
|
||||
if(-e "$gpiodir/gpio$hash->{RPI_pin}" && -w "$gpiodir/gpio$hash->{RPI_pin}/value" && -w "$gpiodir/gpio$hash->{RPI_pin}/direction") { #GPIO bereits exportiert?
|
||||
Log3 $hash, 4, "$name: gpio$hash->{RPI_pin} already exists";
|
||||
#nix tun...ist ja schon da
|
||||
} elsif (-w "$gpiodir/export") { #gpio export Datei mit schreibrechten?
|
||||
Log3 $hash, 4, "$name: write access to file $gpiodir/export, use it to export GPIO";
|
||||
my $exp = IO::File->new("> $gpiodir/export"); #gpio ueber export anlegen
|
||||
print $exp "$hash->{RPI_pin}";
|
||||
$exp->close;
|
||||
} else {
|
||||
if ( defined(my $ret = RPI_GPIO_CHECK_GPIO_UTIL($gpioprg)) ) { #Abbbruch da kein gpio utility vorhanden
|
||||
Log3 $hash, 1, "$name: can't export gpio$hash->{RPI_pin}, no write access to $gpiodir/export and " . $ret;
|
||||
return "$name: can't export gpio$hash->{RPI_pin}, no write access to $gpiodir/export and " . $ret;
|
||||
} else { #nutze GPIO Utility?
|
||||
Log3 $hash, 4, "$name: using gpio utility to export pin";
|
||||
RPI_GPIO_exuexpin($hash, "in");
|
||||
}
|
||||
}
|
||||
|
||||
# wait for Pin export (max 5s)
|
||||
my $checkpath = qq($gpiodir/gpio$hash->{RPI_pin}/value);
|
||||
my $counter = 100;
|
||||
while( $counter ){
|
||||
last if( -e $checkpath && -w $checkpath );
|
||||
Time::HiRes::sleep( 0.05 );
|
||||
$counter --;
|
||||
}
|
||||
unless( $counter ) { #abbrechen wenn export fehlgeschlagen
|
||||
Log3 $hash, 1, "$name: failed to export pin gpio$hash->{RPI_pin}";
|
||||
}
|
||||
|
||||
# create default attributes
|
||||
my $msg = CommandAttr(undef, $name . ' direction input');
|
||||
return $msg if ($msg);
|
||||
select(undef, undef, undef, 0.4);
|
||||
$hash->{fhem}{interfaces} = "switch";
|
||||
return undef;
|
||||
}
|
||||
@ -142,7 +165,6 @@ if ($cmd eq 'on') {
|
||||
if(!defined($setsinpt{$cmd})) {
|
||||
return 'Unknown argument ' . $cmd . ', choose one of ' . join(' ', keys %setsinpt)
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -154,25 +176,33 @@ if ($cmd eq 'on') {
|
||||
sub RPI_GPIO_State($$$$) { #reload readings at FHEM start
|
||||
my ($hash, $tim, $sname, $sval) = @_;
|
||||
Log3 $hash, 4, "$hash->{NAME}: $sname kann auf $sval wiederhergestellt werden $tim";
|
||||
if ( (AttrVal($hash->{NAME},"restoreOnStartup","on") eq "on") && ($sname ne "STATE") ) {
|
||||
|
||||
#if ( (AttrVal($hash->{NAME},"restoreOnStartup","yes") eq "yes") && ($sname ne "STATE") ) {
|
||||
if ( $sname ne "STATE" && AttrVal($hash->{NAME},"restoreOnStartup","last") ne "no") {
|
||||
if (AttrVal($hash->{NAME},"direction","") eq "output") {
|
||||
$hash->{READINGS}{$sname}{VAL} = $sval;
|
||||
$hash->{READINGS}{$sname}{TIME} = $tim;
|
||||
Log3 $hash, 4, "OUTPUT $hash->{NAME}: $sname wiederhergestellt auf $sval";
|
||||
if ($sname eq "state") {
|
||||
my $rval = AttrVal($hash->{NAME},"restoreOnStartup","last");
|
||||
$rval = "last" if ( $rval ne "on" && $rval ne "off" );
|
||||
$sval = $rval eq "last" ? $sval : $rval;
|
||||
#RPI_GPIO_Set($hash,$hash->{NAME},$sname,$sval);
|
||||
RPI_GPIO_Set($hash,$hash->{NAME},$sval);
|
||||
Log3 $hash, 4, "OUTPUT $hash->{NAME}: STATE wiederhergestellt auf $sval";
|
||||
Log3 $hash, 4, "OUTPUT $hash->{NAME}: STATE wiederhergestellt auf $sval (restoreOnStartup=$rval)";
|
||||
}
|
||||
} elsif ( (AttrVal($hash->{NAME},"direction","") eq "input") && (AttrVal($hash->{NAME},"toggletostate","") eq "yes")) {
|
||||
} elsif ( AttrVal($hash->{NAME},"direction","") eq "input") {
|
||||
if ($sname eq "Toggle") {
|
||||
$hash->{READINGS}{$sname}{VAL} = $sval;
|
||||
$hash->{READINGS}{$sname}{TIME} = $tim;
|
||||
Log3 $hash, 4, "INPUT $hash->{NAME}: $sname wiederhergestellt auf $sval";
|
||||
#RPI_GPIO_Set($hash,$hash->{NAME},$sval);
|
||||
if ((AttrVal($hash->{NAME},"toggletostate","") eq "yes")) {
|
||||
readingsBeginUpdate($hash);
|
||||
readingsBulkUpdate($hash, 'state', $sval);
|
||||
readingsEndUpdate($hash, 1);
|
||||
Log3 $hash, 4, "INPUT $hash->{NAME}: $sname und STATE wiederhergestellt auf $sval";
|
||||
Log3 $hash, 4, "INPUT $hash->{NAME}: STATE wiederhergestellt auf $sval";
|
||||
}
|
||||
} elsif ($sname eq "Counter") {
|
||||
$hash->{READINGS}{$sname}{VAL} = $sval;
|
||||
$hash->{READINGS}{$sname}{TIME} = $tim;
|
||||
@ -200,7 +230,6 @@ sub RPI_GPIO_Attr(@) {
|
||||
RemoveInternalTimer($hash);
|
||||
}
|
||||
}
|
||||
|
||||
if ($attr eq 'longpressinterval') {
|
||||
if ( defined($val) ) {
|
||||
unless ( looks_like_number($val) && $val >= 0.1 && $val <= 10 ) {
|
||||
@ -208,44 +237,47 @@ sub RPI_GPIO_Attr(@) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($attr eq 'direction') {
|
||||
if (!$val) { #$val nicht definiert: Einstellungen löschen
|
||||
if (!$val) { #$val nicht definiert: Einstellungen loeschen
|
||||
$msg = "$hash->{NAME}: no direction value. Use input output";
|
||||
} elsif ($val eq "input") {
|
||||
#RPI_GPIO_fileaccess($hash, "direction", "in");
|
||||
RPI_GPIO_exuexpin($hash, "in");
|
||||
Log3 $hash, 5, "$hash->{NAME}: direction: input";
|
||||
delete($hash->{dir_not_set});
|
||||
RPI_GPIO_fileaccess($hash, "direction", "in");
|
||||
#RPI_GPIO_exuexpin($hash, "in");
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr direction: input";
|
||||
} elsif( ( AttrVal($hash->{NAME}, "interrupt", "none") ) ne ( "none" ) ) {
|
||||
$msg = "$hash->{NAME}: Delete attribute interrupt or set it to none for output direction";
|
||||
} elsif ($val eq "output") {
|
||||
#RPI_GPIO_fileaccess($hash, "direction", "out");
|
||||
RPI_GPIO_exuexpin($hash, "out");
|
||||
Log3 $hash, 5, "$hash->{NAME}: direction: output";
|
||||
unless ($hash->{dir_not_set}) { #direction bei output noch nicht setzten (erfolgt bei erstem schreiben vom Wert um kurzes umschalten beim fhem start zu unterbinden)
|
||||
RPI_GPIO_fileaccess($hash, "direction", "out");
|
||||
#RPI_GPIO_exuexpin($hash, "out");
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr direction: output";
|
||||
} else {
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr direction: output vorerst NICHT";
|
||||
}
|
||||
} else {
|
||||
$msg = "$hash->{NAME}: Wrong $attr value. Use input output";
|
||||
}
|
||||
}
|
||||
|
||||
if ($attr eq 'interrupt') {
|
||||
if ( !$val || ($val eq "none") ) {
|
||||
RPI_GPIO_fileaccess($hash, "edge", "none");
|
||||
RPI_GPIO_inthandling($hash, "stop");
|
||||
Log3 $hash, 5, "$hash->{NAME}: interrupt: none";
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr interrupt: none";
|
||||
} elsif (( AttrVal($hash->{NAME}, "direction", "output") ) eq ( "output" )) {
|
||||
$msg = "$hash->{NAME}: Wrong direction value defined for interrupt. Use input";
|
||||
} elsif ($val eq "falling") {
|
||||
RPI_GPIO_fileaccess($hash, "edge", "falling");
|
||||
RPI_GPIO_inthandling($hash, "start");
|
||||
Log3 $hash, 5, "$hash->{NAME}: interrupt: falling";
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr interrupt: falling";
|
||||
} elsif ($val eq "rising") {
|
||||
RPI_GPIO_fileaccess($hash, "edge", "rising");
|
||||
RPI_GPIO_inthandling($hash, "start");
|
||||
Log3 $hash, 5, "$hash->{NAME}: interrupt: rising";
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr interrupt: rising";
|
||||
} elsif ($val eq "both") {
|
||||
RPI_GPIO_fileaccess($hash, "edge", "both");
|
||||
RPI_GPIO_inthandling($hash, "start");
|
||||
Log3 $hash, 5, "$hash->{NAME}: interrupt: both";
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr interrupt: both";
|
||||
} else {
|
||||
$msg = "$hash->{NAME}: Wrong $attr value. Use none, falling, rising or both";
|
||||
}
|
||||
@ -260,9 +292,10 @@ sub RPI_GPIO_Attr(@) {
|
||||
if ($attr eq 'active_low') {
|
||||
if ( !$val || ($val eq "no" ) ) {
|
||||
RPI_GPIO_fileaccess($hash, "active_low", "0");
|
||||
#Log 1, "$hash->{NAME}: interrupt: none";
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr active_low: no";
|
||||
} elsif ($val eq "yes") {
|
||||
RPI_GPIO_fileaccess($hash, "active_low", "1");
|
||||
Log3 $hash, 5, "$hash->{NAME}: set attr active_low: yes";
|
||||
} else {
|
||||
$msg = "$hash->{NAME}: Wrong $attr value. Use yes or no";
|
||||
}
|
||||
@ -273,9 +306,12 @@ sub RPI_GPIO_Attr(@) {
|
||||
$msg = "$hash->{NAME}: debounce_in_ms value to big. Use 0 to 250";
|
||||
}
|
||||
}
|
||||
|
||||
if ($attr eq 'pud_resistor') {
|
||||
if ($attr eq 'pud_resistor') {#nur fuer Raspberry (ueber gpio utility)
|
||||
my $pud;
|
||||
if ( defined(my $ret = RPI_GPIO_CHECK_GPIO_UTIL($gpioprg)) ) {
|
||||
Log3 $hash, 1, "$hash->{NAME}: unable to change pud resistor:" . $ret;
|
||||
return "$hash->{NAME}: " . $ret;
|
||||
} else {
|
||||
if ( !$val ) {
|
||||
} elsif ($val eq "off") {
|
||||
$pud = $gpioprg.' -g mode '.$hash->{RPI_pin}.' tri';
|
||||
@ -290,6 +326,7 @@ sub RPI_GPIO_Attr(@) {
|
||||
$msg = "$hash->{NAME}: Wrong $attr value. Use off, up or down";
|
||||
}
|
||||
}
|
||||
}
|
||||
return ($msg) ? $msg : undef;
|
||||
}
|
||||
|
||||
@ -313,18 +350,20 @@ sub RPI_GPIO_Undef($$) {
|
||||
delete $selectlist{$hash->{NAME}};
|
||||
close($hash->{filehandle});
|
||||
}
|
||||
#unexport Pin alte Version
|
||||
#my $uexp = IO::File->new("> /sys/class/gpio/unexport");
|
||||
#print $uexp "$hash->{RPI_pin}";
|
||||
#$uexp->close;
|
||||
#alternative unexport Pin:
|
||||
if (-w "$gpiodir/unexport") {#unexport Pin alte Version
|
||||
my $uexp = IO::File->new("> $gpiodir/unexport");
|
||||
print $uexp "$hash->{RPI_pin}";
|
||||
$uexp->close;
|
||||
} else {#alternative unexport Pin:
|
||||
RPI_GPIO_exuexpin($hash, "unexport");
|
||||
}
|
||||
Log3 $hash, 1, "$hash->{NAME}: entfernt";
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub RPI_GPIO_Except($) { #called from main if an interrupt occured
|
||||
my ($hash) = @_;
|
||||
#seek($hash->{filehandle},0,0); #an Anfang der Datei springen (ist nötig falls vorher schon etwas gelesen wurde)
|
||||
#seek($hash->{filehandle},0,0); #an Anfang der Datei springen (ist noetig falls vorher schon etwas gelesen wurde)
|
||||
#chomp ( my $firstval = $hash->{filehandle}->getline ); #aktuelle Zeile auslesen und Endezeichen entfernen
|
||||
#my $acttime = gettimeofday();
|
||||
my $eval = RPI_GPIO_fileaccess($hash, "edge"); #Eintstellung Flankensteuerung auslesen
|
||||
@ -336,7 +375,7 @@ sub RPI_GPIO_Except($) { #called from main if an interrupt occured
|
||||
select(undef, undef, undef, $debounce_time);
|
||||
}
|
||||
|
||||
seek($hash->{filehandle},0,0); #an Anfang der Datei springen (ist nötig falls vorher schon etwas gelesen wurde)
|
||||
seek($hash->{filehandle},0,0); #an Anfang der Datei springen (ist noetig falls vorher schon etwas gelesen wurde)
|
||||
chomp ( my $val = $hash->{filehandle}->getline ); #aktuelle Zeile auslesen und Endezeichen entfernen
|
||||
|
||||
if ( ( $val == 1) && ( $eval ne ("falling") ) ) {
|
||||
@ -359,16 +398,16 @@ sub RPI_GPIO_Except($) { #called from main if an interrupt occured
|
||||
$valto = "off";
|
||||
}
|
||||
Log3 $hash, 5, "Toggle ist jetzt $valto";
|
||||
if (( AttrVal($hash->{NAME}, "toggletostate", "no") ) eq ( "yes" )) { #wenn Attr "toggletostate" gesetzt auch die Variable für den STATE wert setzen
|
||||
if (( AttrVal($hash->{NAME}, "toggletostate", "no") ) eq ( "yes" )) { #wenn Attr "toggletostate" gesetzt auch die Variable fuer den STATE wert setzen
|
||||
$valst = $valto;
|
||||
}
|
||||
#Zählfunktion
|
||||
if (!defined($hash->{READINGS}{Counter}{VAL})) { #Zähler existiert nicht -> anlegen
|
||||
Log3 $hash, 5, "Zähler war nicht def";
|
||||
#Zaehlfunktion
|
||||
if (!defined($hash->{READINGS}{Counter}{VAL})) { #Zaehler existiert nicht -> anlegen
|
||||
Log3 $hash, 5, "Zaehler war nicht def";
|
||||
$valcnt = "1";
|
||||
} else {
|
||||
$valcnt = $hash->{READINGS}{Counter}{VAL} + 1;
|
||||
Log3 $hash, 5, "Zähler ist jetzt $valcnt";
|
||||
Log3 $hash, 5, "Zaehler ist jetzt $valcnt";
|
||||
}
|
||||
#langer Testendruck
|
||||
} elsif ($eval eq "both") {
|
||||
@ -388,8 +427,8 @@ sub RPI_GPIO_Except($) { #called from main if an interrupt occured
|
||||
}
|
||||
}
|
||||
|
||||
delete ($hash->{READINGS}{Toggle}) if ($eval ne ("rising" || "falling")); #Reading Toggle löschen wenn Edge weder "rising" noch "falling"
|
||||
delete ($hash->{READINGS}{Longpress}) if ($eval ne "both"); #Reading Longpress löschen wenn edge nicht "both"
|
||||
delete ($hash->{READINGS}{Toggle}) if ($eval ne ("rising" || "falling")); #Reading Toggle loeschen wenn Edge weder "rising" noch "falling"
|
||||
delete ($hash->{READINGS}{Longpress}) if ($eval ne "both"); #Reading Longpress loeschen wenn edge nicht "both"
|
||||
readingsBeginUpdate($hash);
|
||||
readingsBulkUpdate($hash, 'Pinlevel', $valalt);
|
||||
readingsBulkUpdate($hash, 'state', $valst);
|
||||
@ -397,7 +436,7 @@ sub RPI_GPIO_Except($) { #called from main if an interrupt occured
|
||||
readingsBulkUpdate($hash, 'Counter', $valcnt) if ($valcnt);
|
||||
readingsBulkUpdate($hash, 'Longpress', $vallp) if ($vallp);
|
||||
readingsEndUpdate($hash, 1);
|
||||
#Log3 $hash, 5, "RPIGPIO: Except ausgelöst: $hash->{NAME}, Wert: $val, edge: $eval,vt: $valto, $debounce_time s: $firstval";
|
||||
#Log3 $hash, 5, "RPIGPIO: Except ausgeloest: $hash->{NAME}, Wert: $val, edge: $eval,vt: $valto, $debounce_time s: $firstval";
|
||||
}
|
||||
|
||||
sub RPI_GPIO_longpress($) { #for reading longpress
|
||||
@ -442,30 +481,55 @@ sub RPI_GPIO_fileaccess($$;$) { #Fileaccess for GPIO base directory
|
||||
my $fname = $args[0];
|
||||
my $pinroot = qq($gpiodir/gpio$hash->{RPI_pin});
|
||||
my $file =qq($pinroot/$fname);
|
||||
Log3 $hash, 5, "$hash->{NAME}, in fileaccess: $fname " . (defined($args[1])?$args[1]:"");
|
||||
|
||||
if ($hash->{dir_not_set} && $fname eq "value") { #direction setzen (bei output direkt status mit schreiben)
|
||||
delete($hash->{dir_not_set});
|
||||
my $dir = AttrVal($hash->{NAME},"direction","input");
|
||||
$dir = $dir eq "input" ? "in" : "out";
|
||||
if ($dir eq "out" && $fname eq "value" && defined($args[1])) {
|
||||
my $al = AttrVal($hash->{NAME},"active_low","off");
|
||||
my $lev = $al eq "on" ? 0 : 1;
|
||||
$dir = ($args[1] == $lev ? "high" : "low")
|
||||
}
|
||||
#$dir = ($args[1] == 1 ? "high" : "low") if ($dir eq "out" && $fname eq "value" && defined($args[1]));
|
||||
RPI_GPIO_fileaccess($hash, "direction", $dir);
|
||||
Log3 $hash, 4, "$hash->{NAME}: direction gesetzt auf $dir";
|
||||
}
|
||||
|
||||
if (int(@args) < 2){
|
||||
my $fh = IO::File->new("< $file");
|
||||
if (defined $fh) {
|
||||
chomp ( my $pinvalue = $fh->getline );
|
||||
$fh->close;
|
||||
return $pinvalue;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Log3 $hash, 1, "Can't open file: $hash->{NAME}, $fname";
|
||||
}
|
||||
} else {
|
||||
my $value = $args[1];
|
||||
if ($fname eq "direction" && (not -w $file)) { #wenn direction und diese nicht schreibbar mit gpio utility versuchen
|
||||
Log3 $hash, 4, "$hash->{NAME}: direction ueber gpio utility einstellen";
|
||||
RPI_GPIO_exuexpin($hash, $value);
|
||||
#if ( defined(my $ret = RPI_GPIO_CHECK_GPIO_UTIL($gpioprg)) ) {
|
||||
# Log3 $hash, 1, "$hash->{NAME}: " . $ret;
|
||||
#} else {
|
||||
#my $exp = $gpioprg.' -g mode '.$hash->{RPI_pin}. ' '.$value;
|
||||
#$exp = `$exp`;
|
||||
#}
|
||||
} else {
|
||||
my $fh = IO::File->new("> $file");
|
||||
if (defined $fh) {
|
||||
print $fh "$value";
|
||||
$fh->close;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Log3 $hash, 1, "Can't open file: $hash->{NAME}, $fname";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub RPI_GPIO_exuexpin($$) { #export and unexport Pin via GPIO utility
|
||||
sub RPI_GPIO_exuexpin($$) { #export, unexport and direction Pin via GPIO utility
|
||||
my ($hash, $dir) = @_;
|
||||
my $sw;
|
||||
if ($dir eq "unexport") {
|
||||
@ -475,30 +539,38 @@ sub RPI_GPIO_exuexpin($$) { #export and unexport Pin via GPIO utility
|
||||
$sw = "export";
|
||||
$dir = " ".$dir;
|
||||
}
|
||||
#alternative export Pin
|
||||
if(-e $gpioprg) {
|
||||
if(-x $gpioprg) {
|
||||
if(-u $gpioprg) {
|
||||
if ( defined(my $ret = RPI_GPIO_CHECK_GPIO_UTIL($gpioprg)) ) {
|
||||
Log3 $hash, 1, "$hash->{NAME}: " . $ret;
|
||||
} else {
|
||||
my $exp = $gpioprg.' '.$sw.' '.$hash->{RPI_pin}.$dir;
|
||||
$exp = `$exp`;
|
||||
} else {
|
||||
Log3 $hash, 1, "file $gpioprg is not setuid";
|
||||
}
|
||||
} else {
|
||||
Log3 $hash, 1, "file $gpioprg is not executable";
|
||||
}
|
||||
} else {
|
||||
Log3 $hash, 1, "file $gpioprg doesnt exist";
|
||||
}
|
||||
#######################
|
||||
}
|
||||
|
||||
sub RPI_GPIO_CHECK_GPIO_UTIL {
|
||||
my ($gpioprg) = @_;
|
||||
my $ret = undef;
|
||||
#unless (defined($hash->{gpio_util_exists})) {
|
||||
if(-e $gpioprg) {
|
||||
if(-x $gpioprg) {
|
||||
unless(-u $gpioprg) {
|
||||
$ret = "file $gpioprg is not setuid";
|
||||
}
|
||||
} else {
|
||||
$ret = "file $gpioprg is not executable";
|
||||
}
|
||||
} else {
|
||||
$ret = "file $gpioprg doesnt exist";
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
my ($hash, $arg) = @_;
|
||||
my $msg = '';
|
||||
if ( $arg eq "start") {
|
||||
#FH für value-datei
|
||||
#FH fuer value-datei
|
||||
my $pinroot = qq($gpiodir/gpio$hash->{RPI_pin});
|
||||
my $valfile = qq($pinroot/value);
|
||||
$hash->{filehandle} = IO::File->new("< $valfile");
|
||||
@ -517,6 +589,7 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=pod
|
||||
=begin html
|
||||
|
||||
@ -524,25 +597,36 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
<h3>RPI_GPIO</h3>
|
||||
<ul>
|
||||
<a name="RPI_GPIO"></a>
|
||||
<p>
|
||||
Raspberry Pi offers direct access to several GPIO via header P1 (and P5 on V2). The Pinout is shown in table under define.
|
||||
With this module you are able to access these GPIO's directly as output or input. For input you can use either polling or interrupt mode<br><br>
|
||||
With this module you are able to access these GPIO's directly as output or input. For input you can use either polling or interrupt mode<br>
|
||||
In addition to the Raspberry Pi, also BBB, Cubie and almost every linux system which provides gpio access in userspace is supported.<br>
|
||||
<b>Warning: Never apply any external voltage to an output configured pin! GPIO's internal logic operate with 3,3V. Don't exceed this Voltage!</b><br><br>
|
||||
|
||||
<b>preliminary:</b><br>
|
||||
GPIO Pins accessed by sysfs. The files are located in folder /system/class/gpio which can be only accessed by root.
|
||||
This module uses gpio utility from <a href="http://wiringpi.com/download-and-install/">WiringPi</a> library to export and change access rights of GPIO's<br>
|
||||
Install WiringPi:
|
||||
<pre>
|
||||
sudo apt-get update
|
||||
sudo apt-get upgrade
|
||||
sudo apt-get install git-core
|
||||
git clone git://git.drogon.net/wiringPi
|
||||
GPIO Pins accessed by sysfs. The files are located in folder <code>/system/class/gpio</code> and belong to the gpio group (on actual Raspbian distributions since jan 2014).<br>
|
||||
After execution of following commands, GPIO's are usable whithin PRI_GPIO:<br>
|
||||
<ul><code>
|
||||
sudo adduser fhem gpio<br>
|
||||
sudo reboot
|
||||
</code></ul><br>
|
||||
On older Raspbian distributions and if attribute <code>pud_resistor</code> shall be used, aditionally gpio utility from <a href="http://wiringpi.com/download-and-install/">WiringPi</a>
|
||||
library must be installed to export and change access rights of GPIO's, or to set the internal pullup/down resistor.<br>
|
||||
Installation WiringPi:<br>
|
||||
<ul><code>
|
||||
sudo apt-get update<br>
|
||||
sudo apt-get upgrade<br>
|
||||
sudo apt-get install git-core<br>
|
||||
git clone git://git.drogon.net/wiringPi<br>
|
||||
cd wiringPi
|
||||
./build
|
||||
sudo adduser fhem gpio</pre>
|
||||
Thats all<br><br>
|
||||
|
||||
</code></ul><br>
|
||||
On Linux systeme where <code>/system/class/gpio</code> can only accessed as root, GPIO's must exported and their access rights changed before FHEM starts.<br>
|
||||
This can be done in <code>/etc/rc.local</code> (Examole for GPIO22 and 23):<br>
|
||||
<ul><code>
|
||||
echo 22 > /sys/class/gpio/export<br>
|
||||
echo 23 > /sys/class/gpio/export<br>
|
||||
chown -R fhem:root /sys/devices/virtual/gpio/*<br>
|
||||
chown -R fhem:root /sys/class/gpio/*<br>
|
||||
</code></ul><br>
|
||||
<a name="RPI_GPIODefine"></a>
|
||||
<b>Define</b>
|
||||
<ul>
|
||||
@ -649,6 +733,10 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
Sets the GPIO direction to input or output.<br>
|
||||
Default: input, valid values: input, output<br><br>
|
||||
</li>
|
||||
<li>active_low<br>
|
||||
Inverts logical value<br>
|
||||
Default: off, valid values: on, off<br><br>
|
||||
</li>
|
||||
<li>interrupt<br>
|
||||
<b>can only be used with GPIO configured as input</b><br>
|
||||
enables edge detection for GPIO pin<br>
|
||||
@ -668,6 +756,7 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
</li>
|
||||
<li>pud_resistor<br>
|
||||
Sets the internal pullup/pulldown resistor<br>
|
||||
<b>Works only with installed gpio urility from <a href="http://wiringpi.com/download-and-install/">WiringPi</a> Library.</b><br>
|
||||
Default: -, valid values: off, up, down<br><br>
|
||||
</li>
|
||||
<li>debounce_in_ms<br>
|
||||
@ -676,7 +765,7 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
</li>
|
||||
<li>restoreOnStartup<br>
|
||||
Restore Readings and sets after reboot<br>
|
||||
Default: on, valid values: on, off<br><br>
|
||||
Default: last, valid values: last, on, off, no<br><br>
|
||||
</li>
|
||||
<li>longpressinterval<br>
|
||||
<b>works with interrupt set to both only</b><br>
|
||||
@ -697,25 +786,36 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
<h3>RPI_GPIO</h3>
|
||||
<ul>
|
||||
<a name="RPI_GPIO"></a>
|
||||
<p>
|
||||
Das Raspberry Pi ermöglicht direkten Zugriff zu einigen GPIO's über den Pfostenstecker P1 (und P5 bei V2). Die Steckerbelegung ist in den Tabellen unter Define zu finden.
|
||||
Dieses Modul ermöglicht es, die herausgefühten GPIO's direkt als Ein- und Ausgang zu benutzen. Die Eingänge können zyklisch abgefragt werden oder auch sofort bei Pegelwechsel gesetzt werden.<br><br>
|
||||
Dieses Modul ermöglicht es, die herausgeführten GPIO's direkt als Ein- und Ausgang zu benutzen. Die Eingänge können zyklisch abgefragt werden oder auch sofort bei Pegelwechsel gesetzt werden.<br>
|
||||
Neben dem Raspberry Pi können auch die GPIO's von BBB, Cubie und jedem Linuxsystem, das diese im Userspace zugägig macht, genutzt werden.<br>
|
||||
<b>Wichtig: Niemals Spannung an einen GPIO anlegen, der als Ausgang eingestellt ist! Die interne Logik der GPIO's arbeitet mit 3,3V. Ein überschreiten der 3,3V zerstört den GPIO und vielleicht auch den ganzen Prozessor!</b><br><br>
|
||||
|
||||
<b>Vorbereitung:</b><br>
|
||||
Auf GPIO Pins wird im Modul über sysfs zugegriffen. Die Dateien befinden sich unter /system/class/gpio und können nur mit root erreicht werden.
|
||||
Dieses Modul nutzt das gpio Tool von der <a href="http://wiringpi.com/download-and-install/">WiringPi</a>. Bibliothek um GPIS zu exportieren und die korrekten Nutzerrechte zu setzen.
|
||||
Installation WiringPi:
|
||||
<pre>
|
||||
sudo apt-get update
|
||||
sudo apt-get upgrade
|
||||
sudo apt-get install git-core
|
||||
git clone git://git.drogon.net/wiringPi
|
||||
Auf GPIO Pins wird im Modul über sysfs zugegriffen. Die Dateien befinden sich unter <code>/system/class/gpio</code> und sind in der aktuellen Raspbian Distribution (ab Jan 2014) in der Gruppe gpio.<br>
|
||||
Nach dem ausführen folgender Befehle sind die GPIO's von PRI_GPIO aus nutzbar:<br>
|
||||
<ul><code>
|
||||
sudo adduser fhem gpio<br>
|
||||
sudo reboot
|
||||
</code></ul><br>
|
||||
Für ältere Raspbian Distributionen und wenn das Attribut <code>pud_resistor</code> verwendet werden soll, muss zusätzlich das gpio Tool der <a href="http://wiringpi.com/download-and-install/">WiringPi</a>
|
||||
Bibliothek installiert werden, um GPIO's zu exportieren und die korrekten Nutzerrechte zu setzen, bzw. den internen Pullup/down Widerstand zu aktivieren.<br>
|
||||
Installation WiringPi:<br>
|
||||
<ul><code>
|
||||
sudo apt-get update<br>
|
||||
sudo apt-get upgrade<br>
|
||||
sudo apt-get install git-core<br>
|
||||
git clone git://git.drogon.net/wiringPi<br>
|
||||
cd wiringPi
|
||||
./build
|
||||
sudo adduser fhem gpio</pre>
|
||||
Das wars!<br><br>
|
||||
|
||||
</code></ul><br>
|
||||
Für Linux Systeme bei denen der Zugriff auf <code>/system/class/gpio</code> nur mit root Rechten erfolgen kann, müssen die GPIO's vor FHEM start exportiert und von den Rechten her angepasst werden.<br>
|
||||
Dazu in die <code>/etc/rc.local</code> folgendes einfügen (Beispiel für GPIO22 und 23):<br>
|
||||
<ul><code>
|
||||
echo 22 > /sys/class/gpio/export<br>
|
||||
echo 23 > /sys/class/gpio/export<br>
|
||||
chown -R fhem:root /sys/devices/virtual/gpio/*<br>
|
||||
chown -R fhem:root /sys/class/gpio/*<br>
|
||||
</code></ul><br>
|
||||
<a name="RPI_GPIODefine"></a>
|
||||
<b>Define</b>
|
||||
<ul>
|
||||
@ -822,13 +922,17 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
Setzt den GPIO auf Ein- oder Ausgang.<br>
|
||||
Standard: input, gültige Werte: input, output<br><br>
|
||||
</li>
|
||||
<li>active_low<br>
|
||||
Invertieren des logischen Wertes<br>
|
||||
Standard: off, gültige Werte: on, off<br><br>
|
||||
</li>
|
||||
<li>interrupt<br>
|
||||
<b>kann nur gewählt werden, wenn der GPIO als Eingang konfiguriert ist</b><br>
|
||||
Aktiviert Flankenerkennung für den GPIO<br>
|
||||
bei jedem interrupt Ereignis werden die readings Pinlevel und state aktualisiert<br>
|
||||
Standard: none, gültige Werte: none, falling, rising, both<br><br>
|
||||
Bei "both" wird ein reading Longpress angelegt, welches auf on gesetzt wird solange der Pin länger als 1s gedrückt wird<br>
|
||||
Bei "falling" und "rising" wird ein reading Toggle angelegt, das bei jedem Interruptereignis toggelt und das Reading Counter, das bei jedem Ereignis um 1 hochzählt<br><br>
|
||||
Bei "both" wird ein reading Longpress angelegt, welches auf on gesetzt wird solange der Pin länger als 1s gedrückt wird<br>
|
||||
Bei "falling" und "rising" wird ein reading Toggle angelegt, das bei jedem Interruptereignis toggelt und das Reading Counter, das bei jedem Ereignis um 1 hochzählt<br><br>
|
||||
|
||||
</li>
|
||||
<li>poll_interval<br>
|
||||
@ -842,6 +946,7 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
</li>
|
||||
<li>pud_resistor<br>
|
||||
Interner Pullup/down Widerstand<br>
|
||||
<b>Funktioniert aussließlich mit installiertem gpio Tool der <a href="http://wiringpi.com/download-and-install/">WiringPi</a> Bibliothek.</b><br>
|
||||
Standard: -, gültige Werte: off, up, down<br><br>
|
||||
</li>
|
||||
<li>debounce_in_ms<br>
|
||||
@ -849,8 +954,8 @@ sub RPI_GPIO_inthandling($$) { #start/stop Interrupthandling
|
||||
Standard: 0, gültige Werte: Dezimalzahl<br><br>
|
||||
</li>
|
||||
<li>restoreOnStartup<br>
|
||||
Wiederherstellen der Portzust&äuml;nde nach Neustart<br>
|
||||
Standard: on, gültige Werte: on, off<br><br>
|
||||
Wiederherstellen der Portzustände nach Neustart<br>
|
||||
Standard: last, gültige Werte: last, on, off, no<br><br>
|
||||
</li>
|
||||
<li>longpressinterval<br>
|
||||
<b>Funktioniert nur bei auf both gesetztem Attribut interrupt</b><br>
|
||||
|
Loading…
Reference in New Issue
Block a user