Python Forum

Full Version: While loop question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have developed a small standalone device using Pi 3 and Python.
Basically it's 5 buttons that activate a countdown timer for 5 different time lapses.
(Like you would use in interval training) Time shows on a 2x16 lcd.

As these buttons are used randomly, there are 5 separate 'if event_detect(...'
waiting in an ever spinning While loop, (One for each GPIO pin)
All this works (fine). A sixth button does a shutdown -h...

But i am wondering if there is a more elegant way of doing this, because very
soon i may need 10 event_detects...with 10 if statements.

What other coding method, if any, a part from this ever spinning while loop,
could there be to achieve this? Basically it's waiting for random input from any number of GPIO pins.

thx,
Paul
An event-driven application. A process is started only when a certain event has happened. Like pressing a button. You create an event listener and run it. That's all. No endless while loops.
(Dec-21-2017, 04:55 PM)wavic Wrote: [ -> ]An event-driven application. A process is started only when a certain event has happened. Like pressing a button. You create an event listener and run it. That's all. No endless while loops.

Yes, indeed, like we do all the time in visual studio on tablet/phone apps.
Being a python newbie, i thought about that, but i do not know how to implement it in the given situation.

Basically a standalone box with a few buttons.
What event can python listen to, other than one of the buttons being pressed?
And for that i need the endless while loop, no ?
Add a touchscreen to put the event on the lcd? Surely that would be overkill.

What am i missing ?

Thanks
Paul
While this is the obvious approach I can't tell you more because I never needed something like that. Perhaps I have to learn how to do something like that too.

However, I recommend Pygame because it can detect keypress. But how to do it... Don't know. We have Pygame tutorials here, thanks to @metulburr. Take a look
(Dec-22-2017, 07:53 AM)wavic Wrote: [ -> ]While this is the obvious approach I can't tell you more because I never needed something like that. Perhaps I have to learn how to do something like that too.

However, I recommend Pygame because it can detect keypress. But how to do it... Don't know. We have Pygame tutorials here, thanks to @metulburr. Take a look

OK, i looked at a couple of these games.
I see while loops all over.
Probably, if you aim for standalone device, with "buttons only",
you cannot escape while loops of sorts.

The only thing i can see to prevent the while loop from whizzing around zillions of times, waiting for an input,
is to make it the actual clock of eg. 1 second a tick, and build the app around that. Essentially this is what i did.

The fact that my question is not possible, is also an answer Wink

thx,
Paul
There is a way, don't have time to dig it from my downloaded tutorials, books etc.
Take a look at pynput. This is just one example.
(Dec-22-2017, 09:29 AM)wavic Wrote: [ -> ]There is a way, don't have time to dig it from my downloaded tutorials, books etc.
Take a look at pynput. This is just one example.

Ok, will have a look!
thx,
Paul
Using a while loop with a time.sleep() call is fine, and is used in almost all graphical programs in almost all languages.  I don't think that's the issue.  You CAN probably avoid having multiple "if" blocks, though, by storing your events in a dict.

Something like:
def button_1():
    # do something
    pass

def button_2():
    # do something else
    pass

# key=pin, value=callback
events = {
    "GPIO_1": button_1,
    "GPIO_2": button_2
}

while True:
    for pin in events:
        # guessing "1"==pressed
        if raspberry.check_pin(pin, "1"):
            # pin is pressed, fire the event
            events[pin]()
    time.sleep(0.01)
Thanks for thinking creatively.
2 observations:
1. I want to reduce the number of if's inside the while loop,
because it performs a timer function, and i assume every statement "inside" takes away a little
time away from the accuracy. (Although i don't need 1/1000 sec precision.) It's the "idea".
You seem also to take away (sleep(0.01)) a little time with every  if-hit.
2. Although my "C" is a little rusty, i seem to detect a callback function.
I did that in python, and ran into problems until somebody in this forum pointed out that
callbacks and while loops don't mix. (?)

What i did not understand at the start of this (rookie) project, is the fundamental difference between
a program developed say on a windows or android platform with a touchscreen and a
standalone device with no screen.

Your e.g. windows app ("object-oriented"), sits there and does nothing until you press a button.
My device with python always wants to halt, i have to explicitely keep it running with a while loop.
Hardware buttons don't behave like software buttons  :-)

Maybe/Probably there is something i don't know yet about the RPi-python combo to mimic this, and that is really what i am looking for.
Paul
(Dec-29-2017, 08:03 AM)DPaul Wrote: [ -> ]Your e.g. windows app ("object-oriented"), sits there and does nothing until you press a button. My device with python always wants to halt, i have to explicitely keep it running with a while loop. Hardware buttons don't behave like software buttons :-)

Hardware is always there.  Software runs until it's done, then stops.  There's no such thing as software that does nothing until something happens.  The "does nothing" step IS a while loop.  That's the only construct available.  Even if somehow the button had a kernel module that caused an Interrupt Signal to be sent out, so your program could respond immediately to the button press... it'd still be sitting within a while True loop.  Otherwise, the program would reach the end, and stop.  And if it stops, then it can't still be around to respond to events.