Raspberry PI max7219 8×8 LED Matrix – scrolling digital clock

Based on these fine instructions by raspi.tv I wrote some tiny python code to display a scrolling digit clock on an 8 by 8 LED matrix using the Raspberry PI.

The datatype of font5x3 = {“…” : …} is a so-called dictionary and works intuitively. The binary notation such as 0b01110 makes it easy to conceive and edit the pixel based graphics of the font.

My functions creatematrix() and drawmatrix() can also be used for different purpose such as scrolling a text. They can be nested: drawmatrix(creatematrix(“Hello World”), 0, i)
You can easily extend the font5x3 with additional characters. different size such as 8×8 will work seamlessly.

#!/usr/bin/env python

import max7219.led as led
import max7219.canvas as canvas
import time

font5x3 = { # python data type dictionary for the pixelfont
    "0" : [0b01110, 0b10001, 0b01110], # "0"
    "1" : [0b10010, 0b11111, 0b10000], # "1"
    "2" : [0b11001, 0b10101, 0b10010], # "2"
    "3" : [0b10001, 0b10101, 0b01110], # "3"
    "4" : [0b01110, 0b01000, 0b11111], # "4"
    "5" : [0b10111, 0b10101, 0b01001], # "5"
    "6" : [0b01110, 0b10101, 0b01000], # "6"
    "7" : [0b10001, 0b01001, 0b00111], # "7"
    "8" : [0b01010, 0b10101, 0b01010], # "8"
    "9" : [0b00010, 0b10101, 0b01110], # "9"
    ":" : [0b01010], # ":"
    "-" : [0b00100, 0b00100, 0b00100], # "-"
    "|" : [      0], # one blank line
    " " : [      0,       0,       0] # space
    } # create additional letters

def creatematrix(text):
    text = str(text)
    matrix = []
    for i in range(len(text)): # write complete pixelmatrix in a buffer
        if text[i].upper() in font5x3: # check if dictionary entry exists
            matrix = matrix + font5x3[text[i].upper()] # add letter; upper()
            matrix = matrix + [0] # add separator/space
    return matrix

def drawmatrix(matrix, up=0, left=0):
    for i in range(8): # fill the 8x8 matrix buffer canvas.gfxbuf
        if up < 0:
            canvas.gfxbuf[i] = matrix[(i + left) % len(matrix)] << abs(up) # move down
        else:
    canvas.gfxbuf[i] = matrix[(i + left) % len(matrix)] >> abs(up)

led.init()
horz = 0
vert = -2
speed = 15
led.brightness(0) # 0, 3, 7, 15 seems to work

while True:
    m = creatematrix(time.strftime("%k:%M ")) # write temporary matrix m
    horz = (horz + 1) % len(m) # scroll left
    drawmatrix(m, vert, horz) # draw 8x8 frame into gfxbuf 
    canvas.render() # draw gfxbuf into matrix
    time.sleep( 1.0 / speed ) # pause

If you like to include my small 5×3 pixel alphabeth, you can use the following font5x3 definition:

font5x3 = {
    "0" : [0b11111, 0b10001, 0b11111], # "0"
    "1" : [0b10010, 0b11111, 0b10000], # "1"
    "2" : [0b11001, 0b10101, 0b10111], # "2"
    "3" : [0b10101, 0b10101, 0b01110], # "3"
    "4" : [0b01110, 0b01000, 0b11111], # "4"
    "5" : [0b10111, 0b10101, 0b11001], # "5"
    "6" : [0b01110, 0b10101, 0b01000], # "6"
    "7" : [0b10001, 0b01001, 0b00111], # "7"
    "8" : [0b01010, 0b10101, 0b01010], # "8"
    "9" : [0b00010, 0b10101, 0b01110], # "9"

    "A" : [0b11111, 0b01001, 0b11110], # "A"
    "B" : [0b11111, 0b10101, 0b01010], # "B"
    "C" : [0b01110, 0b10001, 0b10001], # "C"
    "D" : [0b11111, 0b10001, 0b01110], # "D"
    "E" : [0b01110, 0b10101, 0b10101], # "E"
    "F" : [0b11110, 0b00101, 0b00101], # "F"
    "G" : [0b01110, 0b10001, 0b11101], # "G"
    "H" : [0b11111, 0b00100, 0b11111], # "H"
    "I" : [0b10001, 0b11111, 0b10001], # "I"
    "J" : [0b10000, 0b10000, 0b01111], # "J"
    "K" : [0b11111, 0b00100, 0b11011], # "K"
    "L" : [0b01111, 0b10000, 0b10000], # "L"
    "M" : [0b11111, 0b00010, 0b11111], # "M"
    "N" : [0b11111, 0b00001, 0b11110], # "N"
    "O" : [0b01110, 0b10001, 0b01110], # "O"
    "P" : [0b11111, 0b01001, 0b00110], # "P"
    "Q" : [0b01110, 0b10001, 0b01111], # "Q"
    "R" : [0b11111, 0b01001, 0b10110], # "R"
    "S" : [0b10010, 0b10101, 0b01001], # "S"
    "T" : [0b00001, 0b11111, 0b00001], # "T"
    "U" : [0b01111, 0b10000, 0b01111], # "U"
    "V" : [0b00111, 0b11000, 0b00111], # "V"
    "W" : [0b11111, 0b01000, 0b11111], # "W"
    "X" : [0b11011, 0b00100, 0b11011], # "X"
    "Y" : [0b00011, 0b11100, 0b00011], # "Y"
    "Z" : [0b11001, 0b10101, 0b10011], # "Z"

    "." : [0b10000], # "."
    "?" : [0b10101, 0b00101, 0b00010], # "?"
    "!" : [0b10111], # "!"
    ";" : [0b11010], # ";"
    "," : [0b11000], # ","
    ":" : [0b01010], # ":"
    "-" : [0b00100, 0b00100, 0b00100], # "-"
    "|" : [ 0],
    " " : [ 0, 0, 0]
    }

The code is not yet ready for multiple 8×8 LED matrices. You can try to work out the following instructions on multiple LED matrices: http://www.tutorials-raspberrypi.de/allgemein/bibliothek-fuer-mehrzeilige-m-x-n-max7219-led-matrizen/

Advertisements