ScrollPhat HD query

This code prints the temp (from envirophat) to the terminal every second.
ScrollPhatHD scrolls only very slowly.

I’m not sure where the scrollphat.clear needs to go, or how to control the scroll speed as it’s much slower than what’s being printed on the terminal.

All help, much appreciated.
Thank you.

import time
import sys
from envirophat import light, weather, motion, analog, leds
import scrollphathd

try:
    while True:
        temperature = weather.temperature()
	temp = " The temp is " + str(weather.temperature())
	

	print str(temp)
	
        scrollphathd.write_string(
        temp, 
        brightness=0.1
        )

#! Show the buffer
	scrollphathd.show()	
#! Scroll the buffer content
	scrollphathd.scroll()

# Where does .clear go?!	        
#  	scrollphathd.clear()

# Why does terminal print every second, but phat is much slower?
        time.sleep(1)
        

except KeyboardInterrupt:
    pass

Here is what I use to scroll the time on a ScrollPhat.

Not a HD version, but the principle should be the same.

#!/usr/bin/env python

import sys
import time

import scrollphat

brightness = scrollphat.set_brightness(5)
             
while True:
    try:
        scrollphat.write_string(time.strftime("%H:%M     "))
        scrollphat.scroll(1)
        time.sleep(0.5)

    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

Adjust the:

    scrollphat.scroll(1)
    time.sleep(0.5)

Until you get the speed you need.

Cheers,

Simon.

I’ve got that OK, tested with the Christmas countdown example project.

I can’t work out how to scroll a reading from envirophat.

Thanks.

Look into string.format - https://docs.python.org/3.1/library/string.html#formatspec

The specification language is fairly opaque but you’ll only generally need a few useful snippets.

A whole number

>>> "{:03d}".format(100)
'100'
>>>

With leading zeros:

>>> "{:03d}".format(99)
'099'

Temperature (or a decimal number with 5 digits and 2 decimal places):

>>> "{:5.2f}".format(19.22)
'19.22'

Pressure (or a decimal number with 7 digits and 2 decimal places):

>>> "{:7.2f}".format(1020.22)
'1020.22'

And you can use these at any point in your text to add your number:

>>> "The temperature is {:5.2f}C".format(22.15)
'The temperature is 22.15C'

And, of course, from a variable too:

temperature = 19.2524
>>> "The temperature is {:5.2f}C".format(temperature)
'The temperature is 19.25C'

I commented out scrollphathd.clear() and now have some scrolling going on.
Scrolphathd shows “Temp is 20.## CC”
Where ## is blocky text
And CC is an offset C on another C

Seems it’s writing on top of itself?

import time
import sys
from envirophat import light, weather, leds
import scrollphathd


try:
    while True:


#	lux = light.light()
	temperature = weather.temperature()	

#	print light.light()
#	print(" Temp is {:5.2f} C".format(temperature))
	
#	scrollphathd.clear()	
        scrollphathd.write_string(" Temp is {:5.2f} C".format(temperature),
        brightness=0.5
        )

		
		
#! Show the buffer
	scrollphathd.show()	
#! Scroll the buffer content
	scrollphathd.scroll()



# Why does terminal print every second, but phat is much slower?
        time.sleep(0.01)
        

except KeyboardInterrupt:
    pass

Using temperature now as it’s easier to change for testing.
But, EVENTUALLY!, I would like Enviorphat to tell me, via a scrollphathd, the status of an up-n-over garage door.

Heading seems ideal for this purpose.

But at this rate, with my lack of python understanding, I may as well just go outside and look at the door myself!

If anyone has a simple dummies guide to linking envirophat & scrollphat, I’d be much appreciative.

Ta.

You have two quite frustratingly opposing problems:

  1. When you call clear it also resets the scroll position
  2. When you write out the temperature without calling clear you’re drawing over the top of the existing text- so the values that change will end up the garbled mess that you’re seeing.

Probably the easiest way to overcome this is to rely on the fact write_string returns the length, in pixels, of the string it has written, so you can:

  1. Write out the string, saving the length
  2. Show the string
  3. Scroll
  4. Clear the area you drew the string
  5. Sleep
  6. Goto 1

Helpfully there’s a clear_rect method you can use in lieu of clear to just empty a particular space in your buffer, versus resetting everything. Text is usually 7 pixels high, so:

text_width = scrollphathd.write_string(...)
# Show, scroll, sleep, etc
scrollphathd.clear_rect(0, 0, text_width, 7)

That makes sence, thank you.
Alas, python indents have never made sense, I’ve tabbed this line around, but always the same error:

File “H1.py”, line 25
_ text_width = scrollphathd.write_string(" Heading {} ".format(hding)_
_ ^_
IndentationError: unexpected indent

import time
import sys
from envirophat import light, weather, leds, motion
import scrollphathd


try:
    while True:


	lux = light.light()
	temperature = weather.temperature()
	hding = motion.heading()

#	print light.light()
#	print(" Temp is {:5.2f} C".format(temperature))
	
#	scrollphathd.clear()	
#        scrollphathd.write_string(" Temp {:5.2f} C".format(temperature)
        scrollphathd.write_string(" Heading {} ".format(hding),
#        scrollphathd.write_string(" Lux {} ".format(lux),
        brightness=0.1
        )

		text_width = scrollphathd.write_string(" Heading {} ".format(hding)
# Show, scroll, sleep, etc
	


#! Show the buffer
		scrollphathd.show()	
#! Scroll the buffer content
		scrollphathd.scroll()


# Sleep
        time.sleep(0.05)
 
 
		scrollphathd.clear_rect(0, 0, text_width, 7)        

except KeyboardInterrupt:
    pass

Alas, Python’s forced indentation-as-syntax definitely has its ups and its downs.

In this case it looks like you might be running into a problem with mixed tabs and spaces in your indents.

From the looks of it, you can fix this by taking the text_width, clear_rect show, and scroll lines, deleting their indentation and replacing it with 8 spaces.

No change from code shown above, other than the suggested 8 spaces on the four lines.
I do tend to use tab rather than spaces, if that makes any difference?

It now says

  File "H1.py", line 33
    scrollphathd.scroll()
               ^
SyntaxError: invalid syntax

How can:

    scrollphathd.scroll()
               ^
SyntaxError: invalid syntax

Be a syntax error, there’s nothing wrong with it whatsoever?!

Anyone?

import time
import sys
from envirophat import motion
import scrollphathd


try:
    while True:

        hding = motion.heading()

        scrollphathd.write_string(" Heading {} ".format(hding),
        brightness=0.1
        )

        text_width = scrollphathd.write_string(" Heading {} ".format(hding),


#! Show the buffer
        scrollphathd.show()	
#! Scroll the buffer content
        scrollphathd.scroll()

# Sleep
        time.sleep(0.05)
  
        scrollphathd.clear_rect(0, 0, text_width, 7)        

except KeyboardInterrupt:
    pass

It’s an error because:

scrollphathd.scroll(what?)

The Documentation for these commands is here:
http://docs.pimoroni.com/scrollphathd/

You have to pass an integer into the scroll() variable like:

scrollphathd.scroll(1)

Cheers,

Simon.

I’ve tried countless figures, positive and negative…
scrollphathd.scroll(n)
scrollphathd.scroll(n,n)
Always still syntax error.

Usually inexplicable syntax errors like this are triggered by problems on previous lines, and not the actual line in question. Look at your code as a whole, and not just a single line.

Syntax especially indents has to be one of the biggest head banging things about Python.
If you open your code in say idle, then do the check module, it might clue you into where things are going wrong.

If the code sample posted above is your actual code, I have a couple of suggestions:

scrollphathd.write_string(" Heading {} ".format(hding),
brightness=0.1
)

Probably split this out so the brightness setting is on a separate earlier line.
(It’s a personal preference, and not strictly needed.)

And,

text_width = scrollphathd.write_string(" Heading {} ".format(hding),

This will never work, because:

  • There is a comma at the end of the command.

  • There is an unclosed bracket.

As Phil stated earlier, the error probably doesn’t lie with the scrollphathd.scroll() but with the preceding lines.

I’ve not got an Enviro pHAT to debug this properly but I managed to get to the point where I was getting IO errors as there was no board communicating with the Pi.

Hope that helps,

Simon.

Thanks everyone for the help and ideas.
I’m a bit busy with real work this week, but really appreciate your advice, and will have a go just as soon as I can.

Thank you! :)

I’ve had a play with this over the Xmas break.
I got better results making use of Pimoroni’s own Santa countdown project.

import time
import sys
from envirophat import light, motion, weather
import scrollphathd
from scrollphathd.fonts import font5x7


str_len = 0
scroll_x = 0


while True:


    hding = motion.heading()
    temperature = weather.temperature()
    lux = light.light()
    rgb = str(light.rgb())[1:-1].replace(' ', '')
        
    scrollphathd.set_brightness(0.2)
    scrollphathd.clear()

str_len = scrollphathd.write_string("  Heading/%i      Temp/%.1fC       Lux/%i      " %(hding, temperature, lux), x=0, y=0, font=font5x7)


    scrollphathd.scroll_to(scroll_x, 0)
    scrollphathd.show()
    time.sleep(0.01)
    scroll_x += 1
    if scroll_x >= str_len:
        scroll_x = 0

What I’d now like to do, is have it scroll some more meaningful text messages.

    if lux <= 100 :

        print("Too dark!")
    else :
        print("Too bright!")   
       
    if hding <= 200 :
        print("Closed")
    else :
        print("Open")

Straightforward enough to print out on the console, but how do I get them on the Scrollphathd?

Thank you.

Replace the “print” with “scrollphathd.write_string” maybe?

I scroll text on my Sense Hat LED matrix.
day, date, time, temp, humidity, pressure etc.
All color coded based on conditions, Blue text for the temperature if the temperature is zero c or lower for example. I’ll post a bit of my code but I don’t think it will help you much.

t = sense.get_temperature()
    t = round(t)
          
    if t <= 0: 
        tc = [w, w, 255]  # Blue
    elif t > 0 and t < 13:
        tc = [255, 255, w]  # Yellow
    elif t >= 13 and t <= 25:
        tc = [w, 255, w]  # Green
    elif t > 25 and t < 30:
        tc = [255, o, w]   # Orange
    elif t >= 30:
        tc = [255, w, w]   # Red   

    msg = "and %sc" % (t)
    sense.show_message(msg, scroll_speed=s, text_colour=tc)

Progress!

This works…

    if lux <= 100 :

        str_len = scrollphathd.write_string(("Too dark!  "), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Too bright!  "), x=0, y=0, font=font5x7)   
       

But if I add a second sensor, the first is overwritten on the scrollphathd…


    if lux <= 100 :

        str_len = scrollphathd.write_string(("Too dark!  "), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Too bright!  "), x=0, y=0, font=font5x7)   
        
    
    scrollphathd.clear()   
      
    if hding <= 200 :
        str_len = scrollphathd.write_string(("Closed"), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Open"), x=0, y=0, font=font5x7)  

Thank you.

Does the first message only scroll the once and stop.
Or repeat endlessly until you do the clear?
You might have to take out the clear or add a time sleep command to add a delay between messages.