3 Replies Latest reply on Apr 29, 2015 9:48 AM by Intel_Peter

    IO event handling framework in python

    LightBulb

      Is there a better way to handle IO events in python without reinventing the wheel/using the loop?

       

      The Galileo project I am working on is mostly a hardware one. As a first step I just want to replace a light switch with a sensor button and a relay. It all works fine and here is the code for the proof of concept. When the event "button is pressed" happens (mraa.Gpio(4).read()=1) the light is turned on mraa.Gpio(8).write(1)

      import mraa
      import time
      
      mraa.Gpio(8).dir(mraa.DIR_OUT)
      
      while True:
      
          if mraa.Gpio(4).read()==1:
              mraa.Gpio(8).write(1)
          else:
              mraa.Gpio(8).write(0)
      
          time.sleep(.5)
      
      
      
      
      
      

       

      Is there a better way to handle such events apart from polling Gpio in a loop? All the debouncing that will have to be implemented too...

      I can imagine this was already implemented a hundred times.

      For example I have found a Linux daemon boilerplate PEP 3143 - Standard daemon process library | Python.org

       

      So I am looking for python package that would give me the ability to assign IOs for events and then just write event handler functions that are reacting to those events. Any thoughts?

        • 1. Re: IO event handling framework in python
          Intel_Peter

          Hello LightBulb,

           

          In this thread: Edison Interrupt Service Routine another Edison user asks a very similar question to yours. There are two examples of how to do it in C and Python. Why don't you take a look at it?

           

          Peter.

          • 2. Re: IO event handling framework in python
            LightBulb

            That example kindly referred me to is the correct one. I saw it before but did not take seriously the first time I saw it because it seemed so easy.

            import mraa  
            import time  
              
            # This is a simple light switch code  
            # connect Touch Sensor to the D4 and the Relay to the D8  
              
            # inside a python interupt you cannot use 'basic' types so you'll need to use objects  
            class Toggle:  
              state = 0  
              
            t=Toggle()  
              
            # D8 for the relay  
            mraa.Gpio(8).dir(mraa.DIR_OUT)  
              
            # D4 for the touch sensor  
            mraa.Gpio(4).dir(mraa.DIR_IN)  
              
            def pressed(args):  
              if t.state==0:  
                mraa.Gpio(8).write(1)  
                t.state = 1  
              else:  
                mraa.Gpio(8).write(0)  
                t.state = 0  
              
            # here the interrupt is set on Gpio(4)  
            # fourth argument is not used  
            mraa.Gpio(4).isr(mraa.EDGE_RISING, pressed, pressed)  
              
            # Delay destructors on script termination otherwise ISR will not run  
            time.sleep(60) 
            
            1 of 1 people found this helpful
            • 3. Re: IO event handling framework in python
              Intel_Peter

              Hi LightBulb,

               

              I'm glad to be of help and I'm very happy that my suggestions was what what you were looking for.

               

              Peter.