The following example takes the display examples a bit further, allowing you to display scrolling text on them.
Open an editor window using:
nano scrolling_clock.py
import time from datetime import datetime from PIL import ImageFont from PIL import Image from PIL import ImageDraw from Adafruit_LED_Backpack import Matrix8x8 display = Matrix8x8.Matrix8x8() display.begin() font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 9) im = Image.new("1", (8, 8), "black") draw = ImageDraw.Draw(im) width, ignore = font.getsize("88 : 88 : 88") def format_time(): d = datetime.now() return "{:%H : %M : %S}".format(d) message = format_time() x = 8 while True: x = x - 1 if x < -(width + 20): x = 8 message = format_time() draw.rectangle((0, 0, 7, 7), outline=0, fill=0) draw.text((x, -1), message, 1, font=font) display.set_image(im) display.write_display() time.sleep(0.1)
sudo python scrolling_clock.py
After initializing the display, the Truetype font FreeSansBold size 9 point is loaded. This seemed to work the best from the pre-installed fonts. This can be changed to one of the other fonts in the directory /usr/share/fonts/truetype/freefont/ if you want to experiment. The fonts are really not intended to work in 8x8 pixels, so the results are legible but not great.
An 8x8 image is created with a bit depth of 1. We are just going to write text in green for now. The following command works out how many pixels wide the text for the time is going to be:
width, ignore = font.getsize("88 : 88 : 88")
The main loop of the program produces the scrolling text effect. It does this by writing the text of the time onto the display with a decreasing x offset, so that the message appears to move. Most of the time, the value of x will be negative (off the left of the display), but the draw.text function automatically crops to the visible part of the display.