Break out of input after interrupt
I'm using input to have the user input something. If the button is pressed (using the expansion board) the input should be interrupted and some other code should be executed. This almost work:
state = 1
handler for interrupt
state = 2
initialize GP17 in gpio mode and make it an input with the pull-up enabled
if state == 1:
input("Give me an input: ")
elif state == 2: print("We've been interrupted") state = 1
I do get into the interrupthandler, however when the code continues it still waits for the user input. If i press anything+enter the code continues into state 2 and back to state 1 as expected. How can I automatically break out of the input prompt after the interrupt?
edit: Sorry for the formatting - I dont know how to get it right..
@robert-hh Thanks! Yeah I can do some stuff in the handler, but as far as I can understand it is recommended to keep the handler short and effective. In this case I would like to transmit (LoRaWAN), receive some data etc.
Anyway - thanks for the help - I'll figure something out. :)
robert-hh last edited by
@mahe I looked a little bit though the code yetserday, and I found no place to force abort an input statement.
But the interrupt happens and the interrupt handler gets control. If you just want to do soemthing and then return to the input, then you can do that in the interrupt handler.
Otherwise, you have to implement your one busy waiting input function, which checks to interrupt events being noticed. But that's not elegant and would cover only a single use case.
@peekay123 @robert-hh Thats correct, sorry if the question was unclear. The problem is that the interrupt handler does not break the 'input-state' (or whatever it's called) and move into state 2. I also looked a bit at try/except, but couldn't figure out how to throw an exception..
This is my first venture into Python, so it's all very new to me. I've been looking a bit at threading, maybe that's a possibility.. Anyways - thanks for the effort!
robert-hh last edited by robert-hh
@peekay123 As far as I understand, this is not the intention of @mahe. He wants the input statement to be interrupted by the external button.
I will check later, if raising a KeyboardInterrupt in the ISR may do the trick. It may not, since there is still a bug in the formware in that KeyboardInterupts are not serviced timely in all circumstances. If yes, the input statement has to be gated by a try/except clause.
Edit: That does not work. The main code still sits in the line edit state of the input statement.
@mahe, you answered you own question when you say:
"If i press anything+enter the code continues into state 2 and back to state 1 as expected"
So set state = 3 instead of 1 so that the code continues. When you are ready for input again, set state = 1 again.