Quick question about GPIO interrupts, threaded code design - Raspberry Pi Forums


i'm more or less programming beginner , i'm hoping can me code design handling interrupts , loops. have following "while" loop wraps rotary encoder (gpio) actions allow user scroll through numbers on lcd display (also gpio) of little device. numbers "target temperatures", might put more context on variable names below. before loop created interrupt listener button press, user presses confirm target temperature they've chosen. when interrupt occurs, calls function (named run in case).

here's question: after interrupt occurs, how can end loop? i'm trying use following code block. when interrupt happens, first couple lines of callback function (run) end interrupt listener , set value in temp1 object should make "get_temp_started()" return 1, hoping end loop. however, understand it, callback function starts new thread, , guess (?) can't manipulate temp1 instance. anyway, here's loop:

code: select all

    gpio.add_event_detect(2, gpio.both, callback=run, bouncetime=300)     while temp1.get_temp_started() != 1:             .....rotary encoder , lcd stuff..... 
here's first few lines of run function:

code: select all

def run(channel):     gpio.remove_event_detect(2)     temp1.start_temp()     ......more stuff...... 
, here temp1 object class:

code: select all

class temperature:     def __init__(self):         self.temp_started = 0     def start_temp(self):         self.temp_started = 1       def get_temp_started(self):         return self.temp_started 
when run this, following ambiguous looking error:
sys.excepthook missing
lost sys.stderr

if comment out temp1.start_temp() line in run function, runs without error , run function proceeds run fine. while loop never closes , rotary encoder , lcd still active.

tips? i'm assuming need way end previous thread or loop, have no idea how to? or maybe bad design overall. can of without interrupts @ point it's less making work, , more learning why it's not working.

thanks!

ok, think figured out. python ending application because main thread has ended. error might timing problem while exiting program. if add "sleep(100)" following while loop works great no errors 100 seconds, , program exits gracefully. i'll force thread stay open loop.

welcome comments on code design. me still seems little awkward, works now.


raspberrypi



Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial