Skip to content

Question: Example Usage with While Loop #17

@dhildreth

Description

@dhildreth

I'm attempting to use this excellent library in a project where I'm attempting to turn on an LED when an interrupt is triggered, but I have a feeling I'm missing something basic. This is the first time I've heard of Twisted Reactor, so I'm wondering if that has something to do with my misunderstanding of what I thought was relatively straightforward.

I have a while loop which reads a light sensor every second. The light sensor (I2C device) is programmed to trigger an interrupt when the value reads below a certain threshold. I have the interrupt pin of the sensor connected to GPIO 65 of my SBC. I then have an LED hooked up to GPIO 64 of my SBC. Something like this:

import time
from twisted.internet import reactor
from sysfs.gpio import Controller, OUTPUT, INPUT, RISING

Controller.available_pins = [64, 65]
led_en = Controller.alloc_pin(64, OUTPUT)

def pin_changed(pin, state):
    print("Pin changed to %d state" % state)
    if state:
        led_en.set()   # Sets pin to high logic level
    else:
        led_en.reset()

intt = Controller.alloc_pin(65, INPUT, pin_changed, RISING)

reactor.run()

while True:

    # Enable sensor light interrupts
    # Read and store sensor light value via I2C
    # Print sensor light value to console for debugging
    print("Testing!")
    sleep(.5)

Anyways, so I'm expecting my while loop to execute, but it never does. Instead, I get "Pin changed to 0 state", from the pin_changed() callback and then nothing!

$ sudo python test-gpio-interrupt.py 
SysfsGPIO: alloc_pin(64, out, None, None, 0)
SysfsGPIO: alloc_pin(65, in, <function pin_changed at 0x76468830>, rising, 0)
Pin changed to 0 state

I then have to Ctrl + C to end the process.

I would've expected the code in the while loop to execute. I must be having a lapse of common sense and I'm hoping you can help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions