ScrollPhat HD query


#1

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

#2

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.


#3

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.


#4

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'

Envirophat light level on Scrollphat HD
#5

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.


#6

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)

#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


#8

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.


#9

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?!


#10

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

#11

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.


#12

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


#13

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.


#14

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.


#15

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.


#16

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! :)