Question

I am designing the program structure for an upcoming project right now and I'm stuck on the following issue:
Boiled down to its most important elements my program is split into two files logic.py and oscilloscope.py.
In the logic.py I initialize a device on /dev/input/.. and set up filters for signal processing and so on. In oscilloscope.py I have an oscilloscope implementation using Pygame that should interpret/display input coming from the registered input device.

Now I have a main loop in logic.py that continuously listens to input coming from the device and then forwards the processed data to the oscilloscope but also I have the main loop from the Pygame oscilloscope implementation.
How do I prevent my program control flow from getting stuck in the Pygame main loop when initializing the oscilloscope from logic.py like so:

from oscilloscope import *
...
... #initializing filters to use, device to listen to etc.
...
self.osc = Oscilloscope(foo, bar) #creating an instance of an oscilloscope implemented with Pygame

self.osc.main() #calling the main loop of the oscilloscope. This handles all the drawing and updating screen.

#usually the flow would stop here as it is stuck in the Pygame main loop
#I need it to not get stuck so I can call the second main loop.

self.main() #captures and processes data from /dev/input/... sends processed data to the Pygame oscilloscope to draw it.

As there is not any actual code yet I hope the comments clarify what I want to do.

Was it helpful?

Solution

How about this: (and I understand that my suggestion may just be theoretical):

You call the self.osc.main() loop from the self.main() loop. To do that, you will have to edit your self.osc.main() function so that it does not run forever but only once. You keep the self.main() loop the same and call the self.osc.main() once every loop.

INSTEAD OF WHAT YOU ARE DOING NOW WHICH IS SOMETHING LIKE THIS:

def main(): #This is the self.osc.main() function
    while True: #run forever
        doSomething()

def main(): #This is the self.main() function
    while True: #also run forever
        doSomethingElse()

YOU CAN DO THIS INSTEAD:

def main(): #This is the self.osc.main() function
    doSomething() #notice I removed the loop. It only runs once. This is because the looping is handled by the other main() function (the one you call self.main())

def main(): #This is the self.main() function
    while True: #also run forever
        self.osc.main() #since we have now changed the self.osc.main() loop to run only once
        doSomethingElse()

I Hope That Helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top