Skip to content

Commit

Permalink
os.system breaks event detection - Matt Kimball (issue 127)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcroston authored and auto3000 committed Jul 28, 2017
1 parent cf4441c commit b0cc5ed
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Change Log
==========

0.6.3a0
0.6.3a1
-------
- Fix code so it builds under PyPy (Gasper Zejn)
- os.system breaks event detection - Matt Kimball (issue 127)

0.6.2
-----
Expand Down
2 changes: 1 addition & 1 deletion RPi/GPIO/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

from RPi._GPIO import *

VERSION = '0.6.3a0'
VERSION = '0.6.3a1'
6 changes: 3 additions & 3 deletions debian_jessie/changelog
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
rpi.gpio (0.6.3a0~jessie-1) unstable; urgency=low
rpi.gpio (0.6.3a1~jessie-1) unstable; urgency=low

* To match new Python package 0.6.3a0
* To match new Python package 0.6.3a1

-- Ben Croston <ben@croston.org> Sat, 15 Oct 2016 19:10:13 +0100
-- Ben Croston <ben@croston.org> Sat, 30 Oct 2016 20:10:13 +0000

rpi.gpio (0.6.2~jessie-1) unstable; urgency=low

Expand Down
6 changes: 3 additions & 3 deletions debian_wheezy/changelog
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
rpi.gpio (0.6.3a0~wheezy-1) unstable; urgency=low
rpi.gpio (0.6.3a1~wheezy-1) unstable; urgency=low

* To match new Python package 0.6.3a0
* To match new Python package 0.6.3a1

-- Ben Croston <ben@croston.org> Sat, 15 Oct 2016 19:13:25 +0100
-- Ben Croston <ben@croston.org> Sat, 30 Oct 2016 20:13:25 +0000

rpi.gpio (0.6.2~wheezy-1) unstable; urgency=low

Expand Down
2 changes: 1 addition & 1 deletion make_deb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# There may be dependencies I've forgotten to mention!
# Version numbers may change as well!

VERSION=0.6.3a0
VERSION=0.6.3a1
DISTRO=`lsb_release -sc`
#DEBFULLNAME="Ben Croston"
#DEBEMAIL="ben@croston.org"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'Topic :: System :: Hardware']

setup(name = 'RPi.GPIO',
version = '0.6.3a0',
version = '0.6.3a1',
author = 'Ben Croston',
author_email = 'ben@croston.org',
description = 'A module to control Raspberry Pi GPIO channels',
Expand Down
24 changes: 19 additions & 5 deletions source/event_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
#include <sys/epoll.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
Expand Down Expand Up @@ -331,10 +332,7 @@ void *poll_thread(void *threadarg)

thread_running = 1;
while (thread_running) {
if ((n = epoll_wait(epfd_thread, &events, 1, -1)) == -1) {
thread_running = 0;
pthread_exit(NULL);
}
n = epoll_wait(epfd_thread, &events, 1, -1);
if (n > 0) {
lseek(events.data.fd, 0, SEEK_SET);
if (read(events.data.fd, &buf, 1) != 1) {
Expand All @@ -353,6 +351,15 @@ void *poll_thread(void *threadarg)
run_callbacks(g->gpio);
}
}
} else if (n == -1) {
/* If a signal is received while we are waiting,
epoll_wait will return with an EINTR error.
Just try again in that case. */
if (errno == EINTR) {
continue;
}
thread_running = 0;
pthread_exit(NULL);
}
}
thread_running = 0;
Expand Down Expand Up @@ -534,7 +541,14 @@ int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int bouncetime,

// wait for edge
while (!finished) {
if ((n = epoll_wait(epfd_blocking, &events, 1, timeout)) == -1) {
n = epoll_wait(epfd_blocking, &events, 1, timeout);
if (n == -1) {
/* If a signal is received while we are waiting,
epoll_wait will return with an EINTR error.
Just try again in that case. */
if (errno == EINTR) {
continue;
}
epoll_ctl(epfd_blocking, EPOLL_CTL_DEL, g->value_fd, &ev);
return -2;
}
Expand Down
56 changes: 55 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
LOOP_OUT = 22
"""

import os
import subprocess
import sys
import warnings
import time
Expand Down Expand Up @@ -395,6 +397,54 @@ def setUp(self):
GPIO.setup(LOOP_IN, GPIO.IN)
GPIO.setup(LOOP_OUT, GPIO.OUT)

# Running a shell command with os.sytem has caused problems
# with sending SIGCHLD to the polling thread, causing it
# to exit. Test for that.
def testShellCmdWithWaitForEdge(self):
self.finished = False
def shellcmd():
for i in range(50):
os.system('sleep 0')
subprocess.call('sleep 0', shell=True)
self.finished = True
def makehigh():
GPIO.output(LOOP_OUT, GPIO.HIGH)

GPIO.output(LOOP_OUT, GPIO.LOW)
t1 = Timer(0.1, shellcmd)
t2 = Timer(0.5, makehigh)
t1.start()
t2.start()
starttime = time.time()
channel = GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=1000)
endtime = time.time()
self.assertGreater(endtime - starttime, 0.5)
self.assertLess(endtime - starttime, 0.6)
self.assertEqual(channel, LOOP_IN)

# make sure tasks in this test have finished before continuing
while not self.finished:
time.sleep(0.1)

def testShellCmdWithEventCallback(self):
self.run_cb = False

def cb(channel):
self.run_cb = True

GPIO.output(LOOP_OUT, GPIO.LOW)
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
time.sleep(0.01)

for i in range(50):
os.system('sleep 0')
subprocess.call('sleep 0', shell=True)

GPIO.output(LOOP_OUT, GPIO.HIGH)
time.sleep(0.01)
GPIO.remove_event_detect(LOOP_IN)
self.assertEqual(self.run_cb, True)

def testWaitForEdgeInLoop(self):
def makelow():
GPIO.output(LOOP_OUT, GPIO.LOW)
Expand All @@ -405,7 +455,11 @@ def makelow():
while True:
t = Timer(0.1, makelow)
t.start()
GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
starttime = time.time()
channel = GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING, timeout=200)
endtime = time.time()
self.assertLess(endtime-starttime, 0.12)
self.assertEqual(channel, LOOP_IN)
GPIO.output(LOOP_OUT, GPIO.HIGH)
count += 1
if time.time() - timestart > 5 or count > 150:
Expand Down

0 comments on commit b0cc5ed

Please sign in to comment.