Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The logic of a main routine
#1
In C all my functions run in the main loop
void main(void)
{
    Init();
    while (1)
    {
        DoSomeStuff();
    }
}
Now I see a Python example
def demo():
    ble = bluetooth.BLE()
    central = BLETemperatureCentral(ble)

    not_found = False

    def on_scan(addr_type, addr, name):
        if addr_type is not None:
            print("Found sensor:", addr_type, addr, name)
            central.connect()
        else:
            nonlocal not_found
            not_found = True
            print("No sensor found.")

    central.scan(callback=on_scan)

    # Wait for connection...
    while not central.is_connected():
        time.sleep_ms(100)
        if not_found:
            return

    print("Connected")

    # Explicitly issue reads, using "print" as the callback.
    while central.is_connected():
        central.read(callback=print)
        time.sleep_ms(2000)

    # Alternative to the above, just show the most recently notified value.
    # while central.is_connected():
    #     print(central.value())
    #     time.sleep_ms(2000)

    print("Disconnected")


if __name__ == "__main__":
    demo()
demo() doesn't run constantly, does it?
Reply
#2
no, it doesn't. this is just a function definition. it will not run unless you call it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Mar-18-2020, 09:25 AM)buran Wrote: no, it doesn't. this is just a function definition. it will not run unless you call it

actually I call it
if __name__ == "__main__":
    demo()
but in order to run constantly - should i create a loop?
while True:
Reply
#4
yes, you should if you want to run it indefinitely. However do you want it? In it there is loop that will run until you are connected (lines 27-29). Also the code structure looks a bit weird - maybe I miss the goal here, but why not connect once and listen while the connection is alive? if you run it in a infinite loop it will try to reconnect again immediately. Also I personally would define on_scan outside the function. But it may require to refactor the code and I am not familiar with the package, so I don't know how much refactoring it will need in order to access central
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Mar-18-2020, 09:42 AM)buran Wrote: yes, you should if you want to run it indefinitely. However do you want it? In it there is loop that will run until you are connected (lines 27-29). Also the code structure looks a bit weird - maybe I miss the goal here, but why not connect once and listen while the connection is alive? if you run it in a infinite loop it will try to reconnect again immediately. Also I personally would define on_scan outside the function. But it may require to refactor the code and I am not familiar with the package, so I don't know how much refactoring it will need in order to access central

The goal is to scan BLE beacons constantly.
In C I would create while(1) loop and some state machine - in case of disconnected - try to connect - in case is connected - scan for beacons.
Reply
#6
Using a loop would be the same here as it would in C. The conditional

if __name__ == "__main__":
instructs the interpreter to check if the file is the main file or imported as a module. If it is imported as a module, the code block in the body of the conditional will not execute. If it is not imported (meaning that this file is being run), the code block will execute. It will only execute once in this case because there is no loop.
Reply
#7
I see. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,617 May-04-2021, 08:58 PM
Last Post: liudr
  A dynamic link library (DLL) initialization routine failed ish93 0 1,776 Jan-11-2021, 08:22 PM
Last Post: ish93
  How to call a routine in another Python program kenwatts275 1 1,642 May-17-2020, 05:37 PM
Last Post: deanhystad
  keyboad scanner routine(linux and windows) 1885 0 1,908 Oct-26-2019, 03:34 PM
Last Post: 1885

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020