AforAlex
Posts: 85
Joined: Thu May 31, 2012 7:56 pm

Multitasking functions ?

Sat Feb 22, 2014 12:06 am

Hi,

Not sure if this is easily possible or not, I'm hoping it is.

I'm playing around with the PiFace CAD (http://www.piface.org.uk/products/pifac ... d_display/). Which is nice, except when there is more than 16 characters that you want to display then you have to scroll the text across the screen ... which isn't so nice.

I've written a little test program that gets the input from the user and then scrolls the text around and around on the LCD screen. All good except that in scrolling the screen this takes up the focus of the program and the user can't enter anything else. This is very much just a simple test/sandbox type program that I quickly put together to illustrate what I'm trying to achieve.

Code: Select all

import pifacecad
import time

#Set up the CAD
cad = pifacecad.PiFaceCAD()
cad.lcd.backlight_on()

#Function to scroll the lcd screen
def scroll_lcd():
        var=1
        while var == 1:
                cad.lcd.move_left()
                time.sleep(0.4)

#Main body of program. Collect the users input and output it to the LCD screen 
#and then scroll

var = 1
while var == 1:
        line_1_input = input()
        cad.lcd.clear()
        cad.lcd.write (line_1_input)

        scroll_lcd()
Can this be done ? I want to be able to launch the function scroll_lcd and for it then to just run happily away in the background.

I've been thinking about this and I think I could achieve this by running the scroll function as a separate script (and I could pass the text that I want it to display via a text file / MySQL Database / XML which one program writes to, the other picks up and displays on the LCD screen ..... but only if that really is the best way.

Any help/advice would be very much appriciated

User avatar
paddyg
Posts: 2387
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Multitasking functions ?

Sat Feb 22, 2014 12:51 am

use threading. Lots of questions on this very forum or look in python docs. One thing, don't be tempted to write the output string to a global variable. This seems the easiest way but it's a slippery slope. Pass it as an argument to the thread function.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

AforAlex
Posts: 85
Joined: Thu May 31, 2012 7:56 pm

Re: Multitasking functions ?

Sun Feb 23, 2014 12:13 pm

Many thanks for pointing in the right direction, very much appreciated :-)

Return to “Python”