Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop question
#1
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
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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
Reply
#8
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)
Reply
#9
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
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A question about 'Event loop is closed' fc5igm 2 2,202 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,668 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,724 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,313 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  while loop question KEYS 2 2,013 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,633 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,848 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 2,470 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Question about for loop not creating an infinite loop. FWendeburg 1 2,117 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Loop Condition Question malonn 6 3,469 Aug-01-2018, 01:56 PM
Last Post: malonn

Forum Jump:

User Panel Messages

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