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

    A good methodology to code Wi Py 2.

    Scheduled Pinned Locked Moved WiPy 2&3
    wi py 2.0programming sty
    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.
    • C Offline
      ColombianAus
      last edited by

      Hello there,

      I would like to read/find some advise in regards to how to create a "infinity loop" in Wi Py 2.0. I have done it by doing the following, but I am not so sure it is the best way.

      so...I have a python.py file, and I might have the following functions defined and a "main" function inside a while(1)

      import <> from <>

      def aFuncion1:
      #some code here...

      def aFuncion2:
      #some code here...

      def main():
      aFuncion1()
      aFuncion2()

      while(1):
      main()

      I have the feeling that I am wasting resources here, and there is a better way. so question time!!

      1. Am I right or wrong in my programming approach?
      2. I am not familiar with Threads? is this the way to go?
      3. Any other ways to be more efficient code/ memory usage?
      4. Any doco that I can be pointed to?

      Cheers
      Colombian

      L bmarkusB 2 Replies Last reply Reply Quote 0
      • L Offline
        livius @ColombianAus
        last edited by

        @ColombianAus

        This depend of code - e.g. better is have pin handler instead read it in every loop.
        You can also use machine.idle to prevent 100% CPU usage
        Without more specific sample of usage it is rather hard to advice something.

        think about people first, then computers

        C 1 Reply Last reply Reply Quote 0
        • C Offline
          ColombianAus @livius
          last edited by

          @livius

          Thank you for your reply. I am totally new to micro python programming style. All I want to do, at the moment is to turn a LED On and Off a fix number of times. Would you mind to elaborate a bit more about the pin handler option?

          Cheers
          ColmobianAus

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @ColombianAus
            last edited by livius

            @ColombianAus

            from machine import Pin
            
            def pin_handler(arg):
                print("got an interrupt in pin %s" % (arg.id()))
            
            p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
            p_in.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin_handler)
            

            but for blinking led it is not good

            import pycom
            import time
            pycom.heartbeat(False)
            for cycles in range(10): # stop after 10 cycles
                pycom.rgbled(0x007f00) # green
                time.sleep(5)
                pycom.rgbled(0x7f7f00) # yellow
                time.sleep(1.5)
                pycom.rgbled(0x7f0000) # red
                time.sleep(4)
            

            look into docs
            https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/led.html

            think about people first, then computers

            1 Reply Last reply Reply Quote 0
            • bmarkusB Offline
              bmarkus @ColombianAus
              last edited by

              @ColombianAus said in A good methodology to code Wi Py 2.:

              while(1):
              main()

              Never write while(1), use while(True) instead.

              1 Reply Last reply Reply Quote 0
              • C Offline
                ColombianAus
                last edited by

                Hi Everyone,

                Ok, so it looks like at the end, it is not that bad to use a While(True) to generate an infinite loop. I know that a ON/OFF LED is not really a good example.

                thank you everyone for the comments

                Cheers
                ColombianAus

                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