How to create Vector robot eye animation with raspberry pi and SPI display?

Viewed 29

I have a raspberry pi 4 (4GB) and a 2 inch Waveshare SPI LCD display. I’m trying to create animated eye expression just like the Vector Robot, but I have no clue on how to code the fluid animation (with acceptable frame rate). Can anyone give me some pointers on this?

Current attempt: I tried Waveshare’s python library with Python PIL, but the frame rate is extremely low… Any help will be appreciated.

Here is my existing testing code, testmotion() basically moves a rectangle to the right 50 times within a for loop. But it took almost 7 seconds to run....

import os
import sys
import time
import logging
import spidev as SPI
from lib import LCD_2inch
from PIL import Image, ImageDraw, ImageFont

class CoreDisplay:
    # Raspberry pi pin configuration:
    RST = 27
    DC = 25
    BL = 18
    bus = 0
    device = 0

    def __init__(self):
        # Display with hardware SPI
        # DON'T CRERATE MULTIPLE DISPLAYER OBJECTS!!!
        self.disp = LCD_2inch.LCD_2inch()
        # initialize library
        self.disp.Init()
        # Clear display
        self.disp.clear() 

    def testmotion(self):
        # test to move a rectangle on screen
        x, y = 20, 20
        old_shape = [(x, y), (x + 40, y + 40)]
        for i in range(50): 
            # Cover the old shape in black
            image = Image.new("RGB", (self.disp.height, self.disp.width ), "BLACK")
            draw = ImageDraw.Draw(image)
            draw.rectangle(old_shape, fill ="Black")

            image = image.rotate(180)
            self.disp.ShowImage(image)

            # draw new shape
            shape = [(x, y), (x + 40, y + 40)]
            image = Image.new("RGB", (self.disp.height, self.disp.width ), "BLACK")
            draw = ImageDraw.Draw(image)
            draw.rectangle(shape, fill ="Cyan")

            image = image.rotate(180)
            self.disp.ShowImage(image)
            x += 1
            old_shape = shape

popDisplay = CoreDisplay()
popDisplay.testmotion()
time.sleep(5)
0 Answers
Related