ncguk
Posts: 27
Joined: Fri Mar 07, 2014 10:04 pm

Python and timed loops

Tue Aug 05, 2014 10:40 pm

Hello all. I have an Adafruit 4-digit 7 segment display w/ i2c backpack and, using Python, am trying to get it to display the time for 30 seconds and then the temperature for 15 seconds on an infinite loop.

Displaying the time and temperature individually works well (thanks to Adafruit's tutorials), but having each displayed for the allotted time while still being "live" (i.e. so the time and temperature will update dynamically) is proving to be a challenge.

From searching around it seems I will have to use threading and possibly threading.Timer but I'm still struggling with the basics, let alone threading. If anyone has any nice, relatively simple, elegant methods or suggestions to point me in the right direction, I'd be very grateful.

Here's the code as it stands:

Code: Select all

#!/usr/bin/python

from Adafruit_LED_Backpack import SevenSegment
import os
import glob
import time

display = SevenSegment.SevenSegment()
display.begin()
display.clear()
display.set_brightness(1)

format = "%H%M"

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

print "Press CTRL+Z to exit"

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c

def mytime():
        colon = True
        now = time.strftime(format)
        display.print_number_str(now)
        display.set_colon(colon)
        display.write_display()
        time.sleep(1.0)
        colon = not colon

def mytemp():
        temp_c = read_temp()
        display.print_float(temp_c, 1, justify_right=False)
        display.set_digit_raw(3, 0x63)
        display.set_colon(False)
        display.write_display()
        time.sleep(1.0)

while True:
        mytime()
        mytemp()
Thanks in advance.

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python and timed loops

Wed Aug 06, 2014 1:00 am

No threading needed. How about

Code: Select all

    while True:
        for i in range(30):
            mytime()
        for i in range(15):
            mytemp()

ncguk
Posts: 27
Joined: Fri Mar 07, 2014 10:04 pm

Re: Python and timed loops

Wed Aug 06, 2014 10:53 am

That works perfectly, thank you very much.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Python and timed loops

Wed Aug 06, 2014 11:48 am

If you want mytime() to flash the colon, then you'll need to move "colon = True" outside of the mytime function, and have "global colon" as the first line of mytime (to indicate that you're accessing the global colon variable, and not a new local colon variable which is only valid within each call of the function).

ncguk
Posts: 27
Joined: Fri Mar 07, 2014 10:04 pm

Re: Python and timed loops

Wed Aug 06, 2014 12:05 pm

AndrewS wrote:If you want mytime() to flash the colon, then you'll need to move "colon = True" outside of the mytime function, and have "global colon" as the first line of mytime (to indicate that you're accessing the global colon variable, and not a new local colon variable which is only valid within each call of the function).
Thanks AndrewS. I'd made it work by moving "colon = True" out of mytime() and moving "colon = not colon" above "my time()" in "while True:", i.e. (in truncated form):

Code: Select all

colon = True

def mytime():
    now = time.strftime(format)
    display.print_number_str(now)
    display.set_colon(colon)
    display.write_display()
    time.sleep(1.0)

while True:
        for i in range(30):
                colon = not colon
                mytime()
        for i in range(15):
                mytemp()
But I can see the method you suggested will be better and neater.

Return to “Python”