Hello there,
I am trying to create a simple dice roller for my Scroll pHAT HD. I have written the following (below) in Python, it works in terminal with no problems… but when I try to display it on my Scroll pHAT HD it will not scroll the text for some reason. It just shows the first part of the “You Rolled A…” string. Furthermore all subsequent sentences just appear on top of the old one… I imagine this is something to do with the clear function.
If anyone can point me in the right direction I would be very grateful.
import random
import signal
import time
import scrollphathd as sphd
def roll(sides=6):
num_rolled = random.randint(1,sides)
return num_rolled
def main():
sides = 6
rolling = True
while rolling:
roll_again = raw_input("Ready to roll? ENTER=Roll.Q=Quit. ")
if roll_again.lower() !=“q”:
num_rolled = roll(sides)
sphd.write_string(“You rolled a”, num_rolled)
sphd.show()
sphd.scroll(1)
time.sleep(0.05)
else:
rolling = False
print ("You We're On A Roll!")
main()
You should use three backticks to format your code for readability, like so:
```
# Code goes here
```
The scroll()
command only moves the buffer as far as you tell it. It needs to be called continuously to keep scrolling.
A continuous blocking* scrolling function might look like this:
import scrollphathd
def scroll_message(message):
scrollphathd.clear() # Clear the display and reset scrolling to (0, 0)
length = scrollphathd.write_string(message) # Write out your message
scrollphathd.show() # Show the result
time.sleep(0.5) # Initial delay before scrolling
# Now for the scrolling loop...
while length > 0:
scrollphathd.scroll(1) # Scroll the buffer one place to the left
scrollphathd.show() # Show the result
length -= 1
time.sleep(0.02) # Delay for each scrolling step
scroll_message("Hello World! How are you today?")
*blocking means your code wont be able to do anything else until the message has finished scrolling
Okay. My first try had a small bug (forgetting import time
notwithstanding) so I’ve fixed up this code and added it as an example: https://github.com/pimoroni/scroll-phat-hd/blob/master/examples/simple-scrolling.py
#!/usr/bin/env python
import time
import scrollphathd
print("""
Scroll pHAT HD: Simple Scrolling
A simple example showing a basic scrolling loop for scrolling
single messages across the display.
Press Ctrl+C to exit.
""")
def scroll_message(message):
scrollphathd.clear() # Clear the display and reset scrolling to (0, 0)
length = scrollphathd.write_string(message) # Write out your message
scrollphathd.show() # Show the result
time.sleep(0.5) # Initial delay before scrolling
length -= scrollphathd.width
# Now for the scrolling loop...
while length > 0:
scrollphathd.scroll(1) # Scroll the buffer one place to the left
scrollphathd.show() # Show the result
length -= 1
time.sleep(0.02) # Delay for each scrolling step
time.sleep(0.5) # Delay at the end of scrolling
scroll_message("Hello World! How are you today?")
Many thanks Phil, I have tried to integrate your suggestions but still facing similar (if different) troubles. Now Python is upset that “length” is “not defined”… which I feel like it is on [Line 12].
import random
import signal
import time
import scrollphathd
def roll(sides=6):
num_rolled = random.randint(1,sides)
return num_rolled
def scroll_message(message, num_rolled):
scrollphathd.clear() # Clear the display and reset scrolling to (0, 0)
length = scrollphathd.write_string() # Write out your message
scrollphathd.show() # Show the result
time.sleep(0.5) # Initial delay before scrolling
length -= scrollphathd.width
def main():
sides = 6
rolling = True
while rolling:
roll_again = raw_input("Ready to roll? ENTER=Roll.Q=Quit. ")
if length > 0:
scrollphathd.scroll(1) # Scroll the buffer one place to the left
scrollphathd.show() # Show the result
length -= 1
time.sleep(0.02) # Delay for each scrolling step
time.sleep(0.5) # Delay at the end of scrolling
if roll_again.lower() !="q":
num_rolled = roll(sides)
scroll_message("You rolled a", num_rolled)
else:
rolling = False
print ("You We're On A Roll!")
main()