3

I have a little issue with my python script.

Actually, I'm working on real led matrix 8x8 and I want to draw a text with animation (right to left, left to right, up to down, down to up).

For example, I want to write the E letter. So I created the E matrix:

matrix =[
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1"]

Like this

But now, imagine I want to move my E to make animation. If I move my E to the left, the eighth column will be empty, and after 1s, the seventh too etc... And when my matrix will be completely empty, my E letter will start in right position again...

Do you have an idea how can I do that with array, matrix or whatever?

Thank you

EDIT, code test with https://www.jdoodle.com/python3-programming-online:

matrix = [  [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0],
            [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0], 
            [0, 0, 0, 1, 0]]

print(matrix)

def shift(my2dArray):
    for _ in range(5): # shift to the left 8 times - brute force
        for row in my2dArray:
            row.pop(0) # removes first element of each row
            row.append("0") # add "0" to the end of the row
        # code to update display here and sleep for a bit

shift(matrix)

print(matrix)
Naografix
  • 847
  • 2
  • 15
  • 35
  • Better to use is maybe "two-dimensional array" for Matrix, like as here: https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python – s3n0 May 23 '18 at 22:18

2 Answers2

1

(As s3n0 suggested) First, you want to have a 2D array declared

 my2dArray = [["1","1","1","1","1","1","1","1"],
    ["1","1","1","1","1","1","1","1"],
    ["1","1","0","0","0","0","0","0"],
    ["1","1","1","1","1","0","0","0"],
    ["1","1","1","1","1","0","0","0"],
    ["1","1","0","0","0","0","0","0"],
    ["1","1","1","1","1","1","1","1"],
    ["1","1","1","1","1","1","1","1"]]

Then, for the sake of a simple algorithm, you can do the following... (assuming an 8x8 grid):

def shift_left(my2dArray):
    for _ in range(8): # shift to the left 8 times - brute force
        for row in my2dArray:
            row.pop(0) # removes first element of each row
            row.append("0") # add "0" to the end of the row
        # code to update display here and sleep for a bit

This is an example of shifting your display to the left, I'm going to leave the rest (other directions) to you to figure out, as they are very similar to this algorithm.

For shifting up and down, simply call the list functions one layer up in the algorithm:

def shift_up(my2dArray):
    for _ in range(8): # shift to the left 8 times - brute force
        my2dArray.pop(0) # removes first element of each column
        my2dArray.append(["0","0","0","0","0","0","0","0"]) # add "0" to the end of the columns
        # code to update display here and sleep for a bit
Austin A
  • 566
  • 2
  • 15
  • Thanks for your answer, but it didn't working for me actually. I try your code with https://www.jdoodle.com/python3-programming-online Check my original post to copy past my code – Naografix May 23 '18 at 22:34
  • I think your "for _ in range()" is too much to move list to the left :)! – Naografix May 23 '18 at 22:37
  • 1
    after each iteration of the `for _ in range(8)`, the matrix will be shifted one to the left. That is why I left the comment `# code to update display here and sleep for a bit` – Austin A May 23 '18 at 22:38
  • Really cool! Thank you for your job! I'm going to try this with my RPI! My other problem will be I want to make the text appear gradually. For the moment our letter is displayed in full, it is necessary that I make it appear progressively! I will use your shifter – Naografix May 23 '18 at 22:52
0

Another way is with numpy. Which is also scalable. No matter the dimension of your array. You just change the shapes. The code i run is below and works fine. Just replace number 8 in the for loop with the same as the shape of the array.

import numpy as np
import time
array = np.random.randint(2,size=(8,8))
print(array)
originalarray = array.copy()
print("------------------------------")
for i in range(8):
 endposition = 8-i-1; # the 8th first iter, the 7th in second iter etc               
for j in range(endposition):
    column = array[:,j+1]
    array[:,j] = column
array[:,endposition:] = np.zeros((8,1)) # all the columns to the right are zeros
time.sleep(1)   # 1 second delay
print(array)
print("----------------------------------")
time.sleep(1)   # 1 second delay
print(originalarray)
A_kat
  • 1,459
  • 10
  • 17