Python Phat HD Help!


#1

So I have written some code which I have pasted below. The code does almost everything I want it to do… but for some reason I cannot get the scroll function to work. The tweet displays on the phat (or rather the first 3 letters do) and that is about it. Text displays fine in console. Any help would be greatly appreciated.

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import scrollphathd
from scrollphathd.fonts import font5x7

scrollphathd.set_brightness(0.5)

#consumer key, consumer secret, access token, access secret.
ckey="“
csecret=”“
atoken=”“
asecret=”"

class listener(StreamListener):
#count=0
def on_data(self, data):

    #while (count<10):
        #count++;
        tweet=data.split(',"text":"')[1].split('","source')[0]
        scrollphathd.write_string(tweet)
        print(tweet+"\n")
        scrollphathd.show()
        scrollphathd.scroll()
        time.sleep(0.25)
        return(True)

def on_error(self, status):
    print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=["#pimoroni"])


#2

You were almost right in your while (count<10), you need to call scrollphathd.scroll() multiple times to scroll through the complete string. But you only need to write the string once.

The pattern you’re looking for would be something like:

text_length = scrollphathd.write_string(tweet)
while text_length  > 0:
    text_length -=1
    scrollphathd.scroll()
    scrollphathd.show()
    time.sleep(0.25)

When you call write_string it returns the length, in pixels, of the written string (this should actually be documented in the docs, but it isn’t. D’oh!).


#3

Thank you, that did indeed help… I now have the following:

class listener(StreamListener):
#count=0
def on_data(self, data):

    tweet=data.split(',"text":"')[1].split('","source')[0]
    text_length = scrollphathd.write_string(tweet)

    while text_length  > 0:
     text_length -=1
     scrollphathd.scroll()
     scrollphathd.show()
     time.sleep(0.05)
     print(tweet+"\n")

def on_error(self, status):
    print (status)

This does scroll the tweet but has added some interesting and new… features.

  1. The tweet seems to miss the first line of pixels on the far left of the display, so you get half and “a”.
  2. The tweet scrolls once and then pauses back at the beginning, have tried playing around with a return function but without any joy.
  3. As you might expect when a new tweet comes in it just displays on top of the old one.
  4. The print display in the terminal goes mad… repeating the tweet over and over again.

Essentially my original aim was to creating something that would scroll the last 3 tweets with a certain hashtag, each time a new tweet comes in the oldest tweet gets kicked out of the queue and the newest 3 carry on in sequence.

I have also tried playing around with .rss feeds to do this and had some success, but so far twitter is eluding me.

Many thanks for all your help :).


#4
  1. You probably want to show your unscrolled text briefly before you start scrolling it
  2. If you’re going to scroll through three tweets, then you would constantly flip through the ones in your list (see below)
  3. You’ll need to clear the display before writing the next tweet
  4. You can move your print() outside of the loop

Ideally you should be storing the Tweets you want to scroll in a Python list, and every time a new Tweet comes in, push it on the start of the list and drop the last item.

Here’s that idea explained by the insanity of a quick Python REPL session:

>>> tweets = []

>>> tweets.insert(0, "New tweet!")
>>> tweets.insert(0, "A newer tweet!")

>>> tweets
['A newer tweet!', 'New tweet!']

>>> tweets.insert(0, "The newest tweet!!!")

>>> tweets
['The newest tweet!!!', 'A newer tweet!', 'New tweet!']

>>> tweets.insert(0, "Uh oh! We only want three!")

>>> tweets
['Uh oh! We only want three!', 'The newest tweet!!!', 'A newer tweet!', 'New tweet!']

>>> tweets[:3]
['Uh oh! We only want three!', 'The newest tweet!!!', 'A newer tweet!']

>>> tweets = tweets[:3]
>>> tweets
['Uh oh! We only want three!', 'The newest tweet!!!', 'A newer tweet!']

#5

Thanks again for your support :).