Skip to content

Instantly share code, notes, and snippets.

@chubbyemu
Last active October 11, 2023 15:31
Show Gist options
  • Save chubbyemu/4ca0c68878c6d978d067da4a36bcc71d to your computer and use it in GitHub Desktop.
Save chubbyemu/4ca0c68878c6d978d067da4a36bcc71d to your computer and use it in GitHub Desktop.
# THIS SCRIPT USES THE LIBRARY AT:
# https://github.com/hzeller/rpi-rgb-led-matrix
# BE SURE TO CLONE IT AND READ THE README, as highlighted in the video :)
import os, time, threading, random
import feedparser
from PIL import Image, ImageFont, ImageDraw
from random import shuffle
BITLY_ACCESS_TOKEN="BITLY_ACCESS_TOKEN"
items=[]
displayItems=[]
feeds=[
#enter all news feeds you want here
"http://www.fda.gov/AboutFDA/ContactFDA/StayInformed/RSSFeeds/PressReleases/rss.xml",
"http://www.fiercepharma.com/feed",
"http://www.fiercebiotech.com/feed",
]
def colorRed():
return (255, 0, 0)
def colorGreen():
return (0, 255, 0)
def colorBlue():
return (0, 0, 255)
def colorRandom():
return (random.randint(0,255), random.randint(0,255), random.randint(0,255))
def populateItems():
#first clear out everything
del items[:]
del displayItems[:]
#delete all the image files
os.system("find . -name \*.ppm -delete")
for url in feeds:
feed=feedparser.parse(url)
posts=feed["items"]
for post in posts:
items.append(post)
shuffle(items)
def createLinks():
try:
populateItems()
for idx, item in enumerate(items):
writeImage(unicode(item["title"]), idx)
except ValueError:
print("Bummer :( I couldn't make you 'dem links :(")
finally:
print("\nWill get more news next half hour!\n\n")
def writeImage(url, count):
bitIndex=0
link, headLine="", url[:]
def randCol(index = -1):
if index % 3 == 0:
return colorRed()
elif index % 3 == 1:
return colorGreen()
elif index % 3 == 2:
return colorBlue()
else:
return colorRandom()
text = ((headLine, randCol(count)), (link, colorRandom()))
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 16)
all_text = ""
for text_color_pair in text:
t = text_color_pair[0]
all_text = all_text + t
width, ignore = font.getsize(all_text)
im = Image.new("RGB", (width + 30, 16), "black")
draw = ImageDraw.Draw(im)
x = 0;
for text_color_pair in text:
t = text_color_pair[0]
c = text_color_pair[1]
draw.text((x, 0), t, c, font=font)
x = x + font.getsize(t)[0]
filename=str(count)+".ppm"
displayItems.append(filename)
im.save(filename)
def run():
print("News Fetched at {}\n".format(time.ctime()))
createLinks()
threading.Timer(len(items) * 60, run).start()
showOnLEDDisplay()
def showOnLEDDisplay():
for disp in displayItems[:60]:
os.system("sudo ./led-matrix -r 16 -c 3 -t 60 -m 25 -D 1 "+disp)
if __name__ == '__main__':
run()
@Jani73
Copy link

Jani73 commented Dec 28, 2020

I tested again with my old files, where this original rssparse.py works. It looks like on line -r 16 -c 3 -t 60 -m 25 -D 1 -t 60 means time in seconds and after that feed changes. These parameters works with some year 2018 rpi-rgb-led-matrix-library.

On demo, there’s some modifications at the end, where time related code is removed. Those might be the missing link, but I don’t understand anything about it.

On demo there are on lines 1177-1178 these signal(SIGTERM, InterruptHandler); signal(SIGINT, InterruptHandler);. If I read right, those are codes for waiting CTRL+C? Could it be possible to add there some timer that interrupts program after 60 seconds and do what CTRL+C now does > changes the feed? Maybe that’s a workaround, but would be nice to get it working with newest library.

@sleep-sort
Copy link

I bet that would do it, let me dig around and see if I can get something working @Jani73 - my python skills are horrible though I do have to warn

@jax200
Copy link

jax200 commented Jan 1, 2021

@Jani73 "...I tested again with my old files, where this original rssparse.py works..." I originally tried this, but it didn't work for me, which I assumed was because the led-matrix command was missing?

os.system("sudo ./led-matrix -r 16 -c 3 -t 60 -m 25 -D 1 "+disp)

Hence moved on to the demo command.

os.system("sudo ./demo -D 1 --led-cols=96 --led-rows=16 --led-brightness=75 --led-chain=3"+disp)

Two days ago I was getting parsed rss feeds nicely showing (but still with the Ctrl-C issue). Then... things went downhill. I started getting error messages regarding import feedparser. I re-built from scratch but now

pi@raspberrypi:~/rpi-rgb-led-matrix/examples-api-use $ python  ./rssparse.py
Traceback (most recent call last):
  File "./rssparse.py", line 7, in <module>
    import feedparser
  File "/usr/local/lib/python2.7/dist-packages/feedparser/__init__.py", line 31, in <module>
    from .api import parse
  File "/usr/local/lib/python2.7/dist-packages/feedparser/api.py", line 56, in <module>
    from . import http
  File "/usr/local/lib/python2.7/dist-packages/feedparser/http.py", line 156
    new_pieces[1] = f'{url_pieces.hostname}:{url_pieces.port}'

I have now even hired a python tutor to help, but he is of the opinion to rebuild some of the components entirely. So stay tuned.

@jax200
Copy link

jax200 commented Jan 1, 2021

btw, I'm wondering if this conversation would be better on the discourse.group area than using chubbyemu's space? Might get other input as well. I posted two weeks ago there but no responses.

https://rpi-rgb-led-matrix.discourse.group

@mcwTexas
Copy link

Is anyone else having issues with quality of the scroll? I am getting readable display but it looks a little blurry no matter what font I've tried. And I've tried about 5. I've also tried changing the font size to 10 as suggested earlier in the thread. I'm not sure if there is some new setting to help with it or if it's due to the matrix library install choice of "convenient" vs. quality already coming back to haunt me. Thanks!

@Jani73
Copy link

Jani73 commented Jan 19, 2021

@mcwTexas, have you checked the readme?

The link in jax200’s message doesn’t work, but here is a straight link.

https://rpi-rgb-led-matrix.discourse.group/t/rssparse-py-needs-some-help/40/22

@mcwTexas
Copy link

@Jani73, thank you sooooo much for providing that link as it did answer my questions and solve my problems!!!!!! Very appreciative!!

@mcwTexas
Copy link

mcwTexas commented Jan 26, 2021

Hi @Jani73 You were so kind before in helping me, I thought I'd try my luck again. I've noticed a new problem and tried to troubleshoot for the past 2 days. I'm a newby so not much I know to do. The newsfeed matrix scroll looks good for x period of time, and then, for no reason I can tell, the data becomes garbled. It looks like multiple threads are trying to be written and display at the same time on the matrix. It happens at various points -sometimes a couple hours after I start the scroll. I can stop the program and restart it, and all works good again - for a while and then it happens again. Watching the code run in the bash window, everything appears to run normal. Here are the settings I'm using on the os.system line. Any thoughts?

os.system("sudo ./rpi-rgb-led-matrix/examples-api-use/scrolling-text-example -f ./rpi-rgb-led-matrix/fonts/7x13.bdf -y5 -s7 -l1 -C "+ randomColor +" -B 0,0,0 --led-cols=64 --led-rows=32 --led-chain=1 --led-slowdown-gpio=4 " +title)

After running the program all night and seeing the display issue again, I checked the task manager. It showed tons of Sh and scrolling-matrix-example processes. I'm now assuming each time through the loop it starts a new process then never kills the process before starting another. While almost all of the processed were using o CPU, a 2 or 3 of the instances were utilizing CPU. So going out on a limb here, is there a command to kill the process/instance at the end of each loop? I feel like that would solve my issue.

@Jani73
Copy link

Jani73 commented Jan 26, 2021

Hi @mcwTexas. I don’t know much about these things. Now I put it running and my task manager doesn’t show increasing number of scrolling-text-example processes. Only one Sh too.
Maybe asking from others, which knows about coding.

My line is
os.system(sudo ./scrolling-text-example -f ../fonts/9x15.bdf -y0 -s9 -l2 -C ”+ randomColor +” —led-cols=32 —led-rows=16 —led-brightness=75 —led-chain=3 ”+title)

feedparser.pyc is also on examples-api-use-folder as is rssparse.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment