Coder Social home page Coder Social logo

Interrupt call_back arguments about rpio HOT 2 CLOSED

metachris avatar metachris commented on July 29, 2024
Interrupt call_back arguments

from rpio.

Comments (2)

metachris avatar metachris commented on July 29, 2024

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

from rpio.

jhburkhard avatar jhburkhard commented on July 29, 2024

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:[email protected] [email protected]

Phone 510-530-8568 Home

Phone 510-338-0045 Cell

From: Chris Hager [mailto:[email protected]]
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()

from rpio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.