Interrupts with Raspberry Pi and PiFace Digital
Question: I have just set up a Raspberry Pi with the PiFace Digital element14 I/O board.
So far, I've followed several steps to get it working such that I can
interface with the I/O ports (control the LED's and operate the switches to do
stuff) The python code I wrote works fine and I can make it do stuff.

Currently I'm just messing around, getting the feel for programming on the Pi,
and trying to get an understanding for the features. I wanted to set-up a
state machine for a simple 6-bit binary counter which counts up and down when
I tell it to, which I was able to do pretty easily. But when I tried to take
it to the next level and use interrupts for setting the state I ran into
issues.

I followed the
[Manual](https://media.readthedocs.org/pdf/pifacedigitalio/latest/pifacedigitalio.pdf)
as well as [This Guide](http://blog.oddbit.com/2013/08/05/interrupts-on-the-
pi/) to get the code for activating the interrupts.

The code I wrote executes without error, however, either the interrupts are
not detected or they don't do anything, I'm not sure which. My code is below.
I know that the while loop works for the 'waiting' and 'counting' states
because I can define the initial condition. It counts properly, so I feel
pretty sure that the while loop is okay, there's just no state changes.

    
    
    import pifacedigitalio as pfio
    import os
    import time
    
    def startCounter(event):
     global state
     state = 'counting'
     print('counter started')
    
    def stopCounter(event):
     global state
     state = 'waiting'
    
    def stopProg(event):
     global state
     state = 'stop'
    
    def resetCounter(event):
     global state
     state = 'reset'
    
    def setLEDs(stateArray):
     i = 0
     for state in stateArray:
      pfio.digital_write(i,state)
      i = i + 1
    
    def calcBools(count):
     binString = bin(count).rsplit('0b')[1]
     stringLength = len(binString)
     zeroString = '0' * (8 - stringLength)
     newString = zeroString + binString
     i = 0
     boolsOut = [0,0,0,0,0,0,0,0]
     for bit in newString:
      if bit == '1':
       boolsOut[i] = 1
      i = i + 1
     return boolsOut
    
    ####################
    ### MAIN PROGRAM ###
    ####################
    
    pfio.init()
    
    pifacedigital = pfio.PiFaceDigital()
    listener = pfio.InputEventListener(chip=pifacedigital)
    
    signalDirection = pfio.IODIR_RISING_EDGE
    listener.register(0, signalDirection, stopProg)
    listener.register(1, signalDirection, startCounter)
    listener.register(2, signalDirection, stopCounter)
    listener.register(3, signalDirection, resetCounter)
    listener.activate()
    
    counter = 0
    running = True
    state = 'waiting'
    setLEDs([0,0,0,0,0,0,0,0])
    direction = 'up'
    
    while(running):
     if state == 'stop':
      running = False
      listener.deactivate()
      counter = 0
     elif state == 'waiting':
      time.sleep(1)
      print('waiting...')
     elif state == 'counting':
      if direction == 'up':
       counter = counter + 1
      else:
       counter = counter - 1
      if counter > 63:
       direction = 'down'
      elif counter == 0:
       direction = 'up'
     elif state == 'reset':
      counter = 0
     else:
      time.sleep(0.1)
    
     setLEDs(calcBools(counter))
     print(counter)
     time.sleep(0.25)
    

So this code doesnt work, and I tried something else which also didnt work
using the pifacecommon library, replacing some of the lines of code with:

    
    
    import pifacecommon as pfc
    
    readport = pfc.mcp23s17.GPIOA    # I also tried GPIOB to no avail
    listener = pfc.interrupts.PortEventListener(readport, 0)
    

After this the listener commands are identical for both methods. Along with
this I attempted to use the pfc.mcp23s17.write command, but apparently it
didn't exist or some foolish excuse like that.

Thanks in advance for reading this and even more if you respond, and more yet
if you have an answer for me!

-Ben

Edit (SOLVED): My answer was in the comments of the 2nd link i provided the
whole time :( Turns out I had everything written correctly, I just needed to
run the file from the terminal and not IDLE3.


Answer: So the answer to my question was in the comments of the [2nd
link](http://blog.oddbit.com/2013/08/05/interrupts-on-the-pi/) I provided the
entire time. All I needed to do was run the program from the terminal, and not
IDLE3.

I believe the reason lies in how interrupts are handled within Linux. Linux
ISR's aren't true ISR's, the processor puts the ISR in a sleep mode until it's
called. Can anyone elaborate on why IDLE3 doesn't support interrupts?

