Book Image

Raspberry Pi LED Blueprints

By : Agus Kurniawan
Book Image

Raspberry Pi LED Blueprints

By: Agus Kurniawan

Overview of this book

Table of Contents (14 chapters)
Raspberry Pi LED Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Displaying a random character on the LED dot matrix display


Using the scenario from the previous section, we can build a simple program to display a random character on an 8 x 8 LED dot matrix module. We can use random.choice(string.ascii_letters) to retrieve a random character in Python. After this, the random value is passed to the MAX7219 library.

Let's start. Create a file named ch04_03.py. The following is completed code for our scenario:

# ch04_03.py file

import max7219.led as led
import time
import random
import string

device = led.matrix(cascaded=1)

print("Running...")
print("Press CTRL+C to exit ")
try:
    while 1:
        character = random.choice(string.ascii_letters)
        print "display ", character
        device.letter(0, ord(character))
        time.sleep(1)

except KeyboardInterrupt:
    device.command(led.constants.MAX7219_REG_SHUTDOWN,0x00)
    time.sleep(0.01)

print("done")

Save this code. Execute this program by typing the following command:

sudo python ch04_03.py...