]> git.feebdaed.xyz Git - 0xmirror/WiringPi.git/commitdiff
Version 3.13 : debounce time added for wiringPiISR and waitForInterrupt library funct...
authorphylax2020 <severin.hoffmann@t-online.de>
Wed, 29 Jan 2025 18:09:32 +0000 (19:09 +0100)
committerphylax2020 <severin.hoffmann@t-online.de>
Wed, 29 Jan 2025 18:09:32 +0000 (19:09 +0100)
VERSION
documentation/deutsch/functions.md
gpio/gpio.c
version.h
wiringPi/wiringPi.c
wiringPi/wiringPi.h

diff --git a/VERSION b/VERSION
index e4fba2183587225f216eeada4c78dfab6b2e65f5..24ee5b1be9961e38a503c8e764b7385dbb6ba124 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.12
+3.13
index a6ece16c762af946c55e4f5a1444072c0080b052..2093650d856369a41e732e422fa22ff08137d99f 100644 (file)
@@ -20,13 +20,13 @@ sudo apt install git
 git clone https://github.com/WiringPi/WiringPi.git
 cd WiringPi
 ./build debian
-mv debian-template/wiringpi-3.0-1.deb .
+mv debian-template/wiringpi-3.13.deb .
 ```
 
 **Debian-Paket installieren:**
 
 ```bash
-sudo apt install ./wiringpi-3.0-1.deb
+sudo apt install ./wiringpi-3.13.deb
 ```
 
 **Debian-Paket deinstallieren:**
index 3fc97a3af86b8c24936f48040870679b98364d76..f79188bf76f6386553c1fcd424e5d4956e0bb234 100644 (file)
@@ -177,7 +177,7 @@ void printgpio(const char* text) {
   }
 }
 
-static void wfi (void) { 
+static void wfi (unsigned int pin, long long int timestamp) { 
   globalCounter++;
   if(globalCounter>=iterations) {
     printgpio("finished\n");
@@ -217,7 +217,7 @@ void doWfi (int argc, char *argv [])
     timeoutSec = atoi(argv [5]);
   }
 
-  if (wiringPiISR (pin, mode, &wfi) < 0)
+  if (wiringPiISR (pin, mode, &wfi, 0) < 0)
   {
     fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
     exit (1) ;
index 225bee746295582a8697861d92496b1637335442..35a30486dcad5df59a8ac19a378014572a3cb52e 100644 (file)
--- a/version.h
+++ b/version.h
@@ -1,3 +1,3 @@
-#define VERSION "3.12"
+#define VERSION "3.13"
 #define VERSION_MAJOR 3
-#define VERSION_MINOR 12
+#define VERSION_MINOR 13
index 50d20fe1edd3d4c3e624d9522cf63bbc1c073378..e3d6a25ed46552dba9103e3d0308a27343f0e0c0 100644 (file)
@@ -473,9 +473,10 @@ static int isrFds [64] =
 
 // ISR Data
 static int chipFd = -1;
-static void (*isrFunctions [64])(void) ;
+static void (*isrFunctions [64])(unsigned int, long long int) ;
 static pthread_t isrThreads[64];
 static int isrMode[64];
+static unsigned long long lastcall[64];
 
 // Doing it the Arduino way with lookup tables...
 //     Yes, it's probably more innefficient than all the bit-twidling, but it
@@ -2566,16 +2567,26 @@ unsigned int digitalReadByte2 (void)
  *     This is actually done via the /dev/gpiochip interface regardless of
  *     the wiringPi access mode in-use. Maybe sometime it might get a better
  *     way for a bit more efficiency.
+ *  Returns timestamp in microseconds, when interrupt happened
  *********************************************************************************
  */
 
-int waitForInterrupt (int pin, int mS)
+long long int waitForInterrupt (int pin, int mS, int bouncetime)  // bounctime in milliseconds
 {
-  int fd, ret;
+  long long int ret;
+  int fd;
   struct pollfd polls ;
   struct gpioevent_data evdata;
   //struct gpio_v2_line_request req2;
+  struct timeval tv_timenow;
+  unsigned long long timenow;
+  int finished = 0;
+  int initial_edge = 1;
 
+  if (wiringPiDebug) {
+    printf ("waitForInterrupt: mS = %d,  bouncetime = %d\n", mS, bouncetime) ;
+  }
+  
   if (wiringPiMode == WPI_MODE_PINS)
     pin = pinToGpio [pin] ;
   else if (wiringPiMode == WPI_MODE_PHYS)
@@ -2590,23 +2601,46 @@ int waitForInterrupt (int pin, int mS)
   polls.revents = 0;
 
   // Wait for it ...
-  ret = poll(&polls, 1, mS);
-  if (ret <= 0) {
-    fprintf(stderr, "wiringPi: ERROR: poll returned=%d\n", ret);
-  } else {
-    //if (polls.revents & POLLIN)
-    if (wiringPiDebug) {
-      printf ("wiringPi: IRQ line %d received %d, fd=%d\n", pin, ret, isrFds[pin]) ;
+  while (!finished) {
+    ret = (long long int)poll(&polls, 1, mS);
+    if (ret < 0) {
+      if (wiringPiDebug) {  
+        fprintf(stderr, "wiringPi: ERROR: poll returned=%lld\n", ret);
+      }
+      break;
+    } else if (ret == 0) { 
+      if (wiringPiDebug) {  
+        fprintf(stderr, "wiringPi: timeout: poll returned=%lld\n", ret);
+      }
+      break;    
     }
-    /* read event data */
-    int readret = read(isrFds [pin], &evdata, sizeof(evdata));
-    if (readret == sizeof(evdata)) {
+    else {
+      //if (polls.revents & POLLIN)
       if (wiringPiDebug) {
-        printf ("wiringPi: IRQ data id: %d, timestamp: %lld\n", evdata.id, evdata.timestamp) ;
+        printf ("wiringPi: IRQ line %d received %lld, fd=%d\n", pin, ret, isrFds[pin]) ;
+      }
+      if (initial_edge) {    // first time triggers with current state, so ignore
+        initial_edge = 0;
+      } else {
+      /* read event data */
+        int readret = read(isrFds [pin], &evdata, sizeof(evdata));
+        if (readret == sizeof(evdata)) {
+          if (wiringPiDebug) {
+            printf ("wiringPi: IRQ data id: %d, timestamp: %lld\n", evdata.id, evdata.timestamp) ;
+          }
+//      ret = evdata.id;
+          gettimeofday(&tv_timenow, NULL);
+          timenow = tv_timenow.tv_sec*1E6 + tv_timenow.tv_usec;
+          if (bouncetime == 0 || timenow - lastcall[pin] > (unsigned int)bouncetime*1000 || lastcall[pin] == 0 || lastcall[pin] > timenow) {
+             lastcall[pin] = timenow;
+             finished = 1;
+             ret = evdata.timestamp / 1000LL;    // nanoseconds u64 to microseconds
+          }
+        } else {
+          ret = -1;
+          break;
+        }
       }
-      ret = evdata.id;
-    } else {
-      ret = 0;
     }
   }
   return ret;
@@ -2683,7 +2717,7 @@ int waitForInterruptClose (int pin) {
     if (wiringPiDebug) {
       printf ("wiringPi: waitForInterruptClose close thread 0x%lX\n", (unsigned long)isrThreads[pin]) ;
     }
-    if (pthread_cancel(isrThreads[pin]) == 0) {
+    if (isrThreads[pin] && pthread_cancel(isrThreads[pin]) == 0) {
       if (wiringPiDebug) {
         printf ("wiringPi: waitForInterruptClose thread canceled successfuly\n") ;
       }
@@ -2696,7 +2730,7 @@ int waitForInterruptClose (int pin) {
   }
   isrFds [pin] = -1;
   isrFunctions [pin] = NULL;
-
+  lastcall[pin] = 0;
   /* -not closing so far - other isr may be using it - only close if no other is using - will code later
   if (chipFd>0) {
     close(chipFd);
@@ -2722,23 +2756,25 @@ int wiringPiISRStop (int pin) {
  *********************************************************************************
  */
 
-static void *interruptHandler (UNU void *arg)
+static void *interruptHandler (void *arg)
 {
   int pin ;
-
+  int bouncetime;
+  
   (void)piHiPri (55) ; // Only effective if we run as root
 
   pin   = pinPass ;
+  bouncetime = *(int *)arg;
   pinPass = -1 ;
-
+  
   for (;;) {
-    int ret = waitForInterrupt(pin, -1);
-    if ( ret> 0) {
+    long long int ret = waitForInterrupt(pin, -1, bouncetime);
+    if ( ret> 0) {      // return timestamp in microseconds
       if (wiringPiDebug) {
         printf ("wiringPi: call function\n") ;
       }
       if(isrFunctions [pin]) {
-        isrFunctions [pin] () ;
+        isrFunctions [pin] ((unsigned int)pin, ret) ;
       }
       // wait again - in the past forever - now can be stopped by  waitForInterruptClose
     } else if( ret< 0) {
@@ -2762,7 +2798,7 @@ static void *interruptHandler (UNU void *arg)
  *********************************************************************************
  */
 
-int wiringPiISR (int pin, int mode, void (*function)(void))
+int wiringPiISR (int pin, int mode, void (*function)(unsigned int, long long int), int bouncetime)
 {
   const int maxpin = GetMaxPin();
 
@@ -2779,11 +2815,12 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
 
   isrFunctions [pin] = function ;
   isrMode[pin] = mode;
+  lastcall[pin] = 0;
   if(waitForInterruptInit (pin, mode)<0) {
     if (wiringPiDebug) {
       fprintf (stderr, "wiringPi: waitForInterruptInit failed\n") ;
     }
-  };
+  }
 
   if (wiringPiDebug) {
     printf ("wiringPi: mutex in\n") ;
@@ -2793,7 +2830,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
     if (wiringPiDebug) {
       printf("wiringPi: pthread_create before 0x%lX\n", (unsigned long)isrThreads[pin]);
     }
-    if (pthread_create (&isrThreads[pin], NULL, interruptHandler, NULL)==0) {
+    if (pthread_create (&isrThreads[pin], NULL, interruptHandler, &bouncetime)==0) {
       if (wiringPiDebug) {
         printf("wiringPi: pthread_create successed, 0x%lX\n", (unsigned long)isrThreads[pin]);
       }
index 79a895af1cc9247bc0323fa8d5b6500912520b61..7659d84c4130d25a26ea923f7ea4bf4b50901dcc 100644 (file)
@@ -289,10 +289,11 @@ extern          void digitalWriteByte2   (int value) ;
 // Interrupts
 //     (Also Pi hardware specific)
 
-extern int  waitForInterrupt    (int pin, int mS) ;
-extern int  wiringPiISR         (int pin, int mode, void (*function)(void)) ;
+extern long long int  waitForInterrupt    (int pin, int mS, int bouncetime) ;
+extern int  wiringPiISR         (int pin, int mode, void (*function)(unsigned int, long long int), int bouncetime) ;
 extern int  wiringPiISRStop     (int pin) ;  //V3.2
 extern int  waitForInterruptClose(int pin) ; //V3.2
+extern int  waitForInterruptInit (int pin, int mode); //v3.2
 
 // Threads