Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Break out of input after interrupt

    Scheduled Pinned Locked Moved Firmware
    esp32other
    6 Posts 3 Posters 1.7k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      mahe
      last edited by mahe

      Hi,

      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

      def interrupthandler(pin):
      print('interruption detected')
      global state
      state = 2

      initialize GP17 in gpio mode and make it an input with the pull-up enabled

      button=Pin('G17',mode=Pin.IN, pull=Pin.PULL_UP)
      button.callback(trigger=Pin.IRQ_FALLING, handler=interrupthandler)

      while True:
      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?

      Thanks!

      edit: Sorry for the formatting - I dont know how to get it right..

      1 Reply Last reply Reply Quote 1
      • P Offline
        peekay123
        last edited by

        @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.

        robert-hhR 1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @peekay123
          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.

          M 1 Reply Last reply Reply Quote 0
          • M Offline
            mahe @robert-hh
            last edited by

            @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-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @mahe
              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.

              M 1 Reply Last reply Reply Quote 0
              • M Offline
                mahe @robert-hh
                last edited by

                @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. :)

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom