Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Interrupt call_back arguments #8

Closed
jhburkhard opened this issue Jul 6, 2013 · 2 comments
Closed

Interrupt call_back arguments #8

jhburkhard opened this issue Jul 6, 2013 · 2 comments

Comments

@jhburkhard
Copy link

I was unable to get a call back to work until I defined it with 3 arguments not 2 as noted in the documentation "def cb( me, pin, val) :" where "me" is a reference to the class so that class variables are referenced as me.xyz in the call back routine. Is this correct or am I doing something wrong?

Python 2.7 on Raspberry Pi wheezy updated.
My version of RPIO is "RPIO v0.10.0 (gpio-lib v0.10.0/0.4.2a)"

@metachris
Copy link
Owner

Typically you'd add interrupts with RPIO.add_interrupt_callback(...) (http://pythonhosted.org/RPIO/rpio_py.html#RPIO.add_interrupt_callback)

@jhburkhard
Copy link
Author

Chris,

My problem was that Python threw an exception when an interrupt occurred saying that the call back routine required 3 arguments when I used “def gpio_callback( gpio_id, val ):”. Changing the function to “def gpio_callback( newarg, gpio_id, val):” worked. The first argument was equivalent to “self” in the call back function.

The following is an abstract of the code that works. I have attached the actual code as testints.py

Class TestInts :

INT_PIN = 4

def setup( self ) :

   self.StateDevice = MCP23008( address = 0x20 )

   # other setup code to configure the MCP23008

   # as StateDevice

RPIO.setup( self.INT_PIN, direction = RPIO.IN)

RPIO.add_interrupt_callback( self.INT_PIN \

          , self.callBack \

          , edge = 'rising' \

          , threaded_callback = True \

          , debounce_timeout_ms = 15 \

          )



def callBack( me, pin, val ) :

            st = me.StateDevice.getRegINTF() # Get pins that changed

            me.StateDevice.clearInterrupt()

Sorry for any confusion my previous post may have caused. I am new to Python but have > 50 yrs programming so far. I do appreciate what you did as it saved me a LOT of work.

mailto:jhburkhard@gmail.com jhburkhard@gmail.com

Phone 510-530-8568 Home

Phone 510-338-0045 Cell

From: Chris Hager [mailto:notifications@github.com]
Sent: Saturday, July 06, 2013 2:56 AM
To: metachris/RPIO
Cc: jhburkhard
Subject: Re: [RPIO] Interrupt call_back arguments (#8)

Typically you'd add interrupts with RPIO.add_interrupt_callback(...) (http://pythonhosted.org/RPIO/rpio_py.html#RPIO.add_interrupt_callback)


Reply to this email directly or view it on GitHub #8 (comment) . https://github.com/notifications/beacon/6V04vPHLwa1iPOOylIwSlySFxeyw6uubf9M255055iUI4OtnKedcpXT2dDLSL_IO.gif

import RPIO
from MCP23008 import *
import time
import signal
import Queue

class TestInts :
INT_GPIO_PIN = 4
intQueue = Queue.Queue()

def setup( self ):
    # Configure all 8 as input pulled high and inverted polarity
    self.StateDevice    = MCP23008( address = 0x20 )        # I2C device for state info
    self.StateDevice.configAllDirection( MCP23008.INPUT )   # Set State device to all input
    self.StateDevice.configAllInputPolarity( 0xFF )         # Set State inverted polarity
    self.StateDevice.setIntActiveHigh()                     # Normal low on INT goes High 
    self.StateDevice.setIntOnChangeAll( True )              # All bits cause Int on any change
    self.StateDevice.clearInterrupt()                       # Clear any pending
    # Set the Pi's GPIO pin as input and no pullup or down
    RPIO.setup( self.INT_GPIO_PIN, direction = RPIO.IN)
    RPIO.add_interrupt_callback(  self.INT_GPIO_PIN \
                                , self.callBack \
                                , edge = 'rising' \
                                , threaded_callback  = True \
                                , debounce_timeout_ms = 15 \
                                )

def callBack( me, pin, val ) :
    st  = me.StateDevice.getRegINTF()       # Get the pins that changed
    me.StateDevice.intDisableAll()          # disable ints on all MCP23008 pins
    reg = me.StateDevice.clearInterrupt()   # Clear the interrupt and return the status reg

print "Interrupt on " + str( pin ) + " INTF Reg " + str( st ) + " val " + str( val )

    t = time.time()
    me.intQueue.put_nowait( [st, reg, t - me.lastIntTime ] )
    me.lastIntTime = t
    me.StateDevice.intEnableAll()           # ?? Will pending int be registered ??

def sig_handler( self, signal, frame ) :
    print "Ctrl-C pressed: exiting."
    RPIO.cleanup()
    self.keepRunning = False

def runit( self ) :
    signal.signal( signal.SIGINT, self.sig_handler )
    self.keepRunning = True
    self.setup()
    self.lastIntTime = time.time()
    RPIO.wait_for_interrupts( threaded = True )
    while( self.keepRunning ) :
        try :
            x = self.intQueue.get( True, 5 )
            print( 'Changed pins {0[0]:#04x}\tIO Pins {0[1]:#02x}\tTime {0[2]:f}'.format( x) )
        except Queue.Empty :
            print "Q is empty timeout"
    RPIO.stop_waiting_for_interrupts()
    RPIO.cleanup()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants