LED - Make TetrisClock

Do you know TetrisClock?
TetrisClock is a WiFi clock made of falling tetris blocks. Runs on an ESP32 with an RGB LED Matrix.

img
<TetrisClock on the RGB LED Matrix by Brian Lough>


I'm a big fan of RGB LED matrix and I wrote many posts about RGB LED Matrix in my blog.
I like to display the TetrisClock screen using OpenCV on the Raspberry Pi to the RGB LED Matrix.
However, the TetrisClock shown in the figure above works on the Arduino family of ESP32 MCUs. I also posted an article implementing TetrisClock on ESP32 at https://iot-for-maker.blogspot.com/2020/04/led-9-rgb-led-matrix-drive-with-esp-32.html. But I wanted to implement this beautiful clock in Raspberry Pi, so I googled hard, but couldn't find any good examples. Eventually, I decided to analyze the code written in C language and implement it in Python and OpenCV.

In my blog, which mainly introduces OpenCV, I introduced 2 articles to implement Tetris characters.
  1. Creating TetrisClock using OpenCV #1
  2. Creating TetrisClock using OpenCV #2

See the blog above for programming to implement Tetris clocks in Python and OpenCV.

<Text Tetris in Python, OpenCV>


Tetris Clock on the RGB LED Matrix using Raspberry Pi

You need the following prior knowledge.

I optimized the code used in the post above to create a tetris_led.py file.
If you import and use this file that implements TetrisChar and TetrisString classes, you can use it very simply.

import argparse
import cv2
import numpy as np
from PIL import Image
from PIL import ImageDraw
from rgbmatrix import RGBMatrix, RGBMatrixOptions
import time
import tetris_led as tetris


def getrevision():
    # Extract board revision from cpuinfo file
    myrevision = "0000"
    try:
        f = open('/proc/cpuinfo','r')
        for line in f:
            if line[0:8]=='Revision':
                length=len(line)
                myrevision = line[11:length-1]
        f.close()
    except:
        myrevision = "0000"

    version = None
    print('Raspberry Pi ReVersion:%s'%(myrevision))

    if myrevision == 'a03111' or myrevision == 'b03112' or myrevision == 'c03111':
        version = 4
    elif myrevision == 'a02082' or myrevision == 'a22082 ' or myrevision == 'a020d3 ':
        version = 3
    elif myrevision == 'a01041' or myrevision == 'a21041  ' or myrevision == 'a22042 ':
        version = 2
    elif myrevision == '900092' or myrevision == '900093  ' or myrevision == '9000C1 ':
        version = 0

    return myrevision, version

parser = argparse.ArgumentParser(description="RGB LED matrix Example")
parser.add_argument("--horizontal", type=int, default = 1, help="horizontal count")
parser.add_argument("--vertical", type=int, default = 1, help="vertical count")
args = parser.parse_args()

revisionm, version = getrevision()
# Configuration for the matrix
options = RGBMatrixOptions()
options.cols = 64
options.rows = 64
options.chain_length = args.horizontal * args.vertical
options.parallel = 1
options.brightness = 80

if(version == 4):
    options.gpio_slowdown =  4
elif(version == 3):
    options.gpio_slowdown =  1.0
else:    
    options.gpio_slowdown =  1

options.show_refresh_rate = 1
options.hardware_mapping = 'regular'  # If you have an Adafruit HAT: 'adafruit-hat'
options.pwm_dither_bits = 0

matrix = RGBMatrix(options = options)
double_buffer = matrix.CreateFrameCanvas()

canvas_w = args.horizontal * options.cols
canvas_h = args.vertical * options.rows
print('Canvas size W[%d] H[%d]'%(canvas_w, canvas_h))
tetris.make_canvas(canvas_h, canvas_w , 0)

tetris.set_scale(1)
tetris_str = tetris.TetrisString(1, tetris.CHAR_HEIGHT * 2, "TETRIS")
tetris_str.animate(matrix, double_buffer)

tetris.set_scale(1)
tetris_str = tetris.TetrisString(1, tetris.CHAR_HEIGHT * 3, "CLOCK")
tetris_str.animate(matrix, double_buffer)


now = time.strftime('%H %M', time.localtime(time.time()))

tetris.set_scale(2)
tetris.set_bottom_shift(0)
tetris_str2 = tetris.TetrisString(1, 0,  now)
tetris_str2.animate(matrix, double_buffer)



time.sleep(10)
<tetris_clock.py>


If you run the code, you can see this LED.

python3 tetris_clock.py


<Tetris Clock on the 64X64 RGB LED matrix>



Wrapping Up

As you can see from the above code, Tetris characters can be easily implemented using only 10 lines of code.
Raspberry Pi has more programming flexibility and less memory burden than ESP32 MCUs. Add your ideas and you'll get a nicer Tetris text.

You can download the source code at https://github.com/raspberry-pi-maker/IoT


댓글

이 블로그의 인기 게시물

LED-12. Displaying HDMI Contents

LED - 5. Raspberry Pi 4 + DietPi Buster + Electrodragon HAT - Part 1

LED-11. Make a very big size 384 X 512 RGB Matrix #6(final)