Ну, я пытаюсь динамически перерисовать матрицу светодиодных точек 16x16, совместимую с Arduino, через python через pyfirmata. Я не могу найти способ пойти об этом ...
То, что я хочу знать, как это сделать, это преобразовать матрицу 16x16 в дисплей светодиодной матрицы с помощью Python.
- Вот пример часов с прокруткой, которые я сделал на python, используя pyfirmata. (этот пример работает с матрицей 8x8 со светодиодной точкой, а не с 16x16, что мне и нужно ...
"""led_matrix.py"""
from time import sleep
from pyfirmata import Arduino
HIGH = 1
LOW = 0
dataIn = 2
load = 3
clock = 4
maxInUse = 1
Intensity = 3
max7219_reg_noop = 0x00
max7219_reg_digit0 = 0x01
max7219_reg_digit1 = 0x02
max7219_reg_digit2 = 0x03
max7219_reg_digit3 = 0x04
max7219_reg_digit4 = 0x05
max7219_reg_digit5 = 0x06
max7219_reg_digit6 = 0x07
max7219_reg_digit7 = 0x08
max7219_reg_decodeMode = 0x09
max7219_reg_intensity = 0x0a
max7219_reg_scanLimit = 0x0b
max7219_reg_shutdown = 0x0c
max7219_reg_displayTest = 0x0f
intensity = 0x01
class LedMatrix:
def __init__(self, board, dataIn, load, clock, maxInUse=1):
self._board = board
self.pins = dict()
self.pins['dataIn'] = dataIn
self.pins['load'] = load
self.pins['clock'] = clock
self.maxInUse = maxInUse
def _digitalWrite(self, pin, val):
self._board.digital[pin].write(val)
def _putByte(self, data):
for i in range(8, 0, -1):
mask = 0x01 << (i - 1)
self._digitalWrite(self.pins["clock"], LOW)
if data & mask:
self._digitalWrite(self.pins["dataIn"], HIGH)
else:
self._digitalWrite(self.pins["dataIn"], LOW)
self._digitalWrite(self.pins["clock"], HIGH)
def maxSingle(self, reg, col):
""" Change the row of lights at reg to read as binary of col. """
self._digitalWrite(self.pins["load"], LOW)
self._putByte(reg)
self._putByte(col)
self._digitalWrite(self.pins["load"], LOW)
self._digitalWrite(self.pins["load"], HIGH)
def maxAll(self, reg, col):
""" Like calling maxSingle on every chained matrix """
self._digitalWrite(self.pins["load"], LOW)
for _ in range(0, self.maxInUse):
self._putByte(reg)
self._putByte(col)
self._digitalWrite(self.pins["load"], LOW)
self._digitalWrite(self.pins["load"], HIGH)
def maxOne(self, maxNr, reg, col):
""" Specify the matrix to be written to with maxNr. Then acts as maxSingle on that specific matrix. """
self._digitalWrite(self.pins["load"], LOW)
for _ in range(self.maxInUse, maxNr, -1):
self._putByte(0)
self._putByte(0)
self._putByte(reg)
self._putByte(col)
for _ in range(maxNr - 1, 0, -1):
self._putByte(0)
self._putByte(0)
self._digitalWrite(self.pins["load"], LOW)
self._digitalWrite(self.pins["load"], HIGH)
def clear(self):
for e in range(1, 9):
self.maxAll(e, 0)
def draw_matrix(self, point_matrix):
for col, pointlist in enumerate(point_matrix):
self.maxSingle(col + 1, int(''.join(str(v) for v in pointlist), 2))
def setup(self):
print('Initializing _matrix...')
self._digitalWrite(13, HIGH)
self.maxAll(max7219_reg_scanLimit, 0x07)
self.maxAll(max7219_reg_decodeMode, 0x00)
self.maxAll(max7219_reg_shutdown, 0x01)
self.maxAll(max7219_reg_displayTest, 0x00)
self.clear()
self.maxAll(max7219_reg_intensity, intensity & intensity)
print('Done')
rotated = True
import numpy as np
def convertarray(array):
m = np.array(array)
m = np.rot90(m, 3)
return m.tolist()
import datetime as date
import json
data = json.loads(open('jsonNums.json', 'r').read())
def getTime():
n = date.datetime.now()
hour = n.hour
minute = n.minute
second = n.second
PM = False
if hour > 12: hour -= 12; PM = True
if len(str(hour)) < 2: hour = str( f"0{hour}" )
hour=str(hour)
if len(str(minute)) < 2: minute = str( f"0{minute}" )
minute = str(minute)
if len(str(second)) < 2: second = str( f"0{second}" )
second = str(second)
pm = "AM"
if PM: pm = "PM"
fullTime = f"{hour}:{minute}.{pm}"
display = []
for i in range(8):
row = [0,0,0,0,0,0,0,0]
for char in fullTime:
if char == "0":
row.extend(data["0"][i])
elif char == "1":
row.extend(data["1"][i])
elif char == "2":
row.extend(data["2"][i])
elif char == "3":
row.extend(data["3"][i])
elif char == "4":
row.extend(data["4"][i])
elif char == "5":
row.extend(data["5"][i])
elif char == "6":
row.extend(data["6"][i])
elif char == "7":
row.extend(data["7"][i])
elif char == "8":
row.extend(data["8"][i])
elif char == "9":
row.extend(data["9"][i])
elif char == ":":
row.extend(data["COLON"][i])
elif char == ".":
row.extend(data["POINT"][i])
elif char == "A":
row.extend(data["AM"][i])
break
elif char == "P":
row.extend(data["PM"][i])
break
row.extend([0,0,0,0,0,0,0,0])
display.append(row)
return display
for row in (getTime()): print(row)
scrollMultiplier = 1
if __name__ == "__main__":
board = Arduino('COM1')
matrix = LedMatrix(board, dataIn, load, clock)
matrix.setup()
print("Loop running")
while True:
# print("New iteration")
displayx = getTime()
# print("Time collected")
r = int(len(displayx[0]) - 8)
# print(f"Repeat = {r}")
for i in range(0, r, scrollMultiplier): # Scrolling effect
x=[]
for XX in displayx:
# print(f"XX = {XX}")
r=[]
nr = XX[i:i+8]
# print(f"nr = {nr}")
x.append(nr)
# print("X.append(nr)")
x = convertarray(x)
matrix.draw_matrix(x)
Кажется, я не могу найти какую-либо помощь в Интернете, поэтому я и спросил здесь ...
Любая помощь приветствуется! :)