Improved SenseHat headlines ticker

Last time I introduced my SenseHat RSS feed display code. Today I have made some improvements to the script.

The Sense HAT provides an 8x8 LED maxtrix display, accelerometer, gyroscope, magnetometer, air pressure sensor, temperature sensor and air pressure sensor, as well as a small joystick.  Basically a bundle of sensors that plug in directly to the GPIO pins on your Raspberry Pi. They are well worth purchasing should you wish to upgrade your Pi.



First off, the list 'feedlink' can be populated with as many RSS feeds as you like.  Here I have three BBC feeds, but they could be substituted for any feed you like.  Currently the ticker loops through all of the articles in each feed. You could change it so that each feed is chained to the end of the previous one.  With this code you can switch the the start of the next feed by shaking the Raspberry Pi.  The shake is detected by a change in the 'pitch' of the SenseHat.  You can change the sensitivity of the shake with the THRESHOLD variable.  The new feed will be displayed after the previous article has finished.
The RSS feed ticker scrolling over the SenseHat, however my camera frame rate can't keep up.

I have added some exception handling to the showFeed routine to handle an index out of bounds error. I think this could occur with the previous code.



#Sense Hat RSS reader
#version 2
#For Python 2
from sense_hat import SenseHat
import feedparser
import time


def showFeed(d, n):
    """Shows feed (d) article (n)"""
    try:
        sense.show_message(d.entries[n].description,
                           back_colour=[255,0,0],
                           text_colour=[255,255,255],
                           scroll_speed=0.07)
    except IndexError as e:
        sense.show_message("ERROR")



        
sense = SenseHat()
sense.set_rotation(270)
sense.low_light = True

feed = []
feedlink = ['http://feeds.bbci.co.uk/news/rss.xml?edition=uk',
            'http://feeds.bbci.co.uk/news/technology/rss.xml',
            'http://feeds.bbci.co.uk/news/uk/rss.xml']

ARTICLE_LIMIT = 20
THRESHOLD = 15 # threshold for tilt (changes feed)

print "Running on SenseHat:"
while True:
    #read the feeds in
    for thisFeed in range(len(feedlink)):
        feed.append(feedparser.parse(feedlink[thisFeed]))

    i = 0 #article pointer
    f = 0 #feed pointer
    
    while i < ARTICLE_LIMIT:
        orientation1 = sense.get_orientation_degrees()
        time.sleep(0.5)
        print 'feed ',f,'article',i
        showFeed(feed[f],i)
        orientation2 = sense.get_orientation_degrees()
        #check for shake
        print 'shake detected ',abs(orientation2['pitch'] - orientation1['pitch'])
        if (abs(orientation2['pitch'] - orientation1['pitch'])> THRESHOLD):
            f += 1 #change feed
            i = 0 # return to start of feed
            if (f == len(feed)):
                f = 0
        else:
            i += 1
    time.sleep(2.5)