Python Forum

Full Version: The logic of a main routine
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
no, it doesn't. this is just a function definition. it will not run unless you call it
(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:
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
(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.
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.
I see. Thank you.