element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum inhibit buttons whilst running script?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 39 replies
  • Answers 7 answers
  • Subscribers 709 subscribers
  • Views 4733 views
  • Users 0 members are here
  • python
  • raspberry_pi
  • raspberry pi
  • piface
Related

inhibit buttons whilst running script?

brew
brew over 10 years ago

Hi there I'm trying to use my Raspberry Pi 1B and PiFace to run a little interactive museum exhibition.

 

Process is Pi is in standby, Standby.py runs from rc.local Relay 1 is closed as this is the main light in the room,

Button Press (SW1) initiates the show

GoShow.py runs, this closes a number of replays which change the lights in the space and trigger a media player for the video the Pi plays a audio file.

 

I want to inhibit the button when GoShow is active, I've tried to 'de-listen' under the GoShow.py but no luck.

 

https://github.com/Brewj/Event-Trigger

 

Anyone any ideas?

 

Cheers

Brew

  • Sign in to reply
  • Cancel
Parents
  • brew
    0 brew over 10 years ago

    This is my code

     

    #!/usr/bin/python

    import pifacedigitalio

    import subprocess

     

    pfd = pifacedigitalio.PiFaceDigital()

     

    pfd.leds[5].turn_on() #Room light ON

     

    def toggle_led0(event):

      subprocess.call("/home/pi/goshow.sh", shell=True)

     

    pifacedigital = pifacedigitalio.PiFaceDigital()

    listener = pifacedigitalio.InputEventListener(chip=pifacedigital)

    listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)

    listener.activate()

     

    This is my first foray into this so apologies if it is obvious, sorry i just don't know how to do it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to brew

    Reading and Writing Files in Python all you need. I will return shortly.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    Actually, doing a quick google it looks like

     

    listener.deactivate()

     

    will unlisten

     

    and listener.activate()

     

    will turn it back on

     

    move

    1.   listener = pfd.InputEventListener(chip=pifacedigital) 

     

    up just below

     

    1. pfd = pifacedigitalio.PiFaceDigital() 

     

    to make sure it's in scope, give that a try and let us know.

     

    Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi, so got it all in had to adjust some of the indents but this is where I'm at

     

    #!/usr/bin/python  
    import pifacedigitalio  
    import subprocess  
    from time import sleep  
        
    pfd = pifacedigitalio.PiFaceDigital()
    pifacedigital = pifacedigitalio.PiFaceDigital() 
    listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
    listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)
    listener.activate()     
        
    pfd.leds[5].turn_on() #Room light ON  
      
    def toggle_led0(event):  
      listener.deactivate() #Inhibit button
      pfd.leds[1].turn_on() #On Air and Cabinet ON  
      sleep (2) #Delay for lights  
      pfd.leds[0].turn_off() #Room light OFF  
      sleep (1)  
      os.system("aplay /home/pi/withy_edit.wav") #Audio GO  
      sleep (3) #End Delay  
      pfd.leds[0].turn_on() #Room light ON  
      sleep (1)  
      pfd.leds[1].turn_off()   
      listener.activate() #re arm button  
      
    if __name__ == "__main__":  
        
      while True:  
      pass 

    however I get the following error now

     

    pi@raspberrypi:~ $ python goshow2.py

    Traceback (most recent call last):

      File "goshow2.py", line 9, in <module>

        listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)

    NameError: name 'toggle_led0' is not defined

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    try this

     

    #!/usr/bin/python  
    import pifacedigitalio  
    import subprocess  
    from time import sleep  
        
    pfd = pifacedigitalio.PiFaceDigital()
    listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
      
        
    pfd.leds[5].turn_on() #Room light ON  
      
    def toggle_led0(event):  
      listener.deactivate() #Inhibit button
      pfd.leds[1].turn_on() #On Air and Cabinet ON  
      sleep (2) #Delay for lights  
      pfd.leds[0].turn_off() #Room light OFF  
      sleep (1)  
      os.system("aplay /home/pi/withy_edit.wav") #Audio GO  
      sleep (3) #End Delay  
      pfd.leds[0].turn_on() #Room light ON  
      sleep (1)  
      pfd.leds[1].turn_off()   
      listener.activate() #re arm button  
      
    if __name__ == "__main__":  
        listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)
      listener.activate()  
    
    
      while True:  
      pass 

     

     

    you'll probably need to adjust the indent

     

     

    Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to mconners

    I think the error was that you were trying to attach the interrupt to an event handler (toggle_led0) before you declared the function, that's why I only said to move the one line

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Michael thank you for your help, I now get this

    Traceback (most recent call last):

      File "goshow2.py", line 7, in <module>

        listener = pifacedigitalio.InputEventListener(chip=pifacedigital) 

    NameError: name 'pifacedigital' is not defined

    pi@raspberrypi:~ $

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to brew

    Whops having made the definitions the following

    pfd = pifacedigitalio.PiFaceDigital() 
    pifacedigital = pifacedigitalio.PiFaceDigital()  
    listener = pifacedigitalio.InputEventListener(chip=pifacedigital)

     

    I get the following error - whoops

     

    pi@raspberrypi:~ $ python goshow2.py

    Exception in thread Thread-1:

    Traceback (most recent call last):

      File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner

        self.run()

      File "/usr/lib/python2.7/threading.py", line 763, in run

        self.__target(*self.__args, **self.__kwargs)

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 341, in handle_events

        function(event)

      File "goshow2.py", line 13, in toggle_led0

        listener.deactivate() #Inhibit button

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 222, in deactivate

        self.dispatcher.join()

      File "/usr/lib/python2.7/threading.py", line 940, in join

        raise RuntimeError("cannot join current thread")

    RuntimeError: cannot join current thread

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    Right now you've got 2 objects that are of type PiFaceDigital

    pfd and pifacedigital

     

    you should probably try to get that to 1, pick one, pfd or pifacedigital and make the appropriate changes to the script

    let's start with that.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Thanks for all the pointer, however the plot continues.

     

    So I have now just got pfd = pifacedigitalio.PiFaceDigital()  however there seems to be a problem with the Listener.deactivate() command in line 3.  If I # comment it and line 23 out the process works fine but if you press the button whilst the script is running it repeats.  This is what I'm trying to prevent.

     

    If i make line 13 live and run the script the first section starts, the room light comes on.  however if I press the button I get this error.

     

    Exception in thread Thread-1:

    Traceback (most recent call last):

      File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner

        self.run()

      File "/usr/lib/python2.7/threading.py", line 763, in run

        self.__target(*self.__args, **self.__kwargs)

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 341, in handle_events

        function(event)

      File "goshow2.py", line 13, in toggle_led0

        listener.deactivate() #Inhibit button

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 222, in deactivate

        self.dispatcher.join()

      File "/usr/lib/python2.7/threading.py", line 940, in join

        raise RuntimeError("cannot join current thread")

    RuntimeError: cannot join current thread

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    Can you post the code in current form?

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Mike thanks for your help

     

    https://github.com/Brewj/Event-Trigger

    goshow.py

     

    #!/usr/bin/python    
    import pifacedigitalio    
    import subprocess    
    from time import sleep 
    import os   
          
    pfd = pifacedigitalio.PiFaceDigital() 
    listener = pifacedigitalio.InputEventListener(chip=pfd)  
    
    
    #Standby State
    pfd.leds[0].turn_on() #Room light ON    
    
     #Go Show   
    def toggle_led0(event):    
      listener.deactivate() #Inhibit button  
      pfd.leds[1].turn_on() #On Air and Cabinet ON, Video Go  
      sleep (2) #Delay for lights    
      pfd.leds[0].turn_off() #Room light OFF    
      sleep (1) #Delay before audio
      os.system("aplay /home/pi/wim.wav") #Audio GO    
      sleep (3) #End Delay    
      pfd.leds[0].turn_on() #Room light ON    
      sleep (1)    
      pfd.leds[1].turn_off() #On Air and Cabinet Off
      #listener.activate() #re arm button    
        
    if __name__ == "__main__":    
      listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)  
      listener.activate()    
    
    while True:    
      pass  

     

    Cheers

    Brew

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Mike thanks for your help

     

    https://github.com/Brewj/Event-Trigger

    goshow.py

     

    #!/usr/bin/python    
    import pifacedigitalio    
    import subprocess    
    from time import sleep 
    import os   
          
    pfd = pifacedigitalio.PiFaceDigital() 
    listener = pifacedigitalio.InputEventListener(chip=pfd)  
    
    
    #Standby State
    pfd.leds[0].turn_on() #Room light ON    
    
     #Go Show   
    def toggle_led0(event):    
      listener.deactivate() #Inhibit button  
      pfd.leds[1].turn_on() #On Air and Cabinet ON, Video Go  
      sleep (2) #Delay for lights    
      pfd.leds[0].turn_off() #Room light OFF    
      sleep (1) #Delay before audio
      os.system("aplay /home/pi/wim.wav") #Audio GO    
      sleep (3) #End Delay    
      pfd.leds[0].turn_on() #Room light ON    
      sleep (1)    
      pfd.leds[1].turn_off() #On Air and Cabinet Off
      #listener.activate() #re arm button    
        
    if __name__ == "__main__":    
      listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)  
      listener.activate()    
    
    while True:    
      pass  

     

    Cheers

    Brew

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • mconners
    0 mconners over 10 years ago in reply to brew

    I think you are running into a scoping problem, I haven't done a lot of interrupt handling in python, but try this

     

    #!/usr/bin/python      
    import pifacedigitalio      
    import subprocess      
    from time import sleep   
    import os     
    
    
    global pfd
    global listener
            
    pfd = pifacedigitalio.PiFaceDigital()   
    listener = pifacedigitalio.InputEventListener(chip=pfd)    
      
      
    #Standby State  
    pfd.leds[0].turn_on() #Room light ON      
      
     #Go Show     
    def toggle_led0(event):  
      global pfd
      global listener
      listener.deactivate() #Inhibit button    
      pfd.leds[1].turn_on() #On Air and Cabinet ON, Video Go    
      sleep (2) #Delay for lights      
      pfd.leds[0].turn_off() #Room light OFF      
      sleep (1) #Delay before audio  
      os.system("aplay /home/pi/wim.wav") #Audio GO      
      sleep (3) #End Delay      
      pfd.leds[0].turn_on() #Room light ON      
      sleep (1)      
      pfd.leds[1].turn_off() #On Air and Cabinet Off  
      listener.activate() #re arm button      
          
    if __name__ == "__main__":   
      global listener   
      listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)    
      listener.activate()      
      
    while True:      
      pass    

     

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Mike and all,

     

    Sadly still no dice

     

    Exception in thread Thread-1:

    Traceback (most recent call last):

      File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner

        self.run()

      File "/usr/lib/python2.7/threading.py", line 763, in run

        self.__target(*self.__args, **self.__kwargs)

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 341, in handle_events

        function(event)

      File "goshow2.py", line 20, in toggle_led0

        listener.deactivate() #Inhibit button

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 222, in deactivate

        self.dispatcher.join()

      File "/usr/lib/python2.7/threading.py", line 940, in join

        raise RuntimeError("cannot join current thread")

    RuntimeError: cannot join current thread

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    In the email that I received a few minutes ago, that had the source attached, I noticed you were missing the two global definitions at the top of the file.

    Between the import statements and the declarations of pfd and listener.

    Did you try that?

     

    I mocked this code the best I could without having the piface and the libraries available.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to mconners

    Also, in the code that was attached, listener was spelled differently in several places

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Mike,

     

    Apologies for that, in my first reply I had missed the definitions, have added them in and corrected the spelling but sadly still no dice.  Thanks for all your time and help though, I really appreciate it.

     

    If anyone has any inspiration this is where it's at.

    #!/usr/bin/python    
    import pifacedigitalio    
    import subprocess    
    from time import sleep 
    import os   
    
    
    global pfd  
    global listener  
          
    pfd = pifacedigitalio.PiFaceDigital() 
    listener = pifacedigitalio.InputEventListener(chip=pfd)  
    
    
    #Standby State
    pfd.leds[0].turn_on() #Room light ON    
    
     #Go Show   
    def toggle_led0(event):  
      global pfd
      global listener  
      listener.deactivate() #Inhibit button  
      pfd.leds[1].turn_on() #On Air and Cabinet ON, Video Go  
      sleep (2) #Delay for lights    
      pfd.leds[0].turn_off() #Room light OFF    
      sleep (1) #Delay before audio
      os.system("aplay /home/pi/wim.wav") #Audio GO    
      sleep (3) #End Delay    
      pfd.leds[0].turn_on() #Room light ON    
      sleep (1)    
      pfd.leds[1].turn_off() #On Air and Cabinet Off
      listener.activate() #re arm button    
        
    if __name__ == "__main__": 
      global listner   
      listener.register(0, pifacedigitalio.IODIR_FALLING_EDGE, toggle_led0)  
      listener.activate()    
      
      
    while True:    
      pass  

     

    And this is the error I'm getting

    pi@raspberrypi:~ $ python goshow2.py

    Exception in thread Thread-1:

    Traceback (most recent call last):

      File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner

        self.run()

      File "/usr/lib/python2.7/threading.py", line 763, in run

        self.__target(*self.__args, **self.__kwargs)

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 341, in handle_events

        function(event)

      File "goshow2.py", line 20, in toggle_led0

        listener.deactivate() #Inhibit button

      File "/usr/lib/python2.7/dist-packages/pifacecommon/interrupts.py", line 222, in deactivate

        self.dispatcher.join()

      File "/usr/lib/python2.7/threading.py", line 940, in join

        raise RuntimeError("cannot join current thread")

    RuntimeError: cannot join current thread

     

    There has got to be a way to deactivate / inhibit the buttons surely even if there was just a wait time attached to the deactivate.  I'll keep searching.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 10 years ago in reply to brew

    I couldn't follow the entire thread, but generally one trick is to just change the problem if there really is no other easy way out. Not looking into the details of it since I'm worse than useless at Python, and now it looks like the high-level API you're using is spitting out lower-level library related messages, another option would have been to revert to the method mentioned earlier of just checking the process list before calling your player. But your code has changed significantly now.

    Alternatively, if you now want to keep close to the current code you have instead of doing a deactivate, you could change the problem by electrically deactivating the switch. For instance, instead of connecting the switch to 0V, you could connect it to another GPIO pin, which is set to logic low when you want the switch activated, and set to logic high when you want the switch deactivated.

    It is an ugly hack, but it is an option.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to shabaz

    Hi Shabaz,

     

    Thanks for that, I'll attempt your ideas this evening, see how far I get.

     

    Cheers

    Brew

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to brew

    One thing, I notice on line 35 of the posted code, listener is spelled wrong, that could make a difference.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to mconners

    Hi Mike,

    I did catch that one in the code on the unit.  Still no dice.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • brew
    0 brew over 10 years ago in reply to shabaz

    Partial success thanks Shabaz, while going though the relay the the button circuit is broken, an acceptable fix but not the ultimate solution.

     

    Cheers

    Brew

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube